本文整理汇总了Python中jnpr.junos.utils.sw.SW.reboot方法的典型用法代码示例。如果您正苦于以下问题:Python SW.reboot方法的具体用法?Python SW.reboot怎么用?Python SW.reboot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jnpr.junos.utils.sw.SW
的用法示例。
在下文中一共展示了SW.reboot方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: InstallOnHost
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
def InstallOnHost(hostname, username, password, softwareFilePath, rebootTime):
dev = Device(host=hostname, user=username, password=password)
softw = SW(dev)
hash = SW.local_md5(softwareFilePath)
softw.install(softwareFilePath, remote_path='/var/tmp', progress=dev, validate=False, checksum=hash, cleanfs=False, no_copy=False, timeout=1800)
if rebootTime != 0:
softw.reboot(rebootTime)
dev.close()
示例2: install_package
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
def install_package(module, device):
junos = SW(device)
package = module.params['src']
no_copy = module.params['no_copy']
def progress_log(dev, report):
module.log(report)
module.log('installing package')
result = junos.install(package, progress=progress_log, no_copy=no_copy)
if not result:
module.fail_json(msg='Unable to install package on device')
if module.params['reboot']:
module.log('rebooting system')
junos.reboot()
示例3: main
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
def main():
#iterate over csv
for row in devList:
#Make Device Connection
dev = Device(host=row[1], user=userName, password=userPassword)
try:
#Print Opening Header for Status
now1 = datetime.datetime.now()
pprint("Work starts on " + row[0] + " | " + row[1] + " at " + now1.strftime("%Y-%m-%d %H:%M"))
#Open Device with Custom Timer
dev.open()
dev.timeout = 900
except Exception as err:
sys.stderr.write('Cannot connect to device: {0}\n'.format(err))
#Do RPC/Work
try:
#Start Work here
#Create an instance of SW
sw = SW(dev)
try:
ok = sw.install(package=package, remote_path=remote_path, progress=myprogress, validate=validate, no_copy=noCopy, timeout=1800)
except Exception as err:
msg = 'Unable to install software, {0}'.format(err)
print(msg)
ok = False
if ok is True:
print('Software installation complete. Rebooting')
rsp = sw.reboot()
print('Upgrade pending reboot cycle, please be patient.')
print(rsp)
else:
msg = 'Unable to install software, {0}'.format(ok)
print(msg)
#Write Element Seperator
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
#Close Device
dev.close()
except Exception as err:
sys.stderr.write('Cannot perform RPC on device: ' + row[1] + '\n'.format(err))
print("+++++++++++++++++++++++++++++++++++++++++++++++++")
示例4: main
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
def main():
print("\nWelcome to Junos Upgrade Tool \n")
# Request which code to upgrade with
fileList = getFileList(code_path)
package = getOptionAnswer("Choose a junos package", fileList)
package = code_path + package
# Request IP for the system to upgrade
host = getInputAnswer("IP Address of the host")
# Get username and password parameters
username=getInputAnswer("\nEnter your device username")
password=getpass(prompt="\nEnter your device password: ")
# initialize logging
logging.basicConfig(filename=logfile, level=logging.INFO, format='%(asctime)s:%(name)s: %(message)s')
logging.getLogger().name = host
sys.stdout.write('Information logged in {0}\n'.format(logfile))
# verify package exists
if (os.path.isfile(package)):
found = True
else:
msg = 'Software package does not exist: {0}. '.format(package)
sys.exit(msg + '\nExiting program')
dev = Device(host,user=username,password=password)
try:
dev.open()
except Exception as err:
sys.stderr.write('Cannot connect to device: {0}\n'.format(err))
return
# Increase the default RPC timeout to accommodate install operations
dev.timeout = 300
# Create an instance of SW
sw = SW(dev)
try:
do_log('Starting the software upgrade process: {0}'.format(package))
ok = sw.install(package=package, remote_path=remote_path, progress=update_progress, validate=validate)
except Exception as err:
msg = 'Unable to install software, {0}'.format(err)
do_log(msg, level='error')
else:
if ok is True:
do_log('Software installation complete. Rebooting')
rsp = sw.reboot()
do_log('Upgrade pending reboot cycle, please be patient.')
do_log(rsp)
# End the NETCONF session and close the connection
dev.close()
示例5: TestSW
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
@patch('jnpr.junos.Device.execute')
def test_sw_install_kwargs_force_host(self, mock_execute):
self.sw.install('file', no_copy=True, force_host=True)
rpc = [
'<request-package-add><force-host/><no-validate/><re1/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><no-validate/><force-host/><re1/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><no-validate/><re1/><force-host/></request-package-add>',
'<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name><re1/></request-package-add>',
'<request-package-add><force-host/><re1/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><re1/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><no-validate/><package-name>/var/tmp/file</package-name><force-host/><re1/></request-package-add>',
'<request-package-add><force-host/><package-name>/var/tmp/file</package-name><no-validate/><re1/></request-package-add>',
'<request-package-add><re1/><no-validate/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><re1/><force-host/><package-name>/var/tmp/file</package-name><no-validate/></request-package-add>',
'<request-package-add><re1/><package-name>/var/tmp/file</package-name><force-host/><no-validate/></request-package-add>',
'<request-package-add><re1/><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><force-host/><re1/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><no-validate/><re1/></request-package-add>',
'<request-package-add><no-validate/><re1/><force-host/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><re1/><no-validate/></request-package-add>',
'<request-package-add><no-validate/><force-host/><package-name>/var/tmp/file</package-name><re1/></request-package-add>',
'<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><force-host/><package-name>/var/tmp/file</package-name><no-validate/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><no-validate/><force-host/></request-package-add>',
'<request-package-add><no-validate/><force-host/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><no-validate/></request-package-add>']
self.assertTrue(etree.tostring(
mock_execute.call_args[0][0]).decode('utf-8') in rpc)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback(self, mock_execute):
rsp = '<rpc-reply><output>junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
msg = 'junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot'
self.assertEqual(self.sw.rollback(), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi(self, mock_execute):
mock_execute.side_effect = self._mock_manager
msg = {'fpc1': "Junos version 'D10.2' will become active at next reboot",
'fpc0': 'JUNOS version "D10.2" will become active at next reboot'}
self.assertEqual(eval(self.sw.rollback()), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi_exception(self, mock_execute):
fname = 'request-package-rollback-multi-error.xml'
mock_execute.side_effect = self._read_file(fname)
self.assertRaises(SwRollbackError, self.sw.rollback)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_exception(self, mock_execute):
rsp = '<rpc-reply><output>WARNING: Cannot rollback, /packages/junos.old is not valid</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
self.assertRaises(SwRollbackError, self.sw.rollback)
def test_sw_inventory(self):
self.sw.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.sw.inventory, {
'current': None, 'rollback': None})
@patch('jnpr.junos.Device.execute')
def test_sw_reboot(self, mock_execute):
示例6: JunosDevice
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
self.native.timeout = 30
@property
def facts(self):
if hasattr(self, '_facts'):
return self._facts
native_facts = self.native.facts
facts = {}
facts['hostname'] = native_facts['hostname']
facts['fqdn'] = native_facts['fqdn']
facts['model'] = native_facts['model']
native_uptime_string = native_facts['RE0']['up_time']
facts['uptime'] = self._uptime_to_seconds(native_uptime_string)
facts['uptime_string'] = self._uptime_to_string(native_uptime_string)
facts['serial_number'] = native_facts['serialnumber']
facts['interfaces'] = self._get_interfaces()
for fact_key in native_facts:
if fact_key.startswith('version') and fact_key != 'version_info':
facts['os_version'] = native_facts[fact_key]
break
facts['vendor'] = self.vendor
self._facts = facts
return self._facts
def _file_copy_local_file_exists(self, filepath):
return os.path.isfile(filepath)
def _file_copy_local_md5(self, filepath, blocksize=2**20):
if self._file_copy_local_file_exists(filepath):
m = hashlib.md5()
with open(filepath, "rb") as f:
buf = f.read(blocksize)
while buf:
m.update(buf)
buf = f.read(blocksize)
return m.hexdigest()
def _file_copy_remote_md5(self, filename):
return self.fs.checksum(filename)
def file_copy_remote_exists(self, src, dest=None):
if dest is None:
dest = os.path.basename(src)
local_hash = self._file_copy_local_md5(src)
remote_hash = self._file_copy_remote_md5(dest)
if local_hash is not None:
if local_hash == remote_hash:
return True
return False
def file_copy(self, src, dest=None):
if dest is None:
dest = os.path.basename(src)
with SCP(self.native) as scp:
scp.put(src, remote_path=dest)
def get_boot_options(self):
return self.facts['os_version']
def set_boot_options(self, sys):
raise NotImplementedError
def reboot(self, timer=0, confirm=False):
if confirm:
self.sw.reboot(in_min=timer)
else:
print('Need to confirm reboot with confirm=True')
@property
def running_config(self):
return self.show('show config')
def save(self, filename=None):
if filename is None:
self.cu.commit()
return
temp_file = NamedTemporaryFile()
temp_file.write(self.show('show config'))
temp_file.flush()
with SCP(self.native) as scp:
scp.put(temp_file.name, remote_path=filename)
temp_file.close()
return True
@property
def startup_config(self):
return self.show('show config')
示例7: Device
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
from jnpr.junos import Device
from jnpr.junos.utils.sw import SW
import sys
dev = Device('host', user='user', password='pwd')
try:
dev.open()
except Exception as err:
print "Unable to connect to host:", err
sys.exit(1)
sft= SW(dev)
print sft.reboot()
#print sft.reboot(in_min=5)
#print ("Before Shutdown\n")
#print sft.poweroff()
#print ("After Shutdown\n")
示例8: TestSW
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
@patch('jnpr.junos.utils.sw.SW.validate')
def test_sw_install_validate(self, mock_validate):
mock_validate.return_value = False
self.assertFalse(self.sw.install('file', validate=True, no_copy=True))
@patch('jnpr.junos.utils.sw.SW.pkgadd')
def test_sw_install_multi_mx(self, mock_pkgadd):
mock_pkgadd.return_value = True
self.sw._multi_RE = True
self.sw._multi_MX = True
self.assertTrue(self.sw.install('file', no_copy=True))
@patch('jnpr.junos.utils.sw.SW.pkgadd')
def test_sw_install_multi_vc(self, mock_pkgadd):
mock_pkgadd.return_value = True
self.sw._multi_RE = True
self.sw._multi_VC = True
self.sw._RE_list = ('version_RE0', 'version_RE1')
self.assertTrue(self.sw.install('file', no_copy=True))
@patch('jnpr.junos.Device.execute')
def test_sw_rollback(self, mock_execute):
# we need proper xml for this test case, update request-package-roll
# back.xml
mock_execute.side_effect = self._mock_manager
self.assertEqual(self.sw.rollback(), '')
def test_sw_inventory(self):
self.sw.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.sw.inventory, {'current': None, 'rollback': None})
@patch('jnpr.junos.Device.execute')
def test_sw_reboot(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.sw._multi_MX = True
self.assertTrue('Shutdown NOW' in self.sw.reboot())
@patch('jnpr.junos.Device.execute')
def test_sw_reboot_multi_re_vc(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.sw._multi_RE = True
self.sw._multi_VC = False
self.assertTrue('Shutdown NOW' in self.sw.reboot())
@patch('jnpr.junos.Device.execute')
def test_sw_reboot_exception(self, mock_execute):
rsp = etree.XML('<rpc-reply><a>test</a></rpc-reply>')
mock_execute.side_effect = RpcError(rsp=rsp)
self.assertRaises(Exception, self.sw.reboot)
@patch('jnpr.junos.Device.execute')
def test_sw_poweroff(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.sw._multi_MX = True
self.assertTrue('Shutdown NOW' in self.sw.poweroff())
@patch('jnpr.junos.Device.execute')
def test_sw_poweroff_exception(self, mock_execute):
rsp = etree.XML('<rpc-reply><a>test</a></rpc-reply>')
mock_execute.side_effect = RpcError(rsp=rsp)
self.assertRaises(Exception, self.sw.poweroff)
@patch('jnpr.junos.Device.execute')
def test_sw_poweroff_multi_re_vc(self, mock_execute):
mock_execute.side_effect = self._mock_manager
示例9: TestSW
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
pkg_set=[
'install.tgz',
'install.tgz'],
cleanfs=False))
@patch('jnpr.junos.utils.sw.SW.pkgadd')
def test_sw_install_mixed_vc_ValueError(self, mock_pkgadd):
mock_pkgadd.return_value = True
self.sw._mixed_VC = True
self.sw.remote_checksum = MagicMock(
return_value='d41d8cd98f00b204e9800998ecf8427e')
self.sw._RE_list = ('version_RE0', 'version_RE1')
with patch('jnpr.junos.utils.sw.SW.local_md5',
MagicMock(return_value='d41d8cd98f00b204e9800998ecf8427e')):
self.assertRaises(
ValueError,
self.sw.install,
pkg_set='install.tgz',
cleanfs=False)
@patch('jnpr.junos.utils.sw.SW.pkgadd')
def test_sw_install_mixed_vc_TypeError(self, mock_pkgadd):
self.assertRaises(TypeError, self.sw.install, cleanfs=False)
@patch('jnpr.junos.Device.execute')
def test_sw_install_kwargs_force_host(self, mock_execute):
self.sw.install('file', no_copy=True, force_host=True)
rpc = """<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>"""
self.assertEqual(etree.tostring(mock_execute.call_args[0][0]),
rpc)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback(self, mock_execute):
rsp = '<rpc-reply><output>junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
msg = 'junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot'
self.assertEqual(self.sw.rollback(), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi(self, mock_execute):
mock_execute.side_effect = self._mock_manager
msg = '{\'fpc1\': "Junos version \'D10.2\' will become active at next reboot", \'fpc0\': \'JUNOS version "D10.2" will become active at next reboot\'}'
self.assertEqual(self.sw.rollback(), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi_exception(self, mock_execute):
fname = 'request-package-rollback-multi-error.xml'
mock_execute.side_effect = self._read_file(fname)
self.assertRaises(SwRollbackError, self.sw.rollback)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_exception(self, mock_execute):
rsp = '<rpc-reply><output>WARNING: Cannot rollback, /packages/junos.old is not valid</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
self.assertRaises(SwRollbackError, self.sw.rollback)
def test_sw_inventory(self):
self.sw.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.sw.inventory, {
'current': None, 'rollback': None})
@patch('jnpr.junos.Device.execute')
def test_sw_reboot(self, mock_execute):
mock_execute.side_effect = self._mock_manager
示例10: TestSW
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
'<request-package-add><package-name>/var/tmp/file</package-name><no-validate/><re1/><force-host/></request-package-add>',
'<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name><re1/></request-package-add>',
'<request-package-add><force-host/><re1/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><re1/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><no-validate/><package-name>/var/tmp/file</package-name><force-host/><re1/></request-package-add>',
'<request-package-add><force-host/><package-name>/var/tmp/file</package-name><no-validate/><re1/></request-package-add>',
'<request-package-add><re1/><no-validate/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><re1/><force-host/><package-name>/var/tmp/file</package-name><no-validate/></request-package-add>',
'<request-package-add><re1/><package-name>/var/tmp/file</package-name><force-host/><no-validate/></request-package-add>',
'<request-package-add><re1/><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><force-host/><re1/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><no-validate/><re1/></request-package-add>',
'<request-package-add><no-validate/><re1/><force-host/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><re1/><no-validate/></request-package-add>',
'<request-package-add><no-validate/><force-host/><package-name>/var/tmp/file</package-name><re1/></request-package-add>',
'<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><force-host/><package-name>/var/tmp/file</package-name><no-validate/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><no-validate/><force-host/></request-package-add>',
'<request-package-add><no-validate/><force-host/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><no-validate/><package-name>/var/tmp/file</package-name><force-host/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><force-host/><no-validate/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><re1/><no-validate/><force-host/></request-package-add>',
'<request-package-add><package-name>/var/tmp/file</package-name><re1/><force-host/><no-validate/></request-package-add>',
'<request-package-add><force-host/><package-name>/var/tmp/file</package-name><re1/><no-validate/></request-package-add>',
'<request-package-add><re1/><package-name>/var/tmp/file</package-name><no-validate/><force-host/></request-package-add>',
'<request-package-add><no-validate/><package-name>/var/tmp/file</package-name><re1/><force-host/></request-package-add>',
'<request-package-add><re1/><no-validate/><force-host/><package-name>/var/tmp/file</package-name></request-package-add>',
'<request-package-add><force-host/><re1/><package-name>/var/tmp/file</package-name><no-validate/></request-package-add>']
self.assertTrue(etree.tostring(
mock_execute.call_args[0][0]).decode('utf-8') in rpc)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback(self, mock_execute):
rsp = '<rpc-reply><output>junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
msg = 'junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot'
self.assertEqual(self.sw.rollback(), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi(self, mock_execute):
mock_execute.side_effect = self._mock_manager
msg = {'fpc1': "Junos version 'D10.2' will become active at next reboot",
'fpc0': 'JUNOS version "D10.2" will become active at next reboot'}
self.assertEqual(eval(self.sw.rollback()), msg)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_multi_exception(self, mock_execute):
fname = 'request-package-rollback-multi-error.xml'
mock_execute.side_effect = self._read_file(fname)
self.assertRaises(SwRollbackError, self.sw.rollback)
@patch('jnpr.junos.Device.execute')
def test_sw_rollback_exception(self, mock_execute):
rsp = '<rpc-reply><output>WARNING: Cannot rollback, /packages/junos.old is not valid</output></rpc-reply>'
mock_execute.side_effect = etree.XML(rsp)
self.assertRaises(SwRollbackError, self.sw.rollback)
def test_sw_inventory(self):
self.sw.dev.rpc.file_list = \
MagicMock(side_effect=self._mock_manager)
self.assertEqual(
self.sw.inventory, {
'current': None, 'rollback': None})
@patch('jnpr.junos.Device.execute')
def test_sw_reboot(self, mock_execute):
示例11: Device
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
from jnpr.junos import Device
from jnpr.junos.utils.sw import SW
dev = Device(host='xxxx', user='demo', password='demo123', gather_facts=False)
dev.open()
def update_progress(dev, report):
print dev.hostname, '> ', report
sw = SW(dev)
ok = sw.install(package=r'/Users/nitinkr/Downloads/jinstall-1x.1xxxx.tgz', progress=update_progress)
# progress takes boolean values too from 1.2.3 version onwards
#ok = sw.install(package=r'/Users/nitinkr/Downloads/jinstall-1x.1xxxx.tgz', progress=True)
if ok:
print 'rebooting'
sw.reboot()
示例12: TestSW
# 需要导入模块: from jnpr.junos.utils.sw import SW [as 别名]
# 或者: from jnpr.junos.utils.sw.SW import reboot [as 别名]
#.........这里部分代码省略.........
self.assertTrue(self.sw.install(pkg_set=["install.tgz", "install.tgz"], cleanfs=False))
@patch("jnpr.junos.utils.sw.SW.pkgadd")
def test_sw_install_mixed_vc_safe_copy_false(self, mock_pkgadd):
mock_pkgadd.return_value = True
self.sw._mixed_VC = True
self.sw.safe_copy = MagicMock(return_value=False)
self.sw.remote_checksum = MagicMock(return_value="d41d8cd98f00b204e9800998ecf8427e")
self.sw._RE_list = ("version_RE0", "version_RE1")
with patch("jnpr.junos.utils.sw.SW.local_md5", MagicMock(return_value="d41d8cd98f00b204e9800998ecf8427e")):
self.assertFalse(self.sw.install(pkg_set=["install.tgz", "install.tgz"], cleanfs=False))
@patch("jnpr.junos.utils.sw.SW.pkgadd")
def test_sw_install_mixed_vc_ValueError(self, mock_pkgadd):
mock_pkgadd.return_value = True
self.sw._mixed_VC = True
self.sw.remote_checksum = MagicMock(return_value="d41d8cd98f00b204e9800998ecf8427e")
self.sw._RE_list = ("version_RE0", "version_RE1")
with patch("jnpr.junos.utils.sw.SW.local_md5", MagicMock(return_value="d41d8cd98f00b204e9800998ecf8427e")):
self.assertRaises(ValueError, self.sw.install, pkg_set="install.tgz", cleanfs=False)
@patch("jnpr.junos.utils.sw.SW.pkgadd")
def test_sw_install_mixed_vc_TypeError(self, mock_pkgadd):
self.assertRaises(TypeError, self.sw.install, cleanfs=False)
@patch("jnpr.junos.Device.execute")
def test_sw_install_kwargs_force_host(self, mock_execute):
self.sw.install("file", no_copy=True, force_host=True)
rpc = """<request-package-add><force-host/><no-validate/><package-name>/var/tmp/file</package-name></request-package-add>"""
self.assertEqual(etree.tostring(mock_execute.call_args[0][0]), rpc)
@patch("jnpr.junos.Device.execute")
def test_sw_rollback(self, mock_execute):
rsp = "<rpc-reply><output>junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot</output></rpc-reply>"
mock_execute.side_effect = etree.XML(rsp)
msg = "junos-vsrx-12.1X46-D30.2-domestic will become active at next reboot"
self.assertEqual(self.sw.rollback(), msg)
@patch("jnpr.junos.Device.execute")
def test_sw_rollback_multi(self, mock_execute):
mock_execute.side_effect = self._mock_manager
msg = "{'fpc1': \"Junos version 'D10.2' will become active at next reboot\", 'fpc0': 'JUNOS version \"D10.2\" will become active at next reboot'}"
self.assertEqual(self.sw.rollback(), msg)
@patch("jnpr.junos.Device.execute")
def test_sw_rollback_multi_exception(self, mock_execute):
fname = "request-package-rollback-multi-error.xml"
mock_execute.side_effect = self._read_file(fname)
self.assertRaises(SwRollbackError, self.sw.rollback)
@patch("jnpr.junos.Device.execute")
def test_sw_rollback_exception(self, mock_execute):
rsp = "<rpc-reply><output>WARNING: Cannot rollback, /packages/junos.old is not valid</output></rpc-reply>"
mock_execute.side_effect = etree.XML(rsp)
self.assertRaises(SwRollbackError, self.sw.rollback)
def test_sw_inventory(self):
self.sw.dev.rpc.file_list = MagicMock(side_effect=self._mock_manager)
self.assertEqual(self.sw.inventory, {"current": None, "rollback": None})
@patch("jnpr.junos.Device.execute")
def test_sw_reboot(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.sw._multi_MX = True
self.assertTrue("Shutdown NOW" in self.sw.reboot())