root / snf-deploy / snfdeploy / lib.py @ eaae0a32
History | View | Annotate | Download (7.3 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
|
3 |
import json |
4 |
import time |
5 |
import ipaddr |
6 |
import os |
7 |
import signal |
8 |
import ConfigParser |
9 |
import sys |
10 |
import re |
11 |
import random |
12 |
import subprocess |
13 |
import shutil |
14 |
import imp |
15 |
import tempfile |
16 |
from snfdeploy import massedit |
17 |
|
18 |
|
19 |
HEADER = '\033[95m'
|
20 |
OKBLUE = '\033[94m'
|
21 |
OKGREEN = '\033[92m'
|
22 |
WARNING = '\033[93m'
|
23 |
FAIL = '\033[91m'
|
24 |
ENDC = '\033[0m'
|
25 |
|
26 |
|
27 |
def disable_color(): |
28 |
global HEADER
|
29 |
global OKBLUE
|
30 |
global OKGREEN
|
31 |
global WARNING
|
32 |
global FAIL
|
33 |
global ENDC
|
34 |
|
35 |
HEADER = ''
|
36 |
OKBLUE = ''
|
37 |
OKGREEN = ''
|
38 |
WARNING = ''
|
39 |
FAIL = ''
|
40 |
ENDC = ''
|
41 |
|
42 |
|
43 |
if not sys.stdout.isatty(): |
44 |
disable_color() |
45 |
|
46 |
|
47 |
class Host(object): |
48 |
def __init__(self, hostname, ip, mac, domain): |
49 |
self.hostname = hostname
|
50 |
self.ip = ip
|
51 |
self.mac = mac
|
52 |
self.domain = domain
|
53 |
|
54 |
@property
|
55 |
def fqdn(self): |
56 |
return self.hostname + "." + self.domain |
57 |
|
58 |
@property
|
59 |
def arecord(self): |
60 |
return self.hostname + " IN A " + self.ip + "\n" |
61 |
|
62 |
@property
|
63 |
def ptrrecord(self): |
64 |
return ".".join(raddr(self.ip)) + " IN PTR " + self.fqdn + ".\n" |
65 |
|
66 |
|
67 |
class Alias(Host): |
68 |
def __init__(self, host, alias): |
69 |
super(Alias, self).__init__(host.hostname, host.ip, host.mac, |
70 |
host.domain) |
71 |
self.alias = alias
|
72 |
|
73 |
@property
|
74 |
def cnamerecord(self): |
75 |
return (self.alias + " IN CNAME " + self.hostname + "." + |
76 |
self.domain + ".\n") |
77 |
|
78 |
@property
|
79 |
def fqdn(self): |
80 |
return self.alias + "." + self.domain |
81 |
|
82 |
|
83 |
class Env(object): |
84 |
|
85 |
def evaluate(self, filename, section): |
86 |
for k, v in self.conf.get_section(filename, section): |
87 |
setattr(self, k, v) |
88 |
|
89 |
def __init__(self, conf): |
90 |
self.conf = conf
|
91 |
for f, sections in conf.files.iteritems(): |
92 |
for s in sections: |
93 |
self.evaluate(f, s)
|
94 |
|
95 |
self.node2hostname = dict(conf.get_section("nodes", "hostnames")) |
96 |
self.node2ip = dict(conf.get_section("nodes", "ips")) |
97 |
self.node2mac = dict(conf.get_section("nodes", "macs")) |
98 |
self.hostnames = [self.node2hostname[n] |
99 |
for n in self.nodes.split(",")] |
100 |
|
101 |
self.ips = [self.node2ip[n] |
102 |
for n in self.nodes.split(",")] |
103 |
|
104 |
self.cluster_hostnames = [self.node2hostname[n] |
105 |
for n in self.cluster_nodes.split(",")] |
106 |
|
107 |
self.cluster_ips = [self.node2ip[n] |
108 |
for n in self.cluster_nodes.split(",")] |
109 |
|
110 |
self.net = ipaddr.IPNetwork(self.subnet) |
111 |
|
112 |
self.nodes_info = {}
|
113 |
self.hosts_info = {}
|
114 |
self.ips_info = {}
|
115 |
for node in self.nodes.split(","): |
116 |
host = Host(self.node2hostname[node],
|
117 |
self.node2ip[node],
|
118 |
self.node2mac[node], self.domain) |
119 |
|
120 |
self.nodes_info[node] = host
|
121 |
self.hosts_info[host.hostname] = host
|
122 |
self.ips_info[host.ip] = host
|
123 |
|
124 |
self.cluster = Host(self.cluster_name, self.cluster_ip, None, |
125 |
self.domain)
|
126 |
self.master = self.nodes_info[self.master_node] |
127 |
|
128 |
self.roles = {}
|
129 |
for role, node in conf.get_section("synnefo", "roles"): |
130 |
self.roles[role] = Alias(self.nodes_info[node], role) |
131 |
setattr(self, role, self.roles[role]) |
132 |
|
133 |
|
134 |
class Conf(object): |
135 |
|
136 |
files = { |
137 |
"nodes": ["network", "info"], |
138 |
"deploy": ["dirs", "packages", "keys", "options"], |
139 |
"vcluster": ["cluster", "image"], |
140 |
"synnefo": ["cred", "synnefo", "roles"], |
141 |
"packages": ["debian", "ganeti", "synnefo", "other"], |
142 |
"ganeti": [],
|
143 |
} |
144 |
|
145 |
def __init__(self, confdir, cluster_name): |
146 |
self.confdir = confdir
|
147 |
self.files["ganeti"] = [cluster_name] |
148 |
for f in self.files.keys(): |
149 |
setattr(self, f, self.read_config(f)) |
150 |
|
151 |
def read_config(self, f): |
152 |
config = ConfigParser.ConfigParser() |
153 |
config.optionxform = str
|
154 |
filename = os.path.join(self.confdir, f) + ".conf" |
155 |
config.read(filename) |
156 |
return config
|
157 |
|
158 |
def set(self, conf, section, option, value): |
159 |
c = getattr(self, conf) |
160 |
c.set(section, option, value) |
161 |
|
162 |
def get(self, conf, section, option): |
163 |
c = getattr(self, conf) |
164 |
return c.get(section, option, True) |
165 |
|
166 |
def get_section(self, conf, section): |
167 |
c = getattr(self, conf) |
168 |
return c.items(section, True) |
169 |
|
170 |
def print_config(self): |
171 |
for f in self.files.keys(): |
172 |
getattr(self, f).write(sys.stdout) |
173 |
|
174 |
def _configure(self, args): |
175 |
for f, sections in self.files.iteritems(): |
176 |
for s in sections: |
177 |
for k, v in self.get_section(f, s): |
178 |
if getattr(args, k, None): |
179 |
self.set(f, s, k, getattr(args, k)) |
180 |
|
181 |
@classmethod
|
182 |
def configure(cls, confdir="/etc/snf-deploy", |
183 |
cluster_name="ganeti1", args=None, autoconf=False): |
184 |
|
185 |
conf = cls(confdir, cluster_name) |
186 |
if args:
|
187 |
conf._configure(args) |
188 |
if autoconf:
|
189 |
conf.autoconf() |
190 |
|
191 |
return conf
|
192 |
|
193 |
def autoconf(self): |
194 |
#domain = get_domain()
|
195 |
#if domain:
|
196 |
# self.nodes.set("network", "domain", get_domain())
|
197 |
self.nodes.set("network", "subnet", "/".join(get_netinfo())) |
198 |
self.nodes.set("network", "gateway", get_default_route()[0]) |
199 |
self.nodes.set("hostnames", "node1", get_hostname()) |
200 |
self.nodes.set("ips", "node1", get_netinfo()[0]) |
201 |
self.nodes.set("info", "nodes", "node1") |
202 |
self.nodes.set("info", "public_iface", get_default_route()[1]) |
203 |
|
204 |
|
205 |
def debug(host, msg): |
206 |
|
207 |
print HEADER + host + \
|
208 |
OKBLUE + ": " + msg + ENDC
|
209 |
|
210 |
|
211 |
def check_pidfile(pidfile): |
212 |
print("Checking pidfile " + pidfile)
|
213 |
try:
|
214 |
f = open(pidfile, "r") |
215 |
pid = f.readline() |
216 |
os.kill(int(pid), signal.SIGKILL)
|
217 |
f.close() |
218 |
os.remove(pidfile) |
219 |
time.sleep(5)
|
220 |
except:
|
221 |
pass
|
222 |
|
223 |
|
224 |
def randomMAC(): |
225 |
mac = [0x52, 0x54, 0x56, |
226 |
random.randint(0x00, 0xff), |
227 |
random.randint(0x00, 0xff), |
228 |
random.randint(0x00, 0xff)] |
229 |
return ':'.join(map(lambda x: "%02x" % x, mac)) |
230 |
|
231 |
|
232 |
def create_dir(d, clean=False): |
233 |
os.system("mkdir -p " + d)
|
234 |
if clean:
|
235 |
try:
|
236 |
os.system("rm -f %s/*" % d)
|
237 |
except:
|
238 |
pass
|
239 |
|
240 |
|
241 |
def get_netinfo(): |
242 |
_, pdev = get_default_route() |
243 |
r = re.compile(".*inet (\S+)/(\S+).*")
|
244 |
s = subprocess.Popen(['ip', 'addr', 'show', 'dev', pdev], |
245 |
stdout=subprocess.PIPE) |
246 |
|
247 |
for line in s.stdout.readlines(): |
248 |
match = r.search(line) |
249 |
if match:
|
250 |
ip, size = match.groups() |
251 |
break
|
252 |
|
253 |
return ip, size
|
254 |
|
255 |
|
256 |
def get_hostname(): |
257 |
s = subprocess.Popen(['hostname', '-s'], stdout=subprocess.PIPE) |
258 |
return s.stdout.readline().strip()
|
259 |
|
260 |
|
261 |
def get_domain(): |
262 |
s = subprocess.Popen(['hostname', '-d'], stdout=subprocess.PIPE) |
263 |
return s.stdout.readline().strip()
|
264 |
|
265 |
|
266 |
def get_default_route(): |
267 |
r = re.compile("default via (\S+) dev (\S+)")
|
268 |
s = subprocess.Popen(['ip', 'route'], stdout=subprocess.PIPE) |
269 |
for line in s.stdout.readlines(): |
270 |
match = r.search(line) |
271 |
if match:
|
272 |
gw, dev = match.groups() |
273 |
break
|
274 |
|
275 |
return (gw, dev)
|
276 |
|
277 |
|
278 |
def import_conf_file(filename, directory): |
279 |
return imp.load_module(filename, *imp.find_module(filename, [directory]))
|
280 |
|
281 |
|
282 |
def raddr(addr): |
283 |
return list(reversed(addr.replace("/", "-").split("."))) |