Module: Gogetit::Util
- Included in:
- CLI, Config, GogetLXD, GogetLibvirt
- Defined in:
- lib/gogetit/util.rb
Instance Method Summary collapse
- #check_ip_available(addresses, maas) ⇒ Object
- #get_gateway(version) ⇒ Object
- #get_http_content(url) ⇒ Object
- #get_provider_of(name, providers) ⇒ Object
- #is_port_open?(ip, port) ⇒ Boolean
- #knife_bootstrap(name, provider, config) ⇒ Object
- #knife_remove(name) ⇒ Object
- #ping_available?(host) ⇒ Boolean
- #run_command(cmd) ⇒ Object
- #run_through_ssh(host, distro_name, commands) ⇒ Object
- #ssh_available?(ip_or_fqdn, user) ⇒ Boolean
- #update_databags(config) ⇒ Object
- #wait_until_available(ip_or_fqdn, distro_name) ⇒ Object
Instance Method Details
#check_ip_available(addresses, maas) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/gogetit/util.rb', line 210 def check_ip_available(addresses, maas) logger.info("Calling <#{__method__.to_s}>") # to do a ping test addresses.each do |ip| abort("#{ip} is already being used.") if ping_available?(ip) end # to check with MAAS ifaces = maas.ip_reserved?(addresses) abort("one of #{addresses.join(', ')} is already being used.") \ unless ifaces return ifaces end |
#get_gateway(version) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/gogetit/util.rb', line 161 def get_gateway(version) IO.popen("ip -#{version.to_s} route").read.each_line do |route| if route.include? 'default' route.split[2] else 'There is no get_gateway!' nil end end end |
#get_http_content(url) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gogetit/util.rb', line 48 def get_http_content(url) logger.info("Calling <#{__method__.to_s}> to get #{url}") uri = URI.parse(url) if is_port_open?(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port) res = http.request_post(uri.path, nil) if res.code == "200" res.body else logger.error("Unable to reach the content of #{url}.") false end else logger.error("Unable to reach the server: #{uri.host} or port: #{uri.port}.") false end end |
#get_provider_of(name, providers) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/gogetit/util.rb', line 16 def get_provider_of(name, providers) logger.info("Calling <#{__method__.to_s}> #{name}") if providers[:lxd].container_exists?(name) logger.info("It is a LXD container.") return 'lxd' elsif providers[:libvirt].domain_exists?(name) logger.info("It is a KVM domain.") return 'libvirt' else puts "#{name} is not found" return nil end end |
#is_port_open?(ip, port) ⇒ Boolean
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gogetit/util.rb', line 30 def is_port_open?(ip, port) logger.info("Calling <#{__method__.to_s}> to check #{ip}:#{port}") begin Timeout::timeout(1) do begin s = TCPSocket.new(ip, port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError return false end end rescue Timeout::Error end return false end |
#knife_bootstrap(name, provider, config) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gogetit/util.rb', line 68 def knife_bootstrap(name, provider, config) logger.info("Calling <#{__method__.to_s}>") config[:chef][:target_environment] ||= '_default' if find_executable 'knife' if system('knife ssl check') install_cmd = "curl \ -l #{config[:chef][:bootstrap][:install_script][provider.to_sym]} \ | sudo bash -s --" knife_cmd = "knife bootstrap -y #{name} \ --node-name #{name} \ --ssh-user ubuntu \ --sudo \ --environment #{config[:chef][:target_environment]} \ --bootstrap-install-command \"#{install_cmd}\"".gsub(/ * /, ' ') puts 'Bootstrapping..' puts knife_cmd system(knife_cmd) end end end |
#knife_remove(name) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/gogetit/util.rb', line 149 def knife_remove(name) logger.info("Calling <#{__method__.to_s}>") if find_executable 'knife' if system('knife ssl check') puts "Deleting node #{name}.." system("knife node delete -y #{name}") puts "Deleting client #{name}.." system("knife client delete -y #{name}") end end end |
#ping_available?(host) ⇒ Boolean
187 188 189 190 191 192 |
# File 'lib/gogetit/util.rb', line 187 def ping_available?(host) # host can be both IP and ip_or_fqdn. logger.info("Calling <#{__method__.to_s}> for #{host}") `ping -c 1 -W 1 #{host}` $?.exitstatus == 0 end |
#run_command(cmd) ⇒ Object
11 12 13 14 |
# File 'lib/gogetit/util.rb', line 11 def run_command(cmd) logger.info("Calling <#{__method__.to_s}> to run '#{cmd}'") system(cmd) end |
#run_through_ssh(host, distro_name, commands) ⇒ Object
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/gogetit/util.rb', line 223 def run_through_ssh(host, distro_name, commands) logger.info("Calling <#{__method__.to_s}>") Net::SSH.start(host, distro_name) do |ssh| commands.each do |cmd| logger.info("'#{cmd}' is being executed..") output = ssh.exec!(cmd) puts output if output != '' end end end |
#ssh_available?(ip_or_fqdn, user) ⇒ Boolean
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/gogetit/util.rb', line 194 def ssh_available?(ip_or_fqdn, user) logger.info("Calling <#{__method__.to_s}> for #{user}@#{ip_or_fqdn}") begin Net::SSH.start( ip_or_fqdn, user, :keys_only => true, :number_of_password_prompts => 0 ) rescue Exception => e puts e return false end true end |
#update_databags(config) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/gogetit/util.rb', line 89 def update_databags(config) logger.info("Calling <#{__method__.to_s}>") data_bags_dir = "#{config[:chef][:chef_repo_root]}/data_bags" puts 'Listing databags..' databags_as_is = `knife data bag list`.split databags_to_be = Dir.entries(data_bags_dir) - ['.', '..'] puts 'Checking databags to delete..' (databags_as_is - databags_to_be).each do |bag| puts "Deleting databag '#{bag}'.." answer = ask( 'Do you really want to delete this?', :echo => true, :limited_to => ['y', 'n'] ) case answer when 'y' run_command("knife data bag delete -y #{bag}") when 'n' puts 'Keeping..' end end puts 'Checking databags to create..' (databags_to_be - databags_as_is).each do |bag| puts "Creating databag '#{bag}'.." run_command("knife data bag create #{bag}") end puts 'Checking items..' databags_to_be.each do |bag| items_as_is = `knife data bag show #{bag}`.split Dir.entries(data_bags_dir+'/'+bag).select do |file| /^.*\.json/.match(file) end.each do |item| item_file = data_bags_dir+'/'+bag+'/'+item item = item.gsub('.json', '') if JSON.parse(File.read(item_file))['vault'] if items_as_is.include? item run_command( "knife vault update #{bag} #{item} --json #{item_file}"\ " --search '*:*' -M client" ) else run_command( "knife vault create #{bag} #{item} --json #{item_file}"\ " --search '*:*' -M client" ) end run_command( "knife vault refresh #{bag} #{item} --clean-unknown-clients -M client" ) else run_command("knife data bag from file #{bag} #{item_file}") end end end end |
#wait_until_available(ip_or_fqdn, distro_name) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/gogetit/util.rb', line 172 def wait_until_available(ip_or_fqdn, distro_name) logger.info("Calling <#{__method__.to_s}> for network connection..") until ping_available?(ip_or_fqdn) logger.info("Calling <#{__method__.to_s}> for ping to be ready..") sleep 3 end logger.info("#{ip_or_fqdn} is now available to ping..") until ssh_available?(ip_or_fqdn, distro_name) logger.info("Calling <#{__method__.to_s}> for ssh to be ready..") sleep 3 end logger.info("#{ip_or_fqdn} is now available to ssh..") end |