Class: CemAcpt::Platform::Gcp::VM

Inherits:
Object
  • Object
show all
Includes:
Logging, Helper
Defined in:
lib/cem_acpt/platform/gcp/compute.rb

Overview

This class represents a GCP VM. It is composed of various component classes.

Constant Summary

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

current_log_config, #current_log_config, current_log_format, #current_log_format, current_log_level, #current_log_level, included, logger, #logger, new_log_config, #new_log_config, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level, new_logger, #new_logger

Methods included from Helper

#add_cmd, #cmd_flag_vars?

Constructor Details

#initialize(name, components: {}) ⇒ VM

Returns a new instance of VM.



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 202

def initialize(name, components: {})
  @name = name
  @components = components
  @machine_type = components[:machine_type]
  @cmd = CemAcpt::Platform::Gcp::Cmd.new(
    project: components[:project][:name],
    zone: components[:project][:zone],
    out_format: 'json',
    local_port: components[:local_port],
  )
  @configured = false
end

Instance Attribute Details

#cmdObject (readonly)

Returns the value of attribute cmd.



200
201
202
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 200

def cmd
  @cmd
end

#diskObject

Returns the value of attribute disk.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def disk
  @disk
end

#machine_typeObject

Returns the value of attribute machine_type.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def machine_type
  @machine_type
end

#metadataObject

Returns the value of attribute metadata.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def 
  @metadata
end

#nameObject

Returns the value of attribute name.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def name
  @name
end

#network_interfaceObject

Returns the value of attribute network_interface.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def network_interface
  @network_interface
end

#projectObject

Returns the value of attribute project.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def project
  @project
end

#service_accountObject

Returns the value of attribute service_account.



199
200
201
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199

def 
  @service_account
end

Instance Method Details

#configure!Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 215

def configure!
  return @configured if @configured

  @project = Project.new(**@components[:project])
  @project.add_cmd(@cmd)
  @service_account = @components.key?(:service_account) ? ServiceAccount.new(**@components[:service_account]) : nil
  @disk = Disk.new(project: @project, **@components[:disk])
  @disk.add_cmd(@cmd)
  @network_interface = NetworkInterface.new(**@components[:network_interface])
  @machine_type = @components[:machine_type]
  @metadata = Metadata.new(**@components[:metadata])
  @metadata.add_cmd(@cmd)
  @configured = true
end

#createObject



242
243
244
245
246
247
248
249
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 242

def create
  # Add the test ssh key to os-login
  logger.debug("Adding test SSH key to os-login for #{name}")
  @cmd.local_exec("compute os-login ssh-keys add --key-file #{@cmd.ssh_key}.pub --project #{project.name} --ttl 4h")
  @cmd.local_exec(create_cmd)
rescue StandardError => e
  raise "Failed to create VM #{name} with command #{create_cmd}: #{e}"
end

#destroyObject



263
264
265
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 263

def destroy
  @cmd.delete_instance(name)
end

#infoObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 230

def info
  describe_cmd = "compute instances describe #{name}"
  logger.debug("Gathering info for VM #{name} with gcloud command: #{describe_cmd}")
  data = @cmd.local_exec(describe_cmd, out_format: 'json')
  opts = @cmd.ssh_opts(instance_name: name)
  {
    node_data: data,
    transport: :ssh,
    ssh_opts: opts,
  }
end

#install_puppet_module_package(module_pkg_path, remote_path, puppet_path) ⇒ Object



267
268
269
270
271
272
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 267

def install_puppet_module_package(module_pkg_path, remote_path, puppet_path)
  @cmd.scp_upload(@name, module_pkg_path, remote_path)
  logger.info("Uploaded module package #{module_pkg_path} to #{remote_path} on #{@name}")
  @cmd.ssh(@name, "sudo #{puppet_path} module install #{remote_path}")
  logger.info("Installed module package #{remote_path} on #{@name}")
end

#ready?Boolean

Returns:

  • (Boolean)


251
252
253
254
255
256
257
258
259
260
261
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 251

def ready?
  logger.debug("Checking if VM #{name} is ready")
  instance_status = @cmd.local_exec('compute instances list', out_filter: "NAME = #{name}").first['status']
  logger.debug("Instance #{name} status: #{instance_status}")
  return false unless instance_status == 'RUNNING'

  logger.debug("Checking instance #{name} SSH connectivity")
  @cmd.ssh_ready?(name)
rescue StandardError, Exception
  false
end