]>
Commit | Line | Data |
---|---|---|
eafe8130 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include <string> | |
5 | #include <vector> | |
6 | #include <cstring> | |
7 | #include <map> | |
8 | ||
f67539c2 | 9 | #include "common/async/context_pool.h" |
eafe8130 TL |
10 | #include "common/ceph_context.h" |
11 | #include "common/ceph_argparse.h" | |
eafe8130 | 12 | #include "common/config.h" |
f67539c2 TL |
13 | #include "global/global_init.h" |
14 | ||
eafe8130 | 15 | #include "auth/KeyRing.h" |
eafe8130 TL |
16 | #include "mon/MonClient.h" |
17 | ||
f67539c2 TL |
18 | #include "mount.ceph.h" |
19 | ||
20effc67 | 20 | using namespace std; |
f67539c2 | 21 | |
eafe8130 TL |
22 | extern "C" void mount_ceph_get_config_info(const char *config_file, |
23 | const char *name, | |
f67539c2 | 24 | bool v2_addrs, |
eafe8130 TL |
25 | struct ceph_config_info *cci) |
26 | { | |
27 | int err; | |
28 | KeyRing keyring; | |
29 | CryptoKey secret; | |
30 | std::string secret_str; | |
31 | std::string monaddrs; | |
32 | vector<const char *> args = { "--name", name }; | |
33 | bool first = true; | |
34 | ||
35 | if (config_file) { | |
36 | args.push_back("--conf"); | |
37 | args.push_back(config_file); | |
38 | } | |
39 | ||
40 | /* Create CephContext */ | |
41 | auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, | |
42 | CODE_ENVIRONMENT_UTILITY, | |
43 | CINIT_FLAG_NO_DAEMON_ACTIONS|CINIT_FLAG_NO_MON_CONFIG); | |
44 | auto& conf = cct->_conf; | |
45 | ||
46 | conf.parse_env(cct->get_module_type()); // environment variables override | |
47 | conf.apply_changes(nullptr); | |
48 | ||
20effc67 TL |
49 | auto fsid = conf.get_val<uuid_d>("fsid"); |
50 | fsid.print(cci->cci_fsid); | |
51 | ||
f67539c2 TL |
52 | ceph::async::io_context_pool ioc(1); |
53 | MonClient monc = MonClient(cct.get(), ioc); | |
eafe8130 TL |
54 | err = monc.build_initial_monmap(); |
55 | if (err) | |
56 | goto scrape_keyring; | |
57 | ||
58 | for (const auto& mon : monc.monmap.addr_mons) { | |
59 | auto& eaddr = mon.first; | |
60 | ||
f67539c2 TL |
61 | /* |
62 | * Filter v1 addrs if we're running in ms_mode=legacy. Filter | |
63 | * v2 addrs for any other ms_mode. | |
64 | */ | |
65 | if (v2_addrs) { | |
66 | if (!eaddr.is_msgr2()) | |
67 | continue; | |
68 | } else { | |
69 | if (!eaddr.is_legacy()) | |
70 | continue; | |
71 | } | |
eafe8130 | 72 | |
1e59de90 | 73 | std::string addr = eaddr.ip_n_port_to_str(); |
eafe8130 TL |
74 | /* If this will overrun cci_mons, stop here */ |
75 | if (monaddrs.length() + 1 + addr.length() + 1 > sizeof(cci->cci_mons)) | |
76 | break; | |
77 | ||
78 | if (first) | |
79 | first = false; | |
80 | else | |
81 | monaddrs += ","; | |
82 | ||
83 | monaddrs += addr; | |
84 | } | |
85 | ||
86 | if (monaddrs.length()) | |
87 | strcpy(cci->cci_mons, monaddrs.c_str()); | |
88 | else | |
9f95a23c | 89 | mount_ceph_debug("Could not discover monitor addresses\n"); |
eafe8130 TL |
90 | |
91 | scrape_keyring: | |
92 | err = keyring.from_ceph_context(cct.get()); | |
93 | if (err) { | |
94 | mount_ceph_debug("keyring.from_ceph_context failed: %d\n", err); | |
95 | return; | |
96 | } | |
97 | ||
98 | if (!keyring.get_secret(conf->name, secret)) { | |
99 | mount_ceph_debug("keyring.get_secret failed\n"); | |
100 | return; | |
101 | } | |
102 | ||
103 | secret.encode_base64(secret_str); | |
104 | ||
105 | if (secret_str.length() + 1 > sizeof(cci->cci_secret)) { | |
106 | mount_ceph_debug("secret is too long\n"); | |
107 | return; | |
108 | } | |
109 | strcpy(cci->cci_secret, secret_str.c_str()); | |
110 | } |