Class: CIDE::CLI

Inherits:
Thor
  • Object
show all
Includes:
Docker, Thor::Actions
Defined in:
lib/cide/cli.rb

Overview

Command-line option-parsing and execution for cide

Instance Method Summary collapse

Methods included from Docker

#docker, id

Instance Method Details

#cleanObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cide/cli.rb', line 282

def clean
  setup_docker

  days_to_keep = options[:days]
  max_images = options[:count]

  x = docker('images', '--no-trunc', capture: true)
  iter = x.lines.each
  iter.next
  cide_image_ids = iter
    .map { |line| line.split(/\s+/) }
    .select { |line| line[0] =~ %r{^cide[/-]} || line[0] == '<none>' }
    .map { |line| line[2] }

  if cide_image_ids.empty?
    puts 'No images found to be cleaned'
    return
  end

  x = docker('inspect', *cide_image_ids, capture: true)
  cide_images = JSON.parse(x.strip)
    .each { |image| image['Created'] = Time.iso8601(image['Created']) }
    .sort { |a, b| a['Created'] <=> b['Created'] }

  if cide_images.size > max_images
    old_cide_images = cide_images[0..-max_images]
      .map { |image| image['Id'] }
  else
    old_times = Time.now - (days_to_keep * 24 * 60 * 60)
    old_cide_images = cide_images
      .select { |image| image['Created'] < old_times }
      .map { |image| image['Id'] }
  end

  if old_cide_images.empty?
    puts 'No images found to be cleaned'
    return
  end

  docker('rmi', '--force', *old_cide_images)
end

#debugObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/cide/cli.rb', line 243

def debug
  setup_docker

  tag = name_to_tag options.name

  ## Config ##
  banner 'Config'
  config = ConfigFile.load(Dir.pwd)
  say_status :config, config.inspect

  ## Run ##
  banner 'Run'
  runner = Runner.new(
    command: ['bash'],
    env: config.env,
    links: config.links,
    tag: tag,
    user: options.user,
  )
  runner.run!(interactive: true)

rescue Docker::Error => ex
  exit ex.exitstatus
rescue RuntimeError => ex
  $stderr.puts ex.to_s
  exit 1
ensure
  runner.cleanup! if runner
end

#execObject



59
60
61
62
63
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cide/cli.rb', line 59

def exec
  setup_docker

  tag = name_to_tag options.name

  banner 'Config'
  config = ConfigFile.load(Dir.pwd)
  say_status :config, config.inspect

  ## Build ##
  banner 'Build'
  builder = Builder.new(config)
  builder.build(
    pull: options.pull,
    ssh_key: File.expand_path(options.ssh_key),
    tag: tag,
  )

  ## Run ##
  banner 'Run'

  command = options.run ? ['sh', '-e', '-c', options.run] : config.run

  runner = Runner.new(
    command: command,
    env: config.env,
    links: config.links,
    tag: tag,
  )
  runner.run!

  ## Export ##
  return unless options.export
  banner 'Export'
  runner.export!(
    guest_dir: options.guest_export_dir || config.export_dir,
    host_dir: options.export_dir || config.export_dir,
  )
rescue Docker::Error => ex
  exit ex.exitstatus
rescue RuntimeError => ex
  $stderr.puts ex.to_s
  exit 1
ensure
  runner.cleanup! if runner
end

#initObject



325
326
327
328
# File 'lib/cide/cli.rb', line 325

def init
  puts "Creating #{CONFIG_FILES.first} with default values"
  create_file CONFIG_FILES.first, File.read(DEFAULT_CIDEFILE)
end

#packageObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cide/cli.rb', line 136

def package
  fail 'missing AWS_BUCKET' if options.upload && !options.aws_bucket

  tag = name_to_tag options.name

  build_root = File.expand_path('.package')
  guest_export_dir = '/cide/package'
  host_export_dir  = File.join(build_root, 'package')

  FileUtils.rm_rf(build_root)

  build_id = options.build_id || (
    timestamp = Time.now.strftime('%Y-%m-%d_%H%M%S')
    git_branch = `git symbolic-ref --short -q HEAD || echo unknown`.strip
    git_rev = `git rev-parse --short HEAD`.strip
    "#{timestamp}.#{git_branch}-#{git_rev}"
  )

  tar_name = "#{options.package}.#{build_id}.tar.gz"
  tar_path = File.join(build_root, tar_name)

  banner 'Config'
  config = ConfigFile.load(Dir.pwd)
  say_status :config, config.inspect

  version_data =
    case config.package && config.package.add_version
    when 'sha'
      `git rev-parse HEAD`.strip
    when 'short_sha'
      `git rev-parse --short HEAD`.strip
    when 'auto'
      build_id
    end

  ## Build ##
  banner 'Build'
  builder = Builder.new(config)
  builder.build(
    pull: options.pull,
    ssh_key: File.expand_path(options.ssh_key),
    tag: tag,
  )

  ## Run ##
  banner 'Run'
  runner = Runner.new(
    command: ['script/build', guest_export_dir],
    env: config.env,
    links: config.links,
    tag: tag,
  )
  runner.run!

  ## Export ##
  banner 'Export'

  runner.export!(
    guest_dir: guest_export_dir,
    host_dir: host_export_dir,
  )

  ## Set version ##
  if version_data
    version_file = File.join(host_export_dir, '.packager', 'version')
    FileUtils.mkdir_p(File.dirname(version_file))
    File.open(version_file, 'w') do |f|
      f.puts version_data
    end
  end

  # Create archive
  puts "Package: #{tar_name}"
  system('tar', '-czf', tar_path, '-C', host_export_dir, '.')

  ## Upload ##

  return unless options.upload
  banner 'Upload'

  require 'aws-sdk'
  s3 = Aws::S3::Client.new
  resp = s3.put_object(
    bucket: options.aws_bucket,
    key: tar_name,
    body: File.open(tar_path),
  )
  p resp
rescue Docker::Error => ex
  exit ex.exitstatus
rescue RuntimeError => ex
  $stderr.puts ex.to_s
  exit 1
ensure
  FileUtils.rm_rf(build_root) if options.upload

  runner.cleanup! if runner
end