Class: OsCtl::Image::Cli::Image

Inherits:
Command
  • Object
show all
Defined in:
lib/osctl/image/cli/image.rb

Constant Summary collapse

FIELDS =
%i(name distribution version arch vendor variant)

Instance Attribute Summary

Attributes inherited from Command

#args, #gopts, #opts

Instance Method Summary collapse

Methods inherited from Command

#initialize, #require_args!, run

Constructor Details

This class inherits a constructor from OsCtl::Image::Cli::Command

Instance Method Details

#buildObject



39
40
41
42
43
44
# File 'lib/osctl/image/cli/image.rb', line 39

def build
  require_args!('image')

  results, _ = build_images(select_images(args[0]))
  process_build_results(results)
end

#build_images(images, rebuild: true) ⇒ Object (protected)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/osctl/image/cli/image.rb', line 137

def build_images(images, rebuild: true)
  cached = []
  op = Operations::Execution::Parallel.new(opts[:jobs])

  images.each do |tpl|
    build = Operations::Image::Build.new(
      File.absolute_path('.'),
      tpl,
      output_dir: opts['output-dir'],
      build_dataset: opts['build-dataset'],
      vendor: opts[:vendor],
    )

    if rebuild || !build.cached?
      op.add(tpl) { build.execute }
    else
      cached << build
    end
  end

  puts 'Building images...'
  results = op.execute
  [results, cached]
end

#deployObject



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
# File 'lib/osctl/image/cli/image.rb', line 74

def deploy
  require_args!('image', 'repository')

  unchanged = false

  # Build images
  images = select_images(args[0])
  build_results, cached_builds = build_images(
    select_images(args[0]),
    rebuild: opts[:rebuild],
  )
  process_build_results(build_results)

  successful_builds =
    build_results.select(&:status).map(&:return_value) \
    + \
    cached_builds

  fail 'no images to test and deploy' if successful_builds.empty?

  if opts['skip-tests']
    puts 'Skipping tests'
    verified_builds = successful_builds
  else
    # Test successfully built images
    tests = TestList.new('.')
    test_results = []

    puts 'Testing images'

    verified_builds = successful_builds.select do |build|
      if image_in_repo_unchanged?(build, args[1])
        unchanged = true
        next false
      end

      results = test_images([build.image], tests, rebuild: false)
      test_results.concat(results)
      results.all?(&:success?)
    end

    process_test_results(test_results)
  end

  if verified_builds.empty?
    if unchanged
      puts 'no images to deploy'
    else
      fail 'no images to deploy'
    end

    return
  end

  # Deploy verified images
  puts 'Deploying images'

  verified_builds.each do |build|
    Operations::Image::Deploy.run(build, args[1], tags: opts[:tag])
  end
end

#image_in_repo_unchanged?(build, repo) ⇒ Boolean (protected)

Parameters:

Returns:

  • (Boolean)


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/osctl/image/cli/image.rb', line 221

def image_in_repo_unchanged?(build, repo)
  img = build.image
  path = Operations::Repository::GetImagePath.run(
    repo,
    {
      distribution: img.distribution,
      version: img.version,
      arch: img.arch,
      variant: img.variant,
      vendor: img.vendor,
    },
    :zfs,
  )

  Operations::File::Compare.run(path, build.output_stream)

rescue OperationError
  false
end

#image_listObject (protected)



273
274
275
# File 'lib/osctl/image/cli/image.rb', line 273

def image_list
  ImageList.new('.')
end

#instantiateObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/osctl/image/cli/image.rb', line 55

def instantiate
  require_args!('image')

  image = image_list.detect { |t| t.name == args[0] }
  fail "image '#{args[0]}' not found" unless image

  ctid = Operations::Image::Instantiate.run(
    File.absolute_path('.'),
    image,
    output_dir: opts['output-dir'],
    build_dataset: opts['build-dataset'],
    vendor: opts[:vendor],
    rebuild: opts[:rebuild],
    ctid: opts[:container],
  )

  puts "Container ID: #{ctid}"
end

#listObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/osctl/image/cli/image.rb', line 9

def list
  if opts[:list]
    puts FIELDS.join("\n")
    return
  end

  tpls = image_list.map do |tpl|
    tpl.load_config

    {
      name: tpl.name,
      distribution: tpl.distribution,
      version: tpl.version,
      arch: tpl.arch,
      vendor: tpl.vendor,
      variant: tpl.variant,
    }
  end

  fmt_opts = {
    layout: :columns,
    sort: opts[:sort] && opts[:sort].split(',').map(&:to_sym),
    header: !opts['hide-header'],
  }

  cols = opts[:output] ? opts[:output].split(',').map(&:to_sym) : FIELDS

  OsCtl::Lib::Cli::OutputFormatter.print(tpls, cols, fmt_opts)
end

#process_build_results(results) ⇒ Object (protected)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/osctl/image/cli/image.rb', line 162

def process_build_results(results)
  puts "Build results:"
  results.each do |res|
    tpl = res.obj
    build = res.return_value

    if res.status
      puts "#{tpl.name}: #{build.output_tar}"
      puts "#{tpl.name}: #{build.output_stream}"
    else
      puts "#{tpl.name}: failed with #{res.exception.class}: #{res.exception.message}"
    end
  end
end

#process_test_results(results) ⇒ Object (protected)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/osctl/image/cli/image.rb', line 199

def process_test_results(results)
  succeded = results.select { |t| t.success? }
  failed = results.reject { |t| t.success? }

  puts "#{results.length} tests run, #{succeded.length} succeeded, "+
       "#{failed.length} failed"
  return if failed.length == 0

  puts
  puts "Failed tests:"

  failed.each_with_index do |st, i|
    puts "#{i+1}) Test #{st.test} on #{st.image}:"
    puts "  Exit status: #{st.exitstatus}"
    puts "  Output:"
    st.output.split("\n").each { |line| puts (' '*4)+line }
    puts
  end
end

#select_images(arg) ⇒ Array<Image> (protected)

Parameters:

  • arg (String)

Returns:



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/osctl/image/cli/image.rb', line 243

def select_images(arg)
  existing_images = image_list

  if arg == 'all'
    existing_images
  else
    arg.split(',').map do |v|
      tpl = existing_images.detect { |t| t.name == v }
      raise GLI::BadCommandLine, "image '#{v}' not found" if tpl.nil?
      tpl
    end
  end
end

#select_tests(arg) ⇒ Array<Test> (protected)

Parameters:

  • arg (String, nil)

Returns:



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/osctl/image/cli/image.rb', line 259

def select_tests(arg)
  existing_tests = TestList.new('.')

  if arg.nil? || arg == 'all'
    existing_tests
  else
    arg.split(',').map do |v|
      test = existing_tests.detect { |t| t.name == v }
      raise GLI::BadCommandLine, "test '#{v}' not found" if test.nil?
      test
    end
  end
end

#testObject



46
47
48
49
50
51
52
53
# File 'lib/osctl/image/cli/image.rb', line 46

def test
  require_args!('image')

  images = select_images(args[0])
  tests = select_tests(args[1])
  results = test_images(images, tests)
  process_test_results(results)
end

#test_images(images, tests, rebuild: nil) ⇒ Object (protected)



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/osctl/image/cli/image.rb', line 177

def test_images(images, tests, rebuild: nil)
  rebuild = opts[:rebuild] if rebuild.nil?
  results = []

  images.each do |tpl|
    results.concat(
      Operations::Test::Image.run(
        File.absolute_path('.'),
        tpl,
        tests,
        output_dir: opts['output-dir'],
        build_dataset: opts['build-dataset'],
        vendor: opts[:vendor],
        rebuild: rebuild,
        keep_failed: opts['keep-failed'],
      )
    )
  end

  results
end