-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #427 from sanfrancrisko/IAC-1266/main/cisco_cfg_ba…
…ckup_restore (FEAT) Add Bolt tasks to backup and restore Cisco device cfgs
- Loading branch information
Showing
13 changed files
with
263 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,4 @@ | |
# | ||
# @note Deprecated, use cisco_ios::install::server | ||
class cisco_ios::server { | ||
include resource_api::server | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper_acceptance' | ||
require 'tempfile' | ||
require 'securerandom' | ||
|
||
SNMP_SERVER_CONTACT = "Acceptance Test Runner #{SecureRandom.hex}".freeze | ||
SNMP_SERVER_CONTACT_VERIFY_CMD = 'show run | inc snmp-server contact'.freeze | ||
|
||
def create_snmp_server_contact_config | ||
new_config_file_path = new_tempfile | ||
File.open(new_config_file_path, 'wb') do |file| | ||
file.write("snmp-server contact #{SNMP_SERVER_CONTACT}\n") | ||
file.write("end\n") | ||
end | ||
new_config_file_path | ||
end | ||
|
||
def snmp_server_contact_set | ||
output, status = Open3.capture2e(bolt_task_command('cli_command', | ||
"command=\"#{SNMP_SERVER_CONTACT_VERIFY_CMD}\"", | ||
'raw=false')) | ||
if status.success? | ||
output.split("\n").each do |line| | ||
return true if line.include? SNMP_SERVER_CONTACT_VERIFY_CMD | ||
end | ||
end | ||
false | ||
end | ||
|
||
describe 'bolt task to backup / restore config' do | ||
before(:all) do | ||
unless ENV['SKIP_STARTUP_RESTORE'] | ||
# Restore the running config back to the startup config | ||
_output, status = Open3.capture2e(bolt_task_command('restore_startup')) | ||
raise 'Error restoring startup config on target' unless status.success? | ||
end | ||
end | ||
|
||
it 'can backup a running config' do | ||
backup_location = new_tempfile | ||
_output, status = Open3.capture2e(bolt_task_command('backup_config', "backup_location=#{backup_location}")) | ||
expect(status.success?).to be true | ||
expect(File.empty?(backup_location)).to be false | ||
end | ||
|
||
it 'can restore a config' do | ||
# Create a simple config that sets the 'snmp server contact' parameter | ||
snmp_server_contact_config = create_snmp_server_contact_config | ||
_output, status = Open3.capture2e(bolt_task_command('restore_config', "backup_location=#{snmp_server_contact_config}")) | ||
expect(status.success?).to be true | ||
|
||
# Query the 'snmp server contact' parameter and ensure it has been set to the expected value | ||
expect(snmp_server_contact_set).to be true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"description": "Backup the Cisco IOS running config to a file", | ||
"input_method": "stdin", | ||
"remote": true, | ||
"parameters": { | ||
"backup_location": { | ||
"description": "File to save running config", | ||
"type": "String[1]" | ||
} | ||
}, | ||
"files": [ | ||
"cisco_ios/lib/puppet/util/task_helper.rb", | ||
"cisco_ios/lib/puppet/transport/cisco_ios.rb", | ||
"cisco_ios/lib/puppet/transport/command.yaml", | ||
"cisco_ios/lib/puppet/transport/schema/cisco_ios.rb", | ||
"cisco_ios/lib/puppet_x/puppetlabs/cisco_ios/utility.rb", | ||
"cisco_ios/lib/puppet_x/puppetlabs/cisco_ios/transport_shim.rb", | ||
"ruby_task_helper/files/task_helper.rb" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/opt/puppetlabs/puppet/bin/ruby | ||
# frozen_string_literal: true | ||
|
||
require_relative '../lib/puppet/util/task_helper' | ||
task = Puppet::Util::TaskHelper.new('cisco_ios') | ||
|
||
result = {} | ||
|
||
unless Puppet.settings.global_defaults_initialized? | ||
Puppet.initialize_settings | ||
end | ||
|
||
begin | ||
rtn = task.transport.run_command_enable_mode('show running-config') | ||
File.open(task.params['backup_location'], 'wb') do |f| | ||
f.write "#{rtn}\n" | ||
end | ||
result = { | ||
backup_location: task.params['backup_location'], | ||
} | ||
rescue StandardError => e | ||
result[:_error] = { | ||
msg: e.message, | ||
kind: 'puppetlabs/cisco_ios', | ||
details: { | ||
class: e.class.to_s, | ||
backtrace: e.backtrace, | ||
}, | ||
} | ||
end | ||
|
||
puts result.to_json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"description": "Restore the config to a Cisco IOS device", | ||
"input_method": "stdin", | ||
"remote": true, | ||
"parameters": { | ||
"backup_location": { | ||
"description": "File to restore to device", | ||
"type": "String[1]" | ||
} | ||
}, | ||
"files": [ | ||
"cisco_ios/lib/puppet/util/task_helper.rb", | ||
"cisco_ios/lib/puppet/transport/cisco_ios.rb", | ||
"cisco_ios/lib/puppet/transport/command.yaml", | ||
"cisco_ios/lib/puppet/transport/schema/cisco_ios.rb", | ||
"cisco_ios/lib/puppet_x/puppetlabs/cisco_ios/utility.rb", | ||
"cisco_ios/lib/puppet_x/puppetlabs/cisco_ios/transport_shim.rb", | ||
"ruby_task_helper/files/task_helper.rb" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/opt/puppetlabs/puppet/bin/ruby | ||
# frozen_string_literal: true | ||
|
||
require_relative '../lib/puppet/util/task_helper' | ||
task = Puppet::Util::TaskHelper.new('cisco_ios') | ||
|
||
CONFIG_LINE_HEADER_CUTOFF = 20 | ||
CONFIG_LINE_ENDING_ID = 'end'.freeze | ||
|
||
result = {} | ||
|
||
unless Puppet.settings.global_defaults_initialized? | ||
Puppet.initialize_settings | ||
end | ||
|
||
VALID_CISCO_HEADERS = ['show running-config', 'Building configuration', 'Current configuration'].freeze | ||
|
||
def config_to_restore(raw_config_output) | ||
header_match_count = 0 | ||
line = 0 | ||
start_line = nil | ||
end_line = nil | ||
|
||
raw_config_output.each do |current_line| | ||
header_match_count += 1 if VALID_CISCO_HEADERS.any? { |vch| current_line.start_with? vch } | ||
start_line = (line + 1) if (header_match_count == 3) && (current_line.start_with? 'version') | ||
end_line = line if current_line.start_with? CONFIG_LINE_ENDING_ID | ||
break if start_line && end_line | ||
# If we haven't found the headers by line 20, let's exit as this doesn't seem to be a valid Cisco | ||
# backup config retrieved using 'show running-config' | ||
if line >= CONFIG_LINE_HEADER_CUTOFF && header_match_count < 3 | ||
raise "Did not detect the following expected headers expected from a valid Cisco backup after processing #{CONFIG_LINE_HEADER_CUTOFF} lines:\n" + | ||
VALID_CISCO_HEADERS.join("\n") | ||
end | ||
line += 1 | ||
end | ||
|
||
raise "Could not determine end of config (was expecting '#{CONFIG_LINE_ENDING_ID}')" unless end_line | ||
raw_config_output[start_line..end_line].map { |l| l.strip } | ||
end | ||
|
||
begin | ||
config_to_restore = config_to_restore(File.readlines(task.params['backup_location'])) | ||
task.transport.restore_config_conf_t_mode(config_to_restore) | ||
|
||
result = { | ||
last_config_change: '', | ||
} | ||
rescue StandardError => e | ||
result[:_error] = { | ||
msg: e.message, | ||
kind: 'puppetlabs/cisco_ios', | ||
details: { | ||
class: e.class.to_s, | ||
backtrace: e.backtrace, | ||
}, | ||
} | ||
end | ||
|
||
puts result.to_json |