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.



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

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

Class Method Details

.create(name) ⇒ Object



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

def self.create(name)
  puts "Creating DigitalOcean droplet..."
  new DigitalOcean::Droplet.new(droplet_options uniquify(name))
end

.droplet_options(name) ⇒ Object



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

def self.droplet_options(name)
  raise "Error: DIGITALOCEAN_API_TOKEN must be set." unless ENV["DIGITALOCEAN_API_TOKEN"]
  {
    image: "docker",
    key_data: key_data,
    name: name,
    region: "nyc3",
    size: "512mb",
    token: ENV["DIGITALOCEAN_API_TOKEN"],
  }
end

.key_dataObject



55
56
57
58
59
# File 'lib/conjure/provision/server.rb', line 55

def self.key_data
  ssh_dir = File.expand_path "~/.ssh"
  raise "Error: ~/.ssh/id_rsa.pub must exist." unless File.exist?(ssh_dir) && File.exist?("#{ssh_dir}/id_rsa.pub")
  File.read "#{ssh_dir}/id_rsa.pub"
end

.uniquify(server_name) ⇒ Object



61
62
63
# File 'lib/conjure/provision/server.rb', line 61

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

Instance Method Details

#install_swapObject



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

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



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

def ip_address
  @server.ip_address
end

#run(command) ⇒ Object



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

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

#send_file(local_name, remote_name) ⇒ Object



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

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

#shell_escape_single_quotes(command) ⇒ Object



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

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

#ssh_optionsObject



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

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

#with_directory(local_path, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/conjure/provision/server.rb', line 65

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