Beispiel #1
0
def add_from_file(filename):
    Device.get_devices_from_file()
    file_path = DB_PATH + filename
    if os.path.isfile(file_path):
        log("Adding devices from '{}'".format(file_path), 'info')
        with open(file_path, 'r') as devices_file:
            all_lines = [line.strip() for line in devices_file.readlines()]
            for device_attribute in range(0, len(all_lines), 5):
                device_exists = False
                for device in devices:
                    if all_lines[device_attribute] in device:
                        log(
                            '[{}] Device already exists'.format(
                                all_lines[device_attribute]), 'info')
                        device_exists = True
                if device_exists == False:
                    device_object = Device(all_lines[device_attribute],
                                           all_lines[device_attribute + 1],
                                           all_lines[device_attribute + 2],
                                           all_lines[device_attribute + 3],
                                           all_lines[device_attribute + 4])
                    devices.append(
                        {all_lines[device_attribute]: device_object})
                    log(
                        '[{}] Device added successfully'.format(
                            all_lines[device_attribute]), 'info')
    Device.save_devices_to_file()
Beispiel #2
0
def generate(name):
    '''
	Summary:
	Generates a configuration file for a specified device.

	Takes: 
	name: Device to generate configuration for
	'''
    Device.get_devices_from_file()

    if os.path.isfile(METADATA_PATH + name + '_metadata.yaml'):
        with open(METADATA_PATH + name + '_metadata.yaml', 'r') as metadata:
            device_metadata = (yaml.safe_load(metadata))
    else:
        log('[{}] Metadata does not exist'.format(name), 'info')
        sys.exit(1)

    device_os = Device.get_device_information(name)['os']

    environment = MODULE_PATH + device_os + '/'

    j2_env = Environment(loader=FileSystemLoader(environment),
                         trim_blocks=True,
                         lstrip_blocks=True)

    for device, data in device_metadata.items():
        configuration = j2_env.get_template('template.j2').render(data)

    with open(CONFIG_PATH + name + '_generated.txt',
              'w') as generated_config_file:
        generated_config_file.write(configuration)

    log('[{}] Generated configuration'.format(name), 'info')
Beispiel #3
0
def run(name):
    '''
	Summary:
	Calls the run method on a Deployment object.

	Takes:
	name: Name of deployment to run
	'''
    Device.get_devices_from_file()
    Deployment.get_deployments_from_file()
    for deployment in deployments:
        if name in deployment:
            deployment[name].run()
            Deployment.save_deployments_to_file()
            return
    log('[{}] Deployment does not exist'.format(name), 'error')
Beispiel #4
0
def remove(name):
    '''
	Summary:
	Removes a device.

	Takes:
	name: Name of device to remove
	'''
    Device.get_devices_from_file()
    for device in devices:
        if name in device:
            devices.remove(device)
            log('[{}] Device removed successfully'.format(name), 'info')
            Device.save_devices_to_file()
            return

    log('[{}] Device does not exist'.format(name), 'error')
Beispiel #5
0
def view(name):
    '''
	Summary:
	Prints attributes of a Device instance.

	Takes: 
	name: Name of device to view information about
	'''
    Device.get_devices_from_file()
    device_information = Device.get_device_information(name)
    if device_information != None:
        log('Name: ' + str(device_information['name']), 'info')
        log('IP: ' + str(device_information['ip']), 'info')
        log('Username: '******'username']), 'info')
        log('Password: '******'password']), 'info')
        log('OS: ' + str(device_information['os']), 'info')
    else:
        log('[{}] Device does not exist'.format(name), 'error')
Beispiel #6
0
def add(name, ip, username, password, os):
    '''
	Summary:
	Adds a device.

	Takes: 
	name: Name of device
	ip: Management IP address of device
	username: Username to authenticate against
	password: Password to authenticate with
	os: Operating system of device cisco_ios|vyos
	'''
    Device.get_devices_from_file()
    for device in devices:
        if name in device:
            log('[{}] Device already exists'.format(name), 'info')
            sys.exit(1)
    device_object = Device(name, ip, username, password, os)
    devices.append({name: device_object})
    log('[{}] Device added successfully'.format(name), 'info')
    Device.save_devices_to_file()
Beispiel #7
0
    def get_configuration(device):
        '''
			Summary:
			Gets current configuration from a device and stores it in a file.

			Takes:
			device: Device to get configuration from
		'''
        device_information = Device.get_device_information(device)

        connection_object = Connection(device_information['name'],
                                       device_information['ip'],
                                       device_information['username'],
                                       device_information['password'],
                                       device_information['os'])
        device_connection = connection_object.get_connection()

        try:
            with open(MODULE_PATH + device_information['os'] +
                      '/commands.json') as command_list_from_file:
                command_list = json.load(command_list_from_file)

            log('[{}] Getting device configuration...'.format(device), 'info')

            output = device_connection.send_command(
                command_list['commands']['config'])

            configuration_lines = output.splitlines()

            with open(CONFIG_PATH + device + '_latest.txt',
                      'w+') as configuration_file:
                for line in configuration_lines:
                    configuration_file.write(line + '\n')

            log(
                '[{}] Device configuration stored as {}_latest.txt in {}'.
                format(device, device, CONFIG_PATH), 'info')

            connection_object.close_connection(device_connection)
        except AttributeError:
            log(
                '[{}] Could not send commands, device unreachable'.format(
                    device), 'error')
Beispiel #8
0
    def deploy_custom_configuration(device):
        '''
			Summary:
			Deploys custom configuration from a file to a device.

			Takes:
			device: Device to deploy configuration to
		'''
        rolled_back = False
        device_information = Device.get_device_information(device)

        connection_object = Connection(device_information['name'],
                                       device_information['ip'],
                                       device_information['username'],
                                       device_information['password'],
                                       device_information['os'])
        device_connection = connection_object.get_connection()

        try:
            Configuration.snapshot_configuration(device, device_connection,
                                                 device_information['os'])

            with open(CONFIG_PATH + device +
                      '_custom_commands.txt') as custom_commands_from_file:
                command_list = custom_commands_from_file.read().splitlines()

            log('[{}] Pushing configuration...'.format(device), 'info')
            device_connection.send_config_set(command_list)

            Configuration.save_configuration(device_information['os'],
                                             device_connection)

            for command in command_list:
                if command != 'no shutdown' and rolled_back == False:
                    rolled_back = Configuration.check_configuration_line(
                        device, device_connection, device_information['os'],
                        command)

            connection_object.close_connection(device_connection)

            Configuration.delete_rollback_configuration(device)
        except AttributeError:
            log('[{}] Could not send commands'.format(device), 'error')
Beispiel #9
0
    def deploy_generated_configuration(device):
        '''
			Summary:
			Deploys configuration generated from device metadata to a device.

			Takes:
			device: Device to deploy configuration to
		'''
        device_information = Device.get_device_information(device)

        connection_object = Connection(device_information['name'],
                                       device_information['ip'],
                                       device_information['username'],
                                       device_information['password'],
                                       device_information['os'])
        device_connection = connection_object.get_connection()

        try:
            Configuration.snapshot_configuration(device, device_connection,
                                                 device_information['os'])

            log('[{}] Pushing configuration...'.format(device), 'info')
            device_connection.send_config_from_file(CONFIG_PATH + device +
                                                    '_generated.txt')

            Configuration.save_configuration(device_information['os'],
                                             device_connection)

            pushed_successfully = Configuration.check_full_configuration(
                device, device_connection, device_information['os'])

            if pushed_successfully == True:
                Configuration.mark_configuration_as_deployed(device)
                Configuration.delete_rollback_configuration(device)

            connection_object.close_connection(device_connection)
        except AttributeError:
            log(
                '[{}] Could not send commands, device unreachable'.format(
                    device), 'error')