Class: Chef::Knife::Cloud::GoogleServerCreate

Inherits:
ServerCreateCommand
  • Object
show all
Includes:
GoogleServiceOptions, ServerCreateOptions
Defined in:
lib/chef/knife/google_server_create.rb

Instance Method Summary collapse

Methods included from GoogleServiceOptions

included

Instance Method Details

#auto_migrate?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/chef/knife/google_server_create.rb', line 269

def auto_migrate?
  preemptible? ? false : config[:auto_migrate]
end

#auto_restart?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/chef/knife/google_server_create.rb', line 273

def auto_restart?
  preemptible? ? false : config[:auto_restart]
end

#before_bootstrapObject



231
232
233
234
235
236
237
238
239
240
241
# File 'lib/chef/knife/google_server_create.rb', line 231

def before_bootstrap
  super

  config[:chef_node_name] = config[:chef_node_name] || instance_name
  config[:bootstrap_ip_address] = ip_address_for_bootstrap

  if config[:image_os_type] == "windows"
    ui.msg("Resetting the Windows login password so the bootstrap can continue...")
    config[:connection_password] = reset_windows_password
  end
end

#before_exec_commandObject



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

def before_exec_command
  super

  @create_options = {
    name: instance_name,
    image: config[:image],
    image_project: config[:image_project],
    network: config[:network],
    subnet: config[:subnet],
    public_ip: config[:public_ip],
    auto_migrate: auto_migrate?,
    auto_restart: auto_restart?,
    preemptible: preemptible?,
    boot_disk_autodelete: config[:boot_disk_autodelete],
    boot_disk_name: config[:boot_disk_name],
    boot_disk_size: boot_disk_size,
    boot_disk_ssd: config[:boot_disk_ssd],
    additional_disks: config[:additional_disks],
    local_ssd: config[:local_ssd],
    interface: config[:interface],
    number_of_local_ssd: number_of_local_ssd,
    can_ip_forward: config[:can_ip_forward],
    machine_type: config[:machine_type],
    service_account_scopes: config[:service_account_scopes],
    service_account_name: config[:service_account_name],
    metadata: ,
    tags: config[:tags],
  }
end

#boot_disk_sizeObject



296
297
298
# File 'lib/chef/knife/google_server_create.rb', line 296

def boot_disk_size
  config[:boot_disk_size].to_i
end

#emailObject



261
262
263
# File 'lib/chef/knife/google_server_create.rb', line 261

def email
  config[:gce_email]
end

#gcewinpass_debug_modeObject



317
318
319
# File 'lib/chef/knife/google_server_create.rb', line 317

def gcewinpass_debug_mode
  Chef::Config[:log_level] == :debug
end

#get_node_name(_name, _prefix) ⇒ Object

overriding this from Chef::Knife::Cloud::ServerCreateCommand.

This gets called in validate_params! in that class before our #before_bootstrap is called, in which it randomly generates a node name, which means we never default to the instance name in our #before_bootstrap method. Instead, we'll just nil this and allow our class here to do The Right Thing.



249
250
251
# File 'lib/chef/knife/google_server_create.rb', line 249

def get_node_name(_name, _prefix)
  nil
end

#instance_nameObject



285
286
287
# File 'lib/chef/knife/google_server_create.rb', line 285

def instance_name
  @name_args.first
end

#ip_address_for_bootstrapObject



277
278
279
280
281
282
283
# File 'lib/chef/knife/google_server_create.rb', line 277

def ip_address_for_bootstrap
  ip = config[:use_private_ip] ? private_ip_for(server) : public_ip_for(server)

  raise "Unable to determine instance IP address for bootstrapping" if ip == "unknown"

  ip
end

#metadataObject



289
290
291
292
293
294
# File 'lib/chef/knife/google_server_create.rb', line 289

def 
  config[:metadata].each_with_object({}) do |item, memo|
    key, value = item.split("=")
    memo[key] = value
  end
end

#number_of_local_ssdObject



300
301
302
# File 'lib/chef/knife/google_server_create.rb', line 300

def number_of_local_ssd
  config[:number_of_local_ssd].to_i
end

#preemptible?Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/chef/knife/google_server_create.rb', line 265

def preemptible?
  config[:preemptible]
end

#projectObject



253
254
255
# File 'lib/chef/knife/google_server_create.rb', line 253

def project
  config[:gce_project]
end

#reset_windows_passwordObject



304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/chef/knife/google_server_create.rb', line 304

def reset_windows_password
  opts = {
    project:       project,
    zone:          zone,
    instance_name: instance_name,
    email:         email,
    username:      config[:connection_user],
    debug:         gcewinpass_debug_mode,
  }
  opts[:timeout] = config[:winpass_timeout] unless config[:winpass_timeout].nil?
  GoogleComputeWindowsPassword.new(**opts).new_password
end

#set_default_configObject



206
207
208
209
210
# File 'lib/chef/knife/google_server_create.rb', line 206

def set_default_config
  # dumb hack for knife-cloud, which expects the user to pass in the WinRM password to use when bootstrapping.
  # We won't know the password until the instance is created and we forcibly reset it.
  config[:connection_password] = "will_change_this_later"
end

#validate_params!Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/chef/knife/google_server_create.rb', line 212

def validate_params!
  check_for_missing_config_values!(:gce_zone, :machine_type, :image, :boot_disk_size, :network)
  raise "You must supply an instance name." if @name_args.first.nil?
  raise "Boot disk size must be between 10 and 10,000" unless valid_disk_size?(boot_disk_size)

  if config[:connection_protocol] == "winrm" && config[:gce_email].nil?
    raise "Please provide your Google Cloud console email address via --gce-email. " \
      "It is required when resetting passwords on Windows hosts."
  end

  raise "Please provide connection port via --connection-port." unless config[:connection_port]
  raise "Please provide image os type via --image-os-type." unless config[:image_os_type]

  ui.warn("Auto-migrate disabled for preemptible instance") if preemptible? && config[:auto_migrate]
  ui.warn("Auto-restart disabled for preemptible instance") if preemptible? && config[:auto_restart]

  super
end

#zoneObject



257
258
259
# File 'lib/chef/knife/google_server_create.rb', line 257

def zone
  config[:gce_zone]
end