CernVM-FS  2.9.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 "download.h"
13 #include "manifest.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 bool kUseSystemProxy = true;
27 const unsigned kDownloadTimeout = 60; // 1 minute
28 const unsigned kDownloadRetries = 1; // 2 attempts in total
29 
30 } // namespace
31 
32 namespace notify {
33 
34 int DoPublish(const std::string& server_url, const std::string& repository_url,
35  bool verbose) {
36  const std::string repo_url = MakeCanonicalPath(repository_url);
37 
38  if (verbose) {
39  LogCvmfs(kLogCvmfs, kLogInfo, "Parameters: ");
40  LogCvmfs(kLogCvmfs, kLogInfo, " CVMFS repository URL: %s",
41  repo_url.c_str());
42  LogCvmfs(kLogCvmfs, kLogInfo, " Notification server URL: %s",
43  server_url.c_str());
44  }
45 
46  // Download repository manifest
47  std::string manifest_contents;
48  const std::string manifest_url = repo_url + "/.cvmfspublished";
49  if (IsHttpUrl(repo_url)) {
50  perf::Statistics stats;
51  UniquePtr<download::DownloadManager> download_manager(
53  assert(download_manager.IsValid());
54  download_manager->Init(kMaxPoolHandles, kUseSystemProxy,
55  perf::StatisticsTemplate("download", &stats));
56 
57  download_manager->SetTimeout(kDownloadTimeout, kDownloadTimeout);
58  download_manager->SetRetryParameters(kDownloadRetries, 500, 2000);
59 
60  download::JobInfo download_manifest(&manifest_url, false, false, NULL);
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  download_manager->Fini();
66  return 6;
67  }
68  manifest_contents = std::string(download_manifest.destination_mem.data,
69  download_manifest.destination_mem.pos);
70  free(download_manifest.destination_mem.data);
71  download_manager->Fini();
72  } else {
73  int fd = open(manifest_url.c_str(), O_RDONLY);
74  if (fd == -1) {
75  LogCvmfs(kLogCvmfs, kLogError, "Could not open manifest file");
76  return 7;
77  }
78  if (!SafeReadToString(fd, &manifest_contents)) {
79  LogCvmfs(kLogCvmfs, kLogError, "Could not read manifest file");
80  close(fd);
81  return 8;
82  }
83  close(fd);
84  }
85 
87  reinterpret_cast<const unsigned char*>(manifest_contents.data()),
88  manifest_contents.size()));
89 
90  if (verbose) {
91  LogCvmfs(kLogCvmfs, kLogInfo, "Current repository manifest:\n%s",
92  manifest->ExportString().c_str());
93  }
94 
95  const std::string repository_name = manifest->repository_name();
96 
97  // Publish message
98  UniquePtr<notify::Publisher> publisher(new notify::PublisherHTTP(server_url));
99 
100  std::string msg_text;
102  msg.version_ = 1;
103  msg.timestamp_ = StringifyTime(std::time(NULL), true);
104  msg.repository_ = repository_name;
105  msg.manifest_ = manifest_contents;
106  msg.ToJSONString(&msg_text);
107 
108  if (!publisher->Publish(msg_text, repository_name)) {
109  LogCvmfs(kLogCvmfs, kLogError, "Could not publish notification");
110  return 9;
111  }
112 
113  assert(publisher->Finalize());
114 
115  return 0;
116 }
117 
118 } // namespace notify
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
std::string manifest_
Definition: messages.h:49
const manifest::Manifest * manifest() const
Definition: repository.h:123
static Manifest * LoadMem(const unsigned char *buffer, const unsigned length)
Definition: manifest.cc:68
bool IsHttpUrl(const std::string &path)
Definition: posix.cc:204
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)
Definition: download.h:87
int DoPublish(const std::string &server_url, const std::string &repository_url, bool verbose)
Definition: cmd_pub.cc:34
const unsigned kDownloadRetries
Definition: cmd_pub.cc:28
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:2006
virtual bool Publish(const std::string &msg, const std::string &topic)=0
Definition: repository.cc:856
std::string repository_
Definition: messages.h:48
struct download::JobInfo::@3 destination_mem
LogFacilities
const unsigned kDownloadTimeout
Definition: cmd_pub.cc:27
std::string MakeCanonicalPath(const std::string &path)
Definition: posix.cc:96
static LogFacilities info
static LogFacilities error
virtual bool Finalize()
Definition: publisher.cc:17
virtual void ToJSONString(std::string *s)
Definition: messages.cc:37