Class: Conjure::Server

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ip_addressObject

Returns the value of attribute ip_address

Returns:

  • (Object)

    the current value of ip_address



4
5
6
# File 'lib/conjure/server.rb', line 4

def ip_address
  @ip_address
end

Class Method Details

.create(name_prefix, options = {}) ⇒ Object



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

def self.create(name_prefix, options = {})
  new DigitalOcean::Droplet.new(droplet_options(name_prefix, options)).ip_address
end

.droplet_options(name_prefix, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/conjure/server.rb', line 25

def self.droplet_options(name_prefix, options = {})
  {
    image: "docker",
    key_data: key_data,
    name_prefix: name_prefix,
    region: "nyc3",
    size: (options[:instance_size] || "512mb"),
  }
end

.key_dataObject



35
36
37
38
39
# File 'lib/conjure/server.rb', line 35

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

.ssh_optionsObject



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

def self.ssh_options
  "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR"
end

Instance Method Details

#quote_command(command) ⇒ Object



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

def quote_command(command)
  "'" + command.gsub("'", "'\"'\"'") + "'"
end

#run(command) ⇒ Object



5
6
7
# File 'lib/conjure/server.rb', line 5

def run(command)
  `ssh #{self.class.ssh_options} root@#{ip_address} #{quote_command command}`
end

#send_file(local_name, remote_name) ⇒ Object



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

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