summaryrefslogtreecommitdiff
path: root/mason/deployment.py
blob: a23b0ea15c69eccf6033c658e7b88ab9225ea8da (plain)
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
# Copyright 2014 Codethink Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import cliapp
import os
import pipes
import shlex
import shutil
import socket
import tempfile
import time
import uuid

import mason


class DeployedSystemInstance(object):

    def __init__(self, deployment, config, host_machine, vm_id, rootfs_path,
            ip_addr=None):
        self.deployment = deployment
        self.config = config
        self.ip_address = ip_addr or self.config['HOSTNAME']
        self.host_machine = host_machine
        self.vm_id = vm_id
        self.rootfs_path = rootfs_path
        self.hostname = self.config['HOSTNAME']
        self.log = None

    @property
    def ssh_host(self):
        # TODO: Stop assuming we ssh into test instances as root
        return 'root@{host}'.format(host=self.ip_address)

    def runcmd(self, argv, chdir='.', **kwargs):
        ssh_cmd = ['ssh', '-o', 'StrictHostKeyChecking=no',
                   '-o', 'UserKnownHostsFile=/dev/null', self.ssh_host]
        cmd = ['sh', '-c', 'cd "$1" && shift && exec "$@"', '-', chdir]
        cmd += argv
        ssh_cmd.append(' '.join(map(pipes.quote, cmd)))
        return cliapp.runcmd(ssh_cmd, **kwargs)

    def _wait_for_dhcp(self, timeout):
        '''Block until given hostname resolves successfully.

        Raises AppException if the hostname has not appeared in 'timeout'
        seconds.

        '''
        start_time = time.time()
        while True:
            try:
                socket.gethostbyname(self.ip_address)
                return
            except socket.gaierror:
                pass
            if time.time() > start_time + timeout:
                raise cliapp.AppException("Host %s did not appear "
                                          "after %i seconds" %
                                          (self.ip_address, timeout))
            time.sleep(0.5)

    def _wait_for_ssh(self, timeout):
        """Wait until the deployed VM is responding via SSH"""
        start_time = time.time()
        while True:
            try:
                self.runcmd(['true'], stdin=None, stdout=self.log, stderr=self.log)
                return
            except cliapp.AppException:
                # TODO: Stop assuming the ssh part of the command is what failed
                if time.time() > start_time + timeout:
                    raise cliapp.AppException("%s sshd did not start after "
                                              "%i seconds" %
                                              (self.ip_address, timeout))
                time.sleep(0.5)

    def _wait_for_cloud_init(self, timeout):
        """Wait until cloud init has resized the disc"""
        start_time = time.time()
        while True:
            try:
                out = self.runcmd(['sh', '-c',
                              'test -e "$1" && echo exists || echo does not exist',
                              '-', '/root/cloud-init-finished'])
            except:
                import traceback
                traceback.print_exc()
                raise
            if out.strip() == 'exists':
                return
            if time.time() > start_time + timeout:
                raise cliapp.AppException("Disc size not increased after "
                                          "%i seconds" % (timeout))
            time.sleep(3)

    def wait_until_online(self, timeout=120):
        self._wait_for_dhcp(timeout)
        self._wait_for_ssh(timeout)
        if self.config['type'] == 'openstack':
            self._wait_for_cloud_init(timeout)
        if self.log:
            self.log.write("Test system %s ready to run tests.\n" %
                           (self.hostname))
            self.log.flush()

    def delete(self):
        # Stop and remove VM
        if self.log:
            self.log.write("Deleting %s test instance\n" % (self.hostname))
        try:
            if self.config['type'] == 'openstack':
                cliapp.runcmd(['nova', 'delete', self.hostname])
            elif self.config['type'] == 'kvm':
                self.host_machine.virsh('destroy', self.vm_id)
        except cliapp.AppException as e:
            # TODO: Stop assuming that delete failed because the instance
            #       wasn't running
            if self.log:
                self.log.write("- Failed\n")
            pass
        if self.log:
            self.log.write("Deleting %s test disc image\n" % (self.hostname))
        try:
            if self.config['type'] == 'openstack':
                cliapp.runcmd(['nova', 'image-delete', self.hostname])
            elif self.config['type'] == 'kvm':
                self.host_machine.virsh('undefine', self.vm_id,
                                        '--remove-all-storage')
        except cliapp.AppException as e:
            # TODO: Stop assuming that image-delete failed because it was
            #       already removed
            if self.log:
                self.log.write("- Failed\n")
            pass


class Deployment(object):

    def __init__(self, cluster_path, name, deployment_config,
                 host_machine, net_id, log_path):
        self.cluster_path = cluster_path
        self.name = name
        self.deployment_config = deployment_config
        self.host_machine = host_machine
        self.net_id = net_id
        self.logfile = open(log_path, 'w+')

    @staticmethod
    def _ssh_host_key_exists(hostname):
        """Check if an ssh host key exists in known_hosts"""
        if not os.path.exists('/root/.ssh/known_hosts'):
            return False
        with open('/root/.ssh/known_hosts', 'r') as known_hosts:
            return any(line.startswith(hostname) for line in known_hosts)

    def _update_known_hosts(self):
        if not self._ssh_host_key_exists(self.host_machine.address):
            with open('/root/.ssh/known_hosts', 'a') as known_hosts:
                cliapp.runcmd(['ssh-keyscan', self.host_machine.address],
                              stdout=known_hosts)

    @staticmethod
    def _generate_sshkey_config(tempdir, config):
        manifest = os.path.join(tempdir, 'manifest')
        with open(manifest, 'w') as f:
            f.write('0040700 0 0 /root/.ssh\n')
            f.write('overwrite 0100600 0 0 /root/.ssh/authorized_keys\n')
        authkeys = os.path.join(tempdir, 'root', '.ssh', 'authorized_keys')
        os.makedirs(os.path.dirname(authkeys))
        with open(authkeys, 'w') as auth_f:
            with open('/root/.ssh/id_rsa.pub', 'r') as key_f:
                shutil.copyfileobj(key_f, auth_f)

        install_files = shlex.split(config.get('INSTALL_FILES', ''))
        install_files.append(manifest)
        yield 'INSTALL_FILES', ' '.join(pipes.quote(f) for f in install_files)

    def deploy(self, deployment_type):
        self._update_known_hosts()

        hostname = str(uuid.uuid4())
        vm_id = hostname
        image_base = self.host_machine.disk_path
        rootpath = '{image_base}/{hostname}.img'.format(image_base=image_base,
                                                        hostname=hostname)
        loc = 'http://{ssh_host}:5000/v2.0'.format(
            ssh_host=self.host_machine.ssh_host, id=vm_id, path=rootpath)

        options = {
            'type': deployment_type,
            'location': loc,
            'HOSTNAME': hostname,
            'DISK_SIZE': '5G',
            'RAM_SIZE': '2G',
            'VERSION_LABEL': 'release-test'
        }
        if deployment_type == 'openstack':
            options.update({
                'OPENSTACK_USER': os.environ['OS_USERNAME'],
                'OPENSTACK_TENANT': os.environ['OS_TENANT_NAME'],
                'OPENSTACK_PASSWORD': os.environ['OS_PASSWORD'],
                'OPENSTACK_IMAGENAME': hostname,
                'CLOUD_INIT': 'yes',
                'KERNEL_ARGS': 'console=tty0 console=ttyS0',
            })
        elif deployment_type == 'kvm':
            options['AUTOSTART'] = 'True'

        tempdir = tempfile.mkdtemp()
        try:
            options.update(
                self._generate_sshkey_config(tempdir,
                                            self.deployment_config))
            # Deploy the image to openstack
            args = ['morph', 'deploy', self.cluster_path, self.name]
            for k, v in options.iteritems():
                args.append('%s.%s=%s' % (self.name, k, v))
            cliapp.runcmd(args, stdin=None,
                          stdout=self.logfile,
                          stderr=self.logfile)
            config = dict(self.deployment_config)
            config.update(options)

            ip_addr = None
            if deployment_type == 'openstack':
                # Boot an instance from the image
                args = ['nova', 'boot',
                    '--flavor', 'm1.medium',
                    '--image', hostname,
                    '--user-data', '/usr/lib/mason/os-init-script',
                    '--nic', "net-id=%s" % (self.net_id),
                    hostname]
                output = cliapp.runcmd(args)

                # Print nova boot output, with adminPass line removed
                output_lines = output.split('\n')
                for line in output_lines:
                    if line.find('adminPass') != -1:
                        password_line = line
                output_lines.remove(password_line)
                output = '\n'.join(output_lines)
                self.logfile.write(output)

                # Get ip address from nova list
                nl = mason.util.NovaList()
                ip_addr = nl.get_nova_ip_for_instance_timeout(hostname)
                self.logfile.write("IP address for instance %s: %s\n" %
                                   (hostname, ip_addr))

                return DeployedSystemInstance(self, config, self.host_machine,
                                              vm_id, rootpath, ip_addr)
            elif deployment_type == 'kvm':
                return DeployedSystemInstance(self, config, self.host_machine,
                                              vm_id, rootpath)
        finally:
            shutil.rmtree(tempdir)
            self.logfile.close()