CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_pub.cc
Go to the documentation of this file.
1 
5 #include "cmd_pub.h"
6 
7 #include <fcntl.h>
8 
9 #include <string>
10 #include <vector>
11 
12 #include "manifest.h"
13 #include "network/download.h"
14 #include "notify/messages.h"
15 #include "notify/publisher_http.h"
16 #include "util/pointer.h"
17 #include "util/posix.h"
18 #include "util/string.h"
19 
20 namespace {
21 
24 
25 const int kMaxPoolHandles = 1;
26 const unsigned kDownloadTimeout = 60; // 1 minute
27 const unsigned kDownloadRetries = 1; // 2 attempts in total
28 
29 } // namespace
30 
31 namespace notify {
32 
33 int DoPublish(const std::string& server_url, const std::string& repository_url,
34  bool verbose) {
35  const std::string repo_url = MakeCanonicalPath(repository_url);
36 
37  if (verbose) {
38  LogCvmfs(kLogCvmfs, kLogInfo, "Parameters: ");
39  LogCvmfs(kLogCvmfs, kLogInfo, " CVMFS repository URL: %s",
40  repo_url.c_str());
41  LogCvmfs(kLogCvmfs, kLogInfo, " Notification server URL: %s",
42  server_url.c_str());
43  }
44 
45  // Download repository manifest
46  std::string manifest_contents;
47  const std::string manifest_url = repo_url + "/.cvmfspublished";
48  if (IsHttpUrl(repo_url)) {
49  perf::Statistics stats;
50  UniquePtr<download::DownloadManager> download_manager(
52  perf::StatisticsTemplate("download", &stats)));
53  assert(download_manager.IsValid());
54 
55  download_manager->SetTimeout(kDownloadTimeout, kDownloadTimeout);
56  download_manager->SetRetryParameters(kDownloadRetries, 500, 2000);
57 
58  cvmfs::MemSink manifest_memsink;
59  download::JobInfo download_manifest(&manifest_url, false, false, NULL,
60  &manifest_memsink);
61  download::Failures retval = download_manager->Fetch(&download_manifest);
62  if (retval != download::kFailOk) {
63  LogCvmfs(kLogCvmfs, kLogError, "Failed to download manifest (%d - %s)",
64  retval, download::Code2Ascii(retval));
65  return 6;
66  }
67  manifest_contents = std::string(
68  reinterpret_cast<char*>(manifest_memsink.data()),
69  manifest_memsink.pos());
70  } else {
71  int fd = open(manifest_url.c_str(), O_RDONLY);
72  if (fd == -1) {
73  LogCvmfs(kLogCvmfs, kLogError, "Could not open manifest file");
74  return 7;
75  }
76  if (!SafeReadToString(fd, &manifest_contents)) {
77  LogCvmfs(kLogCvmfs, kLogError, "Could not read manifest file");
78  close(fd);
79  return 8;
80  }
81  close(fd);
82  }
83 
85  reinterpret_cast<const unsigned char*>(manifest_contents.data()),
86  manifest_contents.size()));
87 
88  if (verbose) {
89  LogCvmfs(kLogCvmfs, kLogInfo, "Current repository manifest:\n%s",
90  manifest->ExportString().c_str());
91  }
92 
93  const std::string repository_name = manifest->repository_name();
94 
95  // Publish message
96  UniquePtr<notify::Publisher> publisher(new notify::PublisherHTTP(server_url));
97 
98  std::string msg_text;
100  msg.version_ = 1;
101  msg.timestamp_ = StringifyTime(std::time(NULL), true);
102  msg.repository_ = repository_name;
103  msg.manifest_ = manifest_contents;
104  msg.ToJSONString(&msg_text);
105 
106  if (!publisher->Publish(msg_text, repository_name)) {
107  LogCvmfs(kLogCvmfs, kLogError, "Could not publish notification");
108  return 9;
109  }
110 
111  assert(publisher->Finalize());
112 
113  return 0;
114 }
115 
116 } // namespace notify
std::string manifest_
Definition: messages.h:49
const manifest::Manifest * manifest() const
Definition: repository.h:125
static Manifest * LoadMem(const unsigned char *buffer, const unsigned length)
Definition: manifest.cc:82
unsigned char * data()
Definition: sink_mem.h:122
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:168
const LogFacilities & kLogError
Definition: cmd_pub.cc:23
const LogFacilities & kLogInfo
Definition: cmd_pub.cc:22
assert((mem||(size==0))&&"Out Of Memory")
string StringifyTime(const time_t seconds, const bool utc)
Definition: string.cc:105
const char * Code2Ascii(const Failures error)
int DoPublish(const std::string &server_url, const std::string &repository_url, bool verbose)
Definition: cmd_pub.cc:33
const unsigned kDownloadRetries
Definition: cmd_pub.cc:27
std::string timestamp_
Definition: messages.h:47
bool IsValid() const
Definition: pointer.h:43
bool SafeReadToString(int fd, std::string *final_result)
Definition: posix.cc:2068
size_t pos()
Definition: sink_mem.h:121
virtual bool Publish(const std::string &msg, const std::string &topic)=0
Definition: repository.cc:820
std::string repository_
Definition: messages.h:48
LogFacilities
const unsigned kDownloadTimeout
Definition: cmd_pub.cc:26
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:98
static LogFacilities info
static LogFacilities error
virtual bool Finalize()
Definition: publisher.cc:17
virtual void ToJSONString(std::string *s)
Definition: messages.cc:37
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528