Statistics
| Branch: | Tag: | Revision:

root / snf-deploy / snfdeploy / lib.py @ 954ff0d8

History | View | Annotate | Download (10.6 kB)

1 0ac84a9a Dimitris Aragiorgis
#!/usr/bin/python
2 0ac84a9a Dimitris Aragiorgis
3 3c3bccab Dimitris Aragiorgis
# Copyright (C) 2010, 2011, 2012, 2013 GRNET S.A. All rights reserved.
4 3c3bccab Dimitris Aragiorgis
#
5 3c3bccab Dimitris Aragiorgis
# Redistribution and use in source and binary forms, with or
6 3c3bccab Dimitris Aragiorgis
# without modification, are permitted provided that the following
7 3c3bccab Dimitris Aragiorgis
# conditions are met:
8 3c3bccab Dimitris Aragiorgis
#
9 3c3bccab Dimitris Aragiorgis
#   1. Redistributions of source code must retain the above
10 3c3bccab Dimitris Aragiorgis
#      copyright notice, this list of conditions and the following
11 3c3bccab Dimitris Aragiorgis
#      disclaimer.
12 3c3bccab Dimitris Aragiorgis
#
13 3c3bccab Dimitris Aragiorgis
#   2. Redistributions in binary form must reproduce the above
14 3c3bccab Dimitris Aragiorgis
#      copyright notice, this list of conditions and the following
15 3c3bccab Dimitris Aragiorgis
#      disclaimer in the documentation and/or other materials
16 3c3bccab Dimitris Aragiorgis
#      provided with the distribution.
17 3c3bccab Dimitris Aragiorgis
#
18 3c3bccab Dimitris Aragiorgis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 3c3bccab Dimitris Aragiorgis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 3c3bccab Dimitris Aragiorgis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 3c3bccab Dimitris Aragiorgis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A. OR
22 3c3bccab Dimitris Aragiorgis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 3c3bccab Dimitris Aragiorgis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 3c3bccab Dimitris Aragiorgis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 3c3bccab Dimitris Aragiorgis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 3c3bccab Dimitris Aragiorgis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 3c3bccab Dimitris Aragiorgis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 3c3bccab Dimitris Aragiorgis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 3c3bccab Dimitris Aragiorgis
# POSSIBILITY OF SUCH DAMAGE.
30 3c3bccab Dimitris Aragiorgis
#
31 3c3bccab Dimitris Aragiorgis
# The views and conclusions contained in the software and
32 3c3bccab Dimitris Aragiorgis
# documentation are those of the authors and should not be
33 3c3bccab Dimitris Aragiorgis
# interpreted as representing official policies, either expressed
34 3c3bccab Dimitris Aragiorgis
# or implied, of GRNET S.A.
35 3c3bccab Dimitris Aragiorgis
36 0ac84a9a Dimitris Aragiorgis
import time
37 0ac84a9a Dimitris Aragiorgis
import ipaddr
38 0ac84a9a Dimitris Aragiorgis
import os
39 0ac84a9a Dimitris Aragiorgis
import signal
40 0ac84a9a Dimitris Aragiorgis
import ConfigParser
41 0ac84a9a Dimitris Aragiorgis
import sys
42 0ac84a9a Dimitris Aragiorgis
import re
43 0ac84a9a Dimitris Aragiorgis
import random
44 0ac84a9a Dimitris Aragiorgis
import subprocess
45 0ac84a9a Dimitris Aragiorgis
import imp
46 0ac84a9a Dimitris Aragiorgis
47 0ac84a9a Dimitris Aragiorgis
48 1bc6d467 Dionysis Grigoropoulos
HEADER = '\033[95m'
49 1bc6d467 Dionysis Grigoropoulos
OKBLUE = '\033[94m'
50 1bc6d467 Dionysis Grigoropoulos
OKGREEN = '\033[92m'
51 1bc6d467 Dionysis Grigoropoulos
WARNING = '\033[93m'
52 1bc6d467 Dionysis Grigoropoulos
FAIL = '\033[91m'
53 1bc6d467 Dionysis Grigoropoulos
ENDC = '\033[0m'
54 1bc6d467 Dionysis Grigoropoulos
55 1bc6d467 Dionysis Grigoropoulos
56 1bc6d467 Dionysis Grigoropoulos
def disable_color():
57 eaae0a32 Erethon
    global HEADER
58 eaae0a32 Erethon
    global OKBLUE
59 eaae0a32 Erethon
    global OKGREEN
60 eaae0a32 Erethon
    global WARNING
61 eaae0a32 Erethon
    global FAIL
62 eaae0a32 Erethon
    global ENDC
63 eaae0a32 Erethon
64 1bc6d467 Dionysis Grigoropoulos
    HEADER = ''
65 1bc6d467 Dionysis Grigoropoulos
    OKBLUE = ''
66 1bc6d467 Dionysis Grigoropoulos
    OKGREEN = ''
67 1bc6d467 Dionysis Grigoropoulos
    WARNING = ''
68 1bc6d467 Dionysis Grigoropoulos
    FAIL = ''
69 1bc6d467 Dionysis Grigoropoulos
    ENDC = ''
70 1bc6d467 Dionysis Grigoropoulos
71 1bc6d467 Dionysis Grigoropoulos
72 1bc6d467 Dionysis Grigoropoulos
if not sys.stdout.isatty():
73 1bc6d467 Dionysis Grigoropoulos
    disable_color()
74 0ac84a9a Dimitris Aragiorgis
75 7d6fed3a Dionysis Grigoropoulos
76 0ac84a9a Dimitris Aragiorgis
class Host(object):
77 3c3bccab Dimitris Aragiorgis
    def __init__(self, hostname, ip, mac, domain, os, passwd):
78 0ac84a9a Dimitris Aragiorgis
        self.hostname = hostname
79 0ac84a9a Dimitris Aragiorgis
        self.ip = ip
80 0ac84a9a Dimitris Aragiorgis
        self.mac = mac
81 0ac84a9a Dimitris Aragiorgis
        self.domain = domain
82 f1b4fdfc Dimitris Aragiorgis
        self.os = os
83 3c3bccab Dimitris Aragiorgis
        self.passwd = passwd
84 0ac84a9a Dimitris Aragiorgis
85 0ac84a9a Dimitris Aragiorgis
    @property
86 0ac84a9a Dimitris Aragiorgis
    def fqdn(self):
87 0ac84a9a Dimitris Aragiorgis
        return self.hostname + "." + self.domain
88 0ac84a9a Dimitris Aragiorgis
89 0ac84a9a Dimitris Aragiorgis
    @property
90 0ac84a9a Dimitris Aragiorgis
    def arecord(self):
91 3c3bccab Dimitris Aragiorgis
        return self.fqdn + " 300 A " + self.ip
92 0ac84a9a Dimitris Aragiorgis
93 0ac84a9a Dimitris Aragiorgis
    @property
94 0ac84a9a Dimitris Aragiorgis
    def ptrrecord(self):
95 3c3bccab Dimitris Aragiorgis
        return ".".join(raddr(self.ip)) + ".in-addr.arpa 300 PTR " + self.fqdn
96 3c3bccab Dimitris Aragiorgis
97 3c3bccab Dimitris Aragiorgis
    @property
98 3c3bccab Dimitris Aragiorgis
    def cnamerecord(self):
99 3c3bccab Dimitris Aragiorgis
        return ""
100 0ac84a9a Dimitris Aragiorgis
101 0ac84a9a Dimitris Aragiorgis
102 0ac84a9a Dimitris Aragiorgis
class Alias(Host):
103 0ac84a9a Dimitris Aragiorgis
    def __init__(self, host, alias):
104 7d6fed3a Dionysis Grigoropoulos
        super(Alias, self).__init__(host.hostname, host.ip, host.mac,
105 3c3bccab Dimitris Aragiorgis
                                    host.domain, host.os, host.passwd)
106 0ac84a9a Dimitris Aragiorgis
        self.alias = alias
107 0ac84a9a Dimitris Aragiorgis
108 0ac84a9a Dimitris Aragiorgis
    @property
109 0ac84a9a Dimitris Aragiorgis
    def cnamerecord(self):
110 3c3bccab Dimitris Aragiorgis
        return self.fqdn + " 300 CNAME " + self.hostname + "." + self.domain
111 3c3bccab Dimitris Aragiorgis
112 3c3bccab Dimitris Aragiorgis
    @property
113 3c3bccab Dimitris Aragiorgis
    def ptrrecord(self):
114 3c3bccab Dimitris Aragiorgis
        return ""
115 3c3bccab Dimitris Aragiorgis
116 3c3bccab Dimitris Aragiorgis
    @property
117 3c3bccab Dimitris Aragiorgis
    def arecord(self):
118 3c3bccab Dimitris Aragiorgis
        return ""
119 0ac84a9a Dimitris Aragiorgis
120 0ac84a9a Dimitris Aragiorgis
    @property
121 0ac84a9a Dimitris Aragiorgis
    def fqdn(self):
122 0ac84a9a Dimitris Aragiorgis
        return self.alias + "." + self.domain
123 0ac84a9a Dimitris Aragiorgis
124 0ac84a9a Dimitris Aragiorgis
125 0ac84a9a Dimitris Aragiorgis
class Env(object):
126 0ac84a9a Dimitris Aragiorgis
127 24dc98e2 Dimitris Aragiorgis
    def update_packages(self, os):
128 24dc98e2 Dimitris Aragiorgis
        for section in self.conf.files[os]:
129 ca1de37b Ilias Tsitsimpis
            self.evaluate(os, section)
130 24dc98e2 Dimitris Aragiorgis
131 0ac84a9a Dimitris Aragiorgis
    def evaluate(self, filename, section):
132 0ac84a9a Dimitris Aragiorgis
        for k, v in self.conf.get_section(filename, section):
133 0ac84a9a Dimitris Aragiorgis
            setattr(self, k, v)
134 0ac84a9a Dimitris Aragiorgis
135 0ac84a9a Dimitris Aragiorgis
    def __init__(self, conf):
136 0ac84a9a Dimitris Aragiorgis
        self.conf = conf
137 0ac84a9a Dimitris Aragiorgis
        for f, sections in conf.files.iteritems():
138 0ac84a9a Dimitris Aragiorgis
            for s in sections:
139 0ac84a9a Dimitris Aragiorgis
                self.evaluate(f, s)
140 0ac84a9a Dimitris Aragiorgis
141 0ac84a9a Dimitris Aragiorgis
        self.node2hostname = dict(conf.get_section("nodes", "hostnames"))
142 0ac84a9a Dimitris Aragiorgis
        self.node2ip = dict(conf.get_section("nodes", "ips"))
143 0ac84a9a Dimitris Aragiorgis
        self.node2mac = dict(conf.get_section("nodes", "macs"))
144 f1b4fdfc Dimitris Aragiorgis
        self.node2os = dict(conf.get_section("nodes", "os"))
145 3c3bccab Dimitris Aragiorgis
        self.node2passwd = dict(conf.get_section("nodes", "passwords"))
146 3c3bccab Dimitris Aragiorgis
147 7d6fed3a Dionysis Grigoropoulos
        self.hostnames = [self.node2hostname[n]
148 7d6fed3a Dionysis Grigoropoulos
                          for n in self.nodes.split(",")]
149 7d6fed3a Dionysis Grigoropoulos
150 7d6fed3a Dionysis Grigoropoulos
        self.ips = [self.node2ip[n]
151 7d6fed3a Dionysis Grigoropoulos
                    for n in self.nodes.split(",")]
152 7d6fed3a Dionysis Grigoropoulos
153 0ac84a9a Dimitris Aragiorgis
        self.net = ipaddr.IPNetwork(self.subnet)
154 0ac84a9a Dimitris Aragiorgis
155 0ac84a9a Dimitris Aragiorgis
        self.nodes_info = {}
156 0ac84a9a Dimitris Aragiorgis
        self.hosts_info = {}
157 0ac84a9a Dimitris Aragiorgis
        self.ips_info = {}
158 0ac84a9a Dimitris Aragiorgis
        for node in self.nodes.split(","):
159 7d6fed3a Dionysis Grigoropoulos
            host = Host(self.node2hostname[node],
160 7d6fed3a Dionysis Grigoropoulos
                        self.node2ip[node],
161 3c3bccab Dimitris Aragiorgis
                        self.node2mac[node], self.domain, self.node2os[node],
162 3c3bccab Dimitris Aragiorgis
                        self.node2passwd[node])
163 0ac84a9a Dimitris Aragiorgis
164 0ac84a9a Dimitris Aragiorgis
            self.nodes_info[node] = host
165 0ac84a9a Dimitris Aragiorgis
            self.hosts_info[host.hostname] = host
166 0ac84a9a Dimitris Aragiorgis
            self.ips_info[host.ip] = host
167 0ac84a9a Dimitris Aragiorgis
168 7d6fed3a Dionysis Grigoropoulos
        self.cluster = Host(self.cluster_name, self.cluster_ip, None,
169 3c3bccab Dimitris Aragiorgis
                            self.domain, None, None)
170 3c3bccab Dimitris Aragiorgis
171 3c3bccab Dimitris Aragiorgis
        # This is needed because "".split(",") -> ['']
172 3c3bccab Dimitris Aragiorgis
        if self.cluster_nodes:
173 d4f28d8f Dimitris Aragiorgis
            self.cluster_nodes = self.cluster_nodes.split(",")
174 3c3bccab Dimitris Aragiorgis
        else:
175 d4f28d8f Dimitris Aragiorgis
            self.cluster_nodes = []
176 3c3bccab Dimitris Aragiorgis
177 3c3bccab Dimitris Aragiorgis
        self.cluster_hostnames = [self.node2hostname[n]
178 3c3bccab Dimitris Aragiorgis
                                  for n in self.cluster_nodes]
179 3c3bccab Dimitris Aragiorgis
180 3c3bccab Dimitris Aragiorgis
        self.cluster_ips = [self.node2ip[n]
181 3c3bccab Dimitris Aragiorgis
                            for n in self.cluster_nodes]
182 3c3bccab Dimitris Aragiorgis
183 0ac84a9a Dimitris Aragiorgis
        self.master = self.nodes_info[self.master_node]
184 0ac84a9a Dimitris Aragiorgis
185 3c3bccab Dimitris Aragiorgis
        self.roles_info = {}
186 0ac84a9a Dimitris Aragiorgis
        for role, node in conf.get_section("synnefo", "roles"):
187 3c3bccab Dimitris Aragiorgis
            self.roles_info[role] = Alias(self.nodes_info[node], role)
188 3c3bccab Dimitris Aragiorgis
            setattr(self, role, self.roles_info[role])
189 3c3bccab Dimitris Aragiorgis
190 3c3bccab Dimitris Aragiorgis
        self.astakos = self.accounts
191 3c3bccab Dimitris Aragiorgis
        # This is the nodes that get nfs mount points
192 3c3bccab Dimitris Aragiorgis
        self.mount = self.ips[:]
193 3c3bccab Dimitris Aragiorgis
        self.mount.remove(self.pithos.ip)
194 3c3bccab Dimitris Aragiorgis
195 3c3bccab Dimitris Aragiorgis
196 3c3bccab Dimitris Aragiorgis
class Status(object):
197 3c3bccab Dimitris Aragiorgis
    STATUSES = [
198 3c3bccab Dimitris Aragiorgis
        "check",
199 3c3bccab Dimitris Aragiorgis
        "prepare",
200 3c3bccab Dimitris Aragiorgis
        "install",
201 3c3bccab Dimitris Aragiorgis
        "restart",
202 3c3bccab Dimitris Aragiorgis
        "initialize",
203 3c3bccab Dimitris Aragiorgis
        "test",
204 3c3bccab Dimitris Aragiorgis
        "ok",
205 3c3bccab Dimitris Aragiorgis
        ]
206 3c3bccab Dimitris Aragiorgis
207 3c3bccab Dimitris Aragiorgis
    def create_section(self, ip):
208 3c3bccab Dimitris Aragiorgis
        try:
209 3c3bccab Dimitris Aragiorgis
            section = self.config.items(ip, True)
210 3c3bccab Dimitris Aragiorgis
        except ConfigParser.NoSectionError:
211 3c3bccab Dimitris Aragiorgis
            self.config.add_section(ip)
212 3c3bccab Dimitris Aragiorgis
213 3c3bccab Dimitris Aragiorgis
    def __init__(self, args):
214 3c3bccab Dimitris Aragiorgis
        self.config = ConfigParser.ConfigParser()
215 3c3bccab Dimitris Aragiorgis
        self.config.optionxform = str
216 3c3bccab Dimitris Aragiorgis
        self.statusfile = os.path.join(args.confdir, "status.conf")
217 3c3bccab Dimitris Aragiorgis
        self.config.read(self.statusfile)
218 3c3bccab Dimitris Aragiorgis
219 3c3bccab Dimitris Aragiorgis
    def check_status(self, ip, component_class):
220 3c3bccab Dimitris Aragiorgis
        try:
221 3c3bccab Dimitris Aragiorgis
            return self.config.get(ip, component_class.__name__, True)
222 3c3bccab Dimitris Aragiorgis
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
223 3c3bccab Dimitris Aragiorgis
            return None
224 3c3bccab Dimitris Aragiorgis
225 3c3bccab Dimitris Aragiorgis
    def update_status(self, ip, component_class, status):
226 3c3bccab Dimitris Aragiorgis
        self.create_section(ip)
227 3c3bccab Dimitris Aragiorgis
        self.config.set(ip, component_class.__name__, status)
228 3c3bccab Dimitris Aragiorgis
229 3c3bccab Dimitris Aragiorgis
    def write_status(self):
230 3c3bccab Dimitris Aragiorgis
        with open(self.statusfile, 'wb') as configfile:
231 3c3bccab Dimitris Aragiorgis
            self.config.write(configfile)
232 0ac84a9a Dimitris Aragiorgis
233 7d6fed3a Dionysis Grigoropoulos
234 0ac84a9a Dimitris Aragiorgis
class Conf(object):
235 0ac84a9a Dimitris Aragiorgis
236 0ac84a9a Dimitris Aragiorgis
    files = {
237 7d6fed3a Dionysis Grigoropoulos
        "nodes": ["network", "info"],
238 bd9b6f21 Christos Stavrakakis
        "deploy": ["dirs", "packages", "keys", "options"],
239 9f93a6fb Dimitris Aragiorgis
        "vcluster": ["cluster", "image", "network"],
240 7d6fed3a Dionysis Grigoropoulos
        "synnefo": ["cred", "synnefo", "roles"],
241 24dc98e2 Dimitris Aragiorgis
        "wheezy": ["debian", "ganeti", "synnefo", "other"],
242 0ac84a9a Dimitris Aragiorgis
    }
243 3bae85da Dimitris Aragiorgis
    confdir = "/etc/snf-deploy"
244 0ac84a9a Dimitris Aragiorgis
245 3bae85da Dimitris Aragiorgis
    def get_ganeti(self, cluster_name):
246 0ac84a9a Dimitris Aragiorgis
        self.files["ganeti"] = [cluster_name]
247 3bae85da Dimitris Aragiorgis
248 3bae85da Dimitris Aragiorgis
    def __init__(self, args):
249 3bae85da Dimitris Aragiorgis
        self.confdir = args.confdir
250 3bae85da Dimitris Aragiorgis
        self.get_ganeti(args.cluster_name)
251 0ac84a9a Dimitris Aragiorgis
        for f in self.files.keys():
252 0ac84a9a Dimitris Aragiorgis
            setattr(self, f, self.read_config(f))
253 3bae85da Dimitris Aragiorgis
        for f, sections in self.files.iteritems():
254 3bae85da Dimitris Aragiorgis
            for s in sections:
255 3bae85da Dimitris Aragiorgis
                for k, v in self.get_section(f, s):
256 3bae85da Dimitris Aragiorgis
                    if getattr(args, k, None):
257 3bae85da Dimitris Aragiorgis
                        self.set(f, s, k, getattr(args, k))
258 3bae85da Dimitris Aragiorgis
        if args.autoconf:
259 3bae85da Dimitris Aragiorgis
            self.autoconf()
260 3bae85da Dimitris Aragiorgis
261 3bae85da Dimitris Aragiorgis
    def autoconf(self):
262 3bae85da Dimitris Aragiorgis
        #domain = get_domain()
263 3bae85da Dimitris Aragiorgis
        #if domain:
264 3bae85da Dimitris Aragiorgis
        #    self.nodes.set("network", "domain", get_domain())
265 3bae85da Dimitris Aragiorgis
        # self.nodes.set("network", "subnet", "/".join(get_netinfo()))
266 3bae85da Dimitris Aragiorgis
        # self.nodes.set("network", "gateway", get_default_route()[0])
267 3bae85da Dimitris Aragiorgis
        self.nodes.set("hostnames", "node1", get_hostname())
268 3bae85da Dimitris Aragiorgis
        self.nodes.set("ips", "node1", get_netinfo()[0])
269 3bae85da Dimitris Aragiorgis
        self.nodes.set("info", "nodes", "node1")
270 3bae85da Dimitris Aragiorgis
        self.nodes.set("info", "public_iface", get_default_route()[1])
271 0ac84a9a Dimitris Aragiorgis
272 0ac84a9a Dimitris Aragiorgis
    def read_config(self, f):
273 0ac84a9a Dimitris Aragiorgis
        config = ConfigParser.ConfigParser()
274 0ac84a9a Dimitris Aragiorgis
        config.optionxform = str
275 0ac84a9a Dimitris Aragiorgis
        filename = os.path.join(self.confdir, f) + ".conf"
276 0ac84a9a Dimitris Aragiorgis
        config.read(filename)
277 0ac84a9a Dimitris Aragiorgis
        return config
278 0ac84a9a Dimitris Aragiorgis
279 0ac84a9a Dimitris Aragiorgis
    def set(self, conf, section, option, value):
280 0ac84a9a Dimitris Aragiorgis
        c = getattr(self, conf)
281 0ac84a9a Dimitris Aragiorgis
        c.set(section, option, value)
282 0ac84a9a Dimitris Aragiorgis
283 0ac84a9a Dimitris Aragiorgis
    def get(self, conf, section, option):
284 0ac84a9a Dimitris Aragiorgis
        c = getattr(self, conf)
285 0ac84a9a Dimitris Aragiorgis
        return c.get(section, option, True)
286 0ac84a9a Dimitris Aragiorgis
287 0ac84a9a Dimitris Aragiorgis
    def get_section(self, conf, section):
288 0ac84a9a Dimitris Aragiorgis
        c = getattr(self, conf)
289 0ac84a9a Dimitris Aragiorgis
        return c.items(section, True)
290 0ac84a9a Dimitris Aragiorgis
291 0ac84a9a Dimitris Aragiorgis
    def print_config(self):
292 0ac84a9a Dimitris Aragiorgis
        for f in self.files.keys():
293 0ac84a9a Dimitris Aragiorgis
            getattr(self, f).write(sys.stdout)
294 0ac84a9a Dimitris Aragiorgis
295 24dc98e2 Dimitris Aragiorgis
296 3c3bccab Dimitris Aragiorgis
def debug(host, msg, info=""):
297 0ac84a9a Dimitris Aragiorgis
298 3c3bccab Dimitris Aragiorgis
    print " ".join([HEADER, host, OKBLUE, msg, OKGREEN, info, ENDC])
299 0ac84a9a Dimitris Aragiorgis
300 0ac84a9a Dimitris Aragiorgis
301 0ac84a9a Dimitris Aragiorgis
def check_pidfile(pidfile):
302 0ac84a9a Dimitris Aragiorgis
    print("Checking pidfile " + pidfile)
303 0ac84a9a Dimitris Aragiorgis
    try:
304 0ac84a9a Dimitris Aragiorgis
        f = open(pidfile, "r")
305 0ac84a9a Dimitris Aragiorgis
        pid = f.readline()
306 0ac84a9a Dimitris Aragiorgis
        os.kill(int(pid), signal.SIGKILL)
307 0ac84a9a Dimitris Aragiorgis
        f.close()
308 0ac84a9a Dimitris Aragiorgis
        os.remove(pidfile)
309 0ac84a9a Dimitris Aragiorgis
        time.sleep(5)
310 0ac84a9a Dimitris Aragiorgis
    except:
311 0ac84a9a Dimitris Aragiorgis
        pass
312 0ac84a9a Dimitris Aragiorgis
313 0ac84a9a Dimitris Aragiorgis
314 ca1de37b Ilias Tsitsimpis
def random_mac():
315 7d6fed3a Dionysis Grigoropoulos
    mac = [0x52, 0x54, 0x56,
316 7d6fed3a Dionysis Grigoropoulos
           random.randint(0x00, 0xff),
317 7d6fed3a Dionysis Grigoropoulos
           random.randint(0x00, 0xff),
318 7d6fed3a Dionysis Grigoropoulos
           random.randint(0x00, 0xff)]
319 0ac84a9a Dimitris Aragiorgis
    return ':'.join(map(lambda x: "%02x" % x, mac))
320 0ac84a9a Dimitris Aragiorgis
321 0ac84a9a Dimitris Aragiorgis
322 0ac84a9a Dimitris Aragiorgis
def create_dir(d, clean=False):
323 0ac84a9a Dimitris Aragiorgis
    os.system("mkdir -p " + d)
324 0ac84a9a Dimitris Aragiorgis
    if clean:
325 0ac84a9a Dimitris Aragiorgis
        try:
326 0ac84a9a Dimitris Aragiorgis
            os.system("rm -f %s/*" % d)
327 0ac84a9a Dimitris Aragiorgis
        except:
328 0ac84a9a Dimitris Aragiorgis
            pass
329 0ac84a9a Dimitris Aragiorgis
330 0ac84a9a Dimitris Aragiorgis
331 0ac84a9a Dimitris Aragiorgis
def get_netinfo():
332 0ac84a9a Dimitris Aragiorgis
    _, pdev = get_default_route()
333 0ac84a9a Dimitris Aragiorgis
    r = re.compile(".*inet (\S+)/(\S+).*")
334 7d6fed3a Dionysis Grigoropoulos
    s = subprocess.Popen(['ip', 'addr', 'show', 'dev', pdev],
335 7d6fed3a Dionysis Grigoropoulos
                         stdout=subprocess.PIPE)
336 7d6fed3a Dionysis Grigoropoulos
337 0ac84a9a Dimitris Aragiorgis
    for line in s.stdout.readlines():
338 0ac84a9a Dimitris Aragiorgis
        match = r.search(line)
339 0ac84a9a Dimitris Aragiorgis
        if match:
340 0ac84a9a Dimitris Aragiorgis
            ip, size = match.groups()
341 0ac84a9a Dimitris Aragiorgis
            break
342 0ac84a9a Dimitris Aragiorgis
343 0ac84a9a Dimitris Aragiorgis
    return ip, size
344 0ac84a9a Dimitris Aragiorgis
345 0ac84a9a Dimitris Aragiorgis
346 0ac84a9a Dimitris Aragiorgis
def get_hostname():
347 0ac84a9a Dimitris Aragiorgis
    s = subprocess.Popen(['hostname', '-s'], stdout=subprocess.PIPE)
348 0ac84a9a Dimitris Aragiorgis
    return s.stdout.readline().strip()
349 0ac84a9a Dimitris Aragiorgis
350 0ac84a9a Dimitris Aragiorgis
351 0ac84a9a Dimitris Aragiorgis
def get_domain():
352 0ac84a9a Dimitris Aragiorgis
    s = subprocess.Popen(['hostname', '-d'], stdout=subprocess.PIPE)
353 0ac84a9a Dimitris Aragiorgis
    return s.stdout.readline().strip()
354 0ac84a9a Dimitris Aragiorgis
355 0ac84a9a Dimitris Aragiorgis
356 0ac84a9a Dimitris Aragiorgis
def get_default_route():
357 0ac84a9a Dimitris Aragiorgis
    r = re.compile("default via (\S+) dev (\S+)")
358 0ac84a9a Dimitris Aragiorgis
    s = subprocess.Popen(['ip', 'route'], stdout=subprocess.PIPE)
359 0ac84a9a Dimitris Aragiorgis
    for line in s.stdout.readlines():
360 0ac84a9a Dimitris Aragiorgis
        match = r.search(line)
361 0ac84a9a Dimitris Aragiorgis
        if match:
362 0ac84a9a Dimitris Aragiorgis
            gw, dev = match.groups()
363 0ac84a9a Dimitris Aragiorgis
            break
364 0ac84a9a Dimitris Aragiorgis
365 0ac84a9a Dimitris Aragiorgis
    return (gw, dev)
366 0ac84a9a Dimitris Aragiorgis
367 0ac84a9a Dimitris Aragiorgis
368 0ac84a9a Dimitris Aragiorgis
def import_conf_file(filename, directory):
369 0ac84a9a Dimitris Aragiorgis
    return imp.load_module(filename, *imp.find_module(filename, [directory]))
370 0ac84a9a Dimitris Aragiorgis
371 0ac84a9a Dimitris Aragiorgis
372 0ac84a9a Dimitris Aragiorgis
def raddr(addr):
373 7d6fed3a Dionysis Grigoropoulos
    return list(reversed(addr.replace("/", "-").split(".")))