Class: Chef::Knife::ZcloudjpMachineCreate

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

Instance Method Summary collapse

Methods included from ZcloudjpBase

included

Instance Method Details

#bootstrap_node(machine, bootstrap_ip_address) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/chef/knife/zcloudjp_machine_create.rb', line 208

def bootstrap_node(machine, bootstrap_ip_address)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [bootstrap_ip_address]
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:first_boot_attributes] = config[:first_boot_attributes]
  bootstrap.config[:ssh_user] = config[:ssh_user] || "root"
  # bootstrap.config[:ssh_password] = machine.password
  bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:host_key_verify] = config[:host_key_verify]
  bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id
  bootstrap.config[:prerelease] = config[:prerelease]
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  # bootstrap will run as root...sudo (by default) also messes up Ohai on CentOS boxes
  bootstrap.config[:use_sudo] = true unless config[:ssh_user] == 'root'
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = config[:environment]
  bootstrap
end

#runObject



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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/chef/knife/zcloudjp_machine_create.rb', line 132

def run
  $stdout.sync = true

  unless Chef::Config[:knife][:zcloudjp_dataset]
    ui.error("You have not provided a valid dataset image value. Please note the short option for this value recently changed from '-i' to '-I'.")
    exit 1
  end
  body = Hash.new()
  body["dataset"]   = config[:dataset]
  body["package"]   = locate_config_value(:package)
  body["name"]      = config[:chef_node_name]
  body["locale"]      = "en"

  Chef::Log.debug("Create machine with parameters below")
  Chef::Log.debug(body)
  

  locate_config_value(:zcloudjp_api_url)
  connection = Faraday.new(:url => locate_config_value(:zcloudjp_api_url), :ssl => {:verify => false}, :headers => {"User-Agent" => "Knife-Zcloudjp/#{::Knife::Zcloudjp::VERSION}"})
 
  response = connection.post do |req|
    req.url '/machines.json'
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
    req.body = body.to_json
  end

  machine = JSON.parse(response.body, :symbolized_names =>true )

  msg_pair("ID", machine['id'])
  msg_pair("ip", machine['ips'].last)
  msg_pair("type", machine['type'])
  msg_pair("dataset", machine['dataset'])
  msg_pair("package", machine['packeage'])
  msg_pair("state", machine['state'])

  bootstrap_ip_address = machine['ips'].last
  config[:chef_node_name] = machine['id'] unless config[:chef_node_name]
  config[:machine_name] = machine['id'].split("/")[0] unless config[:machine_name]

  config[:first_boot_attributes]['zcloudjp'] = {}
  config[:first_boot_attributes]['zcloudjp']['id'] = machine['id']
  config[:first_boot_attributes]['zcloudjp']['type'] = machine['type']
  config[:first_boot_attributes]['zcloudjp']['dataset'] = machine['dataset']



  # wait for provision the machine.
  print(".") until verify_ssh_connection(bootstrap_ip_address) {
    sleep @initial_sleep_delay ||= 10
    # puts("done")
  }

  # for smartdc workaround. check twice.
  sleep 10

  # wait for provision the machine.
  print(".") until verify_ssh_connection(bootstrap_ip_address) {
    sleep @initial_sleep_delay ||= 10
    puts("done")
  }

  # add name tag for zcloud machine
  body = Hash.new()
  body["value"]   = config[:machine_name]

  response = connection.put do |req|
    req.url "/machines/#{machine['id']}/name"
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-API-KEY'] = Chef::Config[:knife][:zcloudjp_api_token]
    req.body = body.to_json
  end

  bootstrap_node(machine, bootstrap_ip_address).run
end

#verify_ssh_connection(hostname) ⇒ Object



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

def verify_ssh_connection(hostname)
  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
rescue Errno::ENETUNREACH
  sleep 2
  false
ensure
  tcp_socket && tcp_socket.close
end