CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swissknife_main.cc
Go to the documentation of this file.
1 
5 #include <string>
6 
7 
8 
9 #include <cassert>
10 
11 #include "statistics_database.h"
12 #include "swissknife.h"
13 
14 #include "swissknife_check.h"
15 #include "swissknife_filestats.h"
16 #include "swissknife_gc.h"
17 #include "swissknife_graft.h"
18 #include "swissknife_history.h"
19 #include "swissknife_info.h"
20 #include "swissknife_ingest.h"
21 #include "swissknife_lease.h"
22 #include "swissknife_letter.h"
23 #include "swissknife_list_reflog.h"
24 #include "swissknife_lsrepo.h"
25 #include "swissknife_migrate.h"
26 #include "swissknife_notify.h"
27 #include "swissknife_pull.h"
28 #include "swissknife_reflog.h"
29 #include "swissknife_scrub.h"
30 #include "swissknife_sign.h"
31 #include "swissknife_sync.h"
32 #include "swissknife_zpipe.h"
33 #include "util/logging.h"
34 #include "util/posix.h"
35 #include "util/string.h"
36 
37 using namespace std; // NOLINT
38 
39 typedef vector<swissknife::Command *> Commands;
41 
42 void Usage() {
44  "CernVM-FS repository storage management commands\n"
45  "Version %s\n"
46  "Usage (normally called from cvmfs_server):\n"
47  " cvmfs_swissknife <command> [options]\n",
48  CVMFS_VERSION);
49 
50  for (unsigned i = 0; i < command_list.size(); ++i) {
52  "Command %s\n"
53  "--------", command_list[i]->GetName().c_str());
54  for (unsigned j = 0; j < command_list[i]->GetName().length(); ++j) {
56  }
59  command_list[i]->GetDescription().c_str());
60  swissknife::ParameterList params = command_list[i]->GetParams();
61  if (!params.empty()) {
62  LogCvmfs(kLogCvmfs, kLogStdout, "Options:");
63  for (unsigned j = 0; j < params.size(); ++j) {
65  params[j].key(), params[j].description().c_str());
66  if (params[j].optional())
67  LogCvmfs(kLogCvmfs, kLogStdout | kLogNoLinebreak, " (optional)");
69  }
70  } // Parameter list
71  } // Command list
72 
74 }
75 
76 
77 int main(int argc, char **argv) {
78  // Set default logging facilities
80 
81  command_list.push_back(new swissknife::CommandCreate());
82  command_list.push_back(new swissknife::CommandUpload());
83  command_list.push_back(new swissknife::CommandRemove());
84  command_list.push_back(new swissknife::CommandPeek());
85  command_list.push_back(new swissknife::CommandSync());
92  command_list.push_back(new swissknife::CommandSign());
93  command_list.push_back(new swissknife::CommandLetter());
94  command_list.push_back(new swissknife::CommandCheck());
96  command_list.push_back(new swissknife::CommandPull());
97  command_list.push_back(new swissknife::CommandZpipe());
98  command_list.push_back(new swissknife::CommandGraft());
99  command_list.push_back(new swissknife::CommandInfo());
100  command_list.push_back(new swissknife::CommandVersion());
101  command_list.push_back(new swissknife::CommandMigrate());
102  command_list.push_back(new swissknife::CommandScrub());
103  command_list.push_back(new swissknife::CommandGc());
106  command_list.push_back(new swissknife::CommandLease());
107  command_list.push_back(new swissknife::Ingest());
108  command_list.push_back(new swissknife::CommandNotify());
110 
111  if (argc < 2) {
112  Usage();
113  return 1;
114  }
115  if ((string(argv[1]) == "--help")) {
116  Usage();
117  return 0;
118  }
119  if ((string(argv[1]) == "--version")) {
121  return 0;
122  }
123 
124  // find the command to be run
125  swissknife::Command *command = NULL;
126  for (unsigned i = 0; i < command_list.size(); ++i) {
127  if (command_list[i]->GetName() == string(argv[1])) {
128  command = command_list[i];
129  break;
130  }
131  }
132 
133  if (NULL == command) {
134  Usage();
135  return 1;
136  }
137 
138  bool display_statistics = false;
139 
140  // parse the command line arguments for the Command
142  optind = 1;
143  string option_string = "";
144  swissknife::ParameterList params = command->GetParams();
145  for (unsigned j = 0; j < params.size(); ++j) {
146  option_string.push_back(params[j].key());
147  if (!params[j].switch_only())
148  option_string.push_back(':');
149  }
150  // Now adding the generic -+ extra option command
151  option_string.push_back(swissknife::Command::kGenericParam);
152  option_string.push_back(':');
153  int c;
154  while ((c = getopt(argc, argv, option_string.c_str())) != -1) {
155  bool valid_option = false;
156  for (unsigned j = 0; j < params.size(); ++j) {
157  if (c == params[j].key()) {
159  valid_option = true;
160  args[c].Reset();
161  if (!params[j].switch_only()) {
162  args[c].Reset(new string(optarg));
163  }
164  break;
165  }
166  }
168  valid_option = true;
169  vector<string> flags = SplitString(optarg,
170  swissknife::
171  Command::kGenericParamSeparator);
172  for (unsigned i = 0; i < flags.size(); ++i) {
173  if (flags[i] == "stats") {
174  display_statistics = true;
175  }
176  }
177  }
178  if (!valid_option) {
179  Usage();
180  return 1;
181  }
182  }
183  for (unsigned j = 0; j < params.size(); ++j) {
184  if (!params[j].optional()) {
185  if (args.find(params[j].key()) == args.end()) {
186  LogCvmfs(kLogCvmfs, kLogStderr, "parameter -%c missing",
187  params[j].key());
188  return 1;
189  }
190  }
191  }
192 
193  // run the command
194  string start_time = GetGMTimestamp();
195  const int retval = command->Main(args);
196  string finish_time = GetGMTimestamp();
197 
198  if (display_statistics) {
199  LogCvmfs(kLogCvmfs, kLogStdout, "Command statistics");
201  command->statistics()
203  }
204 
205  // delete the command list
206  Commands::const_iterator i = command_list.begin();
207  const Commands::const_iterator iend = command_list.end();
208  for (; i != iend; ++i) {
209  delete *i;
210  }
211  command_list.clear();
212 
213  return retval;
214 }
virtual ParameterList GetParams() const =0
std::string GetGMTimestamp(const std::string &format)
Definition: string.cc:633
int Main(const ArgumentList &args)
std::vector< Parameter > ParameterList
Definition: swissknife.h:71
static void Set(LogFacilities info, LogFacilities error)
Definition: logging.cc:48
virtual int Main(const ArgumentList &args)=0
assert((mem||(size==0))&&"Out Of Memory")
Commands command_list
int main()
Definition: helper_allow.cc:16
vector< string > SplitString(const string &str, char delim)
Definition: string.cc:308
perf::Statistics * statistics()
Definition: server_tool.h:47
void Usage()
static const char kGenericParam
Definition: swissknife.h:77
std::map< char, SharedPtr< std::string > > ArgumentList
Definition: swissknife.h:72
std::string PrintList(const PrintOptions print_options)
Definition: statistics.cc:79
vector< swissknife::Command * > Commands
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528