Class: Bosh::Cli::Manifest

Inherits:
Object show all
Defined in:
lib/cli/manifest.rb

Constant Summary collapse

MANIFEST_TARGET_UPGRADE_NOTICE =
<<-EOS.gsub(/^\s*/, '').gsub(/\n$/, '')
  Please upgrade your deployment manifest to use director UUID instead
  of target. Just replace 'target' key with 'director_uuid' key in your
  manifest. You can get your director UUID by targeting your director
  with 'bosh target' and running 'bosh status' command afterwards.
EOS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deployment_file, director) ⇒ Manifest


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

def initialize(deployment_file, director)
  @deployment_file = deployment_file
  @director = director
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash


3
4
5
# File 'lib/cli/manifest.rb', line 3

def hash
  @hash
end

Instance Method Details

#latest_release_versionsObject


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cli/manifest.rb', line 129

def latest_release_versions
  @_latest_release_versions ||= begin
    @director.list_releases.inject({}) do |hash, release|
      name = release['name']
      versions = release['versions'] || release['release_versions'].map { |release_version| release_version['version'] }
      parsed_versions = versions.map do |version|
        {
          original: version,
          parsed: Bosh::Common::Version::ReleaseVersion.parse(version)
        }
      end
      latest_version = parsed_versions.sort_by { |v| v[:parsed] }.last[:original]
      hash[name] = latest_version.to_s
      hash
    end
  end
end

#latest_stemcellsArray


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cli/manifest.rb', line 95

def latest_stemcells
  @_latest_stemcells ||= begin
    stemcells = @director.list_stemcells.inject({}) do |hash, stemcell|
      unless stemcell.is_a?(Hash) && stemcell['name'] && stemcell['version']
        err('Invalid director stemcell list format')
      end
      hash[stemcell['name']] ||= []
      hash[stemcell['name']] << stemcell['version']
      hash
    end

    stemcells.inject({}) do |hash, (name, versions)|
      hash[name] = Bosh::Common::Version::StemcellVersionList.parse(versions).latest.to_s
      hash
    end
  end
end

#loadObject


14
15
16
17
18
19
20
# File 'lib/cli/manifest.rb', line 14

def load
  unless File.exists?(@deployment_file)
    err("Cannot find deployment manifest in '#{@deployment_file}'")
  end

  @hash = load_yaml_file(@deployment_file)
end

#nameObject


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

def name
  @hash['name']
end

#report_manifest_warningsObject


155
156
157
# File 'lib/cli/manifest.rb', line 155

def report_manifest_warnings
  ManifestWarnings.new(@hash).report
end

#resolve_release_aliasesvoid


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cli/manifest.rb', line 115

def resolve_release_aliases
  releases = @hash['releases'] || [@hash['release']]

  releases.each do |release|
    if release['version'] == 'latest'
      latest_release_version = latest_release_versions[release['name']]
      unless latest_release_version
        err("Release '#{release['name']}' not found on director. Unable to resolve 'latest' alias in manifest.")
      end
      release['version'] = latest_release_version
    end
  end
end

#resolve_stemcell_aliasesvoid


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cli/manifest.rb', line 76

def resolve_stemcell_aliases
  return if @hash['resource_pools'].nil?

  @hash['resource_pools'].each do |rp|
    stemcell = rp['stemcell']
    unless stemcell.is_a?(Hash)
      err('Invalid stemcell spec in the deployment manifest')
    end
    if stemcell['version'] == 'latest'
      latest_version = latest_stemcells[stemcell['name']]
      if latest_version.nil?
        err("Latest version for stemcell '#{stemcell['name']}' is unknown")
      end
      stemcell['version'] = latest_version
    end
  end
end

#validate(options = {}) ⇒ Object


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
# File 'lib/cli/manifest.rb', line 22

def validate(options={})
  if @hash['name'].blank?
    err('Deployment name not found in the deployment manifest')
  end

  if @hash['target']
    err(MANIFEST_TARGET_UPGRADE_NOTICE)
  end

  if options[:resolve_properties]
    compiler = DeploymentManifestCompiler.new(Psych.dump(@hash))
    properties = {}

    begin
      say('Getting deployment properties from director...')
      properties = @director.list_properties(name)
    rescue Bosh::Cli::DirectorError
      say('Unable to get properties list from director, ' +
          'trying without it...')
    end

    compiler.properties = properties.inject({}) do |hash, property|
      hash[property['name']] = property['value']
      hash
    end

    @hash = Psych.load(compiler.result)
  end

  if name.blank? || @hash['director_uuid'].blank?
    err("Invalid manifest '#{File.basename(@deployment_file)}': " +
        'name and director UUID are required')
  end

  if @director.uuid != @hash['director_uuid']
    err("Target director UUID doesn't match UUID from deployment manifest")
  end

  if @hash['release'].blank? && @hash['releases'].blank?
    err("Deployment manifest doesn't have release information: '" +
        "please add 'release' or 'releases' section")
  end

  report_manifest_warnings

  @hash
end

#yamlObject


70
71
72
# File 'lib/cli/manifest.rb', line 70

def yaml
  Psych.dump(@hash)
end