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

#buildObject



52
53
54
55
56
57
58
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/cide/cli.rb', line 52

def build
  containers = []

  setup_docker

  ## Config ##
  banner 'Config'
  build = Build::Config.load(Dir.pwd)
  exit 1 if build.nil?
  export_dir = options.export_dir
  export_dir ||= File.dirname(build.export_dir) if build.export_dir
  ssh_key = File.expand_path(options.ssh_key)
  build.run = options.run unless options.run.empty?
  name = CIDE::Docker.id options.name
  tag = "cide/#{name}"
  say_status :config, build.inspect

  ## Build ##
  banner 'Build'
  if build.use_ssh
    unless File.exist?(ssh_key)
      fail ArgumentError, "SSH key #{ssh_key} not found"
    end
    create_tmp_file TEMP_SSH_KEY, File.read(ssh_key)
  end
  create_tmp_file DOCKERFILE, build.to_dockerfile
  build_options = ['--force-rm']
  build_options << '--pull' if options.pull
  build_options.push '-f', DOCKERFILE
  build_options.push '-t', tag
  build_options << '.'
  docker :build, *build_options

  ## CI ##
  banner 'Run'
  build.links.each do |link|
    args = ['--detach']
    link.env.each_pair do |key, value|
      args.push('--env', [key, value].join('='))
    end
    args << link.image
    args << link.run if link.run
    link.id = docker(:run, *args, capture: true).strip
    containers << link.id
  end

  run_options = ['--detach']

  build.env.each_pair do |key, value|
    run_options.push '--env', [key, value].join('=')
  end

  build.links.each do |link|
    run_options.push '--link', [link.id, link.name].join(':')
  end

  id = SecureRandom.hex
  run_options.push '--name', id

  run_options.push tag
  run_options.push(*build.run)

  containers << id
  docker(:run, *run_options, capture: true).strip
  docker(:attach, id)

  say_status :status, 'SUCCESS', :green

  ## Export ##
  return unless options.export
  banner 'Export'
  fail 'export flag set but no export_dir given' if build.export_dir.nil?

  guest_export_dir = File.expand_path(build.export_dir, CIDE_SRC_DIR)
  host_export_dir  = File.expand_path(export_dir, Dir.pwd)
  docker :cp, [id, guest_export_dir].join(':'), host_export_dir
rescue Docker::Error => ex
  say_status :status, 'ERROR', :red
  exit ex.exitstatus
ensure
  linked_containers = containers - [id]
  unless linked_containers.empty?
    infos = docker(
      :inspect,
      *linked_containers,
      capture: true,
      verbose: false,
    )
    JSON.parse(infos).each do |info|
      config = info['Config']
      state = info['State']

      next unless state['Dead'] || state['ExitCode'] > 0

      $stderr.puts "=== Failed linked container #{info['Id']} ==="
      $stderr.puts "Image: #{config['Image']}"
      $stderr.puts "State: #{state.inspect}"
      docker(:logs, '--tail', 20, info['Id'])
    end
  end
  # Shutdown old containers
  unless containers.empty?
    docker :rm, '--force', '--volumes', *containers.reverse,
      verbose: false,
      capture: true
  end
end

#cleanObject



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
264
265
266
267
268
269
270
# File 'lib/cide/cli.rb', line 230

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



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
# File 'lib/cide/cli.rb', line 168

def debug
  containers = []

  setup_docker

  ## Config ##
  banner 'Config'
  build = Build::Config.load
  exit 1 if build.nil?
  name = CIDE::Docker.id options.name
  tag = "cide/#{name}"
  say_status :config, build.inspect

  ## CI ##
  banner 'Run'
  build.links.each do |link|
    args = ['--detach']
    link.env.each_pair do |key, value|
      args.push('--env', [key, value].join('='))
    end
    args << link.image
    args << link.run if link.run
    link.id = docker(:run, *args, capture: true).strip
    containers << link.id
  end

  run_options = ['--rm', '-t', '-i']

  run_options.push '--user', options.user

  build.env.each_pair do |key, value|
    run_options.push '--env', [key, value].join('=')
  end

  build.links.each do |link|
    run_options.push '--link', [link.id, link.name].join(':')
  end

  run_options.push tag
  run_options.push 'bash'

  docker(:run, *run_options)
rescue Docker::Error => ex
  exit ex.exitstatus
ensure
  # Shutdown old containers
  unless containers.empty?
    docker :rm, '--force', '--volumes', *containers.reverse,
      verbose: false,
      capture: true
  end
end

#initObject



273
274
275
276
# File 'lib/cide/cli.rb', line 273

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