CtaAdminCmdParse.hpp 12.7 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
32
using cmdLookup_t    = std::map<std::string, AdminCmd::Cmd>;
using subCmdLookup_t = std::map<std::string, AdminCmd::SubCmd>;
33
34
35
36
37
38
39



/*!
 * Map short and long command names to Protocol Buffer enum values
 */
const cmdLookup_t cmdLookup = {
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
76
77
   { "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 }
78
79
80
81
82
83
84
85
};



/*!
 * Map subcommand names to Protocol Buffer enum values
 */
const subCmdLookup_t subCmdLookup = {
86
87
88
89
90
91
92
93
94
95
96
97
   { "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 }
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
172
173
/*!
 * 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 }
};



174
175
176
177
/*!
 * Help text structure
 */
struct CmdHelp
178
{
179
180
181
182
183
   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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227

   /*!
    * Return the command name
    */
   std::string cmd_name() const {
      std::string cmd = cmd_long + '/' + cmd_short;
      cmd.resize(25, ' ');
      return cmd;
   }

   /*!
    * Return the list of command options
    */
   std::string option_list() const {
      std::string options;
      for(auto sc_it = sub_cmd.begin(); sc_it != sub_cmd.end(); ++sc_it) {
         options += (sc_it == sub_cmd.begin() ? ' ' : '/') + *sc_it;
      }
      options += '\n';
      return options;
   }

   /*!
    * 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;
      }
      if(help_str.size() > 0) {
         help += ' ' + help_str;
      }

      // Show per-option help
      if(sub_cmd.size() > 0) {
         help += ":\n";
      }
      for(auto sc_it = sub_cmd.begin(); sc_it != sub_cmd.end(); ++sc_it) {
         help += '\t' + *sc_it + '\n';
      }
      return help;
   }
228
229
};

230
231
232
233
234
235
236
237
238
239


/*!
 * 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" }, "" }},
240
241
242
   { AdminCmd::CMD_DRIVE,                { "drive",                "dr",  { "up", "down", "ls" },
                                           "(it is a synchronous command)" }},
   { AdminCmd::CMD_GROUPMOUNTRULE,       { "groupmountrule",       "gmr", { "add", "ch", "rm", "ls" }, "" }},
243
244
245
246
247
   { 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" }, "" }},
248
   { AdminCmd::CMD_REQUESTERMOUNTRULE,   { "requestermountrule",   "rmr", { "add", "ch", "rm", "ls" }, "" }},
249
250
251
252
253
   { 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" }, "" }},
254
255
256
257
   { 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)" }},
258
   { AdminCmd::CMD_VERIFY,               { "verify",               "ve",  { "add", "rm", "ls", "err" }, "" }}
259
260
};

261
262


263
264
265
266
/*!
 * Command line options
 */
struct Option
267
{
268
269
270
271
272
273
274
   enum option_t { OPT_FLAG, OPT_BOOL, OPT_INT, OPT_STR };

   option_t    type;
   std::string long_opt;
   std::string short_opt;
   std::string help_txt;
   bool        optional;
275
276
};

277
278
279
280
281
282
/*
 * Enumerate options
 */
const Option opt_header   = { Option::OPT_FLAG, "--header",   "-h", "",              true  };
const Option opt_comment  = { Option::OPT_STR,  "--comment",  "-m", "<\"comment\">", false };
const Option opt_hostname = { Option::OPT_STR,  "--hostname", "-n", "<host_name>",   false };
283

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

287
288
289
290
291
292
293
294
/*!
 * Map valid options to commands
 */
const std::map<cmd_key_t, cmd_val_t> command = {
   { { AdminCmd::CMD_ADMIN, AdminCmd::SUBCMD_ADD }, { opt_hostname, opt_comment }},
   { { AdminCmd::CMD_ADMIN, AdminCmd::SUBCMD_CH  }, { opt_hostname, opt_comment }},
   { { AdminCmd::CMD_ADMIN, AdminCmd::SUBCMD_RM  }, { opt_hostname }},
   { { AdminCmd::CMD_ADMIN, AdminCmd::SUBCMD_LS  }, { opt_header }},
295
};
296

297
}} // namespace cta::admin