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