1
1
# pylint: disable=missing-module-docstring,missing-function-docstring
2
- import os
3
- import sys
2
+ import distutils . version
3
+ import itertools
4
4
import json
5
+ import os
5
6
import pathlib
7
+ import sys
6
8
import types
7
- import itertools
8
- import distutils .version
9
- import requests
9
+
10
10
import invoke
11
11
import invoke .program
12
12
import PyInquirer
13
+ import requests
13
14
14
15
script_dir = os .path .dirname (os .path .realpath (__file__ ))
15
16
17
+
16
18
def ask_for_confirmation (prompt , batch_mode , default ):
17
19
if batch_mode :
18
20
print (prompt )
19
- print ("Batch mode is on. Autoselecting default option ({})" .format (
20
- {True : "yes" , False : "no" }[default ]
21
- ))
21
+ print (
22
+ "Batch mode is on. Autoselecting default option ({})" .format (
23
+ {True : "yes" , False : "no" }[default ]
24
+ )
25
+ )
22
26
confirmed = default
23
27
else :
24
28
conf_questions = [
25
- {
26
- "type" : "confirm" ,
27
- "name" : "continue" ,
28
- "message" : prompt ,
29
- "default" : True
30
- }
29
+ {"type" : "confirm" , "name" : "continue" , "message" : prompt , "default" : True }
31
30
]
32
31
conf_answers = PyInquirer .prompt (conf_questions )
33
32
confirmed = bool (conf_answers ) and conf_answers ["continue" ]
@@ -37,14 +36,17 @@ def ask_for_confirmation(prompt, batch_mode, default):
37
36
38
37
def get_plugin_file_path ():
39
38
with open (
40
- os .path .join (script_dir , "vagrant-plugins-routeros/vagrant_routeros_plugin_version.json" )
39
+ os .path .join (
40
+ script_dir , "vagrant-plugins-routeros/vagrant_routeros_plugin_version.json"
41
+ )
41
42
) as ver_f :
42
43
plugin_version = json .load (ver_f )["vagrant_routeros_plugin_version" ]
43
44
return os .path .join (
44
45
script_dir ,
45
- "vagrant-plugins-routeros/pkg/vagrant-routeros-{}.gem" .format (plugin_version )
46
+ "vagrant-plugins-routeros/pkg/vagrant-routeros-{}.gem" .format (plugin_version ),
46
47
)
47
48
49
+
48
50
def build_routeros (context , routeros_branch ):
49
51
if routeros_branch == "routeros-long-term" :
50
52
branch_name = "6 (long-term)"
@@ -78,18 +80,16 @@ def build_routeros(context, routeros_branch):
78
80
box_file_name = "build/boxes/{}_{}.box" .format (routeros_branch , ros_version )
79
81
if os .path .isfile (box_file_name ):
80
82
print ("'{}' has alredy been built" .format (box_file_name ))
81
- ask_for_confirmation (
82
- "Do you want to rebuild it?" , context .routeros .batch , True
83
- )
83
+ ask_for_confirmation ("Do you want to rebuild it?" , context .routeros .batch , True )
84
84
85
85
packer_error_action = "cleanup" if context .routeros .batch else "ask"
86
86
print ("Building the box..." )
87
87
context .run (
88
- f" packer build -var \ " ros_ver={ ros_version } \" "
89
- f" -var \ " box_file_name={ box_file_name } \" "
88
+ f' packer build -var "ros_ver={ ros_version } " '
89
+ f' -var "box_file_name={ box_file_name } " '
90
90
"-var-file vagrant-plugins-routeros/vagrant_routeros_plugin_version.json "
91
91
f"-on-error={ packer_error_action } -force routeros.pkr.hcl" ,
92
- echo = True
92
+ echo = True ,
93
93
)
94
94
95
95
description_md = pathlib .Path (box_file_name ).with_suffix (".md" )
@@ -107,9 +107,7 @@ def build_plugin(context):
107
107
print (f"Using helper VM in '{ vm_dir } '" )
108
108
current_vm_state = ""
109
109
with context .cd (vm_dir ):
110
- run_result = context .run (
111
- command = "vagrant status --machine-readable" , hide = True
112
- )
110
+ run_result = context .run (command = "vagrant status --machine-readable" , hide = True )
113
111
for line in run_result .stdout .splitlines ():
114
112
values = line .split ("," )
115
113
if len (values ) > 3 :
@@ -143,10 +141,15 @@ def build_plugin(context):
143
141
with context .cd (vm_dir ):
144
142
context .run ("vagrant halt" )
145
143
144
+
146
145
def remove_test_boxes (context ):
147
146
for line in context .run ("vagrant box list" , hide = True ).stdout .splitlines ():
148
147
box_name = line .split (" " )[0 ]
149
- if box_name in ("packer_test_routeros" , "packer_test_routeros-long-term" , "packer_test_routeros7" ):
148
+ if box_name in (
149
+ "packer_test_routeros" ,
150
+ "packer_test_routeros-long-term" ,
151
+ "packer_test_routeros7" ,
152
+ ):
150
153
context .run (f"vagrant box remove -f { box_name } " , pty = True )
151
154
152
155
@@ -157,39 +160,41 @@ def do_cleanup(context):
157
160
158
161
files_2del = (pathlib .Path (script_dir ) / "build" / "boxes" ).glob ("*.box" )
159
162
files_2del = itertools .chain (
160
- files_2del ,
161
- (pathlib .Path (script_dir ) / "build" / "boxes" ).glob ("*.md" )
163
+ files_2del , (pathlib .Path (script_dir ) / "build" / "boxes" ).glob ("*.md" )
162
164
)
163
165
files_2del = itertools .chain (
164
166
files_2del ,
165
- (pathlib .Path (script_dir ) / "vagrant-plugins-routeros" / "pkg" ).glob ("*.gem" )
167
+ (pathlib .Path (script_dir ) / "vagrant-plugins-routeros" / "pkg" ).glob ("*.gem" ),
166
168
)
167
169
files_2del = itertools .chain (
168
- files_2del ,
169
- (pathlib .Path (script_dir ) / "packer_cache" ).rglob ("*" )
170
+ files_2del , (pathlib .Path (script_dir ) / "packer_cache" ).rglob ("*" )
170
171
)
171
172
172
173
for f_2del in files_2del :
173
174
if f_2del .is_file ():
174
175
print (f" Deleting { f_2del } " )
175
176
f_2del .unlink ()
176
177
178
+
177
179
def register_test_box (context , routeros_branch ):
178
180
boxes_dir = pathlib .Path (script_dir ) / "build" / "boxes"
179
- box_versions = [item .stem .split ("_" )[1 ] for item in boxes_dir .glob (f"{ routeros_branch } _*.box" )]
181
+ box_versions = [
182
+ item .stem .split ("_" )[1 ] for item in boxes_dir .glob (f"{ routeros_branch } _*.box" )
183
+ ]
180
184
if len (box_versions ) == 0 :
181
185
sys .exit (
182
186
f"Couldn't find files matching pattern 'build/boxes/{ routeros_branch } "
183
187
f"_*.box'. Use 'inv { routeros_branch } ' to build a box"
184
188
)
185
189
box_file = (
186
- f"{ routeros_branch } _" +
187
- max (box_versions , key = distutils .version .LooseVersion ) +
188
- ".box"
190
+ f"{ routeros_branch } _"
191
+ + max (box_versions , key = distutils .version .LooseVersion )
192
+ + ".box"
189
193
)
190
194
box_file = str (boxes_dir / box_file )
191
195
context .run (f"vagrant box add packer_test_{ routeros_branch } { box_file } " , pty = True )
192
196
197
+
193
198
def test_ping (context , vm_name , ping_target ):
194
199
print (f"Pinging { ping_target } from { vm_name } " )
195
200
ping_output = context .run (
@@ -198,22 +203,25 @@ def test_ping(context, vm_name, ping_target):
198
203
assert "received=3" in ping_output
199
204
assert "packet-loss=0%" in ping_output
200
205
206
+
201
207
@invoke .task (default = True )
202
208
def show_help (context ):
203
209
"""This help message"""
204
- context .run (' invoke --list' )
210
+ context .run (" invoke --list" )
205
211
print ("Use --help parameter to view task's options" )
206
212
print ("Examples:" )
207
213
print (" inv build --help" )
208
214
print (" inv build --batch" )
209
215
print (" inv routeros" )
210
216
print (" inv plugin --batch" )
211
217
218
+
212
219
@invoke .task ()
213
220
def cleanup (context ):
214
221
"""Delete build artefacts and temporary files"""
215
222
do_cleanup (context )
216
223
224
+
217
225
@invoke .task ()
218
226
def test (context ):
219
227
"""Register temporary vagrant boxes and run some tests against them"""
@@ -244,6 +252,7 @@ def test(context):
244
252
print ("Removing test boxes..." )
245
253
remove_test_boxes (context )
246
254
255
+
247
256
@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
248
257
def build (context , batch = False ):
249
258
"""Build all"""
@@ -256,34 +265,39 @@ def build(context, batch=False):
256
265
build_routeros (context , routeros_branch = "routeros7" )
257
266
test (context )
258
267
268
+
259
269
@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
260
270
def routeros_long_term (context , batch = False ):
261
271
"""Build RouterOS (long-term)"""
262
272
263
273
context .routeros .batch = batch
264
274
build_routeros (context , routeros_branch = "routeros-long-term" )
265
275
276
+
266
277
@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
267
278
def routeros (context , batch = False ):
268
279
"""Build RouterOS (stable)"""
269
280
270
281
context .routeros .batch = batch
271
282
build_routeros (context , routeros_branch = "routeros" )
272
283
284
+
273
285
@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
274
286
def routeros7 (context , batch = False ):
275
287
"""Build RouterOS 7 (stable)"""
276
288
277
289
context .routeros .batch = batch
278
290
build_routeros (context , routeros_branch = "routeros7" )
279
291
292
+
280
293
@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
281
294
def plugin (context , batch = False ):
282
295
"""Build 'vagrant-routeros' plugin"""
283
296
284
297
context .routeros .batch = batch
285
298
build_plugin (context )
286
299
300
+
287
301
@invoke .task ()
288
302
def outdated (context ): # pylint: disable=unused-argument
289
303
"""Check if currently published box versions are up to date"""
@@ -293,34 +307,36 @@ def outdated(context): # pylint: disable=unused-argument
293
307
branch_name = "6 (long-term)" ,
294
308
version_url = "http://upgrade.mikrotik.com/routeros/LATEST.6fix" ,
295
309
box_name = "cheretbe/routeros-long-term" ,
296
- box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros-long-term"
310
+ box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros-long-term" ,
297
311
),
298
312
types .SimpleNamespace (
299
313
branch_name = "6 (stable)" ,
300
314
version_url = "http://upgrade.mikrotik.com/routeros/LATEST.6" ,
301
315
box_name = "cheretbe/routeros" ,
302
- box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros"
316
+ box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros" ,
303
317
),
304
318
types .SimpleNamespace (
305
319
branch_name = "7 (stable)" ,
306
320
version_url = "http://upgrade.mikrotik.com/routeros/NEWEST7.stable" ,
307
321
box_name = "cheretbe/routeros7" ,
308
- box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros7"
309
- )
322
+ box_url = "https://app.vagrantup.com/api/v1/box/cheretbe/routeros7" ,
323
+ ),
310
324
]
311
325
312
326
for ros_version in ros_version_info :
313
327
print (f"Checking RouterOS { ros_version .branch_name } version" )
314
328
current_version = distutils .version .LooseVersion (
315
329
requests .get (ros_version .version_url ).text .split (" " )[0 ]
316
330
)
317
- box_version = (
318
- requests . get ( ros_version . box_url ). json ()[ "current_version" ][ " version"]
319
- )
331
+ box_version = requests . get ( ros_version . box_url ). json ()[ "current_version" ][
332
+ " version"
333
+ ]
320
334
box_os_version = distutils .version .LooseVersion (box_version .split ("-" )[0 ])
321
335
322
336
if box_os_version == current_version :
323
- print (f"Published version { box_version } of '{ ros_version .box_name } ' is up to date" )
337
+ print (
338
+ f"Published version { box_version } of '{ ros_version .box_name } ' is up to date"
339
+ )
324
340
elif current_version > box_os_version :
325
341
print (
326
342
f"[!] '{ ros_version .box_name } ' box version { box_version } needs "
@@ -332,10 +348,5 @@ def outdated(context): # pylint: disable=unused-argument
332
348
f"is greater than currently published version { current_version } "
333
349
)
334
350
335
- invoke .main .program .config .update (
336
- {
337
- "routeros" : {
338
- "batch" : False
339
- }
340
- }
341
- )
351
+
352
+ invoke .main .program .config .update ({"routeros" : {"batch" : False }})
0 commit comments