Class: Niso::Cloud::Base

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

Direct Known Subclasses

DropletKit

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/niso/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



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

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

#ask_menu(question, choices) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/niso/cloud/base.rb', line 88

def ask_menu(question, choices)
  answer = @ui.choose do |menu|
    menu.prompt = "#{@ui.color(question, :green, :bold)} "
    choices.each { |n| menu.choice(n) }
  end
  say("=> #{answer}")
  answer
end

#assign_configObject



72
73
74
# File 'lib/niso/cloud/base.rb', line 72

def assign_config
  @config = YAML.load(provider_config_path.read)
end

#instance_config_pathObject



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

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

#proceed?Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/niso/cloud/base.rb', line 97

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

#provider_config_pathObject



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

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
46
# File 'lib/niso/cloud/base.rb', line 14

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

  # get the config { provider }.yml
  assign_config

  if @config['name'] == 'example-droplet-01'
    abort_with "You must update the name in your settings file #{@provider}.yml"
  end

  if @config['access_token'] == 'your_api_key'
    abort_with "Access Token is invalid, please check your #{@provider}.yml"
  end

  # Ask environment and hostname
  @env = ask_menu("environment?", @config['environments'])

  @name = "#{@config['name']}-#{@env}"
  abort_with "#{@name} already exists!" if instance_config_path.exist?

  # get the client @client
  setup_client

  @attributes = {}
  do_setup

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

#teardownObject



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

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?

  @name = ask_menu("which instance?: ", names)

  assign_config

  @instance = YAML.load(instance_config_path.read)

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

  # Run Linode / DigitalOcean specific tasks
  setup_client
  do_teardown

  # Remove the instance config file
  @cli.remove_file instance_config_path

  say 'Done.'
end