Class: OsCtl::Image::Cli::Image
- Inherits:
-
Cli::Command
- Object
- Cli::Command
- OsCtl::Image::Cli::Image
- 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
- #auto_vpsadminos_dir ⇒ Object protected
- #build ⇒ Object
- #build_images(images, rebuild: true) ⇒ Object protected
- #build_script_dir?(path) ⇒ Boolean protected
- #build_scripts_path ⇒ Object protected
- #deploy ⇒ Object
- #image_in_repo_unchanged?(build, repo) ⇒ Boolean protected
- #image_list ⇒ Object protected
- #instantiate ⇒ Object
- #list ⇒ Object
- #process_build_results(results) ⇒ Object protected
- #process_test_results(results) ⇒ Object protected
- #select_images(arg) ⇒ Array<Image> protected
- #select_tests(arg) ⇒ Array<Test> protected
- #test ⇒ Object
- #test_images(images, tests, rebuild: nil) ⇒ Object protected
- #vpsadminos_dir ⇒ Object protected
- #vpsadminos_dir?(path, script_dir = nil) ⇒ Boolean protected
Instance Method Details
#auto_vpsadminos_dir ⇒ Object (protected)
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'lib/osctl/image/cli/image.rb', line 343 def auto_vpsadminos_dir return @auto_vpsadminos_dir if defined?(@auto_vpsadminos_dir) script_dir = File.realpath(build_scripts_path) cur = File.dirname(script_dir) loop do if vpsadminos_dir?(cur, script_dir) return @auto_vpsadminos_dir = cur end parent = File.dirname(cur) break if parent == cur cur = parent end @auto_vpsadminos_dir = nil end |
#build ⇒ Object
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)
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 |
# File 'lib/osctl/image/cli/image.rb', line 152 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], vpsadminos_dir: vpsadminos_dir ) 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)
319 320 321 322 323 |
# File 'lib/osctl/image/cli/image.rb', line 319 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_path ⇒ Object (protected)
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 |
# File 'lib/osctl/image/cli/image.rb', line 293 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 |
#deploy ⇒ Object
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 |
# File 'lib/osctl/image/cli/image.rb', line 90 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)
243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/osctl/image/cli/image.rb', line 243 def image_in_repo_unchanged?(build, repo) path = Operations::Repository::GetImagePath.run( repo, build.image_attrs, :zfs ) Operations::File::Compare.run(path, build.output_stream) rescue OperationError false end |
#image_list ⇒ Object (protected)
289 290 291 |
# File 'lib/osctl/image/cli/image.rb', line 289 def image_list ImageList.new(build_scripts_path) end |
#instantiate ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/osctl/image/cli/image.rb', line 70 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], vpsadminos_dir: vpsadminos_dir, rebuild: opts[:rebuild], ctid: opts[:container] ) puts "Container ID: #{ctid}" end |
#list ⇒ Object
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)
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/osctl/image/cli/image.rb', line 178 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.}" end end end |
#process_test_results(results) ⇒ Object (protected)
219 220 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 219 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 failed 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 failed end |
#select_images(arg) ⇒ Array<Image> (protected)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/osctl/image/cli/image.rb', line 257 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)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/osctl/image/cli/image.rb', line 274 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 |
#test ⇒ Object
60 61 62 63 64 65 66 67 68 |
# 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) failed = process_test_results(results) raise GLI::CustomExit.new('one or more tests failed', 1) unless failed.empty? end |
#test_images(images, tests, rebuild: nil) ⇒ Object (protected)
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 |
# File 'lib/osctl/image/cli/image.rb', line 193 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], vpsadminos_dir: vpsadminos_dir, rebuild:, keep_failed: opts['keep-failed'], ip_allocator: ) ) end results end |
#vpsadminos_dir ⇒ Object (protected)
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/osctl/image/cli/image.rb', line 325 def vpsadminos_dir return @vpsadminos_dir if defined?(@vpsadminos_dir) if gopts['vpsadminos-dir'] path = File.realpath(gopts['vpsadminos-dir']) unless vpsadminos_dir?(path) raise GLI::BadCommandLine, "#{gopts['vpsadminos-dir'].inspect} is not a vpsadminos checkout" end return @vpsadminos_dir = path end @vpsadminos_dir = auto_vpsadminos_dir rescue Errno::ENOENT raise GLI::BadCommandLine, "#{gopts['vpsadminos-dir'].inspect} does not exist" end |
#vpsadminos_dir?(path, script_dir = nil) ⇒ Boolean (protected)
363 364 365 366 367 368 369 370 371 |
# File 'lib/osctl/image/cli/image.rb', line 363 def vpsadminos_dir?(path, script_dir = nil) candidate = File.join(path, 'image-scripts') File.directory?(File.join(path, 'os', 'lib', 'nixos-container')) \ && File.directory?(candidate) \ && (script_dir.nil? || File.realpath(candidate) == script_dir) rescue Errno::ENOENT false end |