Class: Conjure::Provision::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ Server

Returns a new instance of Server.



6
7
8
9
10
# File 'lib/conjure/provision/server.rb', line 6

def initialize(server)
  @server = server
  puts "Configuring droplet..."
  install_swap
end

Class Method Details

.bootstrap_options(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/conjure/provision/server.rb', line 53

def self.bootstrap_options(name)
  ssh_dir = File.expand_path("~/.ssh")
  raise "Error: ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub must exist." unless File.exist?(ssh_dir) && File.exist?("#{ssh_dir}/id_rsa") && File.exist?("#{ssh_dir}/id_rsa.pub")
  {
    :name => name,
    :flavor_id =>  "66",
    :region_id => "4",
    :image_id => "5506141",
    :private_key_path => "#{ssh_dir}/id_rsa",
    :public_key_path => "#{ssh_dir}/id_rsa.pub",
  }
end

.compute_optionsObject



44
45
46
47
48
49
50
51
# File 'lib/conjure/provision/server.rb', line 44

def self.compute_options
  raise "Error: DIGITALOCEAN_API_KEY and DIGITALOCEAN_CLIENT_ID env vars must both be set." unless ENV["DIGITALOCEAN_API_KEY"] && ENV["DIGITALOCEAN_CLIENT_ID"]
  {
    :provider => :digitalocean,
    :digitalocean_api_key => ENV["DIGITALOCEAN_API_KEY"],
    :digitalocean_client_id => ENV["DIGITALOCEAN_CLIENT_ID"],
  }
end

.create(name) ⇒ Object



37
38
39
40
41
42
# File 'lib/conjure/provision/server.rb', line 37

def self.create(name)
  puts "Creating DigitalOcean droplet..."
  connection = Fog::Compute.new compute_options
  delete_default_key connection
  new connection.servers.bootstrap(bootstrap_options uniquify(name))
end

.delete_default_key(connection) ⇒ Object



66
67
68
# File 'lib/conjure/provision/server.rb', line 66

def self.delete_default_key(connection)
  connection.ssh_keys.find{|k| k.name=="fog_default"}.try :destroy
end

.uniquify(server_name) ⇒ Object



70
71
72
# File 'lib/conjure/provision/server.rb', line 70

def self.uniquify(server_name)
  "#{server_name}-#{SecureRandom.hex 4}"
end

Instance Method Details

#install_swapObject



32
33
34
35
# File 'lib/conjure/provision/server.rb', line 32

def install_swap
  run "dd if=/dev/zero of=/root/swapfile bs=1024 count=524288"
  run "mkswap /root/swapfile; swapon /root/swapfile"
end

#ip_addressObject



12
13
14
# File 'lib/conjure/provision/server.rb', line 12

def ip_address
  @server.public_ip_address
end

#run(command) ⇒ Object



16
17
18
# File 'lib/conjure/provision/server.rb', line 16

def run(command)
  `ssh #{ssh_options} root@#{ip_address} '#{shell_escape_single_quotes command}'`
end

#send_file(local_name, remote_name) ⇒ Object



20
21
22
# File 'lib/conjure/provision/server.rb', line 20

def send_file(local_name, remote_name)
  `scp #{ssh_options} #{local_name} root@#{ip_address}:#{remote_name}`
end

#shell_escape_single_quotes(command) ⇒ Object



28
29
30
# File 'lib/conjure/provision/server.rb', line 28

def shell_escape_single_quotes(command)
  command.gsub("'", "'\"'\"'")
end

#ssh_optionsObject



24
25
26
# File 'lib/conjure/provision/server.rb', line 24

def ssh_options
  "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
end

#with_directory(local_path, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/conjure/provision/server.rb', line 74

def with_directory(local_path, &block)
  local_archive = remote_archive = "/tmp/archive.tar.gz"
  remote_path = "/tmp/unpacked_archive"
  `cd #{local_path}; tar czf #{local_archive} *`
  send_file local_archive, remote_archive
  run "mkdir #{remote_path}; cd #{remote_path}; tar mxzf #{remote_archive}"
  yield remote_path
ensure
  `rm #{local_archive}`
  run "rm -Rf #{remote_path} #{remote_archive}"
end