~andrewjbeach/juju-ci-tools/make-local-patcher

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import print_function

__metaclass__ = type

import yaml

from collections import defaultdict
from cStringIO import StringIO
from datetime import datetime, timedelta
import httplib
import os
import socket
import subprocess
import sys
import tempfile
from time import sleep
import urllib2

from jujuconfig import get_selected_environment


WIN_JUJU_CMD = os.path.join('\\', 'Progra~2', 'Juju', 'juju.exe')


class ErroredUnit(Exception):

    def __init__(self, environment, unit_name, state):
        msg = '<%s> %s is in state %s' % (environment, unit_name, state)
        Exception.__init__(self, msg)


class until_timeout:

    """Yields None until timeout is reached.

    :ivar timeout: Number of seconds to wait.
    """
    def __init__(self, timeout):
        self.timeout = timeout
        self.start = self.now()

    def __iter__(self):
        return self

    @staticmethod
    def now():
        return datetime.now()

    def next(self):
        if self.now() - self.start >= timedelta(0, self.timeout):
            raise StopIteration
        return None


def yaml_loads(yaml_str):
    return yaml.safe_load(StringIO(yaml_str))


class CannotConnectEnv(subprocess.CalledProcessError):

    def __init__(self, e):
        super(CannotConnectEnv, self).__init__(e.returncode, e.cmd, e.output)


class JujuClientDevel:
    # This client is meant to work with the latest version of juju.
    # Subclasses will retain support for older versions of juju, so that the
    # latest version is easy to read, and older versions can be trivially
    # deleted.

    def __init__(self, version, full_path):
        self.version = version
        self.full_path = full_path

    @classmethod
    def get_version(cls):
        return subprocess.check_output(('juju', '--version')).strip()

    @classmethod
    def get_full_path(cls):
        if sys.platform == 'win32':
            return WIN_JUJU_CMD
        return subprocess.check_output(('which', 'juju')).rstrip('\n')

    @classmethod
    def by_version(cls):
        version = cls.get_version()
        full_path = cls.get_full_path()
        if version.startswith('1.16'):
            return JujuClient16(version, full_path)
        else:
            return JujuClientDevel(version, full_path)

    def _full_args(self, environment, command, sudo, args):
        # sudo is not needed for devel releases.
        e_arg = () if environment is None else ('-e', environment.environment)
        return ('juju', '--show-log', command,) + e_arg + args

    def bootstrap(self, environment):
        """Bootstrap, using sudo if necessary."""
        if environment.hpcloud:
            constraints = 'mem=4G'
        else:
            constraints = 'mem=2G'
        self.juju(environment, 'bootstrap', ('--constraints', constraints),
                  environment.needs_sudo())

    def destroy_environment(self, environment):
        self.juju(
            None, 'destroy-environment',
            (environment.environment, '--force', '-y'),
            environment.needs_sudo(), check=False)

    def get_juju_output(self, environment, command):
        args = self._full_args(environment, command, False, ())
        with tempfile.TemporaryFile() as stderr:
            try:
                return subprocess.check_output(args, stderr=stderr)
            except subprocess.CalledProcessError as e:
                stderr.seek(0)
                e.stderr = stderr.read()
                if ('Unable to connect to environment' in e.stderr
                        or 'MissingOrIncorrectVersionHeader' in e.stderr
                        or '307: Temporary Redirect' in e.stderr):
                    raise CannotConnectEnv(e)
                print('!!! ' + e.stderr)
                raise

    def get_status(self, environment):
        """Get the current status as a dict."""
        return Status(yaml_loads(self.get_juju_output(environment, 'status')))

    def juju(self, environment, command, args, sudo=False, check=True):
        """Run a command under juju for the current environment."""
        args = self._full_args(environment, command, sudo, args)
        print(' '.join(args))
        sys.stdout.flush()
        if check:
            return subprocess.check_call(args)
        return subprocess.call(args)


class JujuClient16(JujuClientDevel):

    def destroy_environment(self, environment):
        self.juju(environment, 'destroy-environment', ('-y',),
                  environment.needs_sudo(), check=False)

    def _full_args(self, environment, command, sudo, args):
        # juju 1.16.x required sudo, so replace the juju command with it, as
        # appropriate.
        full = super(JujuClient16, self)._full_args(
            environment, command, sudo, args)
        sudo_args = ('sudo', '-E', self.full_path) if sudo else ('juju',)
        return sudo_args + full[1:]


class Status:

    def __init__(self, status):
        self.status = status

    def agent_items(self):
        for machine_name, machine in sorted(self.status['machines'].items()):
            yield machine_name, machine
        for service in sorted(self.status['services'].values()):
            for unit_name, unit in service.get('units', {}).items():
                yield unit_name, unit

    def agent_states(self):
        """Map agent states to the units and machines in those states."""
        states = defaultdict(list)
        for item_name, item in self.agent_items():
            states[item.get('agent-state', 'no-agent')].append(item_name)
        return states

    def check_agents_started(self, environment_name):
        """Check whether all agents are in the 'started' state.

        If not, return agent_states output.  If so, return None.
        If an error is encountered for an agent, raise ErroredUnit
        """
        # Look for errors preventing an agent from being installed
        for item_name, item in self.agent_items():
            state_info = item.get('agent-state-info', '')
            if 'error' in state_info:
                raise ErroredUnit(environment_name, item_name, state_info)
        states = self.agent_states()
        if states.keys() == ['started']:
            return None
        for state, entries in states.items():
            if 'error' in state:
                raise ErroredUnit(environment_name, entries[0],  state)
        return states

    def get_agent_versions(self):
        versions = defaultdict(set)
        for item_name, item in self.agent_items():
            versions[item.get('agent-version', 'unknown')].add(item_name)
        return versions


class Environment:

    def __init__(self, environment, client=None, config=None):
        self.environment = environment
        self.client = client
        self.config = config
        if self.config is not None:
            self.local = bool(self.config.get('type') == 'local')
            self.hpcloud = bool(
                'hpcloudsvc' in self.config.get('auth-url', ''))
        else:
            self.local = False
            self.hpcloud = False

    @classmethod
    def from_config(cls, name):
        client = JujuClientDevel.by_version()
        return cls(name, client, get_selected_environment(name)[0])

    def needs_sudo(self):
        return self.local

    def bootstrap(self):
        return self.client.bootstrap(self)

    def upgrade_juju(self):
        args = ('--version', self.get_matching_agent_version(no_build=True))
        if self.local:
            args += ('--upload-tools',)
        self.client.juju(self, 'upgrade-juju', args)

    def destroy_environment(self):
        return self.client.destroy_environment(self)

    def juju(self, command, *args):
        return self.client.juju(self, command, args)

    def get_status(self):
        return self.client.get_status(self)

    def wait_for_started(self):
        """Wait until all unit/machine agents are 'started'."""
        for ignored in until_timeout(1200):
            try:
                status = self.get_status()
            except CannotConnectEnv:
                print('Supressing "Unable to connect to environment"')
                continue
            states = status.check_agents_started(self.environment)
            if states is None:
                break
            print(format_listing(states, 'started', self.environment))
            sys.stdout.flush()
        else:
            raise Exception('Timed out waiting for agents to start in %s.' %
                            self.environment)
        return status

    def wait_for_version(self, version):
        for ignored in until_timeout(300):
            try:
                versions = self.get_status().get_agent_versions()
            except CannotConnectEnv:
                print('Supressing "Unable to connect to environment"')
                continue
            if versions.keys() == [version]:
                break
            print(format_listing(versions, version, self.environment))
            sys.stdout.flush()
        else:
            raise Exception('Some versions did not update.')

    def get_matching_agent_version(self, no_build=False):
        version_number = self.client.version.split('-')[0]
        if not no_build and self.local:
            version_number += '.1'
        return version_number


def format_listing(listing, expected, environment):
    value_listing = []
    for value, entries in listing.items():
        if value == expected:
            continue
        value_listing.append('%s: %s' % (value, ', '.join(entries)))
    return ('<%s> ' % environment) + ' | '.join(value_listing)


def check_wordpress(environment, host):
    """"Check whether Wordpress has come up successfully.

    Times out after 30 seconds.
    """
    welcome_text = ('Welcome to the famous five minute WordPress'
                    ' installation process!')
    url = 'http://%s/wp-admin/install.php' % host
    for ignored in until_timeout(30):
        try:
            page = urllib2.urlopen(url)
        except (urllib2.URLError, httplib.HTTPException, socket.error):
            pass
        else:
            if welcome_text in page.read():
                break
        # Let's not DOS wordpress
        sleep(1)
    else:
        raise Exception(
            'Cannot get welcome screen at %s %s' % (url, environment))