Module: Gogetit::Util
- Included in:
- Etcd::Etcd, CLI, Config, GogetLXD, GogetLibvirt
- Defined in:
- lib/gogetit/util.rb
Instance Method Summary collapse
- #check_ip_available(addresses, maas, logger) ⇒ Object
- #get_gateway(version) ⇒ Object
- #knife_bootstrap(name, provider, config, logger) ⇒ Object
- #knife_remove(name, logger) ⇒ Object
- #ping_available?(host, logger) ⇒ Boolean
- #recognize_env ⇒ Object
- #run_command(cmd, logger) ⇒ Object
- #run_through_ssh(host, distro_name, commands, logger) ⇒ Object
- #ssh_available?(ip_or_fqdn, user, logger) ⇒ Boolean
-
#symbolize_keys(thing) ⇒ Object
taken from gist.github.com/andrewpcone/11359798.
- #update_databags(config, logger) ⇒ Object
- #wait_until_available(ip_or_fqdn, distro_name, logger) ⇒ Object
Instance Method Details
#check_ip_available(addresses, maas, logger) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/gogetit/util.rb', line 177 def check_ip_available(addresses, maas, logger) 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, logger) 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
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/gogetit/util.rb', line 122 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 |
#knife_bootstrap(name, provider, config, logger) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gogetit/util.rb', line 13 def knife_bootstrap(name, provider, config, logger) 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, logger) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gogetit/util.rb', line 97 def knife_remove(name, logger) 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, logger) ⇒ Boolean
161 162 163 164 165 166 |
# File 'lib/gogetit/util.rb', line 161 def ping_available?(host, logger) # 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 |
#recognize_env ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/gogetit/util.rb', line 109 def recognize_env thedir = 'lib/env' gateway = get_gateway(4) Dir.foreach(thedir) do |item| if item.match(/\.json$/) env_data = JSON.parse(File.read(thedir+'/'+item)) if gateway =~ Regexp.new(env_data['regexp_pattern']) return env_data['name'] end end end end |
#run_command(cmd, logger) ⇒ Object
8 9 10 11 |
# File 'lib/gogetit/util.rb', line 8 def run_command(cmd, logger) logger.info("Calling <#{__method__.to_s}> to run #{cmd}") system(cmd) end |
#run_through_ssh(host, distro_name, commands, logger) ⇒ Object
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/gogetit/util.rb', line 190 def run_through_ssh(host, distro_name, commands, logger) 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, logger) ⇒ Boolean
168 169 170 171 172 173 174 175 |
# File 'lib/gogetit/util.rb', line 168 def ssh_available?(ip_or_fqdn, user, logger) logger.info("Calling <#{__method__.to_s}>") begin Net::SSH.start(ip_or_fqdn, user).class rescue Exception => e puts e end end |
#symbolize_keys(thing) ⇒ Object
taken from gist.github.com/andrewpcone/11359798
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/gogetit/util.rb', line 134 def symbolize_keys(thing) case thing when Array thing.map{|v| symbolize_keys(v)} when Hash inj = thing.inject({}) {|h, (k,v)| h[k] = symbolize_keys(v); h} inj.symbolize_keys else thing end end |
#update_databags(config, logger) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gogetit/util.rb', line 34 def update_databags(config, logger) 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}", logger) 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}", logger) 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", logger ) else run_command( "knife vault create #{bag} #{item} --json #{item_file}"\ " --search '*:*' -M client", logger ) end run_command( "knife vault refresh #{bag} #{item} --clean-unknown-clients -M client", logger ) else run_command("knife data bag from file #{bag} #{item_file}", logger) end end end end |
#wait_until_available(ip_or_fqdn, distro_name, logger) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/gogetit/util.rb', line 146 def wait_until_available(ip_or_fqdn, distro_name, logger) logger.info("Calling <#{__method__.to_s}>") until ping_available?(ip_or_fqdn, logger) 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) 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 |