Class: Chef::Knife::VoxelVoxserversReimage

Inherits:
Chef::Knife
  • Object
show all
Includes:
VoxelBase
Defined in:
lib/chef/knife/voxel_voxservers_reimage.rb

Instance Method Summary collapse

Methods included from VoxelBase

#hapi, included

Instance Method Details

#bootstrap_for_node(device) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/knife/voxel_voxservers_reimage.rb', line 84

def bootstrap_for_node(device)
  bootstrap = Chef::Knife::Bootstrap.new

  bootstrap.name_args = [device['ipassignments']['ipassignment'].select { |i| i['type'] == 'frontend' }.first['content']]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = config[:ssh_user] || "root"
  bootstrap.config[:ssh_password] = device['accessmethods']['accessmethod'].select { |a| a['type'] == 'admin' }.first['password']
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || "d#{device['id']}"
  bootstrap.config[:prerelease] = config[:prerelease]
  bootstrap.config[:bootstrap_version] = config[:bootstrap_version]
  bootstrap.config[:distro] = config[:distro]
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap.config[:template_file] = config[:template_file]
  bootstrap.config[:environment] = config[:environment]
  bootstrap
end

#runObject



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/chef/knife/voxel_voxservers_reimage.rb', line 128

def run
  $stdout.sync = true

  unless @name_args.empty?
    create = hapi.voxel_voxservers_reimage(
      :device_id        => @name_args.first,
      :image_id         => config[:image_id],
      :hostname         => config[:hostname],
      :swap_space       => config[:swap_size]
    )

    if create['stat'] == "fail"
      ui.error(create['err']['msg'])
    else
      sleep 2
      
      device = hapi.voxel_devices_list( :device_id => create['device']['id'], :verbosity => 'extended' )

      if device['stat'] == "fail"
        ui.error(device['err']['msg'])
      else
        device = device['devices']['device']

        puts "#{ui.color("Device ID", :cyan)}: #{device['id']}"
        puts "#{ui.color("Name", :cyan)}: #{device['label']}"
        puts "#{ui.color("Image Id", :cyan)}: #{config[:image_id]}"
        puts "#{ui.color("Facility", :cyan)}: #{device['location']['facility']['code']}"
        puts "#{ui.color("Public IP Address", :cyan)}: #{device['ipassignments']['ipassignment'].select { |i| i['type'] == 'frontend' }.first['content']}"
        puts "#{ui.color("Private IP Address", :cyan)}: #{device['ipassignments']['ipassignment'].select { |i| i['type'] == 'backend' }.first['content']}"
        puts "#{ui.color("Root Password", :cyan)}: #{device['accessmethods']['accessmethod'].select { |a| a['type'] == 'admin' }.first['password']}"

        status = hapi.voxel_voxservers_status( :device_id => device['id'], :verbosity => 'extended' )

        while %w{ QUEUED IN_PROGRESS }.include?( status['devices']['device']['status'] ) do
          print "."
          status = hapi.voxel_voxservers_status( :device_id => device['id'], :verbosity => 'extended' )
          sleep 10
        end

        print "\n#{ui.color("Waiting for sshd", :magenta)}"

        print(".") until tcp_test_ssh(device['ipassignments']['ipassignment'].select { |i| i['type'] == 'frontend' }.first['content']) { sleep @initial_sleep_delay ||= 10; puts("done") }

        bootstrap_for_node(device).run

        puts "#{ui.color("Run List", :cyan)}: #{config[:run_list].join(', ')}"
        puts "#{ui.color("Node Name", :cyan)}: #{config[:node_name] || 'd' + device['id']}"
        puts "#{ui.color("Environment", :cyan)}: #{config[:environment] || '_default'}"
        puts "#{ui.color("Device ID", :cyan)}: #{device['id']}"
        puts "#{ui.color("Name", :cyan)}: #{device['label']}"
        puts "#{ui.color("Image Id", :cyan)}: #{config[:image_id]}"
        puts "#{ui.color("Facility", :cyan)}: #{device['location']['facility']['code']}"
        puts "#{ui.color("Public IP Address", :cyan)}: #{device['ipassignments']['ipassignment'].select { |i| i['type'] == 'frontend' }.first['content']}"
        puts "#{ui.color("Private IP Address", :cyan)}: #{device['ipassignments']['ipassignment'].select { |i| i['type'] == 'backend' }.first['content']}"
        puts "#{ui.color("Root Password", :cyan)}: #{device['accessmethods']['accessmethod'].select { |a| a['type'] == 'admin' }.first['password']}"
      end
    end
  else
    ui.error( "knife voxel voxservers reimage DEVICE_ID (options)" )
  end
end

#tcp_test_ssh(hostname) ⇒ Object



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
# File 'lib/chef/knife/voxel_voxservers_reimage.rb', line 102

def tcp_test_ssh(hostname)
  begin
    tcp_socket = TCPSocket.new(hostname, 22)
    readable = IO.select([tcp_socket], nil, nil, 5)
    if readable
      Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
      yield
      true
    else
      false
    end
  rescue Errno::ETIMEDOUT
    false
  rescue Errno::EPERM
    false
  rescue Errno::ECONNREFUSED
    sleep 2
    false
  rescue Errno::EHOSTUNREACH
    sleep 2
    false
  ensure
    tcp_socket && tcp_socket.close
  end
end