Class: OsCtl::Image::Cli::Image
- Defined in:
- lib/osctl/image/cli/image.rb
Constant Summary collapse
- BUILD_SCRIPTS_DIR =
'/etc/vpsadminos-image-scripts'
- FIELDS =
%i(name distribution version arch vendor variant)
Instance Method Summary collapse
- #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
Methods inherited from Command
Constructor Details
This class inherits a constructor from OsCtl::Image::Cli::Command
Instance Method Details
#build ⇒ Object
44 45 46 47 48 49 |
# File 'lib/osctl/image/cli/image.rb', line 44 def build require_args!('image') results, _ = build_images(select_images(args[0])) process_build_results(results) end |
#build_images(images, rebuild: true) ⇒ Object (protected)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/osctl/image/cli/image.rb', line 142 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)
311 312 313 314 315 |
# File 'lib/osctl/image/cli/image.rb', line 311 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)
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 |
# File 'lib/osctl/image/cli/image.rb', line 285 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
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 |
# File 'lib/osctl/image/cli/image.rb', line 79 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(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? 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)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/osctl/image/cli/image.rb', line 229 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_list ⇒ Object (protected)
281 282 283 |
# File 'lib/osctl/image/cli/image.rb', line 281 def image_list ImageList.new(build_scripts_path) end |
#instantiate ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/osctl/image/cli/image.rb', line 60 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(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 |
#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 |
# 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 fmt_opts = { layout: :columns, cols: param_selector.parse_option(opts[:output]), sort: opts[:sort] && param_selector.parse_option(opts[:sort]), header: !opts['hide-header'], } OsCtl::Lib::Cli::OutputFormatter.print(tpls, **fmt_opts) end |
#process_build_results(results) ⇒ Object (protected)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/osctl/image/cli/image.rb', line 167 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.}" end end end |
#process_test_results(results) ⇒ Object (protected)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/osctl/image/cli/image.rb', line 207 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)
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/osctl/image/cli/image.rb', line 251 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)
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/osctl/image/cli/image.rb', line 267 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
51 52 53 54 55 56 57 58 |
# File 'lib/osctl/image/cli/image.rb', line 51 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)
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/osctl/image/cli/image.rb', line 182 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: rebuild, keep_failed: opts['keep-failed'], ip_allocator: ip_allocator, ) ) end results end |