Class: ForemanGoogle::GoogleCompute

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_google/google_compute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_ipObject (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_timestampObject (readonly)

Returns the value of attribute creation_timestamp.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def creation_timestamp
  @creation_timestamp
end

#disksObject (readonly)

Returns the value of attribute disks.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def disks
  @disks
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def hostname
  @hostname
end

#identityObject (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_idObject (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_typeObject (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

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def name
  @name
end

#networkObject (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_interfacesObject (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

#volumesObject (readonly)

Returns the value of attribute volumes.



5
6
7
# File 'app/models/foreman_google/google_compute.rb', line 5

def volumes
  @volumes
end

#zoneObject (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_nameObject (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_instanceObject



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_volumesObject



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_volumesObject



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

#interfacesObject



53
54
55
# File 'app/models/foreman_google/google_compute.rb', line 53

def interfaces
  @network_interfaces
end

#ip_addressesObject



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

Returns:

  • (Boolean)


18
19
20
# File 'app/models/foreman_google/google_compute.rb', line 18

def persisted?
  !!identity
end

#pretty_image_nameObject



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_typeObject



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_addressObject



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

Returns:

  • (Boolean)


22
23
24
# File 'app/models/foreman_google/google_compute.rb', line 22

def ready?
  status == 'RUNNING'
end

#reloadObject



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_outputObject



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_deleteObject



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

#startObject



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

#statusObject 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

#stopObject



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_sObject



49
50
51
# File 'app/models/foreman_google/google_compute.rb', line 49

def to_s
  @name
end

#vm_descriptionObject



84
85
86
# File 'app/models/foreman_google/google_compute.rb', line 84

def vm_description
  pretty_machine_type
end

#vm_ip_addressObject 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