Skip to content

Commit 4ce56dc

Browse files
committed
updates
1 parent 3ced088 commit 4ce56dc

6 files changed

Lines changed: 54 additions & 36 deletions

File tree

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.0.0
1+
4.0.0

netsim_wrapper/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
from logging import DEBUG
3-
from nwrap import NWrap
3+
4+
from netsim_wrapper.nwrap import NWrap
45

56

67
def check_verbose(params):

netsim_wrapper/netsim.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import re
2-
from logging import DEBUG
3-
from utils import Utils, Singleton
42
from collections import OrderedDict
53

4+
from netsim_wrapper.utils import Utils, Singleton
5+
6+
67

78
class Netsim(metaclass=Singleton):
89
name = __name__

netsim_wrapper/nso.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
from utils import Singleton, Download
21
from collections import defaultdict
32

3+
from netsim_wrapper.utils import Singleton, Download
4+
5+
46

57
class AuthGroup(metaclass=Singleton):
68
name = __name__
79

8-
def local(self):
9-
return """
10+
def add(self, type):
11+
def local():
12+
return """
1013
<devices xmlns="http://tail-f.com/ns/ncs">
1114
<authgroups>
1215
<group>
@@ -20,8 +23,8 @@ def local(self):
2023
</devices>
2124
"""
2225

23-
def system(self):
24-
return """
26+
def system():
27+
return """
2528
<devices xmlns="http://tail-f.com/ns/ncs">
2629
<authgroups>
2730
<group>
@@ -45,13 +48,15 @@ def system(self):
4548
</authgroups>
4649
</devices>
4750
"""
48-
51+
if type == 'system':
52+
return system()
53+
return local()
4954

5055
class AAA(metaclass=Singleton):
5156
name = __name__
5257

5358
def add(self, type):
54-
def admin(self):
59+
def admin():
5560
return """
5661
<config xmlns="http://tail-f.com/ns/config/1.0">
5762
<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
@@ -79,7 +84,7 @@ def admin(self):
7984
</config>
8085
"""
8186

82-
def default(self):
87+
def default():
8388
return """
8489
<config xmlns="http://tail-f.com/ns/config/1.0">
8590
<aaa xmlns="http://tail-f.com/ns/aaa/1.1">
@@ -176,10 +181,7 @@ def compile_neds(self):
176181

177182
def load_authgroup(self, type, path=None): # type -> local/system/custom
178183
path = path or '/tmp/authgroup.xml'
179-
if type == 'local':
180-
self.utils.xml.dump(self.auth.local, path)
181-
if type == 'system':
182-
self.utils.xml.dump(self.auth.system, path)
184+
self.utils.xml.dump(self.auth.add(type), path)
183185
return path
184186

185187
def load_aaa(self, type): # type -> add.admin/add.default
@@ -208,20 +210,24 @@ def download_neds(self):
208210
self.default['neds']
209211
)
210212

211-
def extract_neds(self, extract_tar=True):
213+
def extract_neds(self, signed_bin=True, extract_tar=True):
212214
self.log.debug("extracting neds")
213215
# TODO: need to test
214-
cmd = ['cd', self.default['ppath'], '&&']
216+
files = []
215217
for ned in self.default['neds']:
216218
file = ned.split('/')[-1]
217219
self.log.debug('ned: {}'.format(file))
218220
self.utils.cmd.run(['mv', file, self.default['ppath']])
219-
self.utils.cmd.run(cmd + ['bash', file])
220-
if not extract_tar:
221-
continue
222-
tar_file = self.utils.get_tar(file)
223-
self.utils.cmd.run(cmd + ['tar', '-xvf', tar_file])
224-
self.utils.cmd.run(cmd + ['rm', '-rf', tar_file, file])
221+
files.append(file)
222+
223+
self.utils.change_dir(self.default['ppath'])
224+
for ned in files:
225+
if signed_bin:
226+
self.utils.cmd.run(['bash', ned])
227+
if extract_tar:
228+
tar_file = self.utils.get_tar(ned)
229+
self.utils.cmd.run(['tar', '-xvf', tar_file])
230+
self.utils.cmd.run(['rm', '-rf', tar_file, ned])
225231
self.log.debug('extraction done')
226232

227233
def create_devices(self):
@@ -277,8 +283,10 @@ def analyse_params(self):
277283

278284
self.log.debug("analysing template done")
279285

280-
def apply(self):
286+
def apply(self, username, password):
281287
self.analyse_params()
288+
self.default['username'] = username
289+
self.default['password'] = password
282290

283291
self.log.debug("per-netsim steps")
284292
if self.default['download-neds']:
@@ -307,7 +315,7 @@ def pre_create():
307315
self.nwrap.netsim.start()
308316

309317
if self.default['add-to-nso']:
310-
path = 'tmp/devices.xml'
318+
path = '/tmp/devices.xml'
311319
data = self.nwrap.netsim.ncs_xml_init(sysout=False)
312320
self.utils.xml.dump(data, path)
313321

netsim_wrapper/nwrap.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from utils import Singleton, Download
2-
from netsim import Netsim, NetsimTemplates, NetsimInfo, NetsimDelete
3-
4-
from nso import NsoUtils
51
from collections import defaultdict
62

3+
from netsim_wrapper.utils import Singleton, Download
4+
from netsim_wrapper.netsim import Netsim, NetsimTemplates
5+
from netsim_wrapper.netsim import NetsimInfo, NetsimDelete
6+
from netsim_wrapper.nso import NsoUtils
7+
78

89
help = """
910
Usage nwrap template create [network | device] [yaml | json] [<fileName>]
@@ -235,6 +236,9 @@ def analyse_params(self, params):
235236
def apply(self, params):
236237
params = self.analyse_params(params)
237238

239+
# for k,v in params.items():
240+
# print("{}: {}".format(k, v))
241+
238242
if params['help']:
239243
self.help
240244
if params['version']:
@@ -274,7 +278,7 @@ def apply(self, params):
274278
# feature template load and apply
275279
if params['load']:
276280
self.nwrap.template.load(params['filename'])
277-
self.nso.apply()
281+
self.nso.apply(params['username'], params['password'])
278282

279283
# feature directly calling ncs-netsim commands
280284
if params['pass']:
@@ -307,6 +311,3 @@ def apply(self, params):
307311
self.nwrap.add_device(params['package'], params['device'])
308312
self.utils.exit
309313

310-
# for k,v in params.items():
311-
# print("{}: {}".format(k, v))
312-

netsim_wrapper/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
import subprocess
77

8-
from os import path, remove
8+
from os import path, remove, chdir
99
from collections import OrderedDict
1010
from itertools import chain, takewhile
1111

@@ -167,6 +167,8 @@ def decode(self, args):
167167

168168
def join(self, cmd, sensitive):
169169
# print(cmd)
170+
if type(cmd) == str:
171+
return cmd
170172
cmd_str = ' '.join(cmd)
171173
if sensitive:
172174
cmd_str = re.sub(r":\S+", ':xxxxxx', cmd_str)
@@ -234,6 +236,9 @@ def iloc(self, lst, e): # get_index
234236
except ValueError:
235237
return None
236238

239+
def change_dir(self, path):
240+
chdir(path)
241+
237242
def flatset(slef, s):
238243
return set(chain(*s))
239244

@@ -269,6 +274,8 @@ def filter_alive(self, data):
269274
return devices
270275

271276
def append_log(self, out:str, method='info'):
277+
if not out:
278+
return
272279
for line in out.split('\n'):
273280
if line: getattr(self.log, method)(line)
274281
if self.log.level == logging.DEBUG:
@@ -290,7 +297,7 @@ def setup(self, username, password):
290297
self.cmd += ['-u', "{}:{}".format(username, password)]
291298

292299
def download(self, link):
293-
self.log.debug('download: {}'.format(link))
300+
self.log.info('download: {}'.format(link))
294301
fname = link.split('/')[-1] or 'dummyFile'
295302
out = self.utils.cmd.run(self.cmd + [link, '-o', fname], sensitive=True)
296303
self.utils.append_log(out)

0 commit comments

Comments
 (0)