def setup_config_session(self): """ Setup vyos session. A random uuid is generated as a sesssion identifier ($PPID -Shell PID- could be used as well). """ identifier = uuid4() env = {} env['VYATTA_CHANGES_ONLY_DIR'] = '/opt/vyatta/config/tmp/changes_only_{}'.format(identifier) env['VYATTA_CONFIG_TEMPLATE'] = '/opt/vyatta/share/vyatta-cfg/templates' env['VYATTA_ACTIVE_CONFIGURATION_DIR'] = '/opt/vyatta/config/active' env['VYATTA_EDIT_LEVEL'] = '/' env['VYATTA_TEMP_CONFIG_DIR'] = '/opt/vyatta/config/tmp/new_config_{}'.format(identifier) env['VYATTA_TEMPLATE_LEVEL'] = '/' env['VYATTA_CONFIG_TMP'] = '/opt/vyatta/config/tmp/tmp_{}'.format(identifier) # Add vyos session environment to system environment. This is not good but actually it seems # that is the only way to handle a persistant vyos session after spawning a shell. os.environ.update(env) logger.info('Setting up a configuration session for Vyos') # Spawn shell and setup vyos config session if _run('{} setupSession'.format(VYOS_SHELL_API)): # Unset vyos session environment and raise an exception logger.error('Could not create configuration session') logger.info('Cleaning up session environment variables') clean_environ(env) raise SetupSessionFailed('Could not create session !') self.session_id = identifier self.session_envs = env logger.debug('Session identifier is %s', identifier) logger.debug('Session environment variables: %s', env) logger.info('Configuration session is set up') return True
def setup_config_session(self): """ Setup vyos session. A random uuid is generated as a sesssion identifier ($PPID -Shell PID- could be used as well). """ identifier = uuid4() env = {} env['VYATTA_CHANGES_ONLY_DIR'] = '/opt/vyatta/config/tmp/changes_only_{}'.format( identifier) env['VYATTA_CONFIG_TEMPLATE'] = '/opt/vyatta/share/vyatta-cfg/templates' env['VYATTA_ACTIVE_CONFIGURATION_DIR'] = '/opt/vyatta/config/active' env['VYATTA_EDIT_LEVEL'] = '/' env['VYATTA_TEMP_CONFIG_DIR'] = '/opt/vyatta/config/tmp/new_config_{}'.format( identifier) env['VYATTA_TEMPLATE_LEVEL'] = '/' env['VYATTA_CONFIG_TMP'] = '/opt/vyatta/config/tmp/tmp_{}'.format( identifier) # Add vyos session environment to system environment. This is not good but actually it seems # that is the only way to handle a persistant vyos session after spawning a shell. os.environ.update(env) logger.info('Setting up a configuration session for Vyos') # Spawn shell and setup vyos config session if _run('{} setupSession'.format(VYOS_SHELL_API)): # Unset vyos session environment and raise an exception logger.error('Could not create configuration session') logger.info('Cleaning up session environment variables') clean_environ(env) raise SetupSessionFailed('Could not create session !') self.session_id = identifier self.session_envs = env logger.debug('Session identifier is %s', identifier) logger.debug('Session environment variables: %s', env) logger.info('Configuration session is set up') return True
def teardown_config_session(self): """ End current configuration session. """ if not self.session_exists(): logger.warn('Teardown failed. No session available !') return False if not _run('{} teardownSession'.format(VYOS_SHELL_API)): logger.info('Cleaning up session environment variables') logger.info('Closing Vyos config session') clean_environ(self.session_envs) return True logger.error('Failed to teardown current config session') logger.warn('The Vyos config session may still open !') return False