Class: FuckingShellScripts::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fucking_shell_scripts/server.rb

Constant Summary collapse

NoServerSelected =
Class.new(StandardError)
MissingInstanceID =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options) ⇒ Server

Returns a new instance of Server.



8
9
10
# File 'lib/fucking_shell_scripts/server.rb', line 8

def initialize(connection, options)
  @connection, @options = connection, options
end

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



6
7
8
# File 'lib/fucking_shell_scripts/server.rb', line 6

def server
  @server
end

Instance Method Details

#bootstrapObject



12
13
14
15
# File 'lib/fucking_shell_scripts/server.rb', line 12

def bootstrap
  build
  configure
end

#buildObject



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
# File 'lib/fucking_shell_scripts/server.rb', line 17

def build
  create_server_options = {
    image_id:         options.fetch(:image),
    flavor_id:        options.fetch(:size),
    key_name:         options.fetch(:key_name),
    tags:             { "Name" => name },
    groups:           options.fetch(:security_groups),
    private_key_path: options.fetch(:private_key_path)
  }

  create_server_options.merge!({subnet_id: options[:subnet]}) if options.has_key?(:subnet)
  create_server_options.merge!({block_device_mapping: options[:block_device_mapping]}) if options.has_key?(:block_device_mapping)

  @server = connection.servers.create(create_server_options)
  @server.username = options.fetch(:username) if options[:username]
  $stdout.print "Creating #{options.fetch(:size)} from #{options.fetch(:image)}"

  server.wait_for do
    $stdout.print "."
    ready?
  end

  $stdout.puts "ready!"
  $stdout.puts ""

  $stdout.print "Waiting for ssh access"

  server.wait_for do
    $stdout.print "."
    sshable?
  end

  $stdout.puts "#{server.dns_name} ready!"
end

#configureObject

Raises:



52
53
54
55
56
57
58
# File 'lib/fucking_shell_scripts/server.rb', line 52

def configure
  get(options[:instance_id]) if server.nil?
  raise NoServerSelected, "Unable to find server. Try specifying the server ID." if server.nil?

  FuckingShellScripts::SCP.new(server, options).to_server
  server.ssh(options.fetch(:scripts))
end