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
|
#!/usr/bin/env python
#
# 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.
"""Scripts to delete cluster and it hosts"""
import logging
import os
import os.path
import sys
current_dir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(current_dir)
import switch_virtualenv
from compass.actions import clean
from compass.db.api import adapter_holder as adapter_api
from compass.db.api import database
from compass.db.api import user as user_api
from compass.tasks.client import celery
from compass.utils import flags
from compass.utils import logsetting
from compass.utils import setting_wrapper as setting
flags.add_bool('async',
help='run in async mode',
default=True)
flags.add('os_installers',
help='comma seperated os installers',
default='')
flags.add('package_installers',
help='comma separated package installers',
default='')
def clean_installers():
os_installers = [
os_installer
for os_installer in flags.OPTIONS.os_installers.split(',')
if os_installer
]
package_installers = [
package_installer
for package_installer in flags.OPTIONS.package_installers.split(',')
if package_installer
]
user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
adapters = adapter_api.list_adapters(user=user)
filtered_os_installers = {}
filtered_package_installers = {}
for adapter in adapters:
logging.info(
'got adapter: %s', adapter
)
if 'os_installer' in adapter:
os_installer = adapter['os_installer']
os_installer_name = os_installer['alias']
if not os_installers or os_installer_name in os_installers:
filtered_os_installers[os_installer_name] = os_installer
else:
logging.info(
'ignore os installer %s', os_installer_name
)
else:
logging.info(
'cannot find os installer in adapter %s',
adapter['name']
)
if 'package_installer' in adapter:
package_installer = adapter['package_installer']
package_installer_name = package_installer['alias']
if (
not package_installers or
package_installer_name in package_installers
):
filtered_package_installers[package_installer_name] = (
package_installer
)
else:
logging.info(
'ignore package installer %s', package_installer_name
)
else:
logging.info(
'cannot find package installer in adapter %s',
adapter['name']
)
logging.info(
'clean os installers: %s', filtered_os_installers.keys()
)
logging.info(
'clean package installers: %s', filtered_package_installers.keys()
)
if flags.OPTIONS.async:
for os_installer_name, os_installer in filtered_os_installers.items():
celery.send_task(
'compass.tasks.clean_os_installer',
(
os_installer['name'],
os_installer['settings']
)
)
for package_installer_name, package_installer in (
filtered_package_installers.items()
):
celery.send_task(
'compass.tasks.clean_package_installer',
(
package_installer['name'],
package_installer['settings']
)
)
else:
for os_installer_name, os_installer in (
filtered_os_installers.items()
):
try:
clean.clean_os_installer(
os_installer['name'],
os_installer['settings']
)
except Exception as error:
logging.error(
'failed to clean os installer %s', os_installer_name
)
logging.exception(error)
for package_installer_name, package_installer in (
filtered_package_installers.items()
):
try:
clean.clean_package_installer(
package_installer['name'],
package_installer['settings']
)
except Exception as error:
logging.error(
'failed to clean package installer %s',
package_installer_name
)
logging.exception(error)
if __name__ == '__main__':
flags.init()
logsetting.init()
database.init()
clean_installers()
|