class Shipment::Server::Initializer

Attributes

droplet[RW]
ip_address[RW]
repo[R]
repo_name[R]
repo_url[R]
repo_user[R]

Public Class Methods

new(repo) click to toggle source
# File lib/shipment/server/initializer.rb, line 19
def initialize(repo)
  @repo = repo
  @repo_url, @repo_name, @repo_user = repo.url, repo.name, repo.user
  netrc = Netrc.read
  Digitalocean.client_id, Digitalocean.api_key = netrc["shipment.do"]
end
spin_up(repo) click to toggle source
# File lib/shipment/server/initializer.rb, line 15
def self.spin_up(repo)
  new(repo).spin_up
end

Public Instance Methods

create_droplet() click to toggle source
# File lib/shipment/server/initializer.rb, line 37
def create_droplet
  print "-----> ".green + "Creating Droplet: #{repo_name}"
  self.droplet = Digitalocean::Droplet.create({
    name: repo_name,
    size_id: get_size_id,
    image_id: get_image_id,
    region_id: get_region_id,
    ssh_key_ids: [get_ssh_key_id]
  }).droplet
  print_waiting
end
droplet_created?() click to toggle source
# File lib/shipment/server/initializer.rb, line 98
def droplet_created?
  !!(remote_droplet.status == "active")
end
get_image_id() click to toggle source
# File lib/shipment/server/initializer.rb, line 86
def get_image_id
  Digitalocean::Image.all.images.detect {|image| image.name.match(/Docker/)}.id
end
get_ip_address() click to toggle source
# File lib/shipment/server/initializer.rb, line 102
def get_ip_address
  remote_droplet.ip_address
end
get_region_id() click to toggle source
# File lib/shipment/server/initializer.rb, line 90
def get_region_id
  Digitalocean::Region.all.regions.detect {|region| region.slug == "nyc2"}.id
end
get_size_id() click to toggle source
# File lib/shipment/server/initializer.rb, line 82
def get_size_id
  Digitalocean::Size.all.sizes.detect {|size| size.slug == "2gb"}.id
end
get_ssh_key_id() click to toggle source
# File lib/shipment/server/initializer.rb, line 94
def get_ssh_key_id
  Digitalocean::SshKey.all.ssh_keys.detect {|key| key.name == "shipment"}.id
end
print_waiting() click to toggle source
remote_droplet() click to toggle source
# File lib/shipment/server/initializer.rb, line 106
def remote_droplet
  Digitalocean::Droplet.find(droplet.id).droplet
end
spin_up() click to toggle source
# File lib/shipment/server/initializer.rb, line 26
def spin_up
  create_droplet
  store_droplet_data
  update_ssh_config
  Shipment::Project::Customizer.customize
  Shipment::Server::SSHClient.setup(
    repo: repo,
    ip_address: ip_address
  )
end
store_droplet_data() click to toggle source
# File lib/shipment/server/initializer.rb, line 49
def store_droplet_data
  puts "-----> ".green + "Storing droplet data..."
  File.open("#{ENV['HOME']}/.shipment", "a") do |f|
    f.write "#{droplet.id} : #{repo_name} : #{ip_address}\n"
  end

  puts "-----> ".green + "Creating .shipment file..."
  yaml = {
    id: droplet.id,
    ip_address: ip_address,
    name: repo_name,
    user: repo_user,
    url: repo_url,
    secret: `rake secret`.strip
  }.to_yaml
  File.open(File.join(FileUtils.pwd, ".shipment"), "w+") do |f|
    f.write yaml
  end
end
update_ssh_config() click to toggle source
# File lib/shipment/server/initializer.rb, line 69
      def update_ssh_config
        puts "-----> ".green + "Setting up SSH config..."
        File.open("#{ENV['HOME']}/.ssh/config", "a") do |f|
          f.write <<-SSHCONFIG.gsub(/^ {10}/,'')

          Host #{ip_address}
          Hostname #{ip_address}
          IdentityFile #{ENV['HOME']}/.ssh/shipment_rsa
          User root
          SSHCONFIG
        end
      end