Class: Conjure::Provision::Server
- Inherits:
-
Object
- Object
- Conjure::Provision::Server
- Defined in:
- lib/conjure/provision/server.rb
Class Method Summary collapse
- .create(name) ⇒ Object
- .droplet_options(name) ⇒ Object
- .key_data ⇒ Object
- .uniquify(server_name) ⇒ Object
Instance Method Summary collapse
-
#initialize(server) ⇒ Server
constructor
A new instance of Server.
- #install_swap ⇒ Object
- #ip_address ⇒ Object
- #run(command) ⇒ Object
- #send_file(local_name, remote_name) ⇒ Object
- #shell_escape_single_quotes(command) ⇒ Object
- #ssh_options ⇒ Object
- #with_directory(local_path, &block) ⇒ Object
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( 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.(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_data ⇒ Object
55 56 57 58 59 |
# File 'lib/conjure/provision/server.rb', line 55 def self.key_data ssh_dir = File. "~/.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_swap ⇒ Object
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_address ⇒ Object
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 #{} 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 #{} #{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_options ⇒ Object
25 26 27 |
# File 'lib/conjure/provision/server.rb', line 25 def "-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 |