Class: CemAcpt::Platform::Gcp::VM
- Inherits:
-
Object
- Object
- CemAcpt::Platform::Gcp::VM
- Defined in:
- lib/cem_acpt/platform/gcp/compute.rb
Overview
This class represents a GCP VM. It is composed of various component classes.
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#disk ⇒ Object
Returns the value of attribute disk.
-
#machine_type ⇒ Object
Returns the value of attribute machine_type.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#name ⇒ Object
Returns the value of attribute name.
-
#network_interface ⇒ Object
Returns the value of attribute network_interface.
-
#project ⇒ Object
Returns the value of attribute project.
-
#service_account ⇒ Object
Returns the value of attribute service_account.
Instance Method Summary collapse
- #configure! ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #info ⇒ Object
-
#initialize(name, components: {}) ⇒ VM
constructor
A new instance of VM.
- #install_puppet_module_package(module_pkg_path, remote_path, puppet_path) ⇒ Object
- #ready? ⇒ Boolean
Methods included from Logging
current_log_config, #current_log_config, current_log_format, current_log_level, #current_log_level, included, #logger, new_log_config, #new_log_config, new_log_formatter, new_log_level, #new_log_level
Methods included from Helper
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
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
200 201 202 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 200 def cmd @cmd end |
#disk ⇒ Object
Returns the value of attribute disk.
199 200 201 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199 def disk @disk end |
#machine_type ⇒ Object
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 |
#metadata ⇒ Object
Returns the value of attribute metadata.
199 200 201 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199 def @metadata end |
#name ⇒ Object
Returns the value of attribute name.
199 200 201 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199 def name @name end |
#network_interface ⇒ Object
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 |
#project ⇒ Object
Returns the value of attribute project.
199 200 201 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199 def project @project end |
#service_account ⇒ Object
Returns the value of attribute service_account.
199 200 201 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 199 def service_account @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 |
#create ⇒ Object
242 243 244 245 246 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 242 def create @cmd.local_exec(create_cmd) rescue StandardError => e raise "Failed to create VM #{name} with command #{create_cmd}: #{e}" end |
#destroy ⇒ Object
260 261 262 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 260 def destroy @cmd.delete_instance(name) end |
#info ⇒ Object
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
264 265 266 267 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 264 def install_puppet_module_package(module_pkg_path, remote_path, puppet_path) @cmd.scp_upload(@name, module_pkg_path, remote_path) @cmd.ssh(@name, "sudo #{puppet_path} module install #{remote_path}") end |
#ready? ⇒ Boolean
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/cem_acpt/platform/gcp/compute.rb', line 248 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 false end |