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