Class: Idlc::Build::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/iapi-idlc-sdk-build/metadata.rb

Defined Under Namespace

Classes: MetadataAttribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file = 'metadata') ⇒ Metadata

Returns a new instance of Metadata.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 27

def initialize(name, file = 'metadata')
  @name = name
  @file = file
  @attributes = {}

  # Required
  @attributes['build_stage'] = MetadataAttribute.new(nil, true)
  @attributes['chef_run_list'] = MetadataAttribute.new(nil, true)
  @attributes['instance_type'] = MetadataAttribute.new(nil, true)
  @attributes['os_type'] = MetadataAttribute.new(nil, true)
  @attributes['role'] = MetadataAttribute.new(nil, true)
  @attributes['source_ami_name'] = MetadataAttribute.new(nil, true)
  @attributes['source_ami_owner'] = MetadataAttribute.new(nil, true)
  @attributes['version'] = MetadataAttribute.new(REPO_VERSION, true)
  @attributes['security_group_id'] = MetadataAttribute.new(ENV['PACKER_BUILD_SG_ID'], true)
  @attributes['vpc_id'] = MetadataAttribute.new(ENV['PACKER_BUILD_VPC_ID'], true)
  @attributes['vpc_subnet'] = MetadataAttribute.new(ENV['PACKER_BUILD_SUBNET_ID'], true)
  @attributes['iam_instance_profile'] = MetadataAttribute.new(ENV['PACKER_BUILD_INSTANCE_PROFILE_NAME'], true)

  # Optional Defaults
  @attributes['block_device_mapping'] = MetadataAttribute.new('[]')
  @attributes['job_code'] = MetadataAttribute.new('988')
  @attributes['chef_dir'] = MetadataAttribute.new('c:/windows/temp/packer-chef-client')
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



25
26
27
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 25

def attributes
  @attributes
end

Instance Method Details

#base_dirObject



110
111
112
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 110

def base_dir
  "builds/#{@name}"
end

#dash?(char) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 74

def dash?(char)
  char == '-'
end

#loadObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 52

def load
  raise MissingMetadataFile, "Expected metadata file at: #{}" unless File.exist? 

  YAML.load_file().each do |key, value|
    value = nested_json(value) if key == 'block_device_mapping'

    value = strip_trailing_dash(value) if key == 'source_ami_name'

    puts "WARNING: unrecognized metadata key: '#{key}'" unless @attributes.key? key
    next unless @attributes.key? key

    required = @attributes[key].required?
    @attributes[key] = MetadataAttribute.new(value, required)
  end
end

#metadata_fileObject



102
103
104
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 102

def 
  "#{Dir.pwd}/#{base_dir}/#{@file}"
end

#nested_json(value) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 78

def nested_json(value)
  total_block = []
  value.each do |block|
    total_block.push(block)
  end

  total_block.to_json
end

#requirements_satisfied?Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 87

def requirements_satisfied?
  begin
    @attributes['version'] = MetadataAttribute.new(YAML.load_file(version_file)['version'], true) unless @attributes['version'].defined?
  rescue
    puts('WARNING: \'version\' not specified in environment, metadata, or version file')
  end

  @attributes.each do |key, att|
    raise MissingRequiredMetadataAttribute, "Missing required key: '#{key}' in metadata file" if att.required? && att.value.nil?
    raise MissingRequiredMetadataAttribute, "Metadata key: '#{key}' cannot be nil" if att.required? && att.value.strip.empty?
  end

  true
end

#strip_trailing_dash(value) ⇒ Object



68
69
70
71
72
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 68

def strip_trailing_dash(value)
  return value[0..-2] if dash? value[-1]

  value
end

#version_fileObject



106
107
108
# File 'lib/iapi-idlc-sdk-build/metadata.rb', line 106

def version_file
  "#{Dir.pwd}/#{base_dir}/version"
end