-
Notifications
You must be signed in to change notification settings - Fork 29
/
featureTemplates.py
executable file
·282 lines (245 loc) · 9.27 KB
/
featureTemplates.py
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
#!/usr/bin/python
# Copyright 2015, Schuberg Philis BV
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
# Script to manage / (un)feature custom build templates
# Remi Bergsma - [email protected]
import time
import sys
import getopt
from cloudstackops import cloudstackops
import os.path
from random import choice
from prettytable import PrettyTable
from datetime import date
import re
# Function to handle our arguments
def handleArguments(argv):
global DEBUG
DEBUG = 0
global DRYRUN
DRYRUN = 1
global configProfileName
configProfileName = ''
global zoneName
zoneName = ''
# Usage message
help = "Usage: ./" + os.path.basename(__file__) + ' [options]' + \
'\n --config-profile -c \t\t\tSpecify the CloudMonkey profile name to get the credentials from (or specify in ./config file)' + \
'\n --zone -z <zonename>\t\t\tLimit to this zone' + \
'\n --debug\t\t\t\tEnable debug mode' + \
'\n --exec\t\t\t\tExecute for real'
try:
opts, args = getopt.getopt(
argv, "hc:z:", [
"config-profile=", "zone", "debug", "exec"])
except getopt.GetoptError as e:
print "Error: " + str(e)
print help
sys.exit(2)
if len(opts) == 0:
print help
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print help
sys.exit()
elif opt in ("-c", "--config-profile"):
configProfileName = arg
elif opt in ("-z", "--zone"):
zoneName = arg
elif opt in ("--debug"):
DEBUG = 1
elif opt in ("--exec"):
DRYRUN = 0
# Default to cloudmonkey default config file
if len(configProfileName) == 0:
configProfileName = "config"
# Parse arguments
if __name__ == "__main__":
handleArguments(sys.argv[1:])
# Init our class
c = cloudstackops.CloudStackOps(DEBUG, DRYRUN)
if DEBUG == 1:
print "Warning: Debug mode is enabled!"
if DRYRUN == 1:
print "Warning: dry-run mode is enabled, not running any commands!"
# make credentials file known to our class
c.configProfileName = configProfileName
# Init the CloudStack API
c.initCloudStackAPI()
if len(zoneName) > 1:
zoneID = c.checkCloudStackName(
{'csname': zoneName, 'csApiCall': 'listZones'})
print "Note: Only processing templates in zone " + zoneName
print "Warning: We only manage XenServer templates!"
if DEBUG == 1:
print "API address: " + c.apiurl
print "ApiKey: " + c.apikey
print "SecretKey: " + c.secretkey
# get templates from CloudStack
if len(zoneName) > 1:
templateData = c.listTemplates({'templatefilter': 'all', 'zoneid': zoneID})
else:
templateData = c.listTemplates({'templatefilter': 'all'})
# Sort by creation date, newest first
templateData.sort(key=lambda x: x.created, reverse=True)
if DEBUG == 1:
c.pp.pprint(templateData)
# We need this later
featureTemplates = {}
unfeatureTemplates = {}
deleteTemplates = {}
keepCount = {}
# Keep this many templates per zone per OStype
keepNr = 4
# Let's see what we currently have
for template in templateData:
if template is None:
continue
# Skip templates that are not usable anyway
if template.isready == False:
continue
# Don't mess with BUILTIN and SYSTEM templates, just for sure
if template.templatetype.upper() != "USER":
continue
# Our build templates are XenServer only, don't touch others
if template.hypervisor != "XenServer":
continue
# Delete cross-zone templates as they only bring trouble. But keep
# systemvm templates.
if template.crossZones and "systemvm" not in template.name.lower():
deleteTemplates[template.id] = template
if template.isfeatured:
unfeatureTemplates[template.id] = template
continue
# Find templates that match our build pattern: like m2015-02 or w2015-01
m = re.findall('[mw]\d{4}-\d{2}', template.name)
# Skip all others
if len(m) == 0:
# Only our build templates should be featured. Unfeature the others.
if template.isfeatured:
unfeatureTemplates[template.id] = template
continue
# Extract week / month number
parts = m[0].split('-')
for p in parts:
if p.isdigit():
buildNr = int(p)
elif p.startswith('w'):
buildType = "Week"
elif p.startswith('m'):
buildType = "Month"
# Figure out OS names
osName = template.name[:9].replace("_", "").replace(" ", "_")
# Feature the latest ostypename + zonename combi
key = osName.lower() + template.zonename
if osName.lower().startswith(
("rhel",
"centos_6",
"centos_7",
"ubuntu",
"win")):
if key in featureTemplates:
if featureTemplates[key].created < template.created:
featureTemplates[key] = template
elif template.isfeatured:
unfeatureTemplates[template.id] = template
else:
featureTemplates[key] = template
elif template.isfeatured:
unfeatureTemplates[key] = template
# Mark the ones we will keep and delete
countkey = osName + template.zonename
if countkey in keepCount:
keepCount[countkey] += 1
else:
keepCount[countkey] = 0
if DEBUG == 1:
print "Counter " + countkey + " " + str(keepCount[countkey])
keepkey = countkey + str(keepCount[countkey])
if keepCount[countkey] > keepNr:
deleteTemplates[keepkey] = template
# This is the work we need to process
loopWork = ['featureTemplates', 'unfeatureTemplates', 'deleteTemplates']
# Just print what we would do
if DRYRUN == 1:
for work in loopWork:
t = PrettyTable(["Name",
"Displaytext",
"ostypename",
"zonename",
"isfeatured",
"isready",
"Created",
"CrossZones",
"Type"])
print work + ":"
# Generate table
for templatekey, template in eval(work).iteritems():
t.add_row([template.name,
template.displaytext,
template.ostypename,
template.zonename,
template.isfeatured,
template.isready,
template.created,
template.crossZones,
template.templatetype])
print t
# Make changes to the templates
elif DRYRUN == 0:
for work in loopWork:
print "Processing " + work + ".."
if work == "featureTemplates":
for templatekey, template in eval(work).iteritems():
if DEBUG == 1:
print "DEBUG: Setting feature flag for " + template.name + " .."
if template.isfeatured == False:
result = c.updateTemplatePermissins(
{'templateid': template.id, 'isfeatured': 'true'})
if result == 1:
print "ERROR: Something went wrong!"
else:
print "Feature flag set OK for " + template.name
else:
print "Note: Template " + template.name + " is alreay featured, ignoring."
elif work == "unfeatureTemplates":
for templatekey, template in eval(work).iteritems():
if DEBUG == 1:
print "DEBUG: Unsetting feature flag for " + template.name + " .."
if template.isfeatured:
result = c.updateTemplatePermissins(
{'templateid': template.id, 'isfeatured': 'false'})
if result == 1:
print "ERROR: Something went wrong!"
else:
print "Feature flag removed OK for " + template.name
else:
print "Note: Template " + template.name + " is not featured, ignoring."
elif work == "deleteTemplates":
for templatekey, template in eval(work).iteritems():
if DEBUG == 1:
print "DEBUG: Deleting template " + template.name + " .."
result = c.deleteTemplate({'id': template.id})
if result == 1:
print "ERROR: Something went wrong!"
else:
print "Template " + template.name + " removed OK!"
if DEBUG == 1:
print "Note: We're done!"