forked from ceph/cbt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
137 lines (109 loc) · 3.91 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import argparse
import yaml
import sys
import os
import socket
import logging
logger = logging.getLogger("cbt")
common = {}
cluster = {}
client_endpoints = {}
benchmarks = {}
monitoring_profiles = {}
def _handle_monitoring_legacy():
"""
Inject collectl even if the config says nothing about it to preserve
compatibility with current CBT's configuration files.
"""
global monitoring_profiles
if 'collectl' not in monitoring_profiles:
monitoring_profiles['collectl'] = {}
def initialize(ctx):
global common, cluster, client_endpoints, benchmarks, monitoring_profiles
config = {}
try:
with open(ctx.config_file) as f:
config = yaml.safe_load(f)
except IOError as e:
raise argparse.ArgumentTypeError(str(e))
common = config.get('common', {})
cluster = config.get('cluster', {})
client_endpoints = config.get('client_endpoints', {})
benchmarks = config.get('benchmarks', {})
monitoring_profiles = config.get('monitoring_profiles', dict(collectl={}))
if not cluster:
shutdown('No cluster section found in config file, bailing.')
if not benchmarks:
shutdown('No benchmarks section found in config file, bailing.')
# set the archive_dir from the commandline if present
if ctx.archive:
cluster['archive_dir'] = ctx.archive
if 'archive_dir' not in cluster:
shutdown('No archive dir has been set.')
_handle_monitoring_legacy()
# store cbt configuration in the archive directory
cbt_results = os.path.join(cluster['archive_dir'], 'results')
config_file = os.path.join(cbt_results, 'cbt_config.yaml')
if not os.path.exists(cluster['archive_dir']):
os.makedirs(cluster['archive_dir'])
if not os.path.exists(cbt_results):
os.makedirs(cbt_results)
if not os.path.exists(config_file):
config_dict = dict(cluster=cluster, benchmarks=benchmarks, monitoring_profiles=monitoring_profiles)
with open(config_file, 'w') as fd:
yaml.dump(config_dict, fd, default_flow_style=False)
# set the tmp_dir if not set.
if 'tmp_dir' not in cluster:
cluster['tmp_dir'] = '/tmp/cbt.%s' % os.getpid()
# set the ceph.conf file from the commandline if present
if ctx.conf:
cluster['conf_file'] = ctx.conf
# If no conf file is set, default to /etc/ceph/ceph.conf
# FIXME: We shouldn't have cluster specific defaults in settings.
# Eventually make a base class with specific cluster implementations.
if 'conf_file' not in cluster:
cluster['conf_file'] = '/etc/ceph/ceph.conf'
try:
f = open(cluster['conf_file'])
f.close()
except IOError as e:
shutdown('Was not able to access conf file: %s' % cluster['conf_file'])
def host_info(host):
ret = {}
user = cluster.get('user')
if '@' in host:
user, host = host.split('@')
ret['user'] = user
if user:
ret['user'] = user
ret['host'] = host
ret['addr'] = socket.gethostbyname(host)
return ret
def getnodes(*nodelists):
nodes = []
for nodelist in nodelists:
cur = cluster.get(nodelist, [])
if isinstance(cur, str):
nodes.append(cur)
elif isinstance(cur, dict):
nodes.extend(list(cur.keys()))
elif isinstance(cur, list):
nodes.extend(cur)
else:
raise ValueError("Can't process nodes of type %s - unknown set type: %r",
nodelist, cur)
str_nodes = ','.join(uniquenodes(nodes))
logger.debug("Nodes : %s", str_nodes)
return str_nodes
def uniquenodes(nodes):
unique = [node for node in nodes if node]
ret = []
for host in unique:
info = host_info(host)
host_str = info['host']
if 'user' in info:
host_str = "%s@%s" % (info['user'], host_str)
ret.append(host_str)
return set(ret)
def shutdown(message):
sys.exit(message)