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) ⇒ Stemcell

Returns a new instance of Stemcell.



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

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

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



5
6
7
# File 'lib/cli/stemcell.rb', line 5

def manifest
  @manifest
end

#stemcell_fileObject (readonly)

Returns the value of attribute stemcell_file.



5
6
7
# File 'lib/cli/stemcell.rb', line 5

def stemcell_file
  @stemcell_file
end

Instance Method Details

#perform_validation(options = {}) ⇒ 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cli/stemcell.rb', line 11

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

  say("Verifying tarball...")

  stemcell_mf = "stemcell.MF"

  tar = nil
  step("Read tarball",
       "Cannot read tarball #{@stemcell_file}", :fatal) do
    tgz = Zlib::GzipReader.new(File.open(@stemcell_file,'rb'))
    tar = Minitar.open(tgz)
    !!tar
  end

  manifest = false # assign false because we return something truthy

  image = false
  tar.each do |entry|
    if entry.full_name == stemcell_mf
      tar.extract_entry(tmp_dir, entry)
      manifest = true
    elsif entry.full_name == "image"
      image = true
    end
  end

  step("Manifest exists", "Cannot find stemcell manifest", :fatal) do
    manifest
  end

  step("Stemcell image file",
       "Stemcell image file is missing", :fatal) do
    image
  end

  manifest_file = File.expand_path(stemcell_mf, tmp_dir)
  manifest_yaml = File.read(manifest_file)

  manifest = Psych.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

  true
ensure
  FileUtils.rm_rf(tmp_dir)
end


78
79
80
81
82
83
84
# File 'lib/cli/stemcell.rb', line 78

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

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