Class: Default

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

Constant Summary collapse

DOCKIT_FILE =
'./Dockit.yaml'.freeze
@@root_echoed =
false

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Default

Returns a new instance of Default.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dockit/cli.rb', line 64

def initialize(*args)
  super
  ENV['DOCKER_HOST'] = options.host

  # passed to Excon, default is 60sec,
  Docker.options[:read_timeout] = options.timeout

  unless @@root_echoed
    say "Project root: #{project_root}", :red
    @@root_echoed=true
  end
end

Instance Method Details

#build(service = nil) ⇒ Object



201
202
203
204
205
# File 'lib/dockit/cli.rb', line 201

def build(service=nil)
  exec(service) do |s|
    s.build()
  end
end

#cleanupObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dockit/cli.rb', line 159

def cleanup
  Dockit::Container.clean(
    force: options['force-containers'], except: options['except-containers']
  ) if options[:containers]

  Dockit::Image.clean(
    force: options['force-images'], except: options['except-images']
  ) if options[:images]

  if options[:volumes] && Docker.version['ApiVersion'].to_f >= 1.21
    Dockit::Volume.clean
  else
    say "Volumes not supported on API versions < 1.21", :red
  end
end

#config(service = nil) ⇒ Object



176
177
178
179
180
# File 'lib/dockit/cli.rb', line 176

def config(service=nil)
  exec(service) do |s|
    say s.config.instance_variable_get('@config').to_yaml
  end
end

#git_build(service = nil) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/dockit/cli.rb', line 229

def git_build(service=nil)
  exec(service) do |s|
    unless repos = s.config.get(:repos)
      say "'repos' not defined in config file. Exiting…", :red
      exit 1
    end
    path    = s.config.get(:repos_path)
    treeish = unless path.nil? || path.empty?
                "#{options.branch}:#{path}"
              else
                options.branch
              end
    say "Exporting in #{Dir.pwd}", :green
    say "<- #{repos} #{treeish}", :green

    if options.package
      unless packages = s.config.get(:package)
        say "'packages' not defined in config file. Exiting…", :red
        exit 1
      end

      say '-> package.tar.gz', :green
      export(repos, treeish, 'package.tar.gz', packages)
    end

    say '-> repos.tar.gz', :green
    export(repos, treeish, 'repos.tar.gz')

    hash = `git rev-parse #{options.branch}`.chomp
    say "Creating '.branch' tag file (#{options.branch}:#{hash})", :blue
    File.write('.branch', "#{options.branch}:#{hash}\n")

    s.build
  end
end

#help(*args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/dockit/cli.rb', line 91

def help(*args)
  super
  if args.count < 1
    say "Run 'dockit list' to see the complete list of SERVICEs."
    say "Run 'dockit help COMMAND' to see command specific options."
  end
end

#listObject



106
107
108
109
# File 'lib/dockit/cli.rb', line 106

def list
  _list :modules
  _list :services
end

#pull(registry, service = nil) ⇒ Object



194
195
196
197
198
# File 'lib/dockit/cli.rb', line 194

def pull(registry, service=nil)
  exec(service) do |s|
    s.pull(registry, options[:tag], options[:force])
  end
end

#push(registry, service = nil) ⇒ Object



185
186
187
188
189
# File 'lib/dockit/cli.rb', line 185

def push(registry, service=nil)
  exec(service) do |s|
    s.push(registry, options[:tag], options[:force])
  end
end

#sh(service = nil, *cmd) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dockit/cli.rb', line 123

def sh(service=nil, *cmd)
  exec(service) do |s|
    cmd  = %w[sh -l] if cmd.empty?
    name = ['exec', cmd.first, s.name].join('-')

    # in case image has an entrypoint, use the cmd as the entrypoint
    (entrypoint, *cmd) = cmd
    say "Starting #{name} with #{entrypoint} #{cmd}", :green
    s.start(
      transient: true,
      verbose: options.verbose,
      create: {
        Entrypoint: [entrypoint],
        Cmd: cmd,
        name: name,
        Tty: true,
        AttachStdin: true,
        AttachStdout: true,
        AttachStderr: true,
        OpenStdin: true,
        StdinOnce: true,
      })
  end
end

#start(service = nil, *cmd) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/dockit/cli.rb', line 114

def start(service=nil, *cmd)
  opts = options.merge(create: { tty: options[:transient] })
  opts[:create][:Cmd] = cmd unless cmd.empty?
  exec(service) do |s|
    s.start(opts)
  end
end

#versionObject



100
101
102
# File 'lib/dockit/cli.rb', line 100

def version
  say "Dockit version #{Dockit::VERSION}"
end