summaryrefslogtreecommitdiffstats
path: root/fuel/deploy/cloud/deployment.py
blob: 43bd4b6d44b7b68e763cdba79f8117b04d7e7673 (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
###############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# szilard.cserey@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
###############################################################################


import common
import os
import shutil
import glob
import yaml
import io
import time

N = common.N
E = common.E
R = common.R
RO = common.RO
exec_cmd = common.exec_cmd
run_proc = common.run_proc
parse = common.parse
err = common.err
log = common.log
literal_unicode = common.literal_unicode
literal_unicode_representer = common.literal_unicode_representer
yaml.add_representer(literal_unicode, literal_unicode_representer)
backup = common.backup


class Deployment(object):

    def __init__(self, dea, yaml_config_dir, env_id, node_id_roles_dict,
                 no_health_check):
        self.dea = dea
        self.yaml_config_dir = yaml_config_dir
        self.env_id = env_id
        self.node_id_roles_dict = node_id_roles_dict
        self.no_health_check = no_health_check

    def download_deployment_info(self):
        log('Download deployment info for environment %s' % self.env_id)
        deployment_dir = ('%s/deployment_%s'
                          % (self.yaml_config_dir, self.env_id))
        if os.path.exists(deployment_dir):
            shutil.rmtree(deployment_dir)
        exec_cmd('fuel deployment --env %s --download --dir %s'
                 % (self.env_id, self.yaml_config_dir))

    def upload_deployment_info(self):
        log('Upload deployment info for environment %s' % self.env_id)
        exec_cmd('fuel --env %s deployment --upload --dir %s'
                 % (self.env_id, self.yaml_config_dir))

    def __update_opnfv_dict(self, opnfv_dict, key, node_type, val):
        if val:
            if key not in opnfv_dict:
                opnfv_dict.update({key: {}})
            opnfv_dict[key].update({node_type: val})

    def config_opnfv(self):
        log('Configure OPNFV settings on environment %s' % self.env_id)
        self.download_deployment_info()

        opnfv = {'opnfv': {}}
        dns_list = self.dea.get_dns_list()
        host_list = self.dea.get_hosts()

        ntp_list_for_controller = ''
        for ntp in self.dea.get_ntp_list():
            ntp_list_for_controller += 'server %s\n' % ntp

        ntp_list_for_compute = ''
        for controller_file in glob.glob(
                        '%s/deployment_%s/*controller*.yaml'
                        % (self.yaml_config_dir, self.env_id)):
            with io.open(controller_file) as stream:
                controller = yaml.load(stream)
                ntp_list_for_compute += 'server %s\n' % controller['fqdn']

        self.__update_opnfv_dict(
            opnfv['opnfv'], 'dns', 'controller', dns_list[:])
        self.__update_opnfv_dict(
            opnfv['opnfv'], 'dns', 'compute', dns_list[:])
        self.__update_opnfv_dict(
            opnfv['opnfv'], 'ntp', 'controller',
            literal_unicode(ntp_list_for_controller))
        self.__update_opnfv_dict(
            opnfv['opnfv'], 'ntp', 'compute',
            literal_unicode(ntp_list_for_compute))

        if host_list:
            opnfv['opnfv'].update({'hosts': host_list})

        for node_file in glob.glob('%s/deployment_%s/*.yaml'
                                   % (self.yaml_config_dir, self.env_id)):
            with io.open(node_file) as stream:
                node = yaml.load(stream)
                node.update(opnfv)
            with io.open(node_file, 'w') as stream:
                yaml.dump(node, stream, default_flow_style=False)

        self.upload_deployment_info()

    def run_deploy(self):
        WAIT_LOOP = 180
        SLEEP_TIME = 60
        LOG_FILE = 'cloud.log'

        log('Starting deployment of environment %s' % self.env_id)
        run_proc('fuel --env %s deploy-changes | strings | tee %s'
                 % (self.env_id, LOG_FILE))

        ready = False
        for i in range(WAIT_LOOP):
            env = parse(exec_cmd('fuel env --env %s' % self.env_id))
            log('Environment status: %s' % env[0][E['status']])
            r, _ = exec_cmd('tail -2 %s | head -1' % LOG_FILE, False)
            if r:
                log(r)
            if env[0][E['status']] == 'operational':
                ready = True
                break
            elif (env[0][E['status']] == 'error'
                  or env[0][E['status']] == 'stopped'):
                break
            else:
                time.sleep(SLEEP_TIME)
        exec_cmd('rm %s' % LOG_FILE)

        if ready:
            log('Environment %s successfully deployed' % self.env_id)
        else:
            err('Deployment failed, environment %s is not operational'
                % self.env_id)

    def verify_node_status(self):
        node_list = parse(exec_cmd('fuel node list'))
        failed_nodes = []
        for node in node_list:
            if node[N['status']] != 'ready':
                failed_nodes.append((node[N['id']], node[N['status']]))

        if failed_nodes:
            summary = ''
            for node, status in failed_nodes:
                summary += '[node %s, status %s]\n' % (node, status)
            err('Deployment failed: %s' % summary)

    def health_check(self):
        log('Now running sanity and smoke health checks')
        r = exec_cmd('fuel health --env %s --check sanity,smoke --force'
                     % self.env_id)
        log(r)
        if 'failure' in r:
            err('Healthcheck failed!')

    def deploy(self):
        self.config_opnfv()
        self.run_deploy()
        self.verify_node_status()
        if not self.no_health_check:
            self.health_check()