summaryrefslogtreecommitdiffstats
path: root/compass-tasks-base/deployment/deploy_manager.py
blob: 225c31e90bcd786f42c7d2ddc36f214259b25a8c (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
# Copyright 2014 Huawei Technologies Co. Ltd
#
# 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.

__author__ = "Grace Yu (grace.yu@huawei.com)"

"""Module to get configs from provider and isntallers and update
   them to provider and installers.
"""
from compass.deployment.installers.installer import OSInstaller
from compass.deployment.installers.installer import PKInstaller
from compass.deployment.utils import constants as const
from compass.utils import util


import logging


class DeployManager(object):
    """Deploy manager module."""
    def __init__(self, adapter_info, cluster_info, hosts_info):
        """Init deploy manager."""
        self.os_installer = None
        self.pk_installer = None

        # Get OS installer
        os_installer_name = adapter_info[const.OS_INSTALLER][const.NAME]
        self.os_installer = DeployManager._get_installer(OSInstaller,
                                                         os_installer_name,
                                                         adapter_info,
                                                         cluster_info,
                                                         hosts_info)

        # Get package installer
        pk_info = adapter_info.setdefault(const.PK_INSTALLER, {})
        if pk_info:
            pk_installer_name = pk_info[const.NAME]
            self.pk_installer = DeployManager._get_installer(PKInstaller,
                                                             pk_installer_name,
                                                             adapter_info,
                                                             cluster_info,
                                                             hosts_info)

    @staticmethod
    def _get_installer(installer_type, name, adapter_info, cluster_info,
                       hosts_info):
        """Get installer instance."""
        callback = getattr(installer_type, 'get_installer')
        installer = callback(name, adapter_info, cluster_info, hosts_info)

        return installer

    def deploy(self):
        """Deploy the cluster."""
        deployed_config = self.deploy_os()
        package_deployed_config = self.deploy_target_system()

        util.merge_dict(deployed_config, package_deployed_config)

        return deployed_config

    def check_cluster_health(self, callback_url):
        logging.info("DeployManager check_cluster_health...........")
        self.pk_installer.check_cluster_health(callback_url)

    def clean_progress(self):
        """Clean previous installation log and progress."""
        self.clean_os_installtion_progress()
        self.clean_package_installation_progress()

    def clean_os_installtion_progress(self):
        # OS installer cleans previous installing progress.
        if self.os_installer:
            self.os_installer.clean_progress()

    def clean_package_installation_progress(self):
        # Package installer cleans previous installing progress.
        if self.pk_installer:
            self.pk_installer.clean_progress()

    def prepare_for_deploy(self):
        self.clean_progress()

    def deploy_os(self):
        """Deploy OS to hosts which need to in the cluster.

        Return OS deployed config.
        """
        if not self.os_installer:
            return {}

        pk_installer_config = {}
        if self.pk_installer:
            # generate target system config which will be installed by OS
            # installer right after OS installation is completed.
            pk_installer_config = self.pk_installer.generate_installer_config()
            logging.debug('[DeployManager]package installer config is %s',
                          pk_installer_config)

        # Send package installer config info to OS installer.
        self.os_installer.set_package_installer_config(pk_installer_config)

        # start to deploy OS
        result = self.os_installer.deploy()
        self.reset_server()

        return result

    def poweron_server(self):
        if not self.os_installer:
            return

        host_id_list = self.os_installer.config_manager.get_host_id_list()
        for host_id in host_id_list:
            self.os_installer.poweron(host_id)

    def poweroff_server(self):
        if not self.os_installer:
            return

        host_id_list = self.os_installer.config_manager.get_host_id_list()
        for host_id in host_id_list:
            self.os_installer.poweroff(host_id)

    def reset_server(self):
        if not self.os_installer:
            return

        host_id_list = self.os_installer.config_manager.get_host_id_list()
        for host_id in host_id_list:
            self.os_installer.reset(host_id)

    def deploy_target_system(self):
        """Deploy target system to all hosts in the cluster.

        Return package deployed config.
        """
        if not self.pk_installer:
            return {}

        return self.pk_installer.deploy()

    def redeploy_os(self):
        """Redeploy OS for this cluster without changing configurations."""
        if not self.os_installer:
            logging.info("Redeploy_os: No OS installer found!")
            return

        self.os_installer.redeploy()
        logging.info("Start to redeploy OS for cluster.")

    def redeploy_target_system(self):
        """Redeploy target system for the cluster without changing config."""
        if not self.pk_installer:
            logging.info("Redeploy_target_system: No installer found!")
            return

        self.pk_installer.deploy()
        logging.info("Start to redeploy target system.")

    def redeploy(self):
        """Redeploy the cluster without changing configurations."""
        self.redeploy_os()
        self.redeploy_target_system()

    def remove_hosts(self, package_only=False, delete_cluster=False):
        """Remove hosts from both OS and/or package installlers server side."""
        if self.os_installer and not package_only:
            self.os_installer.delete_hosts()

        if self.pk_installer:
            self.pk_installer.delete_hosts(delete_cluster=delete_cluster)

    def os_installed(self):
        if self.os_installer:
            self.os_installer.ready()
        if self.pk_installer:
            self.pk_installer.os_ready()

    def cluster_os_installed(self):
        if self.os_installer:
            self.os_installer.cluster_ready()
        if self.pk_installer:
            self.pk_installer.cluster_os_ready()

    def package_installed(self):
        if self.pk_installer:
            self.pk_installer.ready()

    def cluster_installed(self):
        if self.pk_installer:
            self.pk_installer.cluster_ready()


class Patcher(DeployManager):
    """Patcher Module."""
    def __init__(self, adapter_info, cluster_info, hosts_info, cluster_hosts):
        self.pk_installer = None
        self.cluster_info = cluster_info
        registered_roles = cluster_info['flavor']['roles']

        pk_info = adapter_info.setdefault(const.PK_INSTALLER, {})
        if pk_info:
            pk_installer_name = pk_info[const.NAME]
            self.pk_installer = Patcher._get_installer(PKInstaller,
                                                       pk_installer_name,
                                                       adapter_info,
                                                       cluster_info,
                                                       hosts_info)

        patched_role_mapping = {}
        for role in registered_roles:
            patched_role_mapping[role] = []
        for host in cluster_hosts:
            if len(host['patched_roles']) == 0:
                continue
            for role in host['patched_roles']:
                patched_role_mapping[role['name']].append(host)
        self.patched_role_mapping = patched_role_mapping

    def patch(self):
        patched_config = self.pk_installer.patch(self.patched_role_mapping)

        return patched_config


class PowerManager(object):
    """Manage host to power on, power off, and reset."""

    def __init__(self, adapter_info, cluster_info, hosts_info):
        os_installer_name = adapter_info[const.OS_INSTALLER][const.NAME]
        self.os_installer = DeployManager._get_installer(OSInstaller,
                                                         os_installer_name,
                                                         adapter_info,
                                                         cluster_info,
                                                         hosts_info)

    def poweron(self):
        if not self.os_installer:
            logging.info("No OS installer found, cannot power on machine!")
            return
        self.os_installer.poweron()

    def poweroff(self):
        if not self.os_installer:
            logging.info("No OS installer found, cannot power on machine!")
            return
        self.os_installer.poweroff()

    def reset(self):
        if not self.os_installer:
            logging.info("No OS installer found, cannot power on machine!")
            return
        self.os_installer.reset()