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

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

Constant Summary collapse

BUILD_SCRIPTS_DIR =
'/etc/vpsadminos-image-scripts'.freeze
FIELDS =
%i[name distribution version arch vendor variant].freeze

Instance Method Summary collapse

Methods inherited from Command

#initialize, run

Constructor Details

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

Instance Method Details

#buildObject



53
54
55
56
57
58
# File 'lib/osctl/image/cli/image.rb', line 53

def build
  require_args!('image')

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

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



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/osctl/image/cli/image.rb', line 150

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(build_scripts_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

#build_script_dir?(path) ⇒ Boolean (protected)

Returns:

  • (Boolean)


320
321
322
323
324
# File 'lib/osctl/image/cli/image.rb', line 320

def build_script_dir?(path)
  File.executable?(File.join(path, 'bin/config')) \
    && File.executable?(File.join(path, 'bin/runner')) \
    && File.executable?(File.join(path, 'bin/test'))
end

#build_scripts_pathObject (protected)

Raises:

  • (GLI::BadCommandLine)


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

def build_scripts_path
  return @build_scripts_path if @build_scripts_path

  # Check option
  if gopts['build-scripts']
    unless build_script_dir?(gopts['build-scripts'])
      raise GLI::BadCommandLine, "#{gopts['build-scripts'].inspect} does not comply with image builder interface"
    end

    return @build_scripts_path = gopts['build-scripts']
  end

  # Check the current working directory
  if build_script_dir?(Dir.pwd)
    return @build_scripts_path = Dir.pwd
  end

  # Check system directory
  if build_script_dir?(BUILD_SCRIPTS_DIR)
    return @build_scripts_path = BUILD_SCRIPTS_DIR
  end

  # Not found
  raise GLI::BadCommandLine, 'Enter into build scripts directory or use option --build-scripts'
end

#deployObject



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

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

  raise '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(build_scripts_path)
    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?
    raise 'no images to deploy' unless unchanged

    puts 'no images to deploy'

    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)


237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/osctl/image/cli/image.rb', line 237

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)



290
291
292
# File 'lib/osctl/image/cli/image.rb', line 290

def image_list
  ImageList.new(build_scripts_path)
end

#instantiateObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/osctl/image/cli/image.rb', line 69

def instantiate
  require_args!('image')

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

  ctid = Operations::Image::Instantiate.run(
    File.absolute_path(build_scripts_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



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/osctl/image/cli/image.rb', line 11

def list
  param_selector = OsCtl::Lib::Cli::ParameterSelector.new(
    all_params: FIELDS
  )

  if opts[:list]
    puts param_selector
    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

  cols = param_selector.parse_option(opts[:output])
  sort = opts[:sort] && param_selector.parse_option(opts[:sort])

  if sort
    sort.each do |v|
      cols << v unless cols.include?(v)
    end
  end

  fmt_opts = {
    layout: :columns,
    cols:,
    sort:,
    header: !opts['hide-header']
  }

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

#process_build_results(results) ⇒ Object (protected)



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/osctl/image/cli/image.rb', line 175

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}" if 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)



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/osctl/image/cli/image.rb', line 215

def process_test_results(results)
  succeded = results.select(&:success?)
  failed = results.reject(&:success?)

  puts "#{results.length} tests run, #{succeded.length} succeeded, " \
       "#{failed.length} failed"
  return if failed.empty?

  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:



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

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:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/osctl/image/cli/image.rb', line 275

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

  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



60
61
62
63
64
65
66
67
# File 'lib/osctl/image/cli/image.rb', line 60

def test
  require_args!('image', optional: %w[test])

  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)



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/osctl/image/cli/image.rb', line 190

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

  ip_allocator = IpAllocator.new('10.100.10.0/24')

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

  results
end