Class: Sunzi::Vps::Compute::Linode

Inherits:
Base
  • Object
show all
Defined in:
lib/sunzi/vps/compute/linode.rb

Instance Attribute Summary

Attributes inherited from Base

#api

Instance Method Summary collapse

Methods inherited from Base

#ask, #down, #initialize, #proceed?, #up

Constructor Details

This class inherits a constructor from Sunzi::Vps::Compute::Base

Instance Method Details

#choose(key, result, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sunzi/vps/compute/linode.rb', line 110

def choose(key, result, options = {})
  label_method = options[:label_method] || :label
  id    = :"#{key}id"
  label = :"#{key}_#{label_method}"

  # Filters
  if options[:filter] and config[options[:filter]]
    result = result.select{|i| i.label.match Regexp.new(config[options[:filter]], Regexp::IGNORECASE) }
  end

  result.each{|i| say "#{i.send(id)}: #{i.send(label_method)}" }
  @attributes[id] = ask("which #{key}?", limited_to: result.map(&id).map(&:to_s), default: result.first.send(id).to_s).to_i
  @attributes[label] = result.find{|i| i.send(id) == @attributes[id] }.send(label_method)
end

#do_downObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sunzi/vps/compute/linode.rb', line 125

def do_down
  @linode_id_hash = { :LinodeID => @instance[:linode_id] }

  # Shutdown first or disk deletion will fail
  say 'shutting down...'
  client.linode.shutdown(@linode_id_hash)
  # Wait until linode.shutdown has completed
  wait_for('linode.shutdown')

  # Delete the instance
  say 'deleting linode...'
  client.linode.delete(@linode_id_hash.merge(:skipChecks => 1))
end

#do_upObject



6
7
8
9
10
11
12
13
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sunzi/vps/compute/linode.rb', line 6

def do_up
  @sshkey = File.read(File.expand_path(config.root_sshkey_path)).chomp
  if @sshkey.match(/\n/)
    abort_with "RootSSHKey #{@sshkey.inspect} must not be multi-line! Check inside \"#{config.root_sshkey_path}\""
  end

  choose(:plan, client.avail.linodeplans)
  choose(:datacenter, client.avail.datacenters, :label_method => :location)
  choose(:distribution, client.avail.distributions, :filter => 'distributions_filter')
  choose(:kernel, client.avail.kernels, :filter => 'kernels_filter')

  # Choose swap size
  @swap_size = ask('swap size in MB?', default: 256).to_i

  # Go ahead?
  proceed?

  # Create
  say "creating a new linode..."
  result = client.linode.create(
    :DatacenterID => @attributes[:datacenterid],
    :PlanID => @attributes[:planid],
    :PaymentTerm => config.payment_term)
  @linodeid = result.linodeid
  say "created a new instance: linodeid = #{@linodeid}"

  result = client.linode.list.select{|i| i.linodeid == @linodeid }.first
  @totalhd = result.totalhd

  # Update settings
  say "Updating settings..."
  @group = config.group[@env]
  settings = { :LinodeID => @linodeid, :Label => @name, :lpm_displayGroup => @group }
  settings.update(config.settings) if config.settings
  client.linode.update(settings)

  # Create a root disk
  say "Creating a root disk..."
  result = client.linode.disk.createfromdistribution(
    :LinodeID => @linodeid,
    :DistributionID => @attributes[:distributionid],
    :Label => "#{@attributes[:distribution_label]} Image",
    :Size => @totalhd - @swap_size,
    :rootPass => config.root_pass,
    :rootSSHKey => @sshkey
  )
  @root_diskid = result.diskid

  # Create a swap disk
  say "Creating a swap disk..."
  result = client.linode.disk.create(
    :LinodeID => @linodeid,
    :Label => "#{@swap_size}MB Swap Image",
    :Type => 'swap',
    :Size => @swap_size
  )
  @swap_diskid = result.diskid

  # Create a config profiile
  say "Creating a config profile..."
  result = client.linode.config.create(
    :LinodeID => @linodeid,
    :KernelID => @attributes[:kernelid],
    :Label => "#{@attributes[:distribution_label]} Profile",
    :DiskList => [ @root_diskid, @swap_diskid ].join(',')
  )
  @config_id = result.configid

  # Add a private IP
  say "Adding a private IP..."
  result = client.linode.ip.list(:LinodeID => @linodeid)
  public_ip = result.first.ipaddress
  result = client.linode.ip.addprivate(:LinodeID => @linodeid)
  result = client.linode.ip.list(:LinodeID => @linodeid).find{|i| i.ispublic == 0 }
  @private_ip = result.ipaddress

  @instance = {
    :linode_id => @linodeid,
    :env => @env,
    :host => @host,
    :fqdn => @fqdn,
    :label => @name,
    :group => @group,
    :plan_id =>             @attributes[:planid],
    :datacenter_id =>       @attributes[:datacenterid],
    :datacenter_location => @attributes[:datacenter_location],
    :distribution_id =>     @attributes[:distributionid],
    :distribution_label =>  @attributes[:distribution_label],
    :kernel_id =>           @attributes[:kernelid],
    :kernel_label =>        @attributes[:kernel_label],
    :swap_size => @swap_size,
    :totalhd => @totalhd,
    :root_diskid => @root_diskid,
    :swap_diskid => @swap_diskid,
    :config_id => @config_id,
    :public_ip => public_ip,
    :private_ip => @private_ip,
  }

  # Boot
  say 'Done. Booting...'
  client.linode.boot(:LinodeID => @linodeid)
end

#wait_for(action) ⇒ Object



139
140
141
142
143
# File 'lib/sunzi/vps/compute/linode.rb', line 139

def wait_for(action)
  begin
    sleep 3
  end until client.linode.job.list(@linode_id_hash).find{|i| i.action == action }.host_success == 1
end