Class: Befog::Commands::Add

Inherits:
Object
  • Object
show all
Includes:
Mixins::Command, Mixins::Configurable, Mixins::Help, Mixins::Safely, Mixins::Scope, Mixins::Traceable
Defined in:
lib/befog/commands/add.rb

Instance Attribute Summary

Attributes included from Mixins::Command

#options

Instance Method Summary collapse

Methods included from Mixins::Safely

#safely

Methods included from Mixins::Traceable

included, #rehearse?, #verbose, #verbose?

Methods included from Mixins::Help

included

Methods included from Mixins::Scope

#_bank, #account_key, #account_secret, #bank, #bank?, #bank_name, #banks, #compute, #flavor, #flavor?, #get_server, #image, #image?, #keypair, #keypair?, #price, #provider, #provider?, #provider_name, #providers, #region, #region?, #security_group, #security_group?, #servers, #servers=

Methods included from Mixins::Configurable

#_configuration, #configuration, #configuration_name, #configuration_path, included, #save

Methods included from Mixins::Command

#command, #error, included, #initialize, #log, #process_options, #usage

Instance Method Details

#generate_idObject



99
100
101
102
# File 'lib/befog/commands/add.rb', line 99

def generate_id
  bank["counter"] ||= 0
  bank["counter"] += 1
end

#generate_server_nameObject



95
96
97
# File 'lib/befog/commands/add.rb', line 95

def generate_server_name
  "#{bank_name}-#{generate_id}"
end

#provision_serverObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/befog/commands/add.rb', line 77

def provision_server
  if options[:spot]
    request = compute.spot_requests.create(
      :price => price.to_f, :instance_count => 1, 
      :tags => {"Name" => generate_server_name},
      :region => region, :flavor_id => flavor, :image_id => image, 
      :groups => [ security_group ], :key_name => keypair)
    server = nil
    Fog.wait_for {(server = compute.servers.get(request.reload.instance_id)) rescue nil}
    server
  else
    compute.servers.create(
        :tags => {"Name" => generate_server_name},
        :region => region, :flavor_id => flavor, :image_id => image, 
        :security_group_ids => security_group, :key_name => keypair)
  end
end

#provision_servers(count) ⇒ Object



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
# File 'lib/befog/commands/add.rb', line 42

def provision_servers(count)
  error("Count must be greater than zero.") unless (count > 0)
  provisioned = []; threads = []
  count.times do |i|
    verbose <<-EOF.gsub(/^\s*/,"")
      Provision server: 
        - region: #{region}
        - type: #{flavor}
        - image: #{image}
        - security group: #{security_group}
        - keypair: #{keypair}
    EOF
    threads << Thread.new do
      safely do
        unless rehearse?
          log "Provisioning server #{i+1} for bank '#{bank_name}'..."
          server = provision_server
          server.wait_for { ready? }
          provisioned << server
          servers << server.id
        end
      end
    end
  end
  unless rehearse?
    $stdout.print "This may take a few minutes .."
    sleep 1 while threads.any? { |t| $stdout.print "."; $stdout.flush ; t.alive? } 
    $stdout.print "\n"
    provisioned.each do |server|
      log "Server #{server.id} is ready at #{server.dns_name}."
    end
    save
  end
end

#runObject



38
39
40
# File 'lib/befog/commands/add.rb', line 38

def run        
  provision_servers(options[:count].to_i)
end