class DO

Constants

REMOTE_CMDS
USERNAME

Public Class Methods

remote_required?(extra_cmds=[]) click to toggle source
# File lib/dockit/digitalocean.rb, line 9
def self.remote_required?(extra_cmds=[])
  ARGV[0] == 'do' && (
    REMOTE_CMDS.include?(ARGV[1]) || extra_cmds.include?(ARGV[1]))
end

Public Instance Methods

available() click to toggle source
# File lib/dockit/digitalocean.rb, line 43
def available
  f = '%-20.20s %-25.25s %s'
  say format(f, 'slug', 'name', 'regions')
  say format(f, '_' * 20, '_' * 25, '_' * 30)

  say(
    client.images.all.select do |i|
      i.slug && options.all || i.name =~ /^Docker/
    end.map do |i|
      format(f, i.slug, i.name, i.regions.join(','))
    end.sort.join("\n")
  )
end
create() click to toggle source
# File lib/dockit/digitalocean.rb, line 26
def create
  if find(options.remote)
    say  "Droplet #{options.remote} exists. Please destroy it first.", :red
    exit 1
  end
  say "creating droplet: #{options.remote}", :green
  d = client.droplets.create(DropletKit::Droplet.new(
                          name: options.remote,
                          region: options.region,
                          size: options[:size],
                          image: options[:image],
                          ssh_keys: client.ssh_keys.all.collect(&:id)))
  say [d.id, d.status, d.name].join(' '), :blue
end
destroy() click to toggle source
# File lib/dockit/digitalocean.rb, line 68
def destroy
  force = options[:force]
  say "Destroying droplet: #{options.remote}", force ? :red : nil
  if force || yes?("Are you sure?", :red)
    client.droplets.delete(id: find(options.remote).id)
  else
    say "#{options.remote} not destroyed", :red
  end
end
list() click to toggle source
# File lib/dockit/digitalocean.rb, line 58
def list
  l = client.droplets.all.collect do |d|
    [d.id, d.name, d.status, d.networks[:v4].first.ip_address]
  end
  l.unshift %w[id name status ip]
  print_table l
end
push(*args) click to toggle source
# File lib/dockit/digitalocean.rb, line 82
def push(*args)
  args = dockit.services.keys if args.empty?
  say "Pushing to #{options.remote} as #{options.user}", :green
  say "Processing images for #{args}"
  args.each do |k|
    s = service(k)
    unless s.image
      say ". #{k}: No image!", :red
      next
    end
    name = s.config.get(:build, :t)
    if name.empty?
      say ". #{k}: not a local build", :red
      next
    end
    id  = s.image.id
    msg = "#{k}(#{id[0..11]}[#{name}]):"
    if ssh(options.remote, options.user,
           "docker images --no-trunc | grep #{id} > /dev/null")
      say ". #{msg} exists", :blue
    else
      if options.backup
        tag = "#{name}:#{options.tag}"
        say "#{msg} tagging as #{tag}", :green
        ssh(options.remote, options.user, "docker tag #{name} #{tag}")
      end
      say "#{msg} pushing", :green
      ssh(options.remote, options.user, 'docker load', "docker save #{name}")
    end
  end
end
start(name) click to toggle source
# File lib/dockit/digitalocean.rb, line 117
def start(name)
  s     = service(name)
  links = config(s, :run,    :Links, 'l')
  binds = config(s, :run,    :Binds, 'v')

  env = config(s, :create, :Env, 'e')
  env << (options[:vars] || {}).collect { |k, v| ['-e', "#{k}='#{v}'"] }
  env << ['-e', "ENV='#{options.env}'"]

  cmd = ['docker', 'run', env, links, binds].join(' ')
  ssh(options.remote, options.user, cmd)
end

Private Instance Methods

client() click to toggle source
# File lib/dockit/digitalocean.rb, line 136
def client
  @client ||= DropletKit::Client.new(
    access_token: File.read(File.join(Dir.home, '.digitalocean', options.token)))
end
config(service, phase, key, flag) click to toggle source
# File lib/dockit/digitalocean.rb, line 166
def config(service, phase, key, flag)
  (service.config.get(phase, key)||[]).collect { |v| ["-#{flag}", "'#{v}'"] }
end
dockit() click to toggle source
# File lib/dockit/digitalocean.rb, line 147
def dockit
  @dockit ||= Dockit::Env.new
end
find(hostname) click to toggle source
# File lib/dockit/digitalocean.rb, line 141
def find(hostname)
  client.droplets.all.find do |d|
    d.name == hostname
  end
end
service(name) click to toggle source
# File lib/dockit/digitalocean.rb, line 151
def service(name)
  Dockit::Service.new(
    service_file(name),
    locals: {env: options.env || ''}.merge(options[:locals]||{}))
end
service_file(name) click to toggle source
# File lib/dockit/digitalocean.rb, line 157
def service_file(name)
  file = dockit.services[name]
  unless file
    say "Service '#{name}' does not exist!", :red
    exit 1
  end
  file
end
ssh(host, user, cmd, src=nil) click to toggle source
# File lib/dockit/digitalocean.rb, line 131
def ssh(host, user, cmd, src=nil)
  src << '|' if src
  system("#{src}ssh #{user}@#{host} #{cmd}")
end