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

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

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.



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

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.



33
34
35
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 33

def refresh_time
  @refresh_time
end

#wait_timeObject (readonly)

Returns the value of attribute wait_time.



33
34
35
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 33

def wait_time
  @wait_time
end

Instance Method Details

#connectionObject



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

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



100
101
102
103
104
105
106
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 100

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)


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

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
  if servers.length > 1
    raise CloudExceptions::ServerCreateError, 'The orchestration created more than one server, ' \
      'but we were only expecting 1'
  end
  raise CloudExceptions::ServerCreateError, 'The orchestration did not create any servers' if servers.empty?

  servers.first
end

#delete_orchestration(orchestration_id) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 108

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 82

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



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

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

#get_server(instance_id) ⇒ Object



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

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

#instance_request(options) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 125

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



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

def list_images
  connection.imagelists.all
end

#list_orchestrationsObject



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

def list_orchestrations
  connection.orchestrations.all
end

#list_serversObject



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

def list_servers
  connection.instances.all
end

#list_shapesObject



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

def list_shapes
  connection.shapes.all
end

#orchestration_summary(orchestration) ⇒ Object



160
161
162
163
164
165
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 160

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



59
60
61
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 59

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

#server_summary(server, _columns_with_info = nil) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 167

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



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
205
206
207
208
# File 'lib/chef/knife/cloud/oraclecloud_service.rb', line 178

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