Class: ForemanGoogle::GCE

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'app/models/foreman_google/gce.rb', line 8

def self.available?
  true
end

.model_nameObject



94
95
96
# File 'app/models/foreman_google/gce.rb', line 94

def self.model_name
  ComputeResource.model_name
end

.provider_friendly_nameObject



105
106
107
# File 'app/models/foreman_google/gce.rb', line 105

def self.provider_friendly_name
  'Google'
end

Instance Method Details

#associated_host(vm) ⇒ Object



132
133
134
# File 'app/models/foreman_google/gce.rb', line 132

def associated_host(vm)
  associate_by('ip', [vm.public_ip_address, vm.private_ip_address])
end

#available_images(filter: filter_for_images) ⇒ Object



86
87
88
# File 'app/models/foreman_google/gce.rb', line 86

def available_images(filter: filter_for_images)
  client.images(filter: filter)
end

#available_networks(_cluster_id = nil) ⇒ Object



33
34
35
# File 'app/models/foreman_google/gce.rb', line 33

def available_networks(_cluster_id = nil)
  client.networks
end

#capabilitiesObject



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

def capabilities
  %i[image new_volume]
end

#console(uuid) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/foreman_google/gce.rb', line 118

def console(uuid)
  vm = find_vm_by_uuid(uuid)

  if vm.ready?
    {
      'output' => vm.serial_port_output, 'timestamp' => Time.now.utc,
      :type => 'log', :name => vm.name
    }
  else
    raise ::Foreman::Exception,
      N_('console is not available at this time because the instance is powered off')
  end
end

#create_vm(args = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/foreman_google/gce.rb', line 60

def create_vm(args = {})
  ssh_args = { username: find_os_image(args[:image_id])&.username, public_key: key_pair.public }
  vm = new_vm(args.merge(ssh_args))

  vm.create_volumes
  vm.create_instance
  vm.set_disk_auto_delete

  find_vm_by_uuid vm.hostname
rescue ::Google::Cloud::Error => e
  vm.destroy_volumes
  raise Foreman::WrappedException.new(e, 'Cannot insert instance!')
end

#destroy_vm(uuid) ⇒ Object



78
79
80
81
82
83
84
# File 'app/models/foreman_google/gce.rb', line 78

def destroy_vm(uuid)
  client.set_disk_auto_delete(zone, uuid)
  client.delete_instance(zone, uuid)
rescue ActiveRecord::RecordNotFound
  # if the VM does not exists, we don't really care.
  true
end

#filter_for_imagesObject



90
91
92
# File 'app/models/foreman_google/gce.rb', line 90

def filter_for_images
  @filter_for_images ||= nil
end

#find_vm_by_uuid(uuid) ⇒ Object



74
75
76
# File 'app/models/foreman_google/gce.rb', line 74

def find_vm_by_uuid(uuid)
  GoogleCompute.new(client: client, zone: zone, identity: uuid.to_s)
end

#google_project_idObject

—-# Google specific #—–



143
144
145
# File 'app/models/foreman_google/gce.rb', line 143

def google_project_id
  client.project_id
end

#machine_typesObject Also known as: available_flavors



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

def machine_types
  client.machine_types(zone)
end

#networksObject



29
30
31
# File 'app/models/foreman_google/gce.rb', line 29

def networks
  client.networks.map(&:name)
end

#new_vm(args = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'app/models/foreman_google/gce.rb', line 50

def new_vm(args = {})
  vm_args = args.deep_symbolize_keys

  # convert rails nested_attributes into a plain hash
  volumes_nested_attrs = vm_args.delete(:volumes_attributes)
  vm_args[:volumes] = nested_attributes_for(:volumes, volumes_nested_attrs) if volumes_nested_attrs

  GoogleCompute.new(client: client, zone: zone, args: vm_args)
end

#new_volume(attrs = {}) ⇒ Object



113
114
115
116
# File 'app/models/foreman_google/gce.rb', line 113

def new_volume(attrs = {})
  default_attrs = { disk_size_gb: 20 }
  Google::Cloud::Compute::V1::AttachedDisk.new(**attrs.merge(default_attrs))
end

#provided_attributesObject



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

def provided_attributes
  super.merge({ ip: :vm_ip_address })
end

#setup_key_pairObject



98
99
100
101
102
103
# File 'app/models/foreman_google/gce.rb', line 98

def setup_key_pair
  require 'sshkey'

  key = ::SSHKey.generate
  build_key_pair name: "foreman-#{id}#{Foreman.uuid}", secret: key.private_key, public: key.ssh_public_key
end

#to_labelObject



12
13
14
# File 'app/models/foreman_google/gce.rb', line 12

def to_label
  "#{name} (#{zone}-#{provider_friendly_name})"
end

#user_data_supported?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/models/foreman_google/gce.rb', line 109

def user_data_supported?
  true
end

#vm_ready(vm) ⇒ Object



147
148
149
150
151
152
# File 'app/models/foreman_google/gce.rb', line 147

def vm_ready(vm)
  vm.wait_for do
    vm.reload
    vm.ready?
  end
end

#vms(attrs = {}) ⇒ Object



136
137
138
139
# File 'app/models/foreman_google/gce.rb', line 136

def vms(attrs = {})
  filtered_attrs = attrs.except(:eager_loading)
  GoogleCloudCompute::ComputeCollection.new(client, zone, filtered_attrs)
end

#zoneObject



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

def zone
  url
end

#zone=(zone) ⇒ Object



46
47
48
# File 'app/models/foreman_google/gce.rb', line 46

def zone=(zone)
  self.url = zone
end

#zonesObject Also known as: available_zones



24
25
26
# File 'app/models/foreman_google/gce.rb', line 24

def zones
  client.zones.map(&:name)
end