Class: OnlyofficeDigitaloceanWrapper::DigitalOceanWrapper

Inherits:
Object
  • Object
show all
Includes:
ExceptionsRetryer, Getters, IpGetters, LoggerWrapper, PowerActions, TokenMethods
Defined in:
lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb

Overview

Class for wrapping DigitalOcean API gem

Constant Summary collapse

DROPLET_SIZES =

Returns list of allowed droplet sizes.

Returns:

  • (Array<String>)

    list of allowed droplet sizes

%w[512mb 1gb 2gb 4gb 8gb 16gb 32gb 48gb 64gb].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TokenMethods

#correct_access_token?, #read_token

Methods included from PowerActions

#power_off_droplet, #power_on_droplet, #reboot_droplet

Methods included from ExceptionsRetryer

#retry_exception

Methods included from LoggerWrapper

#logger

Methods included from IpGetters

#droplet_ip, #public_ip

Methods included from Getters

#droplet_by_name, #get_droplet_id_by_name, #get_droplet_ip_by_name, #get_droplet_status_by_name, #get_image_id_by_name, #get_project_id_by_name, #project_by_name

Constructor Details

#initialize(access_token = nil) ⇒ DigitalOceanWrapper

Returns a new instance of DigitalOceanWrapper.

Raises:

  • (ArgumentError)


27
28
29
30
31
# File 'lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb', line 27

def initialize(access_token = nil)
  access_token ||= read_token
  @client = DropletKit::Client.new(access_token: access_token)
  raise ArgumentError, 'DigitalOceanWrapper: Your Access Token is Incorrect' unless correct_access_token?
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



25
26
27
# File 'lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb', line 25

def client
  @client
end

Instance Method Details

#destroy_droplet_by_name(droplet_name = 'nct-at1') ⇒ Symbol

Destroy droplet by name

Parameters:

  • droplet_name (String) (defaults to: 'nct-at1')

    name of droplet

Returns:

  • (Symbol)

    Droplet status after destruction



90
91
92
93
94
95
# File 'lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb', line 90

def destroy_droplet_by_name(droplet_name = 'nct-at1')
  droplet_id = get_droplet_id_by_name(droplet_name)
  client.droplets.delete(id: droplet_id)
  logger.info("destroy_droplet_by_name(#{droplet_name})")
  wait_until_droplet_have_status(droplet_name, nil)
end

#restore_image_by_name(image_name = 'nct-at-stable', droplet_name = image_name, region = 'nyc3', size = '2gb', tags: nil) ⇒ Object

Restore droplet from image by name

Parameters:

  • image_name (String) (defaults to: 'nct-at-stable')

    name of image

  • droplet_name (String) (defaults to: image_name)

    name for droplet

  • region (String) (defaults to: 'nyc3')

    region to restore

  • size (String) (defaults to: '2gb')

    size of droplet

  • tags (String, Array<String>) (defaults to: nil)

    name of tags to apply

Returns:

  • (Object)

    object with droplet data



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb', line 62

def restore_image_by_name(image_name = 'nct-at-stable',
                          droplet_name = image_name,
                          region = 'nyc3',
                          size = '2gb',
                          tags: nil)
  unless DROPLET_SIZES.include?(size)
    raise DigitalOceanSizeNotSupported,
          "There is no support of droplets with size: #{size}"
  end
  image_id = get_image_id_by_name(image_name)
  droplet = DropletKit::Droplet.new(name: droplet_name,
                                    region: region,
                                    image: image_id.to_i,
                                    tags: Array(tags),
                                    monitoring: true,
                                    size: size)
  created = @client.droplets.create(droplet)
  logger.info("restore_image_by_name(#{image_name}, #{droplet_name})")
  if created.is_a?(String)
    raise "Problem, while creating '#{droplet_name}' from image '#{image_name}'\n" \
          "Error: #{created}"
  end
  created
end

#wait_until_droplet_have_status(droplet_name, status = 'active', params = {}) ⇒ Symbol

Wait until droplet has status

Parameters:

  • droplet_name (String)

    name of droplet

  • status (String) (defaults to: 'active')

    status to wait

  • params (Hash) (defaults to: {})

    additional params

Options Hash (params):

  • :timeout (Integer) — default: 300

    maximum time to wait in seconds

  • :interval (Integer) — default: 10

    interval time between status checks in seconds

Returns:

  • (Symbol)

    droplet status after wait over

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/onlyoffice_digitalocean_wrapper/digitalocean_wrapper.rb', line 40

def wait_until_droplet_have_status(droplet_name, status = 'active', params = {})
  timeout = params.fetch(:timeout, 300)
  interval = params.fetch(:interval, 10)
  counter = 0
  while get_droplet_status_by_name(droplet_name) != status && counter < timeout
    counter += interval
    sleep(interval)
    logger.info("waiting for droplet (#{droplet_name}) to have " \
                "status: #{status} for #{counter} seconds of #{timeout}")
  end
  raise DropletOperationTimeout, "#{droplet_name} was not #{status} for #{timeout}s" if counter >= timeout

  get_droplet_status_by_name(droplet_name)
end