lib.py 7.22 KB
Newer Older
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/python

import json
import time
import ipaddr
import os
import signal
import ConfigParser
import sys
import re
import random
import subprocess
import shutil
import imp
import tempfile
from snfdeploy import massedit


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'


def disable_color():
    HEADER = ''
    OKBLUE = ''
    OKGREEN = ''
    WARNING = ''
    FAIL = ''
    ENDC = ''


if not sys.stdout.isatty():
    disable_color()
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
38

39

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Host(object):
    def __init__(self, hostname, ip, mac, domain):
        self.hostname = hostname
        self.ip = ip
        self.mac = mac
        self.domain = domain

    @property
    def fqdn(self):
        return self.hostname + "." + self.domain

    @property
    def arecord(self):
        return self.hostname + " IN A " + self.ip + "\n"

    @property
    def ptrrecord(self):
        return ".".join(raddr(self.ip)) + " IN PTR " + self.fqdn + ".\n"


class Alias(Host):
    def __init__(self, host, alias):
62
63
        super(Alias, self).__init__(host.hostname, host.ip, host.mac,
                                    host.domain)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
64
65
66
67
        self.alias = alias

    @property
    def cnamerecord(self):
68
69
        return (self.alias + " IN CNAME " + self.hostname + "." +
                self.domain + ".\n")
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    @property
    def fqdn(self):
        return self.alias + "." + self.domain


class Env(object):

    def evaluate(self, filename, section):
        for k, v in self.conf.get_section(filename, section):
            setattr(self, k, v)

    def __init__(self, conf):
        self.conf = conf
        for f, sections in conf.files.iteritems():
            for s in sections:
                self.evaluate(f, s)

        self.node2hostname = dict(conf.get_section("nodes", "hostnames"))
        self.node2ip = dict(conf.get_section("nodes", "ips"))
        self.node2mac = dict(conf.get_section("nodes", "macs"))
91
92
93
94
95
96
97
98
99
100
101
        self.hostnames = [self.node2hostname[n]
                          for n in self.nodes.split(",")]

        self.ips = [self.node2ip[n]
                    for n in self.nodes.split(",")]

        self.cluster_hostnames = [self.node2hostname[n]
                                  for n in self.cluster_nodes.split(",")]

        self.cluster_ips = [self.node2ip[n]
                            for n in self.cluster_nodes.split(",")]
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
102
103
104
105
106
107
108

        self.net = ipaddr.IPNetwork(self.subnet)

        self.nodes_info = {}
        self.hosts_info = {}
        self.ips_info = {}
        for node in self.nodes.split(","):
109
110
111
            host = Host(self.node2hostname[node],
                        self.node2ip[node],
                        self.node2mac[node], self.domain)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
112
113
114
115
116

            self.nodes_info[node] = host
            self.hosts_info[host.hostname] = host
            self.ips_info[host.ip] = host

117
118
        self.cluster = Host(self.cluster_name, self.cluster_ip, None,
                            self.domain)
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
119
120
121
122
123
124
125
        self.master = self.nodes_info[self.master_node]

        self.roles = {}
        for role, node in conf.get_section("synnefo", "roles"):
            self.roles[role] = Alias(self.nodes_info[node], role)
            setattr(self, role, self.roles[role])

126

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
127
128
129
class Conf(object):

    files = {
130
        "nodes": ["network", "info"],
131
        "deploy": ["dirs", "packages", "keys"],
132
133
134
135
        "vcluster": ["cluster", "image"],
        "synnefo": ["cred", "synnefo", "roles"],
        "packages": ["debian", "ganeti", "synnefo", "other"],
        "ganeti": [],
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    }

    def __init__(self, confdir, cluster_name):
        self.confdir = confdir
        self.files["ganeti"] = [cluster_name]
        for f in self.files.keys():
            setattr(self, f, self.read_config(f))

    def read_config(self, f):
        config = ConfigParser.ConfigParser()
        config.optionxform = str
        filename = os.path.join(self.confdir, f) + ".conf"
        config.read(filename)
        return config

    def set(self, conf, section, option, value):
        c = getattr(self, conf)
        c.set(section, option, value)

    def get(self, conf, section, option):
        c = getattr(self, conf)
        return c.get(section, option, True)

    def get_section(self, conf, section):
        c = getattr(self, conf)
        return c.items(section, True)

    def print_config(self):
        for f in self.files.keys():
            getattr(self, f).write(sys.stdout)

    def _configure(self, args):
        for f, sections in self.files.iteritems():
            for s in sections:
                for k, v in self.get_section(f, s):
                    if getattr(args, k, None):
                        self.set(f, s, k, getattr(args, k))

    @classmethod
    def configure(cls, confdir="/etc/snf-deploy",
                  cluster_name="ganeti1", args=None, autoconf=False):

        conf = cls(confdir, cluster_name)
        if args:
            conf._configure(args)
        if autoconf:
            conf.autoconf()

        return conf

    def autoconf(self):
        #domain = get_domain()
        #if domain:
        #    self.nodes.set("network", "domain", get_domain())
        self.nodes.set("network", "subnet", "/".join(get_netinfo()))
        self.nodes.set("network", "gateway", get_default_route()[0])
        self.nodes.set("hostnames", "node1", get_hostname())
        self.nodes.set("ips", "node1", get_netinfo()[0])
        self.nodes.set("info", "nodes", "node1")
        self.nodes.set("info", "public_iface", get_default_route()[1])


def debug(host, msg):

200
    print HEADER + host + \
201
        OKBLUE + ": " + msg + ENDC
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217


def check_pidfile(pidfile):
    print("Checking pidfile " + pidfile)
    try:
        f = open(pidfile, "r")
        pid = f.readline()
        os.kill(int(pid), signal.SIGKILL)
        f.close()
        os.remove(pidfile)
        time.sleep(5)
    except:
        pass


def randomMAC():
218
219
220
221
    mac = [0x52, 0x54, 0x56,
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff),
           random.randint(0x00, 0xff)]
Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    return ':'.join(map(lambda x: "%02x" % x, mac))


def create_dir(d, clean=False):
    os.system("mkdir -p " + d)
    if clean:
        try:
            os.system("rm -f %s/*" % d)
        except:
            pass


def get_netinfo():
    _, pdev = get_default_route()
    r = re.compile(".*inet (\S+)/(\S+).*")
237
238
239
    s = subprocess.Popen(['ip', 'addr', 'show', 'dev', pdev],
                         stdout=subprocess.PIPE)

Dimitris Aragiorgis's avatar
Dimitris Aragiorgis committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
    for line in s.stdout.readlines():
        match = r.search(line)
        if match:
            ip, size = match.groups()
            break

    return ip, size


def get_hostname():
    s = subprocess.Popen(['hostname', '-s'], stdout=subprocess.PIPE)
    return s.stdout.readline().strip()


def get_domain():
    s = subprocess.Popen(['hostname', '-d'], stdout=subprocess.PIPE)
    return s.stdout.readline().strip()


def get_default_route():
    r = re.compile("default via (\S+) dev (\S+)")
    s = subprocess.Popen(['ip', 'route'], stdout=subprocess.PIPE)
    for line in s.stdout.readlines():
        match = r.search(line)
        if match:
            gw, dev = match.groups()
            break

    return (gw, dev)


def import_conf_file(filename, directory):
    return imp.load_module(filename, *imp.find_module(filename, [directory]))


def raddr(addr):
276
    return list(reversed(addr.replace("/", "-").split(".")))