Class | BoxGrinder::ApplianceDefinitionHelper |
In: |
lib/boxgrinder-core/helpers/appliance-definition-helper.rb
lib/boxgrinder-core/helpers/appliance-definition-helper.rb |
Parent: | Object |
appliance_configs | [R] | |
appliance_configs | [R] |
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 24 24: def initialize(options = {}) 25: @log = options[:log] || Logger.new(STDOUT) 26: @appliance_configs = [] 27: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 24 24: def initialize(options = {}) 25: @log = options[:log] || Logger.new(STDOUT) 26: @appliance_configs = [] 27: end
TODO this needs to be rewritten
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 97 97: def parse_yaml(definition) 98: return definition if definition.is_a?(ApplianceConfig) 99: raise "Provided definition is not a Hash." unless definition.is_a?(Hash) 100: 101: appliance_config = ApplianceConfig.new 102: 103: appliance_config.name = definition['name'] unless definition['name'].nil? 104: appliance_config.summary = definition['summary'] unless definition['summary'].nil? 105: 106: definition['variables'].each { |key, value| appliance_config.variables[key] = value } unless definition['variables'].nil? 107: 108: @log.debug "Adding packages to appliance..." 109: 110: unless definition['packages'].nil? 111: if definition['packages'].is_a?(Array) 112: # new format 113: appliance_config.packages = definition['packages'] 114: elsif definition['packages'].is_a?(Hash) 115: # legacy format 116: @log.warn "BoxGrinder Build packages section format has been changed. Support for legacy format will be removed in the future. See http://boxgrinder.org/tutorials/appliance-definition/ for more information about current format." 117: appliance_config.packages = definition['packages']['includes'] if definition['packages']['includes'].is_a?(Array) 118: @log.warn "BoxGrinder Build no longer supports package exclusion, the following packages will not be explicitly excluded: #{definition['packages']['excludes'].join(", ")}." if definition['packages']['excludes'].is_a?(Array) 119: else 120: @log.warn "Unsupported format for packages section." 121: end 122: end 123: 124: @log.debug "#{appliance_config.packages.size} package(s) added to appliance." 125: 126: appliance_config.appliances = definition['appliances'] unless definition['appliances'].nil? 127: appliance_config.repos = definition['repos'] unless definition['repos'].nil? 128: 129: appliance_config.version = definition['version'].to_s unless definition['version'].nil? 130: appliance_config.release = definition['release'].to_s unless definition['release'].nil? 131: 132: unless definition['default_repos'].nil? 133: appliance_config.default_repos = definition['default_repos'] 134: raise "default_repos should be set to true or false" unless appliance_config.default_repos.is_a?(TrueClass) or appliance_config.default_repos.is_a?(FalseClass) 135: end 136: 137: unless definition['os'].nil? 138: appliance_config.os.name = definition['os']['name'].to_s unless definition['os']['name'].nil? 139: appliance_config.os.version = definition['os']['version'].to_s unless definition['os']['version'].nil? 140: appliance_config.os.password = definition['os']['password'].to_s unless definition['os']['password'].nil? 141: end 142: 143: unless definition['hardware'].nil? 144: appliance_config.hardware.arch = definition['hardware']['arch'] unless definition['hardware']['arch'].nil? 145: appliance_config.hardware.cpus = definition['hardware']['cpus'] unless definition['hardware']['cpus'].nil? 146: appliance_config.hardware.memory = definition['hardware']['memory'] unless definition['hardware']['memory'].nil? 147: appliance_config.hardware.network = definition['hardware']['network'] unless definition['hardware']['network'].nil? 148: 149: unless definition['hardware']['partitions'].nil? 150: definition['hardware']['partitions'].each do |key, part| 151: appliance_config.hardware.partitions[key] = part 152: end if definition['hardware']['partitions'].is_a?(Hash) 153: end 154: end 155: 156: definition['post'].each { |key, value| appliance_config.post[key] = value } unless definition['post'].nil? 157: 158: appliance_config 159: end
TODO this needs to be rewritten
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 97 97: def parse_yaml(definition) 98: return definition if definition.is_a?(ApplianceConfig) 99: raise "Provided definition is not a Hash." unless definition.is_a?(Hash) 100: 101: appliance_config = ApplianceConfig.new 102: 103: appliance_config.name = definition['name'] unless definition['name'].nil? 104: appliance_config.summary = definition['summary'] unless definition['summary'].nil? 105: 106: definition['variables'].each { |key, value| appliance_config.variables[key] = value } unless definition['variables'].nil? 107: 108: @log.debug "Adding packages to appliance..." 109: 110: unless definition['packages'].nil? 111: if definition['packages'].is_a?(Array) 112: # new format 113: appliance_config.packages = definition['packages'] 114: elsif definition['packages'].is_a?(Hash) 115: # legacy format 116: @log.warn "BoxGrinder Build packages section format has been changed. Support for legacy format will be removed in the future. See http://boxgrinder.org/tutorials/appliance-definition/ for more information about current format." 117: appliance_config.packages = definition['packages']['includes'] if definition['packages']['includes'].is_a?(Array) 118: @log.warn "BoxGrinder Build no longer supports package exclusion, the following packages will not be explicitly excluded: #{definition['packages']['excludes'].join(", ")}." if definition['packages']['excludes'].is_a?(Array) 119: else 120: @log.warn "Unsupported format for packages section." 121: end 122: end 123: 124: @log.debug "#{appliance_config.packages.size} package(s) added to appliance." 125: 126: appliance_config.appliances = definition['appliances'] unless definition['appliances'].nil? 127: appliance_config.repos = definition['repos'] unless definition['repos'].nil? 128: 129: appliance_config.version = definition['version'].to_s unless definition['version'].nil? 130: appliance_config.release = definition['release'].to_s unless definition['release'].nil? 131: 132: unless definition['default_repos'].nil? 133: appliance_config.default_repos = definition['default_repos'] 134: raise "default_repos should be set to true or false" unless appliance_config.default_repos.is_a?(TrueClass) or appliance_config.default_repos.is_a?(FalseClass) 135: end 136: 137: unless definition['os'].nil? 138: appliance_config.os.name = definition['os']['name'].to_s unless definition['os']['name'].nil? 139: appliance_config.os.version = definition['os']['version'].to_s unless definition['os']['version'].nil? 140: appliance_config.os.password = definition['os']['password'].to_s unless definition['os']['password'].nil? 141: end 142: 143: unless definition['hardware'].nil? 144: appliance_config.hardware.arch = definition['hardware']['arch'] unless definition['hardware']['arch'].nil? 145: appliance_config.hardware.cpus = definition['hardware']['cpus'] unless definition['hardware']['cpus'].nil? 146: appliance_config.hardware.memory = definition['hardware']['memory'] unless definition['hardware']['memory'].nil? 147: appliance_config.hardware.network = definition['hardware']['network'] unless definition['hardware']['network'].nil? 148: 149: unless definition['hardware']['partitions'].nil? 150: definition['hardware']['partitions'].each do |key, part| 151: appliance_config.hardware.partitions[key] = part 152: end if definition['hardware']['partitions'].is_a?(Hash) 153: end 154: end 155: 156: definition['post'].each { |key, value| appliance_config.post[key] = value } unless definition['post'].nil? 157: 158: appliance_config 159: end
Reads definition provided as string. This string can be a YAML document. In this case definition is parsed and an ApplianceConfig object is returned. In other cases it tries to search for a file with provided name.
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 34 34: def read_definitions(definition, content_type = nil) 35: if File.exists?(definition) 36: @log.debug "Reading definition from '#{definition}' file..." 37: 38: definition_file_extension = File.extname(definition) 39: 40: appliance_config = 41: case definition_file_extension 42: when '.appl', '.yml', '.yaml' 43: read_yaml_file(definition) 44: when '.xml' 45: read_xml_file(definition) 46: else 47: unless content_type.nil? 48: case content_type 49: when 'application/x-yaml', 'text/yaml' 50: read_yaml_file(definition) 51: when 'application/xml', 'text/xml', 'application/x-xml' 52: read_xml_file(definition) 53: end 54: end 55: end 56: 57: raise 'Unsupported file format for appliance definition file.' if appliance_config.nil? 58: 59: @appliance_configs << appliance_config 60: appliances = [] 61: 62: @appliance_configs.each { |config| appliances << config.name } 63: 64: appliance_config.appliances.reverse.each do |appliance_name| 65: read_definitions("#{File.dirname(definition)}/#{appliance_name}#{definition_file_extension}") unless appliances.include?(appliance_name) 66: end unless appliance_config.appliances.nil? or !appliance_config.appliances.is_a?(Array) 67: else 68: @log.debug "Reading definition..." 69: 70: @appliance_configs << read_yaml(definition) 71: end 72: end
Reads definition provided as string. This string can be a YAML document. In this case definition is parsed and an ApplianceConfig object is returned. In other cases it tries to search for a file with provided name.
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 34 34: def read_definitions(definition, content_type = nil) 35: if File.exists?(definition) 36: @log.debug "Reading definition from '#{definition}' file..." 37: 38: definition_file_extension = File.extname(definition) 39: 40: appliance_config = 41: case definition_file_extension 42: when '.appl', '.yml', '.yaml' 43: read_yaml_file(definition) 44: when '.xml' 45: read_xml_file(definition) 46: else 47: unless content_type.nil? 48: case content_type 49: when 'application/x-yaml', 'text/yaml' 50: read_yaml_file(definition) 51: when 'application/xml', 'text/xml', 'application/x-xml' 52: read_xml_file(definition) 53: end 54: end 55: end 56: 57: raise 'Unsupported file format for appliance definition file.' if appliance_config.nil? 58: 59: @appliance_configs << appliance_config 60: appliances = [] 61: 62: @appliance_configs.each { |config| appliances << config.name } 63: 64: appliance_config.appliances.reverse.each do |appliance_name| 65: read_definitions("#{File.dirname(definition)}/#{appliance_name}#{definition_file_extension}") unless appliances.include?(appliance_name) 66: end unless appliance_config.appliances.nil? or !appliance_config.appliances.is_a?(Array) 67: else 68: @log.debug "Reading definition..." 69: 70: @appliance_configs << read_yaml(definition) 71: end 72: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 161 161: def read_xml_file(file) 162: raise "Reading XML files is not supported right now. File '#{file}' could not be read." 163: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 161 161: def read_xml_file(file) 162: raise "Reading XML files is not supported right now. File '#{file}' could not be read." 163: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 74 74: def read_yaml(content) 75: begin 76: definition = YAML.load(content) 77: raise "Not a valid YAML content." if definition.nil? or definition == false 78: rescue 79: raise "Provided definition could not be read." 80: end 81: 82: parse_yaml(definition) 83: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 74 74: def read_yaml(content) 75: begin 76: definition = YAML.load(content) 77: raise "Not a valid YAML content." if definition.nil? or definition == false 78: rescue 79: raise "Provided definition could not be read." 80: end 81: 82: parse_yaml(definition) 83: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 85 85: def read_yaml_file(file) 86: begin 87: definition = YAML.load_file(file) 88: raise "Not a valid YAML file." if definition.nil? or definition == false 89: rescue 90: raise "File '#{file}' could not be read." 91: end 92: 93: parse_yaml(definition) 94: end
# File lib/boxgrinder-core/helpers/appliance-definition-helper.rb, line 85 85: def read_yaml_file(file) 86: begin 87: definition = YAML.load_file(file) 88: raise "Not a valid YAML file." if definition.nil? or definition == false 89: rescue 90: raise "File '#{file}' could not be read." 91: end 92: 93: parse_yaml(definition) 94: end