Class: Sunzi::Cloud::Base

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/sunzi/cloud/base.rb

Direct Known Subclasses

DigitalOcean, Linode

Instance Method Summary collapse

Methods included from Utility

#abort_with, #exit_with, #say

Constructor Details

#initialize(cli, provider) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/sunzi/cloud/base.rb', line 8

def initialize(cli, provider)
  @provider = provider
  @cli = cli
  @ui = HighLine.new
end

Instance Method Details

#ask(question, answer_type, &details) ⇒ Object



88
89
90
# File 'lib/sunzi/cloud/base.rb', line 88

def ask(question, answer_type, &details)
  @ui.ask(@ui.color(question, :green, :bold), answer_type, &details)
end

#assign_config_and_dnsObject



75
76
77
78
# File 'lib/sunzi/cloud/base.rb', line 75

def assign_config_and_dns
  @config = YAML.load(provider_config_path.read)
  @dns = Sunzi::DNS.new(@config, @provider) if @config['dns']
end

#instance_config_pathObject



84
85
86
# File 'lib/sunzi/cloud/base.rb', line 84

def instance_config_path
  Pathname.new "#{@provider}/instances/#{@name}.yml"
end

#proceed?Boolean

Returns:

  • (Boolean)


92
93
94
95
# File 'lib/sunzi/cloud/base.rb', line 92

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

#provider_config_pathObject



80
81
82
# File 'lib/sunzi/cloud/base.rb', line 80

def provider_config_path
  Pathname.new "#{@provider}/#{@provider}.yml"
end

#setupObject



14
15
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
41
42
43
44
45
# File 'lib/sunzi/cloud/base.rb', line 14

def setup
  unless File.exist? provider_config_path
    @cli.empty_directory "#{@provider}/instances"
    @cli.template "templates/setup/#{provider_config_path}", provider_config_path
    exit_with "Now go ahead and edit #{@provider}.yml, then run this command again!"
  end

  assign_config_and_dns

  if @config['fqdn']['zone'] == 'example.com'
    abort_with "You must have your own settings in #{@provider}.yml"
  end

  # Ask environment and hostname
  @env = ask("environment? (#{@config['environments'].join(' / ')}): ", String) {|q| q.in = @config['environments'] }.to_s
  @host = ask('hostname? (only the first part of subdomain): ', String).to_s

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

  assign_api
  @attributes = {}
  do_setup

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

  # Register IP to DNS
  @dns.add(@fqdn, @public_ip) if @dns
end

#teardownObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sunzi/cloud/base.rb', line 47

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

  names.each{|i| say i }
  @name = ask("which instance?: ", String) {|q| q.in = names }

  assign_config_and_dns

  @instance = YAML.load(instance_config_path.read)

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

  # Run Linode / DigitalOcean specific tasks
  assign_api
  do_teardown

  # Delete DNS record
  @dns.delete(@instance[ip_key]) if @dns

  # Remove the instance config file
  @cli.remove_file instance_config_path

  say 'Done.'
end