11# pylint: disable=missing-module-docstring,missing-function-docstring
2- import os
3- import sys
2+ import distutils . version
3+ import itertools
44import json
5+ import os
56import pathlib
7+ import sys
68import types
7- import itertools
8- import distutils .version
9- import requests
9+
1010import invoke
1111import invoke .program
1212import PyInquirer
13+ import requests
1314
1415script_dir = os .path .dirname (os .path .realpath (__file__ ))
1516
17+
1618def ask_for_confirmation (prompt , batch_mode , default ):
1719 if batch_mode :
1820 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+ )
2226 confirmed = default
2327 else :
2428 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 }
3130 ]
3231 conf_answers = PyInquirer .prompt (conf_questions )
3332 confirmed = bool (conf_answers ) and conf_answers ["continue" ]
@@ -37,14 +36,17 @@ def ask_for_confirmation(prompt, batch_mode, default):
3736
3837def get_plugin_file_path ():
3938 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+ )
4142 ) as ver_f :
4243 plugin_version = json .load (ver_f )["vagrant_routeros_plugin_version" ]
4344 return os .path .join (
4445 script_dir ,
45- "vagrant-plugins-routeros/pkg/vagrant-routeros-{}.gem" .format (plugin_version )
46+ "vagrant-plugins-routeros/pkg/vagrant-routeros-{}.gem" .format (plugin_version ),
4647 )
4748
49+
4850def build_routeros (context , routeros_branch ):
4951 if routeros_branch == "routeros-long-term" :
5052 branch_name = "6 (long-term)"
@@ -78,18 +80,16 @@ def build_routeros(context, routeros_branch):
7880 box_file_name = "build/boxes/{}_{}.box" .format (routeros_branch , ros_version )
7981 if os .path .isfile (box_file_name ):
8082 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 )
8484
8585 packer_error_action = "cleanup" if context .routeros .batch else "ask"
8686 print ("Building the box..." )
8787 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 } " '
9090 "-var-file vagrant-plugins-routeros/vagrant_routeros_plugin_version.json "
9191 f"-on-error={ packer_error_action } -force routeros.pkr.hcl" ,
92- echo = True
92+ echo = True ,
9393 )
9494
9595 description_md = pathlib .Path (box_file_name ).with_suffix (".md" )
@@ -107,9 +107,7 @@ def build_plugin(context):
107107 print (f"Using helper VM in '{ vm_dir } '" )
108108 current_vm_state = ""
109109 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 )
113111 for line in run_result .stdout .splitlines ():
114112 values = line .split ("," )
115113 if len (values ) > 3 :
@@ -143,10 +141,15 @@ def build_plugin(context):
143141 with context .cd (vm_dir ):
144142 context .run ("vagrant halt" )
145143
144+
146145def remove_test_boxes (context ):
147146 for line in context .run ("vagrant box list" , hide = True ).stdout .splitlines ():
148147 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+ ):
150153 context .run (f"vagrant box remove -f { box_name } " , pty = True )
151154
152155
@@ -157,39 +160,41 @@ def do_cleanup(context):
157160
158161 files_2del = (pathlib .Path (script_dir ) / "build" / "boxes" ).glob ("*.box" )
159162 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" )
162164 )
163165 files_2del = itertools .chain (
164166 files_2del ,
165- (pathlib .Path (script_dir ) / "vagrant-plugins-routeros" / "pkg" ).glob ("*.gem" )
167+ (pathlib .Path (script_dir ) / "vagrant-plugins-routeros" / "pkg" ).glob ("*.gem" ),
166168 )
167169 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 ("*" )
170171 )
171172
172173 for f_2del in files_2del :
173174 if f_2del .is_file ():
174175 print (f" Deleting { f_2del } " )
175176 f_2del .unlink ()
176177
178+
177179def register_test_box (context , routeros_branch ):
178180 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+ ]
180184 if len (box_versions ) == 0 :
181185 sys .exit (
182186 f"Couldn't find files matching pattern 'build/boxes/{ routeros_branch } "
183187 f"_*.box'. Use 'inv { routeros_branch } ' to build a box"
184188 )
185189 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"
189193 )
190194 box_file = str (boxes_dir / box_file )
191195 context .run (f"vagrant box add packer_test_{ routeros_branch } { box_file } " , pty = True )
192196
197+
193198def test_ping (context , vm_name , ping_target ):
194199 print (f"Pinging { ping_target } from { vm_name } " )
195200 ping_output = context .run (
@@ -198,22 +203,25 @@ def test_ping(context, vm_name, ping_target):
198203 assert "received=3" in ping_output
199204 assert "packet-loss=0%" in ping_output
200205
206+
201207@invoke .task (default = True )
202208def show_help (context ):
203209 """This help message"""
204- context .run (' invoke --list' )
210+ context .run (" invoke --list" )
205211 print ("Use --help parameter to view task's options" )
206212 print ("Examples:" )
207213 print (" inv build --help" )
208214 print (" inv build --batch" )
209215 print (" inv routeros" )
210216 print (" inv plugin --batch" )
211217
218+
212219@invoke .task ()
213220def cleanup (context ):
214221 """Delete build artefacts and temporary files"""
215222 do_cleanup (context )
216223
224+
217225@invoke .task ()
218226def test (context ):
219227 """Register temporary vagrant boxes and run some tests against them"""
@@ -244,6 +252,7 @@ def test(context):
244252 print ("Removing test boxes..." )
245253 remove_test_boxes (context )
246254
255+
247256@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
248257def build (context , batch = False ):
249258 """Build all"""
@@ -256,34 +265,39 @@ def build(context, batch=False):
256265 build_routeros (context , routeros_branch = "routeros7" )
257266 test (context )
258267
268+
259269@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
260270def routeros_long_term (context , batch = False ):
261271 """Build RouterOS (long-term)"""
262272
263273 context .routeros .batch = batch
264274 build_routeros (context , routeros_branch = "routeros-long-term" )
265275
276+
266277@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
267278def routeros (context , batch = False ):
268279 """Build RouterOS (stable)"""
269280
270281 context .routeros .batch = batch
271282 build_routeros (context , routeros_branch = "routeros" )
272283
284+
273285@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
274286def routeros7 (context , batch = False ):
275287 """Build RouterOS 7 (stable)"""
276288
277289 context .routeros .batch = batch
278290 build_routeros (context , routeros_branch = "routeros7" )
279291
292+
280293@invoke .task (help = {"batch" : "Batch mode (disables interactive prompts)" })
281294def plugin (context , batch = False ):
282295 """Build 'vagrant-routeros' plugin"""
283296
284297 context .routeros .batch = batch
285298 build_plugin (context )
286299
300+
287301@invoke .task ()
288302def outdated (context ): # pylint: disable=unused-argument
289303 """Check if currently published box versions are up to date"""
@@ -293,34 +307,36 @@ def outdated(context): # pylint: disable=unused-argument
293307 branch_name = "6 (long-term)" ,
294308 version_url = "http://upgrade.mikrotik.com/routeros/LATEST.6fix" ,
295309 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" ,
297311 ),
298312 types .SimpleNamespace (
299313 branch_name = "6 (stable)" ,
300314 version_url = "http://upgrade.mikrotik.com/routeros/LATEST.6" ,
301315 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" ,
303317 ),
304318 types .SimpleNamespace (
305319 branch_name = "7 (stable)" ,
306320 version_url = "http://upgrade.mikrotik.com/routeros/NEWEST7.stable" ,
307321 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+ ),
310324 ]
311325
312326 for ros_version in ros_version_info :
313327 print (f"Checking RouterOS { ros_version .branch_name } version" )
314328 current_version = distutils .version .LooseVersion (
315329 requests .get (ros_version .version_url ).text .split (" " )[0 ]
316330 )
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+ ]
320334 box_os_version = distutils .version .LooseVersion (box_version .split ("-" )[0 ])
321335
322336 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+ )
324340 elif current_version > box_os_version :
325341 print (
326342 f"[!] '{ ros_version .box_name } ' box version { box_version } needs "
@@ -332,10 +348,5 @@ def outdated(context): # pylint: disable=unused-argument
332348 f"is greater than currently published version { current_version } "
333349 )
334350
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