Class: Bosh::Cli::Stemcell

Inherits:
Object show all
Includes:
Validation
Defined in:
lib/cli/stemcell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validation

#errors, #reset_validation, #valid?, #validate

Constructor Details

#initialize(tarball_path, cache) ⇒ Stemcell

Returns a new instance of Stemcell.



9
10
11
12
# File 'lib/cli/stemcell.rb', line 9

def initialize(tarball_path, cache)
  @stemcell_file = File.expand_path(tarball_path, Dir.pwd)
  @cache = cache
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



7
8
9
# File 'lib/cli/stemcell.rb', line 7

def manifest
  @manifest
end

#stemcell_fileObject (readonly)

Returns the value of attribute stemcell_file.



7
8
9
# File 'lib/cli/stemcell.rb', line 7

def stemcell_file
  @stemcell_file
end

Instance Method Details

#perform_validation(options = {}) ⇒ Object



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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cli/stemcell.rb', line 14

def perform_validation(options = {})
  tmp_dir = Dir.mktmpdir

  step("File exists and readable",
       "Cannot find stemcell file #{@stemcell_file}", :fatal) do
    File.exists?(@stemcell_file) && File.readable?(@stemcell_file)
  end

  cache_key = "%s_%s" % [@stemcell_file, File.mtime(@stemcell_file)]

  manifest_yaml = @cache.read(cache_key)

  if manifest_yaml
    say("Using cached manifest...")
  else
    say("Manifest not found in cache, verifying tarball...")

    step("Extract tarball",
         "Cannot extract tarball #{@stemcell_file}", :fatal) do
      `tar -C #{tmp_dir} -xzf #{@stemcell_file} 2>&1`
      $?.exitstatus == 0
    end

    manifest_file = File.expand_path("stemcell.MF", tmp_dir)

    step("Manifest exists", "Cannot find stemcell manifest", :fatal) do
      File.exists?(manifest_file)
    end

    step("Stemcell image file",
         "Stemcell image file is missing", :fatal) do
      File.exists?(File.expand_path("image", tmp_dir))
    end

    say("Writing manifest to cache...")
    manifest_yaml = File.read(manifest_file)
    @cache.write(cache_key, manifest_yaml)
  end

  manifest = YAML.load(manifest_yaml)

  step("Stemcell properties",
       "Manifest should contain valid name, " +
           "version and cloud properties") do
    manifest.is_a?(Hash) && manifest.has_key?("name") &&
        manifest.has_key?("version") &&
        manifest.has_key?("cloud_properties") &&
        manifest["name"].is_a?(String) &&
        (manifest["version"].is_a?(String) ||
            manifest["version"].kind_of?(Numeric)) &&
        (manifest["cloud_properties"].nil? ||
            manifest["cloud_properties"].is_a?(Hash))
  end

  print_info(manifest)
  @manifest = manifest
ensure
  FileUtils.rm_rf(tmp_dir)
end


74
75
76
77
78
79
80
# File 'lib/cli/stemcell.rb', line 74

def print_info(manifest)
  say("\nStemcell info")
  say("-------------")

  say("Name:    %s" % [manifest["name"] || "missing".red])
  say("Version: %s" % [manifest["version"] || "missing".red])
end