CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_mkfs.cc
Go to the documentation of this file.
1 
6 #include "cmd_mkfs.h"
7 
8 #include <unistd.h>
9 
10 #include <string>
11 
12 #include "crypto/signature.h"
13 #include "manifest.h"
14 #include "publish/except.h"
15 #include "publish/repository.h"
16 #include "publish/settings.h"
17 #include "sanitizer.h"
19 #include "util/pointer.h"
20 #include "util/posix.h"
21 
22 namespace publish {
23 
24 int CmdMkfs::Main(const Options &options) {
25  const std::string fqrn = options.plain_args()[0].value_str;
26  const sanitizer::RepositorySanitizer sanitizer;
27  if (!sanitizer.IsValid(fqrn)) {
28  throw EPublish("malformed repository name: " + fqrn);
29  }
31 
32  std::string user_name = GetUserName();
33  if (options.HasNot("owner")) {
35  "Owner of %s [%s]: ", fqrn.c_str(), user_name.c_str());
36  std::string input;
37  int c;
38  while ((c = getchar()) != EOF) {
39  if (c == '\n')
40  break;
41  input.push_back(c);
42  }
43  if (!input.empty())
44  user_name = input;
45  }
46  settings.SetOwner(user_name);
47 
48  // Sanity checks
49  if (options.Has("no-autotags") && options.Has("autotag-span")) {
50  throw EPublish(
51  "options 'no-autotags' and 'autotag-span' are mutually exclusive");
52  }
53  if (options.HasNot("no-autotags") && options.HasNot("autotag-span")
54  && options.Has("gc")) {
56  "Note: Autotagging all revisions impedes garbage collection");
57  }
58 
59  // Needs to be done before the storage and its temp dir is configured
60  if (options.Has("no-publisher")) {
62  settings.GetKeychain()->SetKeychainDir(".");
63  }
64 
65  // Storage configuration
66  if (options.Has("storage")) {
67  if (options.Has("s3config")) {
68  throw EPublish("options 'storage' and 's3config' are mutually exclusive");
69  }
70  settings.GetStorage()->SetLocator(options.GetString("storage"));
71  } else if (options.Has("s3config")) {
72  settings.GetStorage()->MakeS3(
73  options.GetString("s3config"),
74  settings.transaction().spool_area().tmp_dir());
75  }
76  const bool configure_apache =
77  (settings.storage().type() == upload::SpoolerDefinition::Local) &&
78  options.HasNot("no-apache");
79 
80  // Permission check
81  if (geteuid() != 0) {
82  const bool can_unprivileged = options.Has("no-publisher") &&
83  !configure_apache &&
84  (user_name == GetUserName());
85  if (!can_unprivileged)
86  throw EPublish("root privileges required");
87  }
88 
89  // Stratum 0 URL
90  if (options.Has("stratum0")) {
91  settings.SetUrl(options.GetString("stratum0"));
92  } else {
93  const bool need_stratum0 =
94  (settings.storage().type() != upload::SpoolerDefinition::Local) &&
95  options.HasNot("no-publisher");
96  if (need_stratum0) {
97  throw EPublish("repository stratum 0 URL for non-local storage "
98  "(add option -w)");
99  }
100  }
101 
102  // Union file system
103  if (options.HasNot("no-publisher")) {
104  if (options.Has("unionfs")) {
105  settings.GetTransaction()->SetUnionFsType(options.GetString("unionfs"));
106  } else {
107  settings.GetTransaction()->DetectUnionFsType();
108  }
109  } else {
110  if (options.Has("unionfs")) {
111  throw EPublish(
112  "options 'no-publisher' and 'unionfs' are mutually exclusive");
113  }
114  }
115 
116  if (configure_apache) {
117  // TODO(jblomer): Apache configuration
118  }
119 
120  // TODO(jblomer): for local backend we need to create the path as root and
121  // then hand it over
122  const UniquePtr<Publisher> publisher(Publisher::Create(settings));
123  // if (options.Has("no-apache"))
124 
125  LogCvmfs(kLogCvmfs, kLogStdout, "PUBLIC MASTER KEY:\n%s",
126  publisher->signature_mgr()->GetActivePubkeys().c_str());
127  LogCvmfs(kLogCvmfs, kLogStdout, "CERTIFICATE:\n%s",
128  publisher->signature_mgr()->GetCertificate().c_str());
129 
130  LogCvmfs(kLogCvmfs, kLogStdout, "MANIFEST:\n%s",
131  publisher->manifest()->ExportString().c_str());
132 
133  return 0;
134 }
135 
136 } // namespace publish
void SetUnionFsType(const std::string &union_fs)
Definition: settings.cc:124
SettingsTransaction * GetTransaction()
Definition: settings.h:436
SettingsKeychain * GetKeychain()
Definition: settings.h:437
static Publisher * Create(const SettingsPublisher &settings)
std::string GetUserName()
Definition: posix.cc:1280
void SetKeychainDir(const std::string &keychain_dir)
Definition: settings.cc:229
bool Has(const std::string &key) const
Definition: command.h:108
std::string GetString(const std::string &key) const
Definition: command.h:115
const SettingsPublisher & settings() const
Definition: repository.h:316
const std::vector< Argument > & plain_args() const
Definition: command.h:128
virtual int Main(const Options &options)
Definition: cmd_mkfs.cc:24
bool IsValid(const std::string &input) const
Definition: sanitizer.cc:112
void SetUrl(const std::string &url)
Definition: settings.cc:330
const SettingsSpoolArea & spool_area() const
Definition: settings.h:216
const SettingsTransaction & transaction() const
Definition: settings.h:433
void SetLocator(const std::string &locator)
Definition: settings.cc:207
SettingsStorage * GetStorage()
Definition: settings.h:435
const SettingsStorage & storage() const
Definition: settings.h:432
std::string tmp_dir() const
Definition: settings.h:92
bool HasNot(const std::string &key) const
Definition: command.h:111
SettingsSpoolArea * GetSpoolArea()
Definition: settings.h:217
upload::SpoolerDefinition::DriverType type() const
Definition: settings.h:292
void SetOwner(const std::string &user_name)
Definition: settings.cc:339
void MakeS3(const std::string &s3_config, const std::string &tmp_dir)
Definition: settings.cc:186
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:545