Class: DO
- Inherits:
-
Thor
- Object
- Thor
- DO
- 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
- #available ⇒ Object
- #create ⇒ Object
- #destroy ⇒ Object
- #list ⇒ Object
- #push(*args) ⇒ Object
- #start(name) ⇒ Object
Class Method Details
.remote_required?(extra_cmds = []) ⇒ 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
#available ⇒ Object
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 && .all || i.name =~ /^Docker/ end.map do |i| format(f, i.slug, i.name, i.regions.join(',')) end.sort.join("\n") ) end |
#create ⇒ Object
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(.remote) say "Droplet #{.remote} exists. Please destroy it first.", :red exit 1 end say "creating droplet: #{.remote}" d = client.droplets.create(DropletKit::Droplet.new( name: .remote, region: .region, size: [:size], image: [:image], ssh_keys: client.ssh_keys.all.collect(&:id))) say [d.id, d.status, d.name].join(' ') end |
#destroy ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/dockit/digitalocean.rb', line 68 def destroy force = [:force] say "Destroying droplet: #{.remote}", force ? :red : nil if force || yes?("Are you sure?", :red) client.droplets.delete(id: find(.remote).id) else say "#{.remote} not destroyed", :red end end |
#list ⇒ Object
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 #{.remote} as #{.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(.remote, .user, "docker images --no-trunc | grep #{id} > /dev/null") say ". #{msg} exists" else if .backup tag = "#{k}:#{.tag}" say "#{msg} tagging as #{tag}" ssh(.remote, .user, "docker tag #{name} #{tag}") end say "#{msg} pushing" ssh(.remote, .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 << ([:vars]||{}).collect { |k,v| ['-e', "#{k}='#{v}'"]} env << ['-e', "ENV='#{.env}'"] cmd = ['docker', 'run', env, links, binds].join(' ') ssh(.remote, .user, cmd) end |