Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions ansible/roles/vm_set/library/ceos_network.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/python

import datetime
import json
import logging
import subprocess
Expand All @@ -9,6 +8,7 @@

import docker

from ansible.module_utils.debug_utils import config_module_logging
from ansible.module_utils.basic import *

DOCUMENTATION = '''
Expand Down Expand Up @@ -52,13 +52,7 @@
MGMT_TAP_TEMPLATE = '%s-m'
INT_TAP_TEMPLATE = 'eth%d'


def config_logging():
curtime = datetime.datetime.now().isoformat()
logging.basicConfig(
filename=CMD_DEBUG_FNAME % curtime,
format='%(asctime)s %(levelname)s #%(lineno)d: %(message)s',
level=logging.DEBUG)
config_module_logging('ceos_network')


class CeosNetwork(object):
Expand Down Expand Up @@ -364,8 +358,6 @@ def main():
fp_mtu = module.params['fp_mtu']
max_fp_num = module.params['max_fp_num']

config_logging()

try:
cnet = CeosNetwork(name, vm_name, mgmt_bridge, fp_mtu, max_fp_num)

Expand Down
60 changes: 14 additions & 46 deletions ansible/roles/vm_set/library/kickstart.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
#!/usr/bin/python

import datetime
from telnetlib import Telnet
import logging
from ansible.module_utils.debug_utils import config_module_logging

def encode(arg):
if (sys.version_info.major == 3 and sys.version_info.minor >= 5):
return arg.encode("ascii")
else:
return arg


class MyDebug(object):
def __init__(self, filename, enabled=True):
if enabled:
self.fp = open(filename, 'w')
else:
self.fp = None

return

def cleanup(self):
if self.fp:
self.fp.close()
self.fp = None

return

def __del__(self):
self.cleanup()

return

def debug(self, msg):
if self.fp:
self.fp.write('%s\n' % msg)
self.fp.flush()

return

config_module_logging('kickstart')

class EMatchNotFound(Exception):
pass
Expand All @@ -56,10 +29,9 @@ class ENotInEnabled(Exception):


class SerialSession(object):
def __init__(self, port, debug):
def __init__(self, port):
self.enabled = False
self.d = debug
self.d.debug('Starting')
logging.debug('Starting')
self.tn = Telnet('127.0.0.1', port)
self.tn.write(encode('\r\n'))

Expand All @@ -74,17 +46,15 @@ def cleanup(self):
if self.tn:
self.tn.close()
self.tn = None
self.d.cleanup()

return

def pair(self, action, wait_for, timeout):
self.d.debug('output: %s' % action)
self.d.debug('match: %s' % ",".join(wait_for))
logging.debug('output: %s' % action) #lgtm [py/clear-text-logging-sensitive-data]
logging.debug('match: %s' % ",".join(wait_for))
self.tn.write(encode("%s\n" % action))
if wait_for is not None:
index, match, text = self.tn.expect([encode(i) for i in wait_for], timeout)
self.d.debug('Result of matching: %d %s %s' % (index, str(match), text))
logging.debug('Result of matching: %d %s %s' % (index, str(match), text))
if index == -1:
raise EMatchNotFound
else:
Expand All @@ -94,20 +64,20 @@ def pair(self, action, wait_for, timeout):

def login(self, user, password):
try:
self.d.debug('## Getting the login prompt')
logging.debug('## Getting the login prompt')
self.pair('\r', [r'login:'], 240)
except EMatchNotFound:
self.d.debug('No login prompt is found')
logging.debug('No login prompt is found')
raise ELoginPromptNotFound

self.d.debug('## Getting the password prompt')
logging.debug('## Getting the password prompt')
index_password = self.pair(user, [r'assword:', r'>'], 20)
if index_password == 0:
try:
self.d.debug('## Inputing password')
logging.debug('## Inputing password')
self.pair(password, [r'>'], 10)
except EMatchNotFound:
self.d.debug('The original password "%s" is not working' % password)
logging.debug('The original password "%s" is not working' % password) #lgtm [py/clear-text-logging-sensitive-data]
raise EWrongDefaultPassword

return
Expand Down Expand Up @@ -170,9 +140,7 @@ def session(new_params):
('aaa root secret 0 %s' % str(new_params['new_root_password']), [r'\(config\)#']),
]

curtime = datetime.datetime.now().isoformat()
debug = MyDebug('/tmp/debug.%s.%s.txt' % (new_params['hostname'], curtime), enabled=True)
ss = SerialSession(new_params['telnet_port'], debug)
ss = SerialSession(new_params['telnet_port'])
ss.login(new_params['login'], new_params['password'])
ss.enable()
ss.wait_for_warmup()
Expand Down
48 changes: 9 additions & 39 deletions ansible/roles/vm_set/library/sonic_kickstart.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
#!/usr/bin/python

import datetime
from telnetlib import Telnet
import logging
from ansible.module_utils.debug_utils import config_module_logging


class MyDebug(object):
def __init__(self, filename, enabled=True):
if enabled:
self.fp = open(filename, 'w')
else:
self.fp = None

return

def cleanup(self):
if self.fp:
self.fp.close()
self.fp = None

return

def __del__(self):
self.cleanup()

return

def debug(self, msg):
if self.fp:
self.fp.write('%s\n' % msg)
self.fp.flush()

return
config_module_logging('sonic_kickstart')


class EMatchNotFound(Exception):
pass


class SerialSession(object):
def __init__(self, port, debug):
self.d = debug
self.d.debug('Starting')
def __init__(self, port):
logging.debug('Starting')
self.tn = Telnet('127.0.0.1', port)
self.tn.write(b"\r\n")

Expand All @@ -55,17 +28,16 @@ def cleanup(self):
if self.tn:
self.tn.close()
self.tn = None
self.d.cleanup()

return

def pair(self, action, wait_for, timeout=60):
self.d.debug('output: %s' % action)
self.d.debug('match: %s' % ",".join(wait_for))
logging.debug('output: %s' % action) #lgtm [py/clear-text-logging-sensitive-data]
logging.debug('match: %s' % ",".join(wait_for))
self.tn.write(b"%s\n" % action.encode('ascii'))
if wait_for is not None:
index, match, text = self.tn.expect([ x.encode('ascii') for x in wait_for ], timeout)
self.d.debug('Result of matching: %d %s %s' % (index, str(match), text))
logging.debug('Result of matching: %d %s %s' % (index, str(match), text))
if index == -1:
raise EMatchNotFound
else:
Expand Down Expand Up @@ -126,9 +98,7 @@ def session(new_params):
if int(new_params['num_asic']) > 1:
seq.pop(0)

curtime = datetime.datetime.now().isoformat()
debug = MyDebug('/tmp/debug.%s.%s.txt' % (new_params['hostname'], curtime), enabled=True)
ss = SerialSession(new_params['telnet_port'], debug)
ss = SerialSession(new_params['telnet_port'])
ss.login(new_params['login'], new_params['passwords'])
ss.configure(seq)
ss.logout()
Expand Down
13 changes: 3 additions & 10 deletions ansible/roles/vm_set/library/vm_topology.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#!/usr/bin/python

import datetime
import logging
import hashlib
import json
import re
import subprocess
import shlex
import time
import traceback

import logging
import docker

from ansible.module_utils.debug_utils import config_module_logging
from ansible.module_utils.basic import *

DOCUMENTATION = '''
Expand Down Expand Up @@ -136,11 +135,7 @@
SUB_INTERFACE_VLAN_ID = '10'


def config_logging():
curtime = datetime.datetime.now().isoformat()
logging.basicConfig(filename=CMD_DEBUG_FNAME % curtime,
format='%(asctime)s %(levelname)s %(name)s#%(lineno)d: %(message)s',
level=logging.DEBUG)
config_module_logging('vm_topology')


def adaptive_name(template, host, index):
Expand Down Expand Up @@ -1211,8 +1206,6 @@ def main():
if cmd == 'bind_keysight_api_server_ip':
vm_names = []

config_logging()

try:

topo = module.params['topo']
Expand Down