Class: OnlyofficeDigitaloceanWrapper::DigitalOceanWrapper
- Inherits:
-
Object
- Object
- OnlyofficeDigitaloceanWrapper::DigitalOceanWrapper
- 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.
%w[512mb 1gb 2gb 4gb 8gb 16gb 32gb 48gb 64gb].freeze
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
-
#destroy_droplet_by_name(droplet_name = 'nct-at1') ⇒ Symbol
Destroy droplet by name.
-
#initialize(access_token = nil) ⇒ DigitalOceanWrapper
constructor
A new instance of DigitalOceanWrapper.
-
#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.
-
#wait_until_droplet_have_status(droplet_name, status = 'active', params = {}) ⇒ Symbol
Wait until droplet has status.
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
Methods included from LoggerWrapper
Methods included from IpGetters
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.
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
#client ⇒ Object
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
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
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(), 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
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 |