manifest.rb 3.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#
# Copyright 2015 Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'json'

module Omnibus
  class Manifest
    class InvalidManifestFormat < Exception; end
    class NotAManifestEntry < Exception; end
    class MissingManifestEntry < Exception; end

    include Logging

    LATEST_MANIFEST_FORMAT = 1

29 30
    attr_reader :build_version, :build_git_revision
    def initialize(version=nil, git_rev=nil)
31
      @data = {}
32 33
      @build_version = version
      @build_git_revision = git_rev
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    end

    def entry_for(name)
      if @data.key?(name)
        @data[name]
      else
        raise MissingManifestEntry, "No manifest entry found for #{name}"
      end
    end

    def add(name, entry)
      if ! entry.is_a? Omnibus::ManifestEntry
        raise NotAManifestEntry, "#{entry} is not an Omnibus:ManifestEntry"
      end

      if @data.key?(name)
        log.warn(log_key) { "Overritting existing manifest entry for #{name}" }
      end

      @data[name] = entry
      self
    end

57 58 59 60 61 62 63 64 65 66
    def each
      @data.each do |key, entry|
        yield entry
      end
    end

    def entry_names
      @data.keys
    end

67 68 69 70 71
    def to_hash
      software_hash = @data.inject({}) do |memo, (k,v)|
        memo[k] = v.to_hash
        memo
      end
72
      ret = {
73 74 75
        'manifest_format' => LATEST_MANIFEST_FORMAT,
        'software' => software_hash
      }
76 77 78
      ret['build_version'] = build_version if build_version
      ret['build_git_revision'] = build_git_revision if build_git_revision
      ret
79 80
    end

81 82 83 84 85 86 87 88 89
    #
    # The JSON representation of this build manifest.
    #
    # @return [String]
    #
    def to_json
      JSON.pretty_generate(to_hash)
    end

90 91 92 93 94 95 96 97 98
    #
    # Class Methods
    #

    def self.from_hash(manifest_data)
      case manifest_data['manifest_format'].to_i
      when 1
        from_hash_v1(manifest_data)
      else
99
        raise InvalidManifestFormat, "Unknown manifest format version: #{manifest_data['manifest_format']}"
100 101 102 103
      end
    end

    def self.from_hash_v1(manifest_data)
104
      m = Omnibus::Manifest.new(manifest_data['build_version'], manifest_data['build_git_revision'])
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
      manifest_data['software'].each do |name, entry_data|
        m.add(name, Omnibus::ManifestEntry.new(name, keys_to_syms(entry_data)))
      end
      m
    end

    def self.from_file(filename)
      from_hash(JSON.parse(File.read(File.expand_path(filename))))
    end

    private

    #
    # Utility function to convert a Hash with String keys to a Hash
    # with Symbol keys, recursively.
    #
    # @returns [Hash]
    #
    def self.keys_to_syms(h)
      h.inject({}) do |memo, (k, v)|
        memo[k.to_sym] = if v.is_a? Hash
                           keys_to_syms(v)
                         else
                           v
                         end
        memo
      end
    end
  end
end