Module: Kitchen::Driver::Oci::Mixin::Actions
- Included in:
- Kitchen::Driver::Oci
- Defined in:
- lib/kitchen/driver/oci/mixin/actions.rb
Overview
Actions that can be performed on an instance.
Instance Method Summary collapse
-
#are_legacy_imds_endpoints_disbled?(state, inst) ⇒ Boolean
Checks if legacy metadata is disabled.
-
#auth(action) ⇒ Oci::Config, Oci::Api
Creates the OCI config and API clients.
-
#execute_post_create_file(state) ⇒ Object
Reads the specified file and executes as a post create script.
-
#execute_post_create_script(state) ⇒ Object
Executes the post script on the instance.
-
#execute_post_create_string(state) ⇒ Object
Executes the post create script from a String.
-
#instance_options(state, inst) ⇒ Object
Applies instance options.
-
#instance_options? ⇒ Boolean
Attempts to disable IMDSv1 even if not explicitly specified in the config.
-
#launch(state, inst) ⇒ Object
Launches an instance.
-
#reboot(state, inst) ⇒ Object
Reboots an instance.
-
#terminate(state, inst) ⇒ Object
Terminates an instance.
Instance Method Details
#are_legacy_imds_endpoints_disbled?(state, inst) ⇒ Boolean
Checks if legacy metadata is disabled.
109 110 111 112 113 114 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 109 def are_legacy_imds_endpoints_disbled?(state, inst) return unless config[:instance_type] == "compute" imds = inst.api.compute.get_instance(state[:server_id]).data..are_legacy_imds_endpoints_disabled inst.logger.warn("Legacy IMDSv1 endpoint is enabled.") unless imds end |
#auth(action) ⇒ Oci::Config, Oci::Api
Creates the OCI config and API clients.
32 33 34 35 36 37 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 32 def auth(action) oci = Oci::Config.new(config) api = Oci::Api.new(oci.config, config) oci.compartment if action == :create [oci, api] end |
#execute_post_create_file(state) ⇒ Object
Reads the specified file and executes as a post create script.
76 77 78 79 80 81 82 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 76 def execute_post_create_file(state) config[:post_create_script].each do |script| info("Running post create script #{File.basename(script)}") script = File.read script instance.transport.connection(state).execute(script) end end |
#execute_post_create_script(state) ⇒ Object
Executes the post script on the instance.
54 55 56 57 58 59 60 61 62 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 54 def execute_post_create_script(state) return if config[:post_create_script].nil? if config[:post_create_script].is_a?(String) execute_post_create_string(state) elsif config[:post_create_script].is_a?(Array) execute_post_create_file(state) end end |
#execute_post_create_string(state) ⇒ Object
Executes the post create script from a String.
67 68 69 70 71 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 67 def execute_post_create_string(state) info("Running post create script") script = config[:post_create_script] instance.transport.connection(state).execute(script) end |
#instance_options(state, inst) ⇒ Object
Applies instance options.
88 89 90 91 92 93 94 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 88 def (state, inst) return unless inst.logger.info("Applying the following instance options:") config[:instance_options].each { |o, v| inst.logger.info("- #{o}: #{v}") } inst.api.compute.update_instance(state[:server_id], OCI::Core::Models::UpdateInstanceDetails.new(instance_options: OCI::Core::Models::InstanceOptions.new(config[:instance_options]))) end |
#instance_options? ⇒ Boolean
Attempts to disable IMDSv1 even if not explicitly specified in the config. This is in line with current security guidance from OCI. Acts as a guard for setting instance options.
98 99 100 101 102 103 104 105 106 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 98 def return false unless config[:instance_type] == "compute" return false unless config[:instance_options].delete(:post_create) opts = config[:instance_options] opts.merge!(are_legacy_imds_endpoints_disabled: true) unless opts.key?(:are_legacy_imds_endpoints_disabled) # Basically tell me if there's more stuff in there than `are_legacy_imds_endpoints_disabled: false`. If so, then proceed to setting it. opts.reject { |o, v| o == :are_legacy_imds_endpoints_disabled && !v }.any? end |
#launch(state, inst) ⇒ Object
Launches an instance.
43 44 45 46 47 48 49 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 43 def launch(state, inst) state_details = inst.launch state.merge!(state_details) instance.transport.connection(state).wait_until_ready (state, inst) are_legacy_imds_endpoints_disbled?(state, inst) end |
#reboot(state, inst) ⇒ Object
Reboots an instance.
120 121 122 123 124 125 126 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 120 def reboot(state, inst) return unless config[:post_create_reboot] instance.transport.connection(state).close inst.reboot instance.transport.connection(state).wait_until_ready end |
#terminate(state, inst) ⇒ Object
Terminates an instance.
132 133 134 135 136 137 138 139 |
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 132 def terminate(state, inst) instance.transport.connection(state).close inst.terminate if state[:ssh_key] FileUtils.rm_f(state[:ssh_key]) FileUtils.rm_f("#{state[:ssh_key]}.pub") end end |