Class: Sunzi::Vps::Compute::Base

Inherits:
Object
  • Object
show all
Includes:
Actions::Delegate
Defined in:
lib/sunzi/vps/compute/base.rb

Direct Known Subclasses

DigitalOcean, Linode, Vultr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api) ⇒ Base

Returns a new instance of Base.



11
12
13
14
# File 'lib/sunzi/vps/compute/base.rb', line 11

def initialize(api)
  @api = api
  api.dns.verify # fail early if domain is not registered
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



9
10
11
# File 'lib/sunzi/vps/compute/base.rb', line 9

def api
  @api
end

Instance Method Details

#ask(statement, *args) ⇒ Object



67
68
69
# File 'lib/sunzi/vps/compute/base.rb', line 67

def ask(statement, *args)
  Sunzi.thor.ask statement.color(:green).bright, *args
end

#downObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sunzi/vps/compute/base.rb', line 42

def down
  names = Dir.glob("#{api.provider}/instances/*.yml").map{|i| i.split('/').last.sub('.yml','') }
  abort_with "No match found with #{api.provider}/instances/*.yml" if names.empty?

  names.each{|i| say i }
  @name = ask('which instance?', limited_to: names)

  @instance = YAML.load(instance_config_path.read).to_hashugar

  # Are you sure?
  moveon = ask("Are you sure about deleting #{@instance.fqdn} permanently? (y/n)", limited_to: ['y','n'])
  exit unless moveon == 'y'

  # Run Linode / DigitalOcean specific tasks
  do_down

  # Delete DNS record
  api.dns.delete @instance.public_ip

  # Remove the instance config file
  remove_file instance_config_path

  say 'Done.'
end

#proceed?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/sunzi/vps/compute/base.rb', line 71

def proceed?
  moveon = ask("Are you ready to go ahead and create #{@fqdn}? (y/n)", limited_to: ['y','n'])
  exit unless moveon == 'y'
end

#upObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sunzi/vps/compute/base.rb', line 16

def up
  if config.api_key == 'your_api_key'
    abort_with "You must have your own settings in #{api.provider}.yml"
  end

  # Ask environment and hostname
  @env = ask("environment?", limited_to: config.environments, default: 'production')
  @host = ask('hostname? (only the first part of subdomain): ')

  abort_with '"label" field in linode.yml is no longer supported. rename it to "name".' if config.label
  @fqdn = config.fqdn.send(@env).gsub(/%{host}/, @host)
  @name = config.name.send(@env).gsub(/%{host}/, @host)
  abort_with "#{@name} already exists!" if instance_config_path.exist?

  @attributes = {}

  # Run Linode / DigitalOcean specific tasks
  do_up

  # Save instance info
  create_file instance_config_path, YAML.dump(@instance)

  # Register IP to DNS
  api.dns.add(@fqdn, @instance[:public_ip])
end