summaryrefslogtreecommitdiff
path: root/contrib/rackspace/heat/engine/plugins/cloud_server.py
blob: 854bba39904d5bde35eadb6a0da396fc737ab90c (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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import socket
import tempfile

import json
import paramiko
from Crypto.PublicKey import RSA
import novaclient.exceptions as novaexception

from heat.common import exception
from heat.openstack.common import log as logging
from heat.openstack.common.gettextutils import _
from heat.engine import properties
from heat.engine import scheduler
from heat.engine.resources import instance
from heat.engine.resources import nova_utils
from heat.db.sqlalchemy import api as db_api

try:
    import pyrax  # noqa
except ImportError:

    def resource_mapping():
        return {}
else:

    def resource_mapping():
        return {'Rackspace::Cloud::Server': CloudServer}

logger = logging.getLogger(__name__)


class CloudServer(instance.Instance):
    """Resource for Rackspace Cloud Servers."""

    PROPERTIES = (
        FLAVOR, IMAGE, USER_DATA, KEY_NAME, VOLUMES, NAME,
    ) = (
        'flavor', 'image', 'user_data', 'key_name', 'Volumes', 'name',
    )

    properties_schema = {
        FLAVOR: properties.Schema(
            properties.Schema.STRING,
            required=True,
            update_allowed=True
        ),
        IMAGE: properties.Schema(
            properties.Schema.STRING,
            required=True
        ),
        USER_DATA: properties.Schema(
            properties.Schema.STRING
        ),
        KEY_NAME: properties.Schema(
            properties.Schema.STRING
        ),
        VOLUMES: properties.Schema(
            properties.Schema.LIST,
            default=[]
        ),
        NAME: properties.Schema(
            properties.Schema.STRING
        ),
    }

    attributes_schema = {'PrivateDnsName': ('Private DNS name of the specified'
                                            ' instance.'),
                         'PublicDnsName': ('Public DNS name of the specified '
                                           'instance.'),
                         'PrivateIp': ('Private IP address of the specified '
                                       'instance.'),
                         'PublicIp': ('Public IP address of the specified '
                                      'instance.')}

    base_script = """#!/bin/bash

# Install cloud-init and heat-cfntools
%s
# Create data source for cloud-init
mkdir -p /var/lib/cloud/seed/nocloud-net
mv /tmp/userdata /var/lib/cloud/seed/nocloud-net/user-data
touch /var/lib/cloud/seed/nocloud-net/meta-data
chmod 600 /var/lib/cloud/seed/nocloud-net/*

# Run cloud-init & cfn-init
cloud-init start || cloud-init init
bash -x /var/lib/cloud/data/cfn-userdata > /root/cfn-userdata.log 2>&1 ||
exit 42
"""

    # - Ubuntu 12.04: Verified working
    ubuntu_script = base_script % """\
apt-get update
export DEBIAN_FRONTEND=noninteractive
apt-get install -y -o Dpkg::Options::="--force-confdef" -o \
  Dpkg::Options::="--force-confold" cloud-init python-boto python-pip gcc \
  python-dev
pip install heat-cfntools
cfn-create-aws-symlinks --source /usr/local/bin
"""

    # - Fedora 17: Verified working
    # - Fedora 18: Not working.  selinux needs to be in "Permissive"
    #   mode for cloud-init to work.  It's disabled by default in the
    #   Rackspace Cloud Servers image.  To enable selinux, a reboot is
    #   required.
    # - Fedora 19: Verified working
    fedora_script = base_script % """\
yum install -y cloud-init python-boto python-pip gcc python-devel
pip-python install heat-cfntools
cfn-create-aws-symlinks
"""

    # - Centos 6.4: Verified working
    centos_script = base_script % """\
if ! (yum repolist 2> /dev/null | egrep -q "^[\!\*]?epel ");
then
 rpm -ivh http://mirror.rackspace.com/epel/6/i386/epel-release-6-8.noarch.rpm
fi
yum install -y cloud-init python-boto python-pip gcc python-devel \
  python-argparse
pip-python install heat-cfntools
"""

    # - RHEL 6.4: Verified working
    rhel_script = base_script % """\
if ! (yum repolist 2> /dev/null | egrep -q "^[\!\*]?epel ");
then
 rpm -ivh http://mirror.rackspace.com/epel/6/i386/epel-release-6-8.noarch.rpm
fi
# The RPM DB stays locked for a few secs
while fuser /var/lib/rpm/*; do sleep 1; done
yum install -y cloud-init python-boto python-pip gcc python-devel \
  python-argparse
pip-python install heat-cfntools
cfn-create-aws-symlinks
"""

    debian_script = base_script % """\
echo "deb http://mirror.rackspace.com/debian wheezy-backports main" >> \
  /etc/apt/sources.list
apt-get update
apt-get -t wheezy-backports install -y cloud-init
export DEBIAN_FRONTEND=noninteractive
apt-get install -y -o Dpkg::Options::="--force-confdef" -o \
  Dpkg::Options::="--force-confold" python-pip gcc python-dev
pip install heat-cfntools
"""

    # - Arch 2013.6: Not working (deps not in default package repos)
    # TODO(jason): Install cloud-init & other deps from third-party repos
    arch_script = base_script % """\
pacman -S --noconfirm python-pip gcc
"""

    # - Gentoo 13.2: Not working (deps not in default package repos)
    # TODO(jason): Install cloud-init & other deps from third-party repos
    gentoo_script = base_script % """\
emerge cloud-init python-boto python-pip gcc python-devel
"""

    # - OpenSUSE 12.3: Not working (deps not in default package repos)
    # TODO(jason): Install cloud-init & other deps from third-party repos
    opensuse_script = base_script % """\
zypper --non-interactive rm patterns-openSUSE-minimal_base-conflicts
zypper --non-interactive in cloud-init python-boto python-pip gcc python-devel
"""

    # List of supported Linux distros and their corresponding config scripts
    image_scripts = {'arch': None,
                     'centos': centos_script,
                     'debian': None,
                     'fedora': fedora_script,
                     'gentoo': None,
                     'opensuse': None,
                     'rhel': rhel_script,
                     'ubuntu': ubuntu_script}

    script_error_msg = (_("The %(path)s script exited with a non-zero exit "
                        "status.  To see the error message, log into the "
                        "server and view %(log)s"))

    # Template keys supported for handle_update.  Properties not
    # listed here trigger an UpdateReplace
    update_allowed_keys = ('Metadata', 'Properties')

    def __init__(self, name, json_snippet, stack):
        super(CloudServer, self).__init__(name, json_snippet, stack)
        self.stack = stack
        self._private_key = None
        self._server = None
        self._distro = None
        self._public_ip = None
        self._private_ip = None
        self._flavor = None
        self._image = None

    @property
    def server(self):
        """Get the Cloud Server object."""
        if not self._server:
            logger.debug(_("Calling nova().servers.get()"))
            self._server = self.nova().servers.get(self.resource_id)
        return self._server

    @property
    def distro(self):
        """Get the Linux distribution for this server."""
        if not self._distro:
            logger.debug(_("Calling nova().images.get()"))
            image_data = self.nova().images.get(self.image)
            self._distro = image_data.metadata['os_distro']
        return self._distro

    @property
    def script(self):
        """Get the config script for the Cloud Server image."""
        return self.image_scripts[self.distro]

    @property
    def flavor(self):
        """Get the flavors from the API."""
        if not self._flavor:
            flavor = self.properties[self.FLAVOR]
            self._flavor = nova_utils.get_flavor_id(self.nova(), flavor)
        return self._flavor

    @property
    def image(self):
        if not self._image:
            self._image = nova_utils.get_image_id(self.nova(),
                                                  self.properties[self.IMAGE])
        return self._image

    @property
    def private_key(self):
        """Return the private SSH key for the resource."""
        if self._private_key:
            return self._private_key
        if self.id is not None:
            private_key = db_api.resource_data_get(self, 'private_key')
            if not private_key:
                return None
            self._private_key = private_key
            return private_key

    @private_key.setter
    def private_key(self, private_key):
        """Save the resource's private SSH key to the database."""
        self._private_key = private_key
        if self.id is not None:
            db_api.resource_data_set(self, 'private_key', private_key, True)

    def _get_ip(self, ip_type):
        """Return the IP of the Cloud Server."""
        if ip_type in self.server.addresses:
            for ip in self.server.addresses[ip_type]:
                if ip['version'] == 4:
                    return ip['addr']

        raise exception.Error(_("Could not determine the %(ip)s IP of "
                                "%(image)s.") %
                              {'ip': ip_type,
                               'image': self.properties[self.IMAGE]})

    @property
    def public_ip(self):
        """Return the public IP of the Cloud Server."""
        if not self._public_ip:
            self._public_ip = self._get_ip('public')
        return self._public_ip

    @property
    def private_ip(self):
        """Return the private IP of the Cloud Server."""
        if not self._private_ip:
            self._private_ip = self._get_ip('private')
        return self._private_ip

    @property
    def has_userdata(self):
        if self.properties[self.USER_DATA] or self.metadata != {}:
            return True
        else:
            return False

    def validate(self):
        """Validate user parameters."""
        self.flavor
        self.image

        # It's okay if there's no script, as long as user_data and
        # metadata are empty
        if not self.script and self.has_userdata:
            return {'Error': "user_data/metadata are not supported for image"
                    " %s." % self.properties[self.IMAGE]}

    def _run_ssh_command(self, command):
        """Run a shell command on the Cloud Server via SSH."""
        with tempfile.NamedTemporaryFile() as private_key_file:
            private_key_file.write(self.private_key)
            private_key_file.seek(0)
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy())
            ssh.connect(self.public_ip,
                        username="root",
                        key_filename=private_key_file.name)
            chan = ssh.get_transport().open_session()
            chan.settimeout(self.stack.timeout_mins * 60.0)
            chan.exec_command(command)
            try:
                # The channel timeout only works for read/write operations
                chan.recv(1024)
            except socket.timeout:
                raise exception.Error("SSH command timed out after %s minutes"
                                      % self.stack.timeout_mins)
            else:
                return chan.recv_exit_status()
            finally:
                ssh.close()
                chan.close()

    def _sftp_files(self, files):
        """Transfer files to the Cloud Server via SFTP."""
        with tempfile.NamedTemporaryFile() as private_key_file:
            private_key_file.write(self.private_key)
            private_key_file.seek(0)
            pkey = paramiko.RSAKey.from_private_key_file(private_key_file.name)
            transport = paramiko.Transport((self.public_ip, 22))
            transport.connect(hostkey=None, username="root", pkey=pkey)
            sftp = paramiko.SFTPClient.from_transport(transport)
            try:
                for remote_file in files:
                    sftp_file = sftp.open(remote_file['path'], 'w')
                    sftp_file.write(remote_file['data'])
                    sftp_file.close()
            except:
                raise
            finally:
                sftp.close()
                transport.close()

    def handle_create(self):
        """Create a Rackspace Cloud Servers container.

        Rackspace Cloud Servers does not have the metadata service
        running, so we have to transfer the user-data file to the
        server and then trigger cloud-init.
        """
        # Generate SSH public/private keypair
        if self._private_key is not None:
            rsa = RSA.importKey(self._private_key)
        else:
            rsa = RSA.generate(1024)
        self.private_key = rsa.exportKey()
        public_keys = [rsa.publickey().exportKey('OpenSSH')]
        if self.properties.get(self.KEY_NAME):
            key_name = self.properties[self.KEY_NAME]
            public_keys.append(nova_utils.get_keypair(self.nova(),
                                                      key_name).public_key)
        personality_files = {
            "/root/.ssh/authorized_keys": '\n'.join(public_keys)}

        # Create server
        client = self.nova().servers
        logger.debug(_("Calling nova().servers.create()"))
        server = client.create(self.physical_resource_name(),
                               self.image,
                               self.flavor,
                               files=personality_files)

        # Save resource ID to db
        self.resource_id_set(server.id)

        return server, scheduler.TaskRunner(self._attach_volumes_task())

    def _attach_volumes_task(self):
        tasks = (scheduler.TaskRunner(self._attach_volume, volume_id, device)
                 for volume_id, device in self.volumes())
        return scheduler.PollingTaskGroup(tasks)

    def _attach_volume(self, volume_id, device):
        logger.debug(_("Calling nova().volumes.create_server_volume()"))
        self.nova().volumes.create_server_volume(self.server.id,
                                                 volume_id,
                                                 device or None)
        yield
        volume = self.cinder().get(volume_id)
        while volume.status in ('available', 'attaching'):
            yield
            volume.get()

        if volume.status != 'in-use':
            raise exception.Error(volume.status)

    def _detach_volumes_task(self):
        tasks = (scheduler.TaskRunner(self._detach_volume, volume_id)
                 for volume_id, device in self.volumes())
        return scheduler.PollingTaskGroup(tasks)

    def _detach_volume(self, volume_id):
        volume = self.cinder().get(volume_id)
        volume.detach()
        yield
        while volume.status in ('in-use', 'detaching'):
            yield
            volume.get()

        if volume.status != 'available':
            raise exception.Error(volume.status)

    def check_create_complete(self, cookie):
        """Check if server creation is complete and handle server configs."""
        if not super(CloudServer, self).check_create_complete(cookie):
            return False

        server = cookie[0]
        server.get()
        if 'rack_connect' in self.context.roles:  # Account has RackConnect
            if 'rackconnect_automation_status' not in server.metadata:
                logger.debug(_("RackConnect server does not have the "
                               "rackconnect_automation_status metadata tag "
                               "yet"))
                return False

            rc_status = server.metadata['rackconnect_automation_status']
            logger.debug(_("RackConnect automation status: ") + rc_status)

            if rc_status == 'DEPLOYING':
                return False

            elif rc_status == 'DEPLOYED':
                self._public_ip = None  # The public IP changed, forget old one

            elif rc_status == 'FAILED':
                raise exception.Error(_("RackConnect automation FAILED"))

            elif rc_status == 'UNPROCESSABLE':
                reason = server.metadata.get(
                    "rackconnect_unprocessable_reason", None)
                if reason is not None:
                    logger.warning(_("RackConnect unprocessable reason: ")
                                   + reason)
                # UNPROCESSABLE means the RackConnect automation was
                # not attempted (eg. Cloud Server in a different DC
                # than dedicated gear, so RackConnect does not apply).
                # It is okay if we do not raise an exception.

            else:
                raise exception.Error(_("Unknown RackConnect automation "
                                        "status: ") + rc_status)

        if 'rax_managed' in self.context.roles:  # Managed Cloud account
            if 'rax_service_level_automation' not in server.metadata:
                logger.debug(_("Managed Cloud server does not have the "
                             "rax_service_level_automation metadata tag yet"))
                return False

            mc_status = server.metadata['rax_service_level_automation']
            logger.debug(_("Managed Cloud automation status: ") + mc_status)

            if mc_status == 'In Progress':
                return False

            elif mc_status == 'Complete':
                pass

            elif mc_status == 'Build Error':
                raise exception.Error(_("Managed Cloud automation failed"))

            else:
                raise exception.Error(_("Unknown Managed Cloud automation "
                                      "status: ") + mc_status)

        if self.has_userdata:
            # Create heat-script and userdata files on server
            raw_userdata = self.properties[self.USER_DATA] or ''
            userdata = nova_utils.build_userdata(self, raw_userdata)

            files = [{'path': "/tmp/userdata", 'data': userdata},
                     {'path': "/root/heat-script.sh", 'data': self.script}]
            self._sftp_files(files)

            # Connect via SSH and run script
            cmd = "bash -ex /root/heat-script.sh > /root/heat-script.log 2>&1"
            exit_code = self._run_ssh_command(cmd)
            if exit_code == 42:
                raise exception.Error(self.script_error_msg %
                                      {'path': "cfn-userdata",
                                       'log': "/root/cfn-userdata.log"})
            elif exit_code != 0:
                raise exception.Error(self.script_error_msg %
                                      {'path': "heat-script.sh",
                                       'log': "/root/heat-script.log"})

        return True

    # TODO(jason): Make this consistent with Instance and inherit
    def _delete_server(self, server):
        """Return a coroutine that deletes the Cloud Server."""
        server.delete()
        while True:
            yield
            try:
                server.get()
                if server.status == "DELETED":
                    break
                elif server.status == "ERROR":
                    raise exception.Error(_("Deletion of server %s failed.") %
                                          server.name)
            except novaexception.NotFound:
                break

    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        """Try to update a Cloud Server's parameters.

        If the Cloud Server's Metadata or flavor changed, update the
        Cloud Server.  If any other parameters changed, re-create the
        Cloud Server with the new parameters.
        """

        if 'Metadata' in tmpl_diff:
            self.metadata = json_snippet['Metadata']
            metadata_string = json.dumps(self.metadata)

            files = [{'path': "/var/cache/heat-cfntools/last_metadata",
                      'data': metadata_string}]
            self._sftp_files(files)

            command = "bash -x /var/lib/cloud/data/cfn-userdata > " + \
                      "/root/cfn-userdata.log 2>&1"
            exit_code = self._run_ssh_command(command)
            if exit_code != 0:
                raise exception.Error(self.script_error_msg %
                                      {'path': "cfn-userdata",
                                       'log': "/root/cfn-userdata.log"})

        if self.FLAVOR in prop_diff:
            flav = json_snippet['Properties'][self.FLAVOR]
            new_flavor = nova_utils.get_flavor_id(self.nova(), flav)
            self.server.resize(new_flavor)
            resize = scheduler.TaskRunner(nova_utils.check_resize,
                                          self.server,
                                          flav)
            resize.start()
            return resize

    def _resolve_attribute(self, key):
        """Return the method that provides a given template attribute."""
        attribute_function = {'PublicIp': self.public_ip,
                              'PrivateIp': self.private_ip,
                              'PublicDnsName': self.public_ip,
                              'PrivateDnsName': self.public_ip}
        if key not in attribute_function:
            raise exception.InvalidTemplateAttribute(resource=self.name,
                                                     key=key)
        function = attribute_function[key]
        logger.info('%s._resolve_attribute(%s) == %s'
                    % (self.name, key, function))
        return unicode(function)