aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/buildslave/openstack.py
blob: ec5c4861654fa72655bb8b4cd77740e3a58788bc (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
# This file is part of Buildbot.  Buildbot 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.
#
# 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.
#
# Portions Copyright Buildbot Team Members
# Portions Copyright 2013 Cray Inc.

import time

from twisted.internet import defer, threads
from twisted.python import log

from buildbot.buildslave.base import AbstractLatentBuildSlave
from buildbot import config, interfaces

try:
    import novaclient.exceptions as nce
    from novaclient.v1_1 import client
    _hush_pyflakes = [nce, client]
except ImportError:
    nce = None
    client = None


ACTIVE = 'ACTIVE'
BUILD = 'BUILD'
DELETED = 'DELETED'
UNKNOWN = 'UNKNOWN'

class OpenStackLatentBuildSlave(AbstractLatentBuildSlave):

    instance = None
    _poll_resolution = 5 # hook point for tests

    def __init__(self, name, password,
                 flavor,
                 image,
                 os_username,
                 os_password,
                 os_tenant_name,
                 os_auth_url,
                 meta=None,
                 max_builds=None, notify_on_missing=[], missing_timeout=60*20,
                 build_wait_timeout=60*10, properties={}, locks=None):

        if not client or not nce:
            config.error("The python module 'novaclient' is needed  "
                         "to use a OpenStackLatentBuildSlave")

        AbstractLatentBuildSlave.__init__(
            self, name, password, max_builds, notify_on_missing,
            missing_timeout, build_wait_timeout, properties, locks)
        self.flavor = flavor
        self.image = image
        self.os_username = os_username
        self.os_password = os_password
        self.os_tenant_name = os_tenant_name
        self.os_auth_url = os_auth_url
        self.meta = meta

    def _getImage(self, os_client):
        # If self.image is a callable, then pass it the list of images. The
        # function should return the image's UUID to use.
        if callable(self.image):
            image_uuid = self.image(os_client.images.list())
        else:
            image_uuid = self.image
        return image_uuid

    def start_instance(self, build):
        if self.instance is not None:
            raise ValueError('instance active')
        return threads.deferToThread(self._start_instance)

    def _start_instance(self):
        # Authenticate to OpenStack.
        os_client = client.Client(self.os_username, self.os_password,
                                  self.os_tenant_name, self.os_auth_url)
        image_uuid = self._getImage(os_client)
        flavor_id = self.flavor
        boot_args = [self.slavename, image_uuid, flavor_id]
        boot_kwargs = {}
        if self.meta is not None:
            boot_kwargs['meta'] = self.meta
        self.instance = os_client.servers.create(*boot_args, **boot_kwargs)
        log.msg('%s %s starting instance %s (image %s)' %
                (self.__class__.__name__, self.slavename, self.instance.id,
                 image_uuid))
        duration = 0
        interval = self._poll_resolution
        inst = self.instance
        while inst.status == BUILD:
            time.sleep(interval)
            duration += interval
            if duration % 60 == 0:
                log.msg('%s %s has waited %d minutes for instance %s' %
                        (self.__class__.__name__, self.slavename, duration//60,
                         self.instance.id))
            try:
                inst = os_client.servers.get(self.instance.id)
            except nce.NotFound:
                log.msg('%s %s instance %s (%s) went missing' %
                        (self.__class__.__name__, self.slavename,
                         self.instance.id, self.instance.name))
                raise interfaces.LatentBuildSlaveFailedToSubstantiate(
                    self.instance.id, self.instance.status)
        if inst.status == ACTIVE:
            minutes = duration//60
            seconds = duration%60
            log.msg('%s %s instance %s (%s) started '
                    'in about %d minutes %d seconds' %
                    (self.__class__.__name__, self.slavename,
                     self.instance.id, self.instance.name, minutes, seconds))
            return [self.instance.id, image_uuid,
                    '%02d:%02d:%02d' % (minutes//60, minutes%60, seconds)]
        else:
            log.msg('%s %s failed to start instance %s (%s)' %
                    (self.__class__.__name__, self.slavename,
                     self.instance.id, inst.status))
            raise interfaces.LatentBuildSlaveFailedToSubstantiate(
                self.instance.id, self.instance.status)

    def stop_instance(self, fast=False):
        if self.instance is None:
            # be gentle.  Something may just be trying to alert us that an
            # instance never attached, and it's because, somehow, we never
            # started.
            return defer.succeed(None)
        instance = self.instance
        self.instance = None
        return threads.deferToThread(self._stop_instance, instance, fast)

    def _stop_instance(self, instance, fast):
        # Authenticate to OpenStack. This is needed since it seems the update
        # method doesn't do a whole lot of updating right now.
        os_client = client.Client(self.os_username, self.os_password,
                                  self.os_tenant_name, self.os_auth_url)
        # When the update method does work, replace the lines like below with
        # instance.update().
        try:
            inst = os_client.servers.get(instance.id)
        except nce.NotFound:
            # If can't find the instance, then it's already gone.
            log.msg('%s %s instance %s (%s) already terminated' %
                    (self.__class__.__name__, self.slavename, instance.id,
                     instance.name))
            return
        if inst.status not in (DELETED, UNKNOWN):
            inst.delete()
            log.msg('%s %s terminating instance %s (%s)' %
                    (self.__class__.__name__, self.slavename, instance.id,
                     instance.name))
        duration = 0
        interval = self._poll_resolution
        if fast:
            goal = (DELETED, UNKNOWN)
        else:
            goal = (DELETED,)
        while inst.status not in goal:
            time.sleep(interval)
            duration += interval
            if duration % 60 == 0:
                log.msg(
                    '%s %s has waited %d minutes for instance %s to end' %
                    (self.__class__.__name__, self.slavename, duration//60,
                     instance.id))
            try:
                inst = os_client.servers.get(instance.id)
            except nce.NotFound:
                break
        log.msg('%s %s instance %s %s '
                'after about %d minutes %d seconds' %
                (self.__class__.__name__, self.slavename,
                 instance.id, goal, duration//60, duration%60))