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.

Author:

  • Justin Steele <justin.steele@oracle.com>

Instance Method Summary collapse

Instance Method Details

#are_legacy_imds_endpoints_disbled?(state, inst) ⇒ Boolean

Checks if legacy metadata is disabled.

Returns:

  • (Boolean)


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.instance_options.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.

Parameters:

  • action (Symbol)

    the name of the method that called this method.

Returns:



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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)



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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)



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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)



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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)

  • inst (Class)

    the specific class of instance being rebooted.



88
89
90
91
92
93
94
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 88

def instance_options(state, inst)
  return unless instance_options?

  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.

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/kitchen/driver/oci/mixin/actions.rb', line 98

def instance_options?
  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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)

  • inst (Class)

    the specific class of instance being launched.



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
  instance_options(state, inst)
  are_legacy_imds_endpoints_disbled?(state, inst)
end

#reboot(state, inst) ⇒ Object

Reboots an instance.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)

  • inst (Class)

    the specific class of instance being rebooted.



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.

Parameters:

  • state (Hash)

    (see Kitchen::StateFile)

  • inst (Class)

    the specific class of instance being launched.



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