Class: Migrant::Clouds::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/migrant/clouds/base.rb

Overview

Base class for cloud service providers. All service providers use Fog to handle bootstrapping servers and running stuff Cloud service providers are responsible for starting up the server, setting up ssh access with the provided keypair, and (eventually) disabling the root user.

Direct Known Subclasses

AWS, Rackspace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
# File 'lib/migrant/clouds/base.rb', line 22

def initialize(env)
  @environment = env
  @server_def = {:private_key_path => @environment.setting('ssh.private_key'),
    :public_key_path => @environment.setting('ssh.public_key')}
  flavor_id = @environment.setting('provider.flavor_id')
  @server_def[:flavor_id] = flavor_id unless flavor_id.nil?
  image_id = @environment.setting('provider.image_id')
  @server_def[:image_id] = image_id unless image_id.nil?
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



32
33
34
# File 'lib/migrant/clouds/base.rb', line 32

def connection
  @connection
end

Class Method Details

.register(shortcut) ⇒ Object

Register a provider



12
13
14
15
# File 'lib/migrant/clouds/base.rb', line 12

def self.register(shortcut)
  @@clouds ||= {}
  @@clouds[shortcut] = self
end

.registered(shortcut) ⇒ Object

Get a registered provider



18
19
20
# File 'lib/migrant/clouds/base.rb', line 18

def self.registered(shortcut)
  @@clouds[shortcut]
end

Instance Method Details

#bootstrap_serverObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/migrant/clouds/base.rb', line 38

def bootstrap_server
  @environment.ui.info "Launching Server..."
  @environment.server = @connection.servers.bootstrap(@server_def)
  @environment.ui.notice "Server Launched!"
  ip_address = @environment.setting('provider.ip_address')
  unless ip_address.nil?
    @connection.associate_address(@environment.server.id,ip_address)
  end
  log_server_info
end

#connectObject



34
35
36
# File 'lib/migrant/clouds/base.rb', line 34

def connect
  raise "Invalid Action for Base Class"
end

#connect_to_server(box) ⇒ Object

Set up connection information for a server and set up SSH keypair access

box - Migrant::Box object with information about the server to connect to

Does not return anything but sets up @environment.server



58
59
60
61
62
63
64
65
66
# File 'lib/migrant/clouds/base.rb', line 58

def connect_to_server(box)
  @environment.ui.notice "Connecting to #{box.provider.to_s} server: #{box.id}"
  @environment.server = @connection.servers.get(box.id)
  if (@environment.server.nil?)
    @environment.ui.error "Cannot connect to server!"
    raise "Cannot find server"
  end
  @environment.server.merge_attributes(@server_def)
end

#destroy(box) ⇒ Object

Asks the user if they want to shut down a box

box - A Migrant::Box with information about the box

returns true if the user decided to shut down the box



88
89
90
91
92
93
94
95
96
# File 'lib/migrant/clouds/base.rb', line 88

def destroy(box)
  if (@environment.ui.yes?("Are you sure you want to shut down box #{box.name}?",:red))
    @environment.ui.notice "Shutting down box #{box.name} id: #{box.id}"
    connect
    @connection.servers.get(box.id).destroy
    return true
  end
  return false
end

#execute(commands) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/migrant/clouds/base.rb', line 68

def execute(commands)
  server = @environment.server
  commands.each do |cmd|
    @environment.ui.info("$ #{cmd}")
    result = server.ssh cmd
    if (result[0].status != 0)
      @environment.ui.info(result[0].stdout)
      @environment.ui.error "Error executing command: #{cmd}\n#{result.inspect}"
      raise "Remote Script Error"
    end
    @environment.ui.info(result[0].stdout)
    @environment.ui.warn(result[0].stderr) if result[0].stderr
  end
end

#log_server_infoObject



49
50
51
# File 'lib/migrant/clouds/base.rb', line 49

def log_server_info
  @environment.ui.notice "  ID:         #{@environment.server.id}"
end