Module: Gogetit::Util

Included in:
Etcd::Etcd, CLI, Config, GogetLXD, GogetLibvirt
Defined in:
lib/gogetit/util.rb

Instance Method Summary collapse

Instance Method Details

#check_ip_available(addresses, maas, logger) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/gogetit/util.rb', line 175

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/gogetit/util.rb', line 120

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/gogetit/util.rb', line 95

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



159
160
161
162
163
164
# File 'lib/gogetit/util.rb', line 159

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_envObject



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gogetit/util.rb', line 107

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



188
189
190
191
192
193
194
195
196
197
# File 'lib/gogetit/util.rb', line 188

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



166
167
168
169
170
171
172
173
# File 'lib/gogetit/util.rb', line 166

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



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gogetit/util.rb', line 132

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
# 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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/gogetit/util.rb', line 144

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