~matsubara/maas/remove-network-config-hack

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
from subprocess import Popen, PIPE
from testtools.content import text_content
from testtools.matchers import Contains
from functools import wraps
import errno
import os
import signal


CLUSTER_CONTROLLER_IP = '192.168.20.5'
SQUID_PROXY_URL = 'http://10.98.0.13:3128'
SQUID_DEB_PROXY_URL = 'http://10.98.0.13:8000'


class TimeoutError(Exception):
    pass


# Timeout decorator from
# http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
    def decorator(func):
        def _handle_timeout(signum, frame):
            raise TimeoutError(error_message)

        def wrapper(*args, **kwargs):
            signal.signal(signal.SIGALRM, _handle_timeout)
            signal.alarm(seconds)
            try:
                result = func(*args, **kwargs)
            finally:
                signal.alarm(0)
            return result

        return wraps(func)(wrapper)

    return decorator


def run_command(args):
    """A wrapper to Popen to run commands in the command-line."""
    process = Popen(args, stdout=PIPE, stderr=PIPE, stdin=PIPE)
    stdout, stderr = process.communicate()
    return stdout, stderr


def update_pxe_config():
    """Update the files necessary for PXE booting.

    This helper updates the import_ephemerals and import_pxe_files files to
    use a mirror and a proxy respectively.
    """
    ephemerals_fd = open("/etc/maas/import_ephemerals", "r+")
    ephemerals = ephemerals_fd.read()
    # Update mirrors for both trunk and the quantal SRU.
    ephemerals_snippet = (
        '\n'
        'CLOUD_IMAGES_ARCHIVE="http://10.98.0.13/mirrors/maas-ephemeral"\n'
        'REMOTE_IMAGES_MIRROR="http://10.98.0.13/mirrors/maas-ephemeral"\n'
    #    XXX: rvb 2013-01-18: Do not use the daily ephemeral images as
    #    they seem to be broken (more precisely, the image from 20130107 is
    #    and the one from 20121008 isn't);  investigation is underway.
    #   'STREAM=daily\n'
        )
    ephemerals += ephemerals_snippet
    ephemerals_fd.seek(0)
    ephemerals_fd.write(ephemerals)
    ephemerals_fd.close()
    # XXX: matsubara Bug=1074167
    # Update import_pxe_files to not download squashfs images
    # and use a proxy.
    pxe_fd = open('/etc/maas/import_pxe_files', "r+")
    pxe_file = pxe_fd.read()
    pxe_snippet = 'export http_proxy="%s"\n' % SQUID_PROXY_URL
    pxe_file = pxe_snippet + pxe_file
    pxe_fd.seek(0)
    pxe_fd.write(pxe_file)
    pxe_fd.close()


def assertStartedUpstartService(runner, service_name, log_file=None):
    """Assert that the upstart service 'service_name' is running."""
    if log_file is not None:
        try:
            log_file_content = file(log_file).read()
            runner.addDetail(
                "File %s content" % log_file,
                text_content(log_file_content))
        except IOError:
            runner.addDetail(
                "File %s content" % log_file,
                text_content("could not be read"))
    output, err = run_command(["service", service_name, "status"])
    runner.assertThat(output, Contains(service_name + " start/running"))