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.



6
7
8
9
10
11
12
13
14
# File 'app/models/foreman_google/google_compute.rb', line 6

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.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def associate_external_ip
  @associate_external_ip
end

#creation_timestampObject (readonly)

Returns the value of attribute creation_timestamp.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def creation_timestamp
  @creation_timestamp
end

#disksObject (readonly)

Returns the value of attribute disks.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def disks
  @disks
end

#hostnameObject (readonly)

Returns the value of attribute hostname.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def hostname
  @hostname
end

#identityObject (readonly)

Returns the value of attribute identity.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def identity
  @identity
end

#image_idObject (readonly)

Returns the value of attribute image_id.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def image_id
  @image_id
end

#machine_typeObject (readonly)

Returns the value of attribute machine_type.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def machine_type
  @machine_type
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def name
  @name
end

#networkObject (readonly)

Returns the value of attribute network.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def network
  @network
end

#network_interfacesObject (readonly)

Returns the value of attribute network_interfaces.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def network_interfaces
  @network_interfaces
end

#volumesObject (readonly)

Returns the value of attribute volumes.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def volumes
  @volumes
end

#zoneObject (readonly)

Returns the value of attribute zone.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def zone
  @zone
end

#zone_nameObject (readonly)

Returns the value of attribute zone_name.



3
4
5
# File 'app/models/foreman_google/google_compute.rb', line 3

def zone_name
  @zone_name
end

Instance Method Details

#create_instanceObject



68
69
70
71
# File 'app/models/foreman_google/google_compute.rb', line 68

def create_instance
  args = GoogleCloudCompute::ComputeAttributes.new(@client).for_create(self)
  @client.insert_instance(@zone, args)
end

#create_volumesObject



55
56
57
58
59
60
# File 'app/models/foreman_google/google_compute.rb', line 55

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



62
63
64
65
66
# File 'app/models/foreman_google/google_compute.rb', line 62

def destroy_volumes
  @volumes.each do |volume|
    @client.delete_disk(@zone, volume.device_name)
  end
end

#interfacesObject



51
52
53
# File 'app/models/foreman_google/google_compute.rb', line 51

def interfaces
  @network_interfaces
end

#ip_addressesObject



115
116
117
# File 'app/models/foreman_google/google_compute.rb', line 115

def ip_addresses
  [vm_ip_address, private_ip_address]
end

#persisted?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/foreman_google/google_compute.rb', line 16

def persisted?
  !!identity
end

#pretty_image_nameObject



99
100
101
102
103
104
105
106
# File 'app/models/foreman_google/google_compute.rb', line 99

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



77
78
79
80
# File 'app/models/foreman_google/google_compute.rb', line 77

def pretty_machine_type
  return @machine_type unless @instance
  @instance.machine_type.split('/').last
end

#private_ip_addressObject



93
94
95
96
97
# File 'app/models/foreman_google/google_compute.rb', line 93

def private_ip_address
  return unless @instance.network_interfaces.any?

  @instance.network_interfaces.first.network_i_p
end

#ready?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/foreman_google/google_compute.rb', line 20

def ready?
  status == 'RUNNING'
end

#reloadObject



24
25
26
27
28
# File 'app/models/foreman_google/google_compute.rb', line 24

def reload
  return unless identity
  load
  self
end

#serial_port_outputObject



111
112
113
# File 'app/models/foreman_google/google_compute.rb', line 111

def serial_port_output
  @client.serial_port_output(@zone, @identity)&.contents
end

#set_disk_auto_deleteObject



73
74
75
# File 'app/models/foreman_google/google_compute.rb', line 73

def set_disk_auto_delete
  @client.set_disk_auto_delete(@zone, @name)
end

#startObject



37
38
39
40
# File 'app/models/foreman_google/google_compute.rb', line 37

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



32
33
34
# File 'app/models/foreman_google/google_compute.rb', line 32

def status
  persisted? && @instance.status
end

#stopObject



42
43
44
45
# File 'app/models/foreman_google/google_compute.rb', line 42

def stop
  raise Foreman::Exception('unable to stop machine that is not persisted') unless persisted?
  @client.stop(@zone, identity)
end

#to_sObject



47
48
49
# File 'app/models/foreman_google/google_compute.rb', line 47

def to_s
  @name
end

#vm_descriptionObject



82
83
84
# File 'app/models/foreman_google/google_compute.rb', line 82

def vm_description
  pretty_machine_type
end

#vm_ip_addressObject Also known as: public_ip_address



86
87
88
89
90
# File 'app/models/foreman_google/google_compute.rb', line 86

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



108
109
# File 'app/models/foreman_google/google_compute.rb', line 108

def volumes_attributes=(_attrs)
end

#wait_for(&block) ⇒ Object



119
120
121
# File 'app/models/foreman_google/google_compute.rb', line 119

def wait_for(&block)
  @client.wait_for(&block)
end