CtaAdminCmdParse.hpp 17.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*!
 * @project        The CERN Tape Archive (CTA)
 * @brief          Definitions for parsing the options of the CTA Admin command-line tool
 * @description    CTA Admin command using Google Protocol Buffers and XRootD SSI transport
 * @copyright      Copyright 2017 CERN
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
 *
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
 *
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <map>
#include <set>
#include <string>

#include "xroot_plugins/messages/CtaFrontendApi.hpp"

namespace cta {
namespace admin {

31
using cmdLookup_t    = std::map<std::string, AdminCmd::Cmd>;
32
using subcmdLookup_t = std::map<std::string, AdminCmd::SubCmd>;
33
34
35
36
37

/*!
 * Map short and long command names to Protocol Buffer enum values
 */
const cmdLookup_t cmdLookup = {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
   { "admin",                   AdminCmd::CMD_ADMIN },
   { "ad",                      AdminCmd::CMD_ADMIN },
   { "adminhost",               AdminCmd::CMD_ADMINHOST },
   { "ah",                      AdminCmd::CMD_ADMINHOST },
   { "archivefile",             AdminCmd::CMD_ARCHIVEFILE },
   { "af",                      AdminCmd::CMD_ARCHIVEFILE },
   { "archiveroute",            AdminCmd::CMD_ARCHIVEROUTE },
   { "ar",                      AdminCmd::CMD_ARCHIVEROUTE },
   { "drive",                   AdminCmd::CMD_DRIVE },
   { "dr",                      AdminCmd::CMD_DRIVE },
   { "groupmountrule",          AdminCmd::CMD_GROUPMOUNTRULE },
   { "gmr",                     AdminCmd::CMD_GROUPMOUNTRULE },
   { "listpendingarchives",     AdminCmd::CMD_LISTPENDINGARCHIVES },
   { "lpa",                     AdminCmd::CMD_LISTPENDINGARCHIVES },
   { "listpendingretrieves",    AdminCmd::CMD_LISTPENDINGRETRIEVES },
   { "lpr",                     AdminCmd::CMD_LISTPENDINGRETRIEVES },
   { "logicallibrary",          AdminCmd::CMD_LOGICALLIBRARY },
   { "ll",                      AdminCmd::CMD_LOGICALLIBRARY },
   { "mountpolicy",             AdminCmd::CMD_MOUNTPOLICY },
   { "mp",                      AdminCmd::CMD_MOUNTPOLICY },
   { "repack",                  AdminCmd::CMD_REPACK },
   { "re",                      AdminCmd::CMD_REPACK },
   { "requestermountrule",      AdminCmd::CMD_REQUESTERMOUNTRULE },
   { "rmr",                     AdminCmd::CMD_REQUESTERMOUNTRULE },
   { "showqueues",              AdminCmd::CMD_SHOWQUEUES },
   { "sq",                      AdminCmd::CMD_SHOWQUEUES },
   { "shrink",                  AdminCmd::CMD_SHRINK },
   { "sh",                      AdminCmd::CMD_SHRINK },
   { "storageclass",            AdminCmd::CMD_STORAGECLASS },
   { "sc",                      AdminCmd::CMD_STORAGECLASS },
   { "tape",                    AdminCmd::CMD_TAPE },
   { "ta",                      AdminCmd::CMD_TAPE },
   { "tapepool",                AdminCmd::CMD_TAPEPOOL },
   { "tp",                      AdminCmd::CMD_TAPEPOOL },
   { "test",                    AdminCmd::CMD_TEST },
   { "te",                      AdminCmd::CMD_TEST },
   { "verify",                  AdminCmd::CMD_VERIFY },
   { "ve",                      AdminCmd::CMD_VERIFY }
76
77
78
79
80
81
82
};



/*!
 * Map subcommand names to Protocol Buffer enum values
 */
83
const subcmdLookup_t subcmdLookup = {
84
85
86
87
88
89
90
91
92
93
94
95
   { "add",                     AdminCmd::SUBCMD_ADD },
   { "ch",                      AdminCmd::SUBCMD_CH },
   { "err",                     AdminCmd::SUBCMD_ERR },
   { "label",                   AdminCmd::SUBCMD_LABEL },
   { "ls",                      AdminCmd::SUBCMD_LS },
   { "reclaim",                 AdminCmd::SUBCMD_RECLAIM },
   { "rm",                      AdminCmd::SUBCMD_RM },
   { "up",                      AdminCmd::SUBCMD_UP },
   { "down",                    AdminCmd::SUBCMD_DOWN },
   { "read",                    AdminCmd::SUBCMD_READ },
   { "write",                   AdminCmd::SUBCMD_WRITE },
   { "write_auto",              AdminCmd::SUBCMD_WRITE_AUTO }
96
97
98
99
};



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*!
 * Map boolean options to Protocol Buffer enum values
 */
const std::map<std::string, OptionBoolean::Key> boolOptions = {
   // Boolean options
   { "--all",                   OptionBoolean::BOOL_ALL },
   { "--disabled",              OptionBoolean::BOOL_DISABLED },
   { "--encrypted",             OptionBoolean::BOOL_ENCRYPTED },
   { "--force",                 OptionBoolean::BOOL_FORCE },
   { "--full",                  OptionBoolean::BOOL_FULL },
   { "--lbp",                   OptionBoolean::BOOL_LBP },

   // hasOption options
   { "--checkchecksum",         OptionBoolean::BOOL_CHECK_CHECKSUM },
   { "--extended",              OptionBoolean::BOOL_EXTENDED },
   { "--header",                OptionBoolean::BOOL_SHOW_HEADER },
   { "--justexpand",            OptionBoolean::BOOL_JUSTEXPAND },
   { "--justrepack",            OptionBoolean::BOOL_JUSTREPACK },
   { "--summary",               OptionBoolean::BOOL_SUMMARY }
};



/*!
 * Map integer options to Protocol Buffer enum values
 */
const std::map<std::string, OptionInteger::Key> intOptions = {
   { "--archivepriority",       OptionInteger::INT_ARCHIVE_PRIORITY },
   { "--capacity",              OptionInteger::INT_CAPACITY },
   { "--copynb",                OptionInteger::INT_COPY_NUMBER },
   { "--firstfseq",             OptionInteger::INT_FIRST_FSEQ },
   { "--id",                    OptionInteger::INT_ARCHIVE_FILE_ID },
   { "--lastfseq",              OptionInteger::INT_LAST_FSEQ },
   { "--maxdrivesallowed",      OptionInteger::INT_MAX_DRIVES_ALLOWED },
   { "--minarchiverequestage",  OptionInteger::INT_MIN_ARCHIVE_REQUEST_AGE },
   { "--minretrieverequestage", OptionInteger::INT_MIN_RETRIEVE_REQUEST_AGE },
   { "--number",                OptionInteger::INT_NUMBER_OF_FILES },
   { "--partial",               OptionInteger::INT_PARTIAL }, 
   { "--partialtapesnumber",    OptionInteger::INT_PARTIAL_TAPES_NUMBER },
   { "--retrievepriority",      OptionInteger::INT_RETRIEVE_PRIORITY },
   { "--size",                  OptionInteger::INT_FILE_SIZE }                  
};



/*!
 * Map string options to Protocol Buffer enum values
 */
const std::map<std::string, OptionString::Key> strOptions = {
   { "--comment",               OptionString::STR_COMMENT },
   { "--diskid",                OptionString::STR_DISKID },
   { "--drive",                 OptionString::STR_DRIVE },
   { "--encryptionkey",         OptionString::STR_ENCRYPTION_KEY },
   { "--file",                  OptionString::STR_FILENAME },
   { "--group",                 OptionString::STR_GROUP },
   { "--hostname",              OptionString::STR_HOSTNAME },
   { "--input",                 OptionString::STR_INPUT },
   { "--instance",              OptionString::STR_INSTANCE },
   { "--logicallibrary",        OptionString::STR_LOGICAL_LIBRARY },
   { "--mountpolicy",           OptionString::STR_MOUNT_POLICY },
   { "--output",                OptionString::STR_OUTPUT },
   { "--owner",                 OptionString::STR_OWNER },
   { "--path",                  OptionString::STR_PATH },
   { "--storageclass",          OptionString::STR_STORAGE_CLASS },
   { "--tag",                   OptionString::STR_TAG },
   { "--tapepool",              OptionString::STR_TAPE_POOL },
   { "--username",              OptionString::STR_USERNAME },
   { "--vid",                   OptionString::STR_VID }
};



172
/*!
173
174
175
176
177
178
 * Command line option help structure
 */
struct Option
{
   enum option_t { OPT_FLAG, OPT_BOOL, OPT_INT, OPT_STR };

179
180
181
182
183
184
   option_t    type;          //!< Option type
   std::string alias;         //!< Alias for lookups (needed for duplicate options)
   std::string long_opt;      //!< Long command option
   std::string short_opt;     //!< Short command option
   std::string help_txt;      //!< Option help text
   bool        is_optional;   //!< Option is optional or compulsory
185

186
187
188
   /*!
    * Constructor
    */
189
   Option(option_t otype, const std::string &olong, const std::string &oshort, const std::string &ohelp, const std::string &oalias = "") :
190
191
192
193
      type(otype),
      long_opt(olong),
      short_opt(oshort),
      help_txt(ohelp),
194
195
196
      is_optional(false) {
         alias = (oalias.size() == 0) ? olong : oalias;
   }
197

198
199
200
201
202
203
   /*!
    * Per-option help
    */
   std::string help()
   {
      std::string help = " ";
204
      help += is_optional ? "[" : "";
205
      help += long_opt + '/' + short_opt + help_txt;
206
      help += is_optional ? "]" : "";
207
208
209
210
211
212
213
214
215
216
217
218
219
220
      return help;
   }
};

using cmd_key_t = std::pair<AdminCmd::Cmd, AdminCmd::SubCmd>;
using cmd_val_t = std::vector<Option>;

// Forward declaration of command options map (see definition below)
extern const std::map<cmd_key_t, cmd_val_t> cmdOptions;



/*!
 * Command/subcommand help structure
221
222
 */
struct CmdHelp
223
{
224
225
226
227
228
   std::string cmd_long;                //!< Command string (long version)
   std::string cmd_short;               //!< Command string (short version)
   std::vector<std::string> sub_cmd;    //!< Subcommands which are valid for this command, listed in
                                        //!< the order that they should be displayed in the help
   std::string help_str;                //!< Optional extra help text for the command
229
230

   /*!
231
    * Return the short help message
232
    */
233
234
235
   std::string short_help() const {
      std::string help = cmd_long + '/' + cmd_short;
      help.resize(25, ' ');
236
237

      for(auto sc_it = sub_cmd.begin(); sc_it != sub_cmd.end(); ++sc_it) {
238
         help += (sc_it == sub_cmd.begin() ? ' ' : '/') + *sc_it;
239
      }
240
      return help;
241
242
243
244
245
246
247
248
249
250
251
   }

   /*!
    * Return the detailed help message
    */
   std::string help() const {
      std::string help = cmd_short + '/' + cmd_long;

      for(auto sc_it = sub_cmd.begin(); sc_it != sub_cmd.end(); ++sc_it) {
         help += (sc_it == sub_cmd.begin() ? ' ' : '/') + *sc_it;
      }
252
      help += (help_str.size() > 0) ? ' ' + help_str : "";
253

254
255
256
257
258
259
      // Find the length of the longest subcommand (if there is one)
      auto max_sub_cmd = std::max_element(sub_cmd.begin(), sub_cmd.end(),
                         [](std::string const& lhs, std::string const& rhs) { return lhs.size() < rhs.size(); });
      help += (max_sub_cmd != sub_cmd.end()) ? ":\n" : "\n";

      // Per-subcommand help
260
      for(auto sc_it = sub_cmd.begin(); sc_it != sub_cmd.end(); ++sc_it) {
261
262
         std::string cmd_name = *sc_it;
         cmd_name.resize(max_sub_cmd->size(), ' ');
263
         help += "  " + cmd_name;
264
265
266
267
268
269
270
271
272

         auto key = cmd_key_t{ cmdLookup.at(cmd_short), subcmdLookup.at(*sc_it) };
         auto options = cmdOptions.at(key);

         for(auto op_it = options.begin(); op_it != options.end(); ++op_it)
         {
            help += op_it->help();
         }
         help += '\n';
273
274
275
      }
      return help;
   }
276
277
};

278
279
280
281
282
283
284
285
286
287


/*!
 * Specify the help text for commands
 */
const std::map<AdminCmd::Cmd, CmdHelp> cmdHelp = {
   { AdminCmd::CMD_ADMIN,                { "admin",                "ad",  { "add", "ch", "rm", "ls" }, "" }},
   { AdminCmd::CMD_ADMINHOST,            { "adminhost",            "ah",  { "add", "ch", "rm", "ls" }, "" }},
   { AdminCmd::CMD_ARCHIVEFILE,          { "archivefile",          "af",  { "ls" }, "" }},
   { AdminCmd::CMD_ARCHIVEROUTE,         { "archiveroute",         "ar",  { "add", "ch", "rm", "ls" }, "" }},
288
289
290
   { AdminCmd::CMD_DRIVE,                { "drive",                "dr",  { "up", "down", "ls" },
                                           "(it is a synchronous command)" }},
   { AdminCmd::CMD_GROUPMOUNTRULE,       { "groupmountrule",       "gmr", { "add", "ch", "rm", "ls" }, "" }},
291
292
293
294
295
   { AdminCmd::CMD_LISTPENDINGARCHIVES,  { "listpendingarchives",  "lpa", { }, "" }},
   { AdminCmd::CMD_LISTPENDINGRETRIEVES, { "listpendingretrieves", "lpr", { }, "" }},
   { AdminCmd::CMD_LOGICALLIBRARY,       { "logicallibrary",       "ll",  { "add", "ch", "rm", "ls" }, "" }},
   { AdminCmd::CMD_MOUNTPOLICY,          { "mountpolicy",          "mp",  { "add", "ch", "rm", "ls" }, "" }},
   { AdminCmd::CMD_REPACK,               { "repack",               "re",  { "add", "rm", "ls", "err" }, "" }},
296
   { AdminCmd::CMD_REQUESTERMOUNTRULE,   { "requestermountrule",   "rmr", { "add", "ch", "rm", "ls" }, "" }},
297
298
299
300
301
   { AdminCmd::CMD_SHOWQUEUES,           { "showqueues",           "sq",  { }, "" }},
   { AdminCmd::CMD_SHRINK,               { "shrink",               "sh",  { }, "" }},
   { AdminCmd::CMD_STORAGECLASS,         { "storageclass",         "sc",  { "add", "ch", "rm", "ls" }, "" }},
   { AdminCmd::CMD_TAPE,                 { "tape",                 "ta",  { "add", "ch", "rm", "reclaim", "ls", "label" }, "" }},
   { AdminCmd::CMD_TAPEPOOL,             { "tapepool",             "tp",  { "add", "ch", "rm", "ls" }, "" }},
302
303
304
305
   { AdminCmd::CMD_TEST,                 { "test",                 "te",  { "read", "write", "write_auto" },
                                           "(to be run on an empty self-dedicated\n"
                                           "drive; it is a synchronous command that returns performance stats and errors;\n"
                                           "all locations are local to the tapeserver)" }},
306
   { AdminCmd::CMD_VERIFY,               { "verify",               "ve",  { "add", "rm", "ls", "err" }, "" }}
307
308
};

309
310


311
312
313
/*
 * Enumerate options
 */
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const Option opt_comment       { Option::OPT_STR,  "--comment",            "-m", " <\"comment\">" };
const Option opt_copynb        { Option::OPT_INT,  "--copynb",             "-c", " <copy_number>" };
const Option opt_encrypted     { Option::OPT_BOOL, "--encrypted",          "-e", " <\"true\" or \"false\">" };
const Option opt_header        { Option::OPT_FLAG, "--header",             "-h", "" };
const Option opt_hostname      { Option::OPT_STR,  "--name",               "-n", " <host_name>", "--hostname" };
const Option opt_instance      { Option::OPT_STR,  "--instance",           "-i", " <instance_name>" };
const Option opt_logicallibrary{ Option::OPT_STR,  "--name",               "-n", " <logical_library_name>", "--logicallibrary" };
const Option opt_partialfiles  { Option::OPT_INT,  "--partial",            "-p", " <number_of_files_per_tape>" };
const Option opt_partialtapes  { Option::OPT_INT,  "--partialtapesnumber", "-p", " <number_of_partial_tapes>" };
const Option opt_storageclass  { Option::OPT_STR,  "--storageclass",       "-s", " <storage_class_name>" };
const Option opt_tapepool      { Option::OPT_STR,  "--tapepool",           "-n", " <tapepool_name>" };
const Option opt_tapepool_name { Option::OPT_STR,  "--name",               "-n", " <tapepool_name>", "--tapepool" };
const Option opt_username      { Option::OPT_STR,  "--username",           "-u", " <user_name>" };


329
330
331
332
333
334



/*!
 * Make an option optional
 */
335
inline Option optional(Option opt) { opt.is_optional = true; return opt; }
336

337
338


339
340
341
/*!
 * Map valid options to commands
 */
342
const std::map<cmd_key_t, cmd_val_t> cmdOptions = {
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
   {{ AdminCmd::CMD_ADMIN,                AdminCmd::SUBCMD_ADD },  { opt_username, opt_comment }},
   {{ AdminCmd::CMD_ADMIN,                AdminCmd::SUBCMD_CH  },  { opt_username, opt_comment }},
   {{ AdminCmd::CMD_ADMIN,                AdminCmd::SUBCMD_RM  },  { opt_username }},
   {{ AdminCmd::CMD_ADMIN,                AdminCmd::SUBCMD_LS  },  { optional(opt_header) }},
   {{ AdminCmd::CMD_ADMINHOST,            AdminCmd::SUBCMD_ADD },  { opt_hostname, opt_comment }},
   {{ AdminCmd::CMD_ADMINHOST,            AdminCmd::SUBCMD_CH  },  { opt_hostname, opt_comment }},
   {{ AdminCmd::CMD_ADMINHOST,            AdminCmd::SUBCMD_RM  },  { opt_hostname }},
   {{ AdminCmd::CMD_ADMINHOST,            AdminCmd::SUBCMD_LS  },  { optional(opt_header) }},
   {{ AdminCmd::CMD_ARCHIVEROUTE,         AdminCmd::SUBCMD_ADD },  { opt_instance, opt_storageclass, opt_copynb, opt_tapepool, opt_comment }},
   {{ AdminCmd::CMD_ARCHIVEROUTE,         AdminCmd::SUBCMD_CH  },  { opt_instance, opt_storageclass, opt_copynb, optional(opt_tapepool), optional(opt_comment) }},
   {{ AdminCmd::CMD_ARCHIVEROUTE,         AdminCmd::SUBCMD_RM  },  { opt_instance, opt_storageclass, opt_copynb }},
   {{ AdminCmd::CMD_ARCHIVEROUTE,         AdminCmd::SUBCMD_LS  },  { optional(opt_header) }},
   {{ AdminCmd::CMD_LOGICALLIBRARY,       AdminCmd::SUBCMD_ADD },  { opt_logicallibrary, opt_comment }},
   {{ AdminCmd::CMD_LOGICALLIBRARY,       AdminCmd::SUBCMD_CH  },  { opt_logicallibrary, opt_comment }},
   {{ AdminCmd::CMD_LOGICALLIBRARY,       AdminCmd::SUBCMD_RM  },  { opt_logicallibrary }},
   {{ AdminCmd::CMD_LOGICALLIBRARY,       AdminCmd::SUBCMD_LS  },  { optional(opt_header) }},
   {{ AdminCmd::CMD_TAPEPOOL,             AdminCmd::SUBCMD_ADD },  { opt_tapepool_name, opt_partialtapes, opt_encrypted, opt_comment }},
   {{ AdminCmd::CMD_TAPEPOOL,             AdminCmd::SUBCMD_CH  },  { opt_tapepool_name, optional(opt_partialtapes), optional(opt_encrypted), optional(opt_comment) }},
   {{ AdminCmd::CMD_TAPEPOOL,             AdminCmd::SUBCMD_RM  },  { opt_tapepool_name }},
   {{ AdminCmd::CMD_TAPEPOOL,             AdminCmd::SUBCMD_LS  },  { optional(opt_header) }},
   {{ AdminCmd::CMD_LISTPENDINGARCHIVES,  AdminCmd::SUBCMD_NONE }, { }},
   {{ AdminCmd::CMD_LISTPENDINGRETRIEVES, AdminCmd::SUBCMD_NONE }, { }},
   {{ AdminCmd::CMD_SHOWQUEUES,           AdminCmd::SUBCMD_NONE }, { }},
   {{ AdminCmd::CMD_SHRINK,               AdminCmd::SUBCMD_NONE }, { }},
367
};
368

369
370


371
372


373
}} // namespace cta::admin