summaryrefslogtreecommitdiffstats
path: root/compass-tasks-base/tasks/tasks.py
blob: f649afd7a94c8c1f68315386552f50d3f6eee840 (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
# 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.

"""Module to define celery tasks.

   .. moduleauthor:: Xiaodong Wang <xiaodongwang@huawei.com>
"""
import logging

from celery.signals import celeryd_init
from celery.signals import setup_logging

from compass.actions import clean
from compass.actions import delete
from compass.actions import deploy
from compass.actions import install_callback
from compass.actions import patch
from compass.actions import poll_switch
from compass.actions import update_progress
from compass.db.api import adapter_holder as adapter_api
from compass.db.api import database
from compass.db.api import metadata_holder as metadata_api
from compass.log_analyzor import progress_calculator

from compass.tasks.client import celery
from compass.utils import flags
from compass.utils import logsetting
from compass.utils import setting_wrapper as setting


@celeryd_init.connect()
def global_celery_init(**_):
    """Initialization code."""
    flags.init()
    flags.OPTIONS.logfile = setting.CELERY_LOGFILE
    logsetting.init()
    database.init()
    adapter_api.load_adapters()
    metadata_api.load_metadatas()
    adapter_api.load_flavors()
    progress_calculator.load_calculator_configurations()


@setup_logging.connect()
def tasks_setup_logging(**_):
    """Setup logging options from compass setting."""
    flags.init()
    flags.OPTIONS.logfile = setting.CELERY_LOGFILE
    logsetting.init()


@celery.task(name='compass.tasks.pollswitch')
def pollswitch(
    poller_email, ip_addr, credentials,
    req_obj='mac', oper='SCAN'
):
    """Query switch and return expected result.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch credentials
    :type credentials: dict
    :param reqObj: the object requested to query from switch.
    :type reqObj: str
    :param oper: the operation to query the switch (SCAN, GET, SET).
    :type oper: str
    """
    try:
        poll_switch.poll_switch(
            poller_email, ip_addr, credentials,
            req_obj=req_obj, oper=oper
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.cluster_health')
def health_check(cluster_id, send_report_url, useremail):
    """Verify the deployed cluster functionally works.

       :param cluster_id: ID of the cluster
       :param send_report_url: The URL which reports should send back
    """
    try:
        deploy.health_check(cluster_id, send_report_url, useremail)
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.deploy_cluster')
def deploy_cluster(deployer_email, cluster_id, clusterhost_ids):
    """Deploy the given cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    :param clusterhost_ids: the id of the hosts in the cluster
    :type clusterhost_ids: list of int
    """
    try:
        deploy.deploy(cluster_id, clusterhost_ids, deployer_email)
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.redeploy_cluster')
def redeploy_cluster(deployer_email, cluster_id):
    """Redeploy the given cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    """
    try:
        deploy.redeploy(cluster_id, deployer_email)
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.patch_cluster')
def patch_cluster(patcher_email, cluster_id):
    """Patch the existing cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    """
    try:
        patch.patch(cluster_id, patcher_email)
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.reinstall_cluster')
def reinstall_cluster(installer_email, cluster_id, clusterhost_ids):
    """reinstall the given cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    :param clusterhost_ids: the id of the hosts in the cluster
    :type clusterhost_ids: list of int
    """
    try:
        deploy.redeploy(cluster_id, clusterhost_ids, installer_email)
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.delete_cluster')
def delete_cluster(
    deleter_email, cluster_id, clusterhost_ids,
    delete_underlying_host=False
):
    """Delete the given cluster.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    :param clusterhost_ids: the id of the hosts in the cluster
    :type clusterhost_ids: list of int
    """
    try:
        delete.delete_cluster(
            cluster_id, clusterhost_ids, deleter_email,
            delete_underlying_host=delete_underlying_host
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.delete_cluster_host')
def delete_cluster_host(
    deleter_email, cluster_id, host_id,
    delete_underlying_host=False
):
    """Delte the given cluster host.

    :param cluster_id: id of the cluster
    :type cluster_id: int
    :param host_id: id of the host
    :type host_id: int
    """
    try:
        delete.delete_cluster_host(
            cluster_id, host_id, deleter_email,
            delete_underlying_host=delete_underlying_host
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.delete_host')
def delete_host(deleter_email, host_id, cluster_ids):
    """Delete the given host.

    :param host_id: id of the host
    :type host_id: int
    :param cluster_ids: list of cluster id
    :type cluster_ids: list of int
    """
    try:
        delete.delete_host(
            host_id, cluster_ids, deleter_email
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.clean_os_installer')
def clean_os_installer(
    os_installer_name, os_installer_settings
):
    """Clean os installer."""
    try:
        clean.clean_os_installer(
            os_installer_name, os_installer_settings
        )
    except Exception as error:
        logging.excception(error)


@celery.task(name='compass.tasks.clean_package_installer')
def clean_package_installer(
    package_installer_name, package_installer_settings
):
    """Clean package installer."""
    try:
        clean.clean_package_installer(
            package_installer_name, package_installer_settings
        )
    except Exception as error:
        logging.excception(error)


@celery.task(name='compass.tasks.poweron_host')
def poweron_host(host_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.poweroff_host')
def poweroff_host(host_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.reset_host')
def reset_host(host_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.poweron_machine')
def poweron_machine(machine_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.poweroff_machine')
def poweroff_machine(machine_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.reset_machine')
def reset_machine(machine_id):
    """Deploy the given cluster."""
    pass


@celery.task(name='compass.tasks.os_installed')
def os_installed(
    host_id, clusterhosts_ready,
    clusters_os_ready
):
    """callback when os is installed."""
    try:
        install_callback.os_installed(
            host_id, clusterhosts_ready,
            clusters_os_ready
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.package_installed')
def package_installed(
    cluster_id, host_id, cluster_ready, host_ready
):
    """callback when package is installed."""
    try:
        install_callback.package_installed(
            cluster_id, host_id, cluster_ready, host_ready
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.cluster_installed')
def cluster_installed(
    cluster_id, clusterhosts_ready
):
    """callback when package is installed."""
    try:
        install_callback.cluster_installed(
            cluster_id, clusterhosts_ready
        )
    except Exception as error:
        logging.exception(error)


@celery.task(name='compass.tasks.update_progress')
def update_clusters_progress():
    """Calculate the installing progress of the given cluster."""
    logging.info('update_clusters_progress')
    try:
        update_progress.update_progress()
    except Exception as error:
        logging.exception(error)