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

#availableObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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}"
  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(' ')
end

#destroyObject



68
69
70
71
72
73
74
75
76
# 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

#listObject



58
59
60
61
62
63
64
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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)
    unless name.present?
      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"
    else
      if options.backup
        tag = "#{k}:#{options.tag}"
        say "#{msg} tagging as #{tag}"
        ssh(options.remote, options.user, "docker tag #{name} #{tag}")
      end
      say "#{msg} pushing"
      ssh(options.remote, options.user, 'docker load', "docker save #{name}")
    end
  end
end

#start(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dockit/digitalocean.rb', line 117

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