Class: Chef::Knife::Cloud::OraclecloudService

Inherits:
Service
  • Object
show all
Includes:
OraclecloudServiceHelpers
Defined in:
lib/chef/knife/cloud/oraclecloud_service.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OraclecloudServiceHelpers

#check_for_missing_config_values!, #create_service_instance, #verify_ssl?

Constructor Details

#initialize(options = {}) ⇒ OraclecloudService

Returns a new instance of OraclecloudService.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 33

def initialize(options = {})
  super(options)

  @username        = options[:username]
  @password        = options[:password]
  @api_url         = options[:api_url]
  @identity_domain = options[:identity_domain]
  @verify_ssl      = options[:verify_ssl]
  @wait_time       = options[:wait_time]
  @refresh_time    = options[:refresh_time]
  @private_cloud   = options[:private_cloud]
end

Instance Attribute Details

#refresh_timeObject (readonly)

Returns the value of attribute refresh_time.



31
32
33
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 31

def refresh_time
  @refresh_time
end

#wait_timeObject (readonly)

Returns the value of attribute wait_time.



31
32
33
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 31

def wait_time
  @wait_time
end

Instance Method Details

#connectionObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 46

def connection
  @client ||= OracleCloud::Client.new(
    api_url:         @api_url,
    identity_domain: @identity_domain,
    username:        @username,
    password:        @password,
    verify_ssl:      @verify_ssl,
    private_cloud:   @private_cloud
  )
end

#create_orchestration(options) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 96

def create_orchestration(options)
  connection.orchestrations.create(
    name: options[:name],
    description: "#{options[:name]} by #{connection.username} via Knife",
    instances: [ instance_request(options) ]
  )
end

#create_server(options = {}) ⇒ Object

Raises:

  • (CloudExceptions::ServerCreateError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 61

def create_server(options = {})
  orchestration = create_orchestration(options)
  orchestration.start
  ui.msg("Orchestration #{orchestration.name_with_container} started - waiting for it to complete...")
  wait_for_status(orchestration, 'ready')
  ui.msg("Orchestration started successfully.\n")
  orchestration_summary(orchestration)
  ui.msg('')

  servers = orchestration.instances
  raise CloudExceptions::ServerCreateError, 'The orchestration created more than one server, ' \
    'but we were only expecting 1' if servers.length > 1
  raise CloudExceptions::ServerCreateError, 'The orchestration did not create any servers' if servers.length == 0

  servers.first
end

#delete_orchestration(orchestration_id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 104

def delete_orchestration(orchestration_id)
  orchestration = get_orchestration(orchestration_id)
  orchestration_summary(orchestration)
  ui.msg('')

  ui.confirm('Do you really want to delete this orchestration')

  ui.msg('Stopping the orchestration and any instances...')
  orchestration.stop
  wait_for_status(orchestration, 'stopped')

  ui.msg('Deleting the orchestration and any instances...')
  orchestration.delete

  ui.msg('Delete request complete.')
end

#delete_server(instance_id) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 78

def delete_server(instance_id)
  instance = get_server(instance_id)
  server_summary(instance)
  ui.msg('')

  unless instance.orchestration.nil?
    ui.error('Unable to delete this server.  Delete the orchestration instead.')
    exit(1)
  end

  ui.confirm('Do you really want to delete this server')

  ui.msg('Deleting the instance...')
  instance.delete

  ui.msg('Delete request complete.')
end

#get_orchestration(orchestration_id) ⇒ Object



152
153
154
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 152

def get_orchestration(orchestration_id)
  connection.orchestrations.by_name(orchestration_id)
end

#get_server(instance_id) ⇒ Object



148
149
150
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 148

def get_server(instance_id)
  connection.instances.by_name(instance_id)
end

#instance_request(options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 121

def instance_request(options)
  connection.instance_request(
    name:      options[:name],
    shape:     options[:shape],
    imagelist: options[:image],
    sshkeys:   options[:sshkeys],
    label:     options[:label],
    public_ip: options[:public_ip]
  )
end

#list_imagesObject



140
141
142
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 140

def list_images
  connection.imagelists.all
end

#list_orchestrationsObject



136
137
138
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 136

def list_orchestrations
  connection.orchestrations.all
end

#list_serversObject



132
133
134
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 132

def list_servers
  connection.instances.all
end

#list_shapesObject



144
145
146
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 144

def list_shapes
  connection.shapes.all
end

#orchestration_summary(orchestration) ⇒ Object



156
157
158
159
160
161
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 156

def orchestration_summary(orchestration)
  msg_pair('Orchestration ID', orchestration.name_with_container)
  msg_pair('Description', orchestration.description)
  msg_pair('Status', orchestration.status)
  msg_pair('Instance Count', orchestration.instance_count)
end

#prepend_identity_domain(path) ⇒ Object



57
58
59
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 57

def prepend_identity_domain(path)
  "#{connection.full_identity_domain}/#{path}"
end

#server_summary(server, _columns_with_info = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 163

def server_summary(server, _columns_with_info = nil)
  msg_pair('Server Label', server.label)
  msg_pair('Status', server.status)
  msg_pair('Hostname', server.hostname)
  msg_pair('IP Address', server.ip_address.nil? ? 'none' : server.ip_address)
  msg_pair('Public IP Addresses', server.public_ip_addresses.empty? ? 'none' : server.public_ip_addresses.join(', '))
  msg_pair('Image', server.image)
  msg_pair('Shape', server.shape)
  msg_pair('Orchestration', server.orchestration.nil? ? 'none' : server.orchestration)
end

#wait_for_status(item, requested_status) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 174

def wait_for_status(item, requested_status)
  last_status = ''

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

        if current_status == requested_status
          print "\n"
          break
        end

        if last_status == current_status
          print '.'
        else
          last_status = current_status
          print "\n"
          print "Current status: #{current_status}."
        end

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