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



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

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



188
189
190
# File 'lib/kitchen/driver/oraclecloud.rb', line 188

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

#destroy(state) ⇒ Object



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

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



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

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

#nameObject



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

def name
  'OracleCloud'
end

#oraclecloud_clientObject



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

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],
    private_cloud:   config[:private_cloud]
  )
end

#orchestration(name = nil) ⇒ Object



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

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



196
197
198
# File 'lib/kitchen/driver/oraclecloud.rb', line 196

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

#project_nameObject



200
201
202
# File 'lib/kitchen/driver/oraclecloud.rb', line 200

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

#public_ipObject



208
209
210
211
212
# File 'lib/kitchen/driver/oraclecloud.rb', line 208

def public_ip
  return nil unless config[:public_ip]

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

#refresh_timeObject



184
185
186
# File 'lib/kitchen/driver/oraclecloud.rb', line 184

def refresh_time
  config[:refresh_time].to_i
end

#serverObject



129
130
131
# File 'lib/kitchen/driver/oraclecloud.rb', line 129

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

#server_ip_addressObject



133
134
135
136
# File 'lib/kitchen/driver/oraclecloud.rb', line 133

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

#sshkeysObject



204
205
206
# File 'lib/kitchen/driver/oraclecloud.rb', line 204

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

#usernameObject



192
193
194
# File 'lib/kitchen/driver/oraclecloud.rb', line 192

def username
  config[:username]
end

#wait_for_server(state) ⇒ Object



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

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



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
177
178
# File 'lib/kitchen/driver/oraclecloud.rb', line 149

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



180
181
182
# File 'lib/kitchen/driver/oraclecloud.rb', line 180

def wait_time
  config[:wait_time].to_i
end