Class: ForemanGoogle::GoogleCompute
- Inherits:
-
Object
- Object
- ForemanGoogle::GoogleCompute
- Defined in:
- app/models/foreman_google/google_compute.rb
Instance Attribute Summary collapse
-
#associate_external_ip ⇒ Object
readonly
Returns the value of attribute associate_external_ip.
-
#creation_timestamp ⇒ Object
readonly
Returns the value of attribute creation_timestamp.
-
#disks ⇒ Object
readonly
Returns the value of attribute disks.
-
#hostname ⇒ Object
readonly
Returns the value of attribute hostname.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#image_id ⇒ Object
readonly
Returns the value of attribute image_id.
-
#machine_type ⇒ Object
readonly
Returns the value of attribute machine_type.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#network ⇒ Object
readonly
Returns the value of attribute network.
-
#network_interfaces ⇒ Object
readonly
Returns the value of attribute network_interfaces.
-
#volumes ⇒ Object
readonly
Returns the value of attribute volumes.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
-
#zone_name ⇒ Object
readonly
Returns the value of attribute zone_name.
Instance Method Summary collapse
- #create_instance ⇒ Object
- #create_volumes ⇒ Object
- #destroy_volumes ⇒ Object
-
#initialize(client:, zone:, identity: nil, instance: nil, args: {}) ⇒ GoogleCompute
constructor
A new instance of GoogleCompute.
- #interfaces ⇒ Object
- #ip_addresses ⇒ Object
- #persisted? ⇒ Boolean
- #pretty_image_name ⇒ Object
- #pretty_machine_type ⇒ Object
- #private_ip_address ⇒ Object
- #ready? ⇒ Boolean
- #reload ⇒ Object
- #serial_port_output ⇒ Object
- #set_disk_auto_delete ⇒ Object
- #start ⇒ Object
-
#status ⇒ Object
(also: #state)
if nil, instance is not persisted as VM on GCE.
- #stop ⇒ Object
- #to_s ⇒ Object
- #vm_description ⇒ Object
- #vm_ip_address ⇒ Object (also: #public_ip_address)
- #volumes_attributes=(_attrs) ⇒ Object
- #wait_for(&block) ⇒ Object
Constructor Details
#initialize(client:, zone:, identity: nil, instance: nil, args: {}) ⇒ GoogleCompute
Returns a new instance of GoogleCompute.
8 9 10 11 12 13 14 15 16 |
# File 'app/models/foreman_google/google_compute.rb', line 8 def initialize(client:, zone:, identity: nil, instance: nil, args: {}) @client = client @zone = zone @identity = identity @instance = instance load if identity && instance.nil? load_attributes(args) end |
Instance Attribute Details
#associate_external_ip ⇒ Object (readonly)
Returns the value of attribute associate_external_ip.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def associate_external_ip @associate_external_ip end |
#creation_timestamp ⇒ Object (readonly)
Returns the value of attribute creation_timestamp.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def @creation_timestamp end |
#disks ⇒ Object (readonly)
Returns the value of attribute disks.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def disks @disks end |
#hostname ⇒ Object (readonly)
Returns the value of attribute hostname.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def hostname @hostname end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def identity @identity end |
#image_id ⇒ Object (readonly)
Returns the value of attribute image_id.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def image_id @image_id end |
#machine_type ⇒ Object (readonly)
Returns the value of attribute machine_type.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def machine_type @machine_type end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def @metadata end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def name @name end |
#network ⇒ Object (readonly)
Returns the value of attribute network.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def network @network end |
#network_interfaces ⇒ Object (readonly)
Returns the value of attribute network_interfaces.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def network_interfaces @network_interfaces end |
#volumes ⇒ Object (readonly)
Returns the value of attribute volumes.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def volumes @volumes end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def zone @zone end |
#zone_name ⇒ Object (readonly)
Returns the value of attribute zone_name.
5 6 7 |
# File 'app/models/foreman_google/google_compute.rb', line 5 def zone_name @zone_name end |
Instance Method Details
#create_instance ⇒ Object
70 71 72 73 |
# File 'app/models/foreman_google/google_compute.rb', line 70 def create_instance args = GoogleCloudCompute::ComputeAttributes.new(@client).for_create(self) @client.insert_instance(@zone, args) end |
#create_volumes ⇒ Object
57 58 59 60 61 62 |
# File 'app/models/foreman_google/google_compute.rb', line 57 def create_volumes @volumes.each do |vol| @client.insert_disk(@zone, vol.insert_attrs) wait_for { @client.disk(@zone, vol.device_name).status == 'READY' } end end |
#destroy_volumes ⇒ Object
64 65 66 67 68 |
# File 'app/models/foreman_google/google_compute.rb', line 64 def destroy_volumes @volumes.each do |volume| @client.delete_disk(@zone, volume.device_name) end end |
#interfaces ⇒ Object
53 54 55 |
# File 'app/models/foreman_google/google_compute.rb', line 53 def interfaces @network_interfaces end |
#ip_addresses ⇒ Object
117 118 119 |
# File 'app/models/foreman_google/google_compute.rb', line 117 def ip_addresses [vm_ip_address, private_ip_address] end |
#persisted? ⇒ Boolean
18 19 20 |
# File 'app/models/foreman_google/google_compute.rb', line 18 def persisted? !!identity end |
#pretty_image_name ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'app/models/foreman_google/google_compute.rb', line 101 def pretty_image_name return unless @instance.disks.any? disk_name = @instance.disks.first.source.split('/').last image_name = @client.disk(@zone_name, disk_name).source_image image_name.split('/').last end |
#pretty_machine_type ⇒ Object
79 80 81 82 |
# File 'app/models/foreman_google/google_compute.rb', line 79 def pretty_machine_type return @machine_type unless @instance @instance.machine_type.split('/').last end |
#private_ip_address ⇒ Object
95 96 97 98 99 |
# File 'app/models/foreman_google/google_compute.rb', line 95 def private_ip_address return unless @instance.network_interfaces.any? @instance.network_interfaces.first.network_i_p end |
#ready? ⇒ Boolean
22 23 24 |
# File 'app/models/foreman_google/google_compute.rb', line 22 def ready? status == 'RUNNING' end |
#reload ⇒ Object
26 27 28 29 30 |
# File 'app/models/foreman_google/google_compute.rb', line 26 def reload return unless identity load self end |
#serial_port_output ⇒ Object
113 114 115 |
# File 'app/models/foreman_google/google_compute.rb', line 113 def serial_port_output @client.serial_port_output(@zone, @identity)&.contents end |
#set_disk_auto_delete ⇒ Object
75 76 77 |
# File 'app/models/foreman_google/google_compute.rb', line 75 def set_disk_auto_delete @client.set_disk_auto_delete(@zone, @name) end |
#start ⇒ Object
39 40 41 42 |
# File 'app/models/foreman_google/google_compute.rb', line 39 def start raise Foreman::Exception('unable to start machine that is not persisted') unless persisted? @client.start(@zone, identity) end |
#status ⇒ Object Also known as: state
if nil, instance is not persisted as VM on GCE
34 35 36 |
# File 'app/models/foreman_google/google_compute.rb', line 34 def status persisted? && @instance.status end |
#stop ⇒ Object
44 45 46 47 |
# File 'app/models/foreman_google/google_compute.rb', line 44 def stop raise Foreman::Exception('unable to stop machine that is not persisted') unless persisted? @client.stop(@zone, identity) end |
#to_s ⇒ Object
49 50 51 |
# File 'app/models/foreman_google/google_compute.rb', line 49 def to_s @name end |
#vm_description ⇒ Object
84 85 86 |
# File 'app/models/foreman_google/google_compute.rb', line 84 def vm_description pretty_machine_type end |
#vm_ip_address ⇒ Object Also known as: public_ip_address
88 89 90 91 92 |
# File 'app/models/foreman_google/google_compute.rb', line 88 def vm_ip_address return if @instance.network_interfaces.empty? @instance.network_interfaces.first.access_configs.first&.nat_i_p end |
#volumes_attributes=(_attrs) ⇒ Object
110 111 |
# File 'app/models/foreman_google/google_compute.rb', line 110 def volumes_attributes=(_attrs) end |
#wait_for(&block) ⇒ Object
121 122 123 |
# File 'app/models/foreman_google/google_compute.rb', line 121 def wait_for(&block) @client.wait_for(&block) end |