-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf5_tmsh.py
371 lines (280 loc) · 11.4 KB
/
f5_tmsh.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python
# Copyright 2014, Rackspace US, Inc.
#
# 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.
#
# (c) 2015, Dennis DeMarco <dennis.demarco@rackspace.com>
from F5rest import F5rest
from F5rest import translate_dash
import argparse
def list_to_dict(list):
data = {}
for i in range(0, len(list),2):
key = translate_dash(list[i])
value = list[i+1]
data[key] = value
return data
def members_parse(members):
tmp = []
pos = 0
pri = False
for item in members:
if item == 'priority-group':
pri = True
if (item != 'priority-group' and not pri):
tmp.append({'name': item})
if (item != 'priority-group' and pri):
pri=False
tmp[pos]['priorityGroup']=item
pos=pos+1
return tmp
def parse_args(token,args):
subsets = ['profiles', 'persist',
'source-address-translation','members'
]
token_words = len(token.split())
arg_list= args.split()
target = arg_list[token_words].split('/')
if len(target) > 1:
partition = target[1]
name = target[2]
else:
partition = "Common"
name = target[0]
set=0
data = {}
pos = token_words+1
word_string = False
build_string = ""
subset_flag = False
data_return = {}
tmp = []
for word in arg_list[pos:]:
if word == 'replace-all-with':
continue
if word == '{':
set=set+1
continue
if word == '}':
set=set-1
if set ==1:
subset_flag=False
continue
if word in subsets:
subset_flag = True
subset_key = word
if word[0] == '"' and word_string == False:
word_string=True
if word_string == False:
if subset_flag == False:
if not data.has_key(set):
data[set] = [word]
else:
data[set].append(word)
else:
if not data.has_key(subset_key):
data[subset_key] = []
else:
data[subset_key].append(word)
if word_string:
if len(build_string) >0:
build_string=build_string + " " + word
else:
build_string=build_string + word
if word[-1] == '"' and word_string == True:
data[set].append(build_string)
build_string = ""
word_string=False
try:
tmp = data[1]
except:
pass
for i in range(0, len(tmp),2):
key = translate_dash(tmp[i])
value = tmp[i+1]
data_return[key] = value
for key in data.keys():
if key in subsets:
if key == 'profiles':
data_return[key] = [{ 'name': data[key][0]}]
if key == 'persist':
data_return[key] = data[key][0]
if key == 'source-address-translation':
data_return[translate_dash(key)]=list_to_dict(data[key])
if key == 'members':
data_return[key]=members_parse(data[key])
return name,partition,data_return
def status_code(status_obj):
if status_obj.status_code == 409:
return "Exists"
if status_obj.status_code ==200:
return "Ok"
if status_obj.status_code ==404:
return "Error"
if status_obj.status_code ==400:
return status_obj.text
return status_obj.text
def do_create_partition(token,args,my_f5):
partition = args.split(token)[1].lstrip()
result = my_f5.create_partition(partition)
print "Creating New F5 Partition .... [%s] ... [%s]" % (partition,status_code(result))
def do_modify_globalSettings(token,args,my_f5):
result = my_f5.cli_service_number()
print "Modifying CLI gloabl-settings ... [%s]" % status_code(result)
def do_create_monitor_mysql(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_monitor_mysql(partition,name,**data)
print "Creating MySQL Monitor ... [%s] ... [%s]" % (name,status_code(result))
def do_create_monitor_http(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_monitor_http(partition,name,**data)
print "Creating Http Monitor ... [%s] ... [%s]" % (name,status_code(result))
def do_create_monitor_https(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_monitor_https(partition,name,**data)
print "Creating Https Monitor ... [%s] ... [%s]" % (name,status_code(result))
def do_create_monitor_tcp(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_monitor_tcp(partition,name,**data)
print "Creating TCP Monitor ... [%s] ... [%s]" % (name,status_code(result))
def do_install_cert(token,args,my_f5):
(file,partition,data) = parse_args(token,args)
path = '/var/tmp/' + file
name = file[:-4]
print "SFTP ssl file ...[%s]" % file
my_f5.put_sftp(file, path)
result = my_f5.install_cert(partition,name,path)
print "Installing cert ... [%s] ... [%s]" % (name,status_code(result))
def do_install_key(token,args,my_f5):
(file,partition,data) = parse_args(token,args)
path = '/var/tmp/' + file
name = file[:-4]
print "SFTP ssl file ...[%s]" % file
my_f5.put_sftp(file, path)
result = my_f5.install_key(partition,name,path)
print "Installing key ... [%s] ... [%s]" % (name,status_code(result))
def do_create_profile_client_ssl(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_profile_client_ssl(partition,name,**data)
print "Creating Profile client-ssl ... [%s] ... [%s]" % (name,status_code(result))
def do_create_profile_server_ssl(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_profile_server_ssl(partition,name,**data)
print "Creating Profile ... [%s] ... [%s]" % (name,status_code(result))
def do_create_persistence_source_addr(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_persistence_source_addr(partition,name,**data)
print "Creating Persistence source-addr... [%s] ... [%s]" % (name,status_code(result))
def do_create_persistence_cookie(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_persistence_cookie(partition,name,**data)
print "Creating Persistence cookie... [%s] ... [%s]" % (name,status_code(result))
def do_create_node(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_node(partition,name,**data)
print "Creating Node ... [%s] ... [%s]" % (name,status_code(result))
def do_create_pool(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_pool(partition,name,**data)
print "Creating Pool ... [%s] ... [%s]" % (name,status_code(result))
def do_create_monitor_external(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
result = my_f5.create_monitor_external(partition,name,**data)
print "Creating external monitor ... [%s] ...[%s]" % (name,status_code(result))
def do_create_sys_file_external(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
path=data['sourcePath']
file = path.split('/')[-1]
print "SFTP external monitor ...[%s]" % file
my_f5.put_sftp(file, '/config/monitors/' + file)
result = my_f5.create_sys_file_external(partition,name,path)
print "Creating sys file .. [%s] ...[%s]" % (name,status_code(result))
def do_create_virtual(token,args,my_f5):
(name,partition,data) = parse_args(token,args)
result = my_f5.create_virtual(partition,name,**data)
print "Creating Virtual Server ... [%s] .... [%s]" % (name,status_code(result))
def do_create_snatpool(token,args,my_f5):
(name,partition,data)=parse_args(token,args)
# snat pool has just a list of members not a dict unlike almost every other rest call.
tmp = []
for member in data['members']:
tmp.append(member['name'])
result = my_f5.create_snatpool(partition,name,tmp)
print "Creating SNATPool ... [%s] ...[%s]" % (name,status_code(result))
def do_nothing(token,args,my_f5):
pass
def parse_line(line,my_f5):
tokens_callables = {'create auth partition': do_create_partition,
'modify cli global-settings service number': do_modify_globalSettings,
'create ltm snatpool': do_create_snatpool,
'create ltm monitor mysql': do_create_monitor_mysql,
'create ltm monitor http ' : do_create_monitor_http,
'create ltm monitor https ': do_create_monitor_https,
'create ltm monitor tcp': do_create_monitor_tcp,
'create ltm monitor external': do_create_monitor_external,
'create sys file external-monitor': do_create_sys_file_external,
'install sys crypto cert': do_install_cert,
'install sys crypto key': do_install_key,
'create ltm profile client-ssl':do_create_profile_client_ssl,
'create ltm profile server-ssl':do_create_profile_server_ssl,
'create ltm persistence source-addr': do_create_persistence_source_addr,
'create ltm persistence cookie': do_create_persistence_cookie,
'create ltm node': do_create_node,
'create ltm pool': do_create_pool,
'create ltm virtual': do_create_virtual
}
for token in tokens_callables.keys():
if (line.find(token) >= 0):
functionToCall = tokens_callables[token]
functionToCall(token,line,my_f5)
def args():
"""Setup argument Parsing."""
parser = argparse.ArgumentParser(
usage='%(prog)s',
description='Rackspace Openstack, F5 Rest from Inventory Generator output',
epilog='F5 tmsh parser Licensed "Apache 2.0"')
parser.add_argument(
'-f',
'--file',
help='Inventory file. Default: [ %(default)s ]',
required=False,
default='rpc_f5_config.sh'
)
parser.add_argument(
'--username',
help='user name to connect to F5',
required=True,
default=None
)
parser.add_argument(
'--hostname',
help='F5 hostname to configure',
required=True,
default=None
)
parser.add_argument(
'--password',
help='Password to connect to F5.',
required=True,
default=None
)
return vars(parser.parse_args())
def main():
user_args = args()
with open(user_args['file']) as f:
content = f.read().splitlines()
my_f5 = F5rest(user_args['hostname'],user_args['username'],user_args['password'])
for line in content:
parse_line(line,my_f5)
if __name__ == "__main__":
main()