class Chef::Knife::Cloud::OraclecloudService

Attributes

refresh_time[R]
wait_time[R]

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method
# 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

Public Instance Methods

connection() click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_images() click to toggle source
# File lib/chef/knife/cloud/oraclecloud_service.rb, line 144
def list_images
  connection.imagelists.all
end
list_orchestrations() click to toggle source
# File lib/chef/knife/cloud/oraclecloud_service.rb, line 140
def list_orchestrations
  connection.orchestrations.all
end
list_servers() click to toggle source
# File lib/chef/knife/cloud/oraclecloud_service.rb, line 136
def list_servers
  connection.instances.all
end
list_shapes() click to toggle source
# File lib/chef/knife/cloud/oraclecloud_service.rb, line 148
def list_shapes
  connection.shapes.all
end
orchestration_summary(orchestration) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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