Loading [MathJax]/extensions/tex2jax.js
LSST Applications g0fba68d861+83433b07ee,g16d25e1f1b+23bc9e47ac,g1ec0fe41b4+3ea9d11450,g1fd858c14a+9be2b0f3b9,g2440f9efcc+8c5ae1fdc5,g35bb328faa+8c5ae1fdc5,g4a4af6cd76+d25431c27e,g4d2262a081+c74e83464e,g53246c7159+8c5ae1fdc5,g55585698de+1e04e59700,g56a49b3a55+92a7603e7a,g60b5630c4e+1e04e59700,g67b6fd64d1+3fc8cb0b9e,g78460c75b0+7e33a9eb6d,g786e29fd12+668abc6043,g8352419a5c+8c5ae1fdc5,g8852436030+60e38ee5ff,g89139ef638+3fc8cb0b9e,g94187f82dc+1e04e59700,g989de1cb63+3fc8cb0b9e,g9d31334357+1e04e59700,g9f33ca652e+0a83e03614,gabe3b4be73+8856018cbb,gabf8522325+977d9fabaf,gb1101e3267+8b4b9c8ed7,gb89ab40317+3fc8cb0b9e,gc0af124501+57ccba3ad1,gcf25f946ba+60e38ee5ff,gd6cbbdb0b4+1cc2750d2e,gd794735e4e+7be992507c,gdb1c4ca869+be65c9c1d7,gde0f65d7ad+c7f52e58fe,ge278dab8ac+6b863515ed,ge410e46f29+3fc8cb0b9e,gf35d7ec915+97dd712d81,gf5e32f922b+8c5ae1fdc5,gf618743f1b+747388abfa,gf67bdafdda+3fc8cb0b9e,w.2025.18
LSST Data Management Base Package
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
apdb_cli.py
Go to the documentation of this file.
1# This file is part of dax_apdb
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22from __future__ import annotations
23
24__all__ = ["main"]
25
26import argparse
27from collections.abc import Sequence
28
29from .. import scripts
30from . import options
31from .logging_cli import LoggingCli
32
33
34def main(args: Sequence[str] | None = None) -> None:
35 """APDB command line tools."""
36 parser = argparse.ArgumentParser(description="APDB command line tools")
37 log_cli = LoggingCli(parser)
38
39 subparsers = parser.add_subparsers(title="available subcommands", required=True)
40 _create_sql_subcommand(subparsers)
44 _list_index_subcommand(subparsers)
45 _metadata_subcommand(subparsers)
47 _metrics_subcommand(subparsers)
48
49 parsed_args = parser.parse_args(args)
50 log_cli.process_args(parsed_args)
51
52 kwargs = vars(parsed_args)
53 # Strip keywords not understood by scripts.
54 method = kwargs.pop("method")
55 method(**kwargs)
56
57
58def _create_sql_subcommand(subparsers: argparse._SubParsersAction) -> None:
59 parser = subparsers.add_parser("create-sql", help="Create new APDB instance in SQL database.")
60 parser.add_argument("db_url", help="Database URL in SQLAlchemy format for APDB instance.")
61 parser.add_argument("output_config", help="Name of the new configuration file for created APDB instance.")
64 parser.add_argument(
65 "--drop", help="If True then drop existing tables.", default=False, action="store_true"
66 )
67 parser.set_defaults(method=scripts.create_sql)
68
69
70def _create_cassandra_subcommand(subparsers: argparse._SubParsersAction) -> None:
71 parser = subparsers.add_parser("create-cassandra", help="Create new APDB instance in Cassandra cluster.")
72 parser.add_argument("host", help="One or more host names for Cassandra cluster.", nargs="+")
73 parser.add_argument(
74 "keyspace", help="Cassandra keyspace name for APDB tables, will be created if does not exist."
75 )
76 parser.add_argument("output_config", help="Name of the new configuration file for created APDB instance.")
79 parser.add_argument(
80 "--drop", help="If True then drop existing tables.", default=False, action="store_true"
81 )
82 parser.set_defaults(method=scripts.create_cassandra)
83
84
85def _list_cassandra_subcommand(subparsers: argparse._SubParsersAction) -> None:
86 parser = subparsers.add_parser("list-cassandra", help="List APDB instances in Cassandra cluster.")
87 parser.add_argument("host", help="One of the host names for Cassandra cluster.")
88 parser.add_argument(
89 "-v",
90 "--verbose",
91 help="Provide full list of roles and associated permissions.",
92 default=False,
93 action="store_true",
94 )
95 parser.set_defaults(method=scripts.list_cassandra)
96
97
98def _delete_cassandra_subcommand(subparsers: argparse._SubParsersAction) -> None:
99 parser = subparsers.add_parser("delete-cassandra", help="Delete APDB instance from Cassandra cluster.")
100 parser.add_argument("host", help="One of the host names for Cassandra cluster.")
101 parser.add_argument("keyspace", help="Cassandra keyspace name for APDB tables.")
102 parser.add_argument(
103 "-y",
104 "--confirm",
105 help="Assume 'yes' answer for confirmation.",
106 default=False,
107 action="store_true",
108 )
109 parser.set_defaults(method=scripts.delete_cassandra)
110
111
112def _list_index_subcommand(subparsers: argparse._SubParsersAction) -> None:
113 parser = subparsers.add_parser("list-index", help="List contents of APDB index file.")
114 parser.add_argument(
115 "index_path", help="Location of index file, if missing then $DAX_APDB_INDEX_URI is used.", nargs="?"
116 )
117 parser.set_defaults(method=scripts.list_index)
118
119
120def _metadata_subcommand(subparsers: argparse._SubParsersAction) -> None:
121 parser = subparsers.add_parser("metadata", help="Operations with APDB metadata table.")
122 subparsers = parser.add_subparsers(title="available subcommands", required=True)
123 _metadata_set_subcommand(subparsers)
124 _metadata_get_subcommand(subparsers)
125 _metadata_show_subcommand(subparsers)
127
128
129def _metadata_show_subcommand(subparsers: argparse._SubParsersAction) -> None:
130 parser = subparsers.add_parser("show", help="Show contents of APDB metadata table.")
131 parser.add_argument(
132 "-j",
133 "--json",
134 dest="use_json",
135 help="Dump metadata in JSON format.",
136 default=False,
137 action="store_true",
138 )
139 parser.add_argument("config", help="Path or URI of APDB configuration file.")
140 parser.set_defaults(method=scripts.metadata_show)
141
142
143def _metadata_get_subcommand(subparsers: argparse._SubParsersAction) -> None:
144 parser = subparsers.add_parser("get", help="Print value of the metadata item.")
145 parser.add_argument("config", help="Path or URI of APDB configuration file.")
146 parser.add_argument("key", help="Metadata key, arbitrary string.")
147 parser.set_defaults(method=scripts.metadata_get)
148
149
150def _metadata_set_subcommand(subparsers: argparse._SubParsersAction) -> None:
151 parser = subparsers.add_parser("set", help="Add or update metadata item.")
152 parser.add_argument(
153 "-f",
154 "--force",
155 help="Force update of the existing key.",
156 default=False,
157 action="store_true",
158 )
159 parser.add_argument("config", help="Path or URI of APDB configuration file.")
160 parser.add_argument("key", help="Metadata key, arbitrary string.")
161 parser.add_argument("value", help="Corresponding metadata value.")
162 parser.set_defaults(method=scripts.metadata_set)
163
164
165def _metadata_delete_subcommand(subparsers: argparse._SubParsersAction) -> None:
166 parser = subparsers.add_parser("delete", help="Delete metadata item.")
167 parser.add_argument("config", help="Path or URI of APDB configuration file.")
168 parser.add_argument("key", help="Metadata key, arbitrary string.")
169 parser.set_defaults(method=scripts.metadata_delete)
170
171
172def _convert_legacy_config_subcommand(subparsers: argparse._SubParsersAction) -> None:
173 parser = subparsers.add_parser("convert-legacy-config", help="Convert legacy pex_config to YAML.")
174 parser.add_argument("legacy_config", help="Path or URI of APDB legacy configuration file.")
175 parser.add_argument("new_config", help="Path or URI to write new YAML configuration file.")
176 parser.set_defaults(method=scripts.convert_legacy_config)
177
178
179def _metrics_subcommand(subparsers: argparse._SubParsersAction) -> None:
180 parser = subparsers.add_parser("metrics", help="Operations with metrics produced by APDB.")
181 subparsers = parser.add_subparsers(title="available subcommands", required=True)
182 _metrics_log_to_influx(subparsers)
183
184
185def _metrics_log_to_influx(subparsers: argparse._SubParsersAction) -> None:
186 parser = subparsers.add_parser(
187 "log-to-infux", help="Extract metrics from log files and dump as InfluxDB data."
188 )
189 parser.add_argument(
190 "file", help="Name(s) of the log file to parse, '-' to read from standard input.", nargs="+"
191 )
192 parser.add_argument(
193 "-c",
194 "--context-keys",
195 help=("Names of keys to extract from message context, comma-separated. " "Default: %(default)s "),
196 default="instrument,visit,detector,day_obs",
197 metavar="KEY[,KEY,...]",
198 )
199 parser.add_argument(
200 "-t",
201 "--extra-tags",
202 help="Extra tags and their values, comma-separated.",
203 default="",
204 metavar="TAG=VALUE[,TAG=VALUE...]",
205 )
206 parser.add_argument(
207 "-r",
208 "--replication",
209 help="Log is produced by replication service.",
210 action="store_true",
211 default=False,
212 )
213 parser.add_argument(
214 "-p",
215 "--prefix",
216 help="Additional prefix for metrics names.",
217 default="",
218 )
219 parser.add_argument(
220 "-H",
221 "--no-header",
222 help="Do not add DML header.",
223 action="store_true",
224 default=False,
225 )
226 parser.add_argument(
227 "-d",
228 "--header-database",
229 help="Database name to use for header, default: %(default)s.",
230 default="telegraf",
231 )
232 parser.add_argument(
233 "--fix-row-count",
234 help="Fix incorrect inserted row counts by parsing additional data from logs.",
235 action="store_true",
236 default=False,
237 )
238 parser.set_defaults(method=scripts.metrics_log_to_influx)
None _metrics_log_to_influx(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:185
None _create_sql_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:58
None _convert_legacy_config_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:172
None _metadata_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:120
None _metadata_get_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:143
None _list_cassandra_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:85
None main(Sequence[str]|None args=None)
Definition apdb_cli.py:34
None _metrics_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:179
None _delete_cassandra_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:98
None _create_cassandra_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:70
None _list_index_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:112
None _metadata_delete_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:165
None _metadata_set_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:150
None _metadata_show_subcommand(argparse._SubParsersAction subparsers)
Definition apdb_cli.py:129
None common_apdb_options(argparse.ArgumentParser parser)
Definition options.py:67
None cassandra_config_options(argparse.ArgumentParser parser)
Definition options.py:99
None sql_config_options(argparse.ArgumentParser parser)
Definition options.py:78