Class: DO

Inherits:
Thor
  • Object
show all
Defined in:
lib/dockit/digitalocean.rb

Constant Summary collapse

USERNAME =
'root'.freeze
REMOTE_CMDS =
%w[start push create].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.remote_required?(extra_cmds = []) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# 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

Instance Method Details

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dockit/digitalocean.rb', line 25

def create
  if find(options.remote)
    say  "Droplet #{options.remote} exists. Please destroy it first.", :red
    exit 1
  end
  say "creating droplet: #{options.remote}"
  d = client.droplets.create(DropletKit::Droplet.new(
                          name: options.remote,
                          region: options.region,
                          size: options[:size],
                          image: 'docker',
                          ssh_keys: client.ssh_keys.all.collect(&:id)))
  say  [d.id, d.status, d.name].join(' ')
end

#destroyObject



51
52
53
54
55
56
57
58
59
# File 'lib/dockit/digitalocean.rb', line 51

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

#listObject



41
42
43
44
45
46
47
# File 'lib/dockit/digitalocean.rb', line 41

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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dockit/digitalocean.rb', line 65

def push(*args)
  args = dockit.services.keys if args.empty?
  say "Processing images for #{args}"
  args.each do |k|
    s = service(k)
    unless s.image
      say ". No image for #{k}!", :red
      next
    end
    name    = s.config.get(:build, :t)
    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 ". Exists #{msg}"
    else
      if options.backup
        tag = "#{k}:#{options.tag}"
        say "Tagging #{name} as #{tag}"
        ssh(options.remote, options.user, "docker tag #{name} #{tag}")
      end
      say ". Pushing #{msg}"
      ssh(options.remote, options.user, 'docker load', "docker save #{name}")
    end
  end
end

#start(name) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dockit/digitalocean.rb', line 95

def start(name)
  s     = service(name)
  name  = s.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