Class: Kitchen::Driver::Oraclecloud

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/oraclecloud.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Instance Method Details

#create(state) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kitchen/driver/oraclecloud.rb', line 49

def create(state)
  return if state[:orchestration_id]

  info('Creating Oracle Cloud orchestration...')
  orchestration

  info("Orchestration #{orchestration.name_with_container} created. Starting...")
  orchestration.start
  wait_for_status(orchestration, 'ready')

  state[:orchestration_id] = orchestration.name_with_container

  ip_address = server_ip_address
  raise 'No IP address returned for Oracle Cloud instance' if ip_address.nil?

  state[:hostname] = ip_address

  wait_for_server(state)
  info("Server #{orchestration_name} ready.")
end

#descriptionObject



186
187
188
# File 'lib/kitchen/driver/oraclecloud.rb', line 186

def description
  config[:description].nil? ? "#{instance.name} for #{username} via Test Kitchen" : config[:description]
end

#destroy(state) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kitchen/driver/oraclecloud.rb', line 70

def destroy(state)
  return if state[:orchestration_id].nil?

  info("Looking up orchestration #{state[:orchestration_id]}...")

  begin
    orchestration(state[:orchestration_id])
  rescue OracleCloud::Exception::HTTPNotFound
    warn("No orchestration found with ID #{state[:orchestration_id]}, assuming it has been destroyed already.")
    return
  end

  info("Stopping orchestration #{orchestration.name_with_container} and associated instance...")
  orchestration.stop
  wait_for_status(orchestration, 'stopped')

  info("Deleting orchestration #{orchestration.name_with_container} and associated instance...")
  orchestration.delete
  info('Orchestration deleted.')
end

#instance_requestObject



117
118
119
120
121
122
123
124
125
# File 'lib/kitchen/driver/oraclecloud.rb', line 117

def instance_request
  oraclecloud_client.instance_request(
    name:      orchestration_name,
    shape:     config[:shape],
    imagelist: config[:image],
    sshkeys:   sshkeys,
    public_ip: public_ip
  )
end

#nameObject



45
46
47
# File 'lib/kitchen/driver/oraclecloud.rb', line 45

def name
  'OracleCloud'
end

#oraclecloud_clientObject



91
92
93
94
95
96
97
98
99
# File 'lib/kitchen/driver/oraclecloud.rb', line 91

def oraclecloud_client
  @client ||= OracleCloud::Client.new(
    username:        config[:username],
    password:        config[:password],
    api_url:         config[:api_url],
    identity_domain: config[:identity_domain],
    verify_ssl:      config[:verify_ssl]
  )
end

#orchestration(name = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kitchen/driver/oraclecloud.rb', line 101

def orchestration(name = nil)
  return @orchestration if @orchestration

  if name
    @orchestration = oraclecloud_client.orchestrations.by_name(name)
  else
    @orchestration = oraclecloud_client.orchestrations.create(
      name: orchestration_name,
      description: description,
      instances: [ instance_request ]
    )
  end

  @orchestration
end

#orchestration_nameObject



194
195
196
# File 'lib/kitchen/driver/oraclecloud.rb', line 194

def orchestration_name
  "TK-#{project_name}-#{instance.name.gsub(/\s+/, '')}"
end

#project_nameObject



198
199
200
# File 'lib/kitchen/driver/oraclecloud.rb', line 198

def project_name
  @project_name ||= config[:project_name].nil? ? SecureRandom.uuid : config[:project_name].gsub(/\s+/, '')
end

#public_ipObject



206
207
208
209
210
# File 'lib/kitchen/driver/oraclecloud.rb', line 206

def public_ip
  return nil unless config[:public_ip]

  (config[:public_ip] == 'pool') ? :pool : "ipreservation:#{config[:public_ip]}"
end

#refresh_timeObject



182
183
184
# File 'lib/kitchen/driver/oraclecloud.rb', line 182

def refresh_time
  config[:refresh_time].to_i
end

#serverObject



127
128
129
# File 'lib/kitchen/driver/oraclecloud.rb', line 127

def server
  @server ||= orchestration.instances.first
end

#server_ip_addressObject



131
132
133
134
# File 'lib/kitchen/driver/oraclecloud.rb', line 131

def server_ip_address
  public_ips = server.public_ip_addresses
  public_ips.empty? ? server.ip_address : public_ips.first
end

#sshkeysObject



202
203
204
# File 'lib/kitchen/driver/oraclecloud.rb', line 202

def sshkeys
  config[:sshkeys].map { |key| "#{oraclecloud_client.compute_identity_domain}/#{key}" }
end

#usernameObject



190
191
192
# File 'lib/kitchen/driver/oraclecloud.rb', line 190

def username
  config[:username]
end

#wait_for_server(state) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/kitchen/driver/oraclecloud.rb', line 136

def wait_for_server(state)
  info("Server #{orchestration_name} created. Waiting until ready...")
  begin
    instance.transport.connection(state).wait_until_ready
  rescue
    error("Server #{orchestration_name} not reachable. Destroying server...")
    destroy(state)
    raise
  end
end

#wait_for_status(item, requested_status) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/kitchen/driver/oraclecloud.rb', line 147

def wait_for_status(item, requested_status)
  last_status = ''

  begin
    Timeout.timeout(wait_time) do
      loop do
        item.refresh
        current_status = item.status

        if item.error?
          error_str = "Request encountered an error: #{item.errors}"
          error(error_str)
          raise error_str
        end

        unless last_status == current_status
          last_status = current_status
          info("Current status: #{current_status}.")
        end

        break if current_status == requested_status

        sleep refresh_time
      end
    end
  rescue Timeout::Error
    error("Request did not complete in #{wait_time} seconds. Check the Oracle Cloud Web UI for more information.")
    raise
  end
end

#wait_timeObject



178
179
180
# File 'lib/kitchen/driver/oraclecloud.rb', line 178

def wait_time
  config[:wait_time].to_i
end