Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / crush / CrushLocation.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "include/compat.h"
5 #include "CrushLocation.h"
6 #include "CrushWrapper.h"
7 #include "common/config.h"
8 #include "include/str_list.h"
9 #include "common/debug.h"
10 #include "include/compat.h"
11
12 #include "common/SubProcess.h"
13
14 #include <vector>
15
16 int CrushLocation::update_from_conf()
17 {
18   if (cct->_conf->crush_location.length())
19     return _parse(cct->_conf->crush_location);
20   return 0;
21 }
22
23 int CrushLocation::_parse(const std::string& s)
24 {
25   std::multimap<std::string,std::string> new_crush_location;
26   std::vector<std::string> lvec;
27   get_str_vec(s, ";, \t", lvec);
28   int r = CrushWrapper::parse_loc_multimap(lvec, &new_crush_location);
29   if (r < 0) {
30     lderr(cct) << "warning: crush_location '" << cct->_conf->crush_location
31                << "' does not parse, keeping original crush_location "
32                << loc << dendl;
33     return -EINVAL;
34   }
35   std::lock_guard<std::mutex> l(lock);
36   loc.swap(new_crush_location);
37   lgeneric_dout(cct, 10) << "crush_location is " << loc << dendl;
38   return 0;
39 }
40
41 int CrushLocation::update_from_hook()
42 {
43   if (cct->_conf->crush_location_hook.length() == 0)
44     return 0;
45  
46   if (0 != access(cct->_conf->crush_location_hook.c_str(), R_OK)) {
47     lderr(cct) << "the user define crush location hook: " << cct->_conf->crush_location_hook
48                << " may not exist or can not access it" << dendl;
49     return errno;
50   }
51
52   SubProcessTimed hook(
53     cct->_conf->crush_location_hook.c_str(),
54     SubProcess::CLOSE, SubProcess::PIPE, SubProcess::PIPE,
55     cct->_conf->crush_location_hook_timeout);
56   hook.add_cmd_args(
57     "--cluster", cct->_conf->cluster.c_str(),
58     "--id", cct->_conf->name.get_id().c_str(),
59     "--type", cct->_conf->name.get_type_str(),
60     NULL);
61   int ret = hook.spawn();
62   if (ret != 0) {
63     lderr(cct) << "error: failed run " << cct->_conf->crush_location_hook << ": "
64                << hook.err() << dendl;
65     return ret;
66   }
67
68   bufferlist bl;
69   ret = bl.read_fd(hook.get_stdout(), 100 * 1024);
70   if (ret < 0) {
71     lderr(cct) << "error: failed read stdout from "
72                << cct->_conf->crush_location_hook
73                << ": " << cpp_strerror(-ret) << dendl;
74     bufferlist err;
75     err.read_fd(hook.get_stderr(), 100 * 1024);
76     lderr(cct) << "stderr:\n";
77     err.hexdump(*_dout);
78     *_dout << dendl;
79     return ret;
80   }
81
82   if (hook.join() != 0) {
83     lderr(cct) << "error: failed to join: " << hook.err() << dendl;
84     return -EINVAL;
85   }
86
87   std::string out;
88   bl.copy(0, bl.length(), out);
89   out.erase(out.find_last_not_of(" \n\r\t")+1);
90   return _parse(out);
91 }
92
93 int CrushLocation::init_on_startup()
94 {
95   if (cct->_conf->crush_location.length()) {
96     return update_from_conf();
97   }
98   if (cct->_conf->crush_location_hook.length()) {
99     return update_from_hook();
100   }
101
102   // start with a sane default
103   char hostname[HOST_NAME_MAX + 1];
104   int r = gethostname(hostname, sizeof(hostname)-1);
105   if (r < 0)
106     strcpy(hostname, "unknown_host");
107   // use short hostname
108   for (unsigned i=0; hostname[i]; ++i) {
109     if (hostname[i] == '.') {
110       hostname[i] = '\0';
111       break;
112     }
113   }
114   std::lock_guard<std::mutex> l(lock);
115   loc.clear();
116   loc.insert(make_pair<std::string,std::string>("host", hostname));
117   loc.insert(make_pair<std::string,std::string>("root", "default"));
118   lgeneric_dout(cct, 10) << "crush_location is (default) " << loc << dendl;
119   return 0;
120 }