Module: Proxy::Salt::CLI
- Extended by:
- Log, Util
- Defined in:
- lib/smart_proxy_salt/cli.rb
Overview
CLI methods
Class Method Summary collapse
- .append_value_to_file(filepath, value) ⇒ Object
- .autosign_create_hostname(hostname) ⇒ Object
- .autosign_create_key(key) ⇒ Object
- .autosign_file ⇒ Object
- .autosign_key_file ⇒ Object
- .autosign_list ⇒ Object
- .autosign_remove_hostname(hostname) ⇒ Object
- .autosign_remove_key(key) ⇒ Object
- .highstate(host) ⇒ Object
- .key_accept(host) ⇒ Object
- .key_delete(host) ⇒ Object
- .key_list ⇒ Object
- .key_reject(host) ⇒ Object
- .remove_value_from_file(filepath, value) ⇒ Object
Class Method Details
.append_value_to_file(filepath, value) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/smart_proxy_salt/cli.rb', line 64 def append_value_to_file(filepath, value) File.open(filepath, File::CREAT|File::RDWR) do |file| unless file.any? { |line| line.chomp == value} file.puts value end end logger.info "Added an entry to '#{filepath}' successfully." true rescue IOError => e logger.info "Attempted to add an entry to '#{filepath}', but an exception occurred: #{e}" false end |
.autosign_create_hostname(hostname) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/smart_proxy_salt/cli.rb', line 21 def autosign_create_hostname(hostname) if append_value_to_file(autosign_file, hostname) { message: 'Added hostname successfully.' } else { message: 'Failed to add hostname.' \ ' See smart proxy error log for more information.' } end end |
.autosign_create_key(key) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/smart_proxy_salt/cli.rb', line 39 def autosign_create_key(key) if append_value_to_file(autosign_key_file, key) { message: 'Added key successfully.' } else { message: 'Failed to add key.' \ ' See smart proxy error log for more information.' } end end |
.autosign_file ⇒ Object
13 14 15 |
# File 'lib/smart_proxy_salt/cli.rb', line 13 def autosign_file Proxy::Salt::Plugin.settings.autosign_file end |
.autosign_key_file ⇒ Object
17 18 19 |
# File 'lib/smart_proxy_salt/cli.rb', line 17 def autosign_key_file Proxy::Salt::Plugin.settings.autosign_key_file end |
.autosign_list ⇒ Object
57 58 59 60 61 62 |
# File 'lib/smart_proxy_salt/cli.rb', line 57 def autosign_list return [] unless File.exist?(autosign_file) File.read(autosign_file).split("\n").reject do |v| v =~ /^\s*#.*|^$/ ## Remove comments and empty lines end.map(&:chomp) end |
.autosign_remove_hostname(hostname) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/smart_proxy_salt/cli.rb', line 30 def autosign_remove_hostname(hostname) if remove_value_from_file(autosign_file, hostname) { message: 'Removed hostname successfully.' } else { message: 'Failed to remove hostname.' \ ' See smart proxy error log for more information.' } end end |
.autosign_remove_key(key) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/smart_proxy_salt/cli.rb', line 48 def autosign_remove_key(key) if remove_value_from_file(autosign_key_file, key) { message: 'Removed key successfully.' } else { message: 'Failed to remove key.' \ ' See smart proxy error log for more information.' } end end |
.highstate(host) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/smart_proxy_salt/cli.rb', line 103 def highstate(host) find_salt_binaries cmd = [@sudo, '-u', Proxy::Salt::Plugin.settings.salt_command_user, @salt, '--async', escape_for_shell(host), 'state.highstate'] logger.info "Will run state.highstate for #{host}. Full command: #{cmd.join(' ')}" shell_command(cmd) end |
.key_accept(host) ⇒ Object
122 123 124 125 126 |
# File 'lib/smart_proxy_salt/cli.rb', line 122 def key_accept(host) find_salt_binaries cmd = [@sudo, '-u', Proxy::Salt::Plugin.settings.salt_command_user, @salt_key, '--include-rejected', '--yes', '-a', escape_for_shell(host)] shell_command(cmd) end |
.key_delete(host) ⇒ Object
110 111 112 113 114 |
# File 'lib/smart_proxy_salt/cli.rb', line 110 def key_delete(host) find_salt_binaries cmd = [@sudo, '-u', Proxy::Salt::Plugin.settings.salt_command_user, @salt_key, '--yes', '-d', escape_for_shell(host)] shell_command(cmd) end |
.key_list ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/smart_proxy_salt/cli.rb', line 128 def key_list find_salt_binaries command = "#{@sudo} -u #{Proxy::Salt::Plugin.settings.salt_command_user} #{@salt_key} --finger-all --output=json" logger.debug "Executing #{command}" response = `#{command}` unless $CHILD_STATUS == 0 logger.warn "Failed to run salt-key: #{response}" raise 'Execution of salt-key failed, check log files' end keys_hash = {} sk_hash = JSON.parse(response) accepted_minions = sk_hash['minions'] accepted_minions.each_key { |accepted_minion| keys_hash[accepted_minion] = { 'state' => 'accepted', 'fingerprint' => accepted_minions[accepted_minion] } } if sk_hash.key? 'minions' rejected_minions = sk_hash['minions_rejected'] rejected_minions.each_key { |rejected_minion| keys_hash[rejected_minion] = { 'state' => 'rejected', 'fingerprint' => rejected_minions[rejected_minion] } } if sk_hash.key? 'minions_rejected' unaccepted_minions = sk_hash['minions_pre'] unaccepted_minions.each_key { |unaccepted_minion| keys_hash[unaccepted_minion] = { 'state' => 'unaccepted', 'fingerprint' => unaccepted_minions[unaccepted_minion] } } if sk_hash.key? 'minions_pre' keys_hash end |
.key_reject(host) ⇒ Object
116 117 118 119 120 |
# File 'lib/smart_proxy_salt/cli.rb', line 116 def key_reject(host) find_salt_binaries cmd = [@sudo, '-u', Proxy::Salt::Plugin.settings.salt_command_user, @salt_key, '--include-accepted', '--yes', '-r', escape_for_shell(host)] shell_command(cmd) end |
.remove_value_from_file(filepath, value) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/smart_proxy_salt/cli.rb', line 77 def remove_value_from_file(filepath, value) return true unless File.exist?(filepath) found = false entries = File.readlines(filepath).collect do |line| entry = line.chomp if entry == value found = true nil elsif entry == "" nil else line end end.uniq.compact if found File.write(filepath, entries.join()) logger.info "Removed an entry from '#{filepath}' successfully." end true rescue IOError => e logger.info "Attempted to remove an entry from '#{filepath}', but an exception occurred: #{e}" false end |