Class: ForemanGoogle::GCE
- Inherits:
-
ComputeResource
- Object
- ComputeResource
- ForemanGoogle::GCE
- Defined in:
- app/models/foreman_google/gce.rb
Class Method Summary collapse
Instance Method Summary collapse
- #associated_host(vm) ⇒ Object
- #available_images(filter: filter_for_images) ⇒ Object
- #available_networks(_cluster_id = nil) ⇒ Object
- #capabilities ⇒ Object
- #console(uuid) ⇒ Object
- #create_vm(args = {}) ⇒ Object
- #destroy_vm(uuid) ⇒ Object
- #filter_for_images ⇒ Object
- #find_vm_by_uuid(uuid) ⇒ Object
-
#google_project_id ⇒ Object
—-# Google specific #—–.
- #machine_types ⇒ Object (also: #available_flavors)
- #networks ⇒ Object
- #new_vm(args = {}) ⇒ Object
- #new_volume(attrs = {}) ⇒ Object
- #provided_attributes ⇒ Object
- #setup_key_pair ⇒ Object
- #to_label ⇒ Object
- #user_data_supported? ⇒ Boolean
- #vm_ready(vm) ⇒ Object
- #vms(attrs = {}) ⇒ Object
- #zone ⇒ Object
- #zone=(zone) ⇒ Object
- #zones ⇒ Object (also: #available_zones)
Class Method Details
.available? ⇒ Boolean
10 11 12 |
# File 'app/models/foreman_google/gce.rb', line 10 def self.available? true end |
.model_name ⇒ Object
96 97 98 |
# File 'app/models/foreman_google/gce.rb', line 96 def self.model_name ComputeResource.model_name end |
.provider_friendly_name ⇒ Object
107 108 109 |
# File 'app/models/foreman_google/gce.rb', line 107 def self.provider_friendly_name 'Google' end |
Instance Method Details
#associated_host(vm) ⇒ Object
134 135 136 |
# File 'app/models/foreman_google/gce.rb', line 134 def associated_host(vm) associate_by('ip', [vm.public_ip_address, vm.private_ip_address]) end |
#available_images(filter: filter_for_images) ⇒ Object
88 89 90 |
# File 'app/models/foreman_google/gce.rb', line 88 def available_images(filter: filter_for_images) client.images(filter: filter) end |
#available_networks(_cluster_id = nil) ⇒ Object
35 36 37 |
# File 'app/models/foreman_google/gce.rb', line 35 def available_networks(_cluster_id = nil) client.networks end |
#capabilities ⇒ Object
18 19 20 |
# File 'app/models/foreman_google/gce.rb', line 18 def capabilities %i[image new_volume] end |
#console(uuid) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/foreman_google/gce.rb', line 120 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
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/foreman_google/gce.rb', line 62 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
80 81 82 83 84 85 86 |
# File 'app/models/foreman_google/gce.rb', line 80 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_images ⇒ Object
92 93 94 |
# File 'app/models/foreman_google/gce.rb', line 92 def filter_for_images @filter_for_images ||= nil end |
#find_vm_by_uuid(uuid) ⇒ Object
76 77 78 |
# File 'app/models/foreman_google/gce.rb', line 76 def find_vm_by_uuid(uuid) GoogleCompute.new(client: client, zone: zone, identity: uuid.to_s) end |
#google_project_id ⇒ Object
—-# Google specific #—–
145 146 147 |
# File 'app/models/foreman_google/gce.rb', line 145 def google_project_id client.project_id end |
#machine_types ⇒ Object Also known as: available_flavors
39 40 41 |
# File 'app/models/foreman_google/gce.rb', line 39 def machine_types client.machine_types(zone) end |
#networks ⇒ Object
31 32 33 |
# File 'app/models/foreman_google/gce.rb', line 31 def networks client.networks.map(&:name) end |
#new_vm(args = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'app/models/foreman_google/gce.rb', line 52 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
115 116 117 118 |
# File 'app/models/foreman_google/gce.rb', line 115 def new_volume(attrs = {}) default_attrs = { disk_size_gb: 20 } Google::Cloud::Compute::V1::AttachedDisk.new(**attrs.merge(default_attrs)) end |
#provided_attributes ⇒ Object
22 23 24 |
# File 'app/models/foreman_google/gce.rb', line 22 def provided_attributes super.merge({ ip: :vm_ip_address }) end |
#setup_key_pair ⇒ Object
100 101 102 103 104 105 |
# File 'app/models/foreman_google/gce.rb', line 100 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_label ⇒ Object
14 15 16 |
# File 'app/models/foreman_google/gce.rb', line 14 def to_label "#{name} (#{zone}-#{provider_friendly_name})" end |
#user_data_supported? ⇒ Boolean
111 112 113 |
# File 'app/models/foreman_google/gce.rb', line 111 def user_data_supported? true end |
#vm_ready(vm) ⇒ Object
149 150 151 152 153 154 |
# File 'app/models/foreman_google/gce.rb', line 149 def vm_ready(vm) vm.wait_for do vm.reload vm.ready? end end |
#vms(attrs = {}) ⇒ Object
138 139 140 141 |
# File 'app/models/foreman_google/gce.rb', line 138 def vms(attrs = {}) filtered_attrs = attrs.except(:eager_loading) GoogleCloudCompute::ComputeCollection.new(client, zone, filtered_attrs) end |
#zone ⇒ Object
44 45 46 |
# File 'app/models/foreman_google/gce.rb', line 44 def zone url end |
#zone=(zone) ⇒ Object
48 49 50 |
# File 'app/models/foreman_google/gce.rb', line 48 def zone=(zone) self.url = zone end |
#zones ⇒ Object Also known as: available_zones
26 27 28 |
# File 'app/models/foreman_google/gce.rb', line 26 def zones client.zones.map(&:name) end |