Module: Bootprint::Schema
- Defined in:
- lib/bootprint/schema.rb
Constant Summary collapse
- CURRENT_VERSION =
2- ENVIRONMENT_SECTIONS =
%w[runtime dependencies native_libraries configuration filesystem operating_system].freeze
Class Method Summary collapse
- .default_filesystem ⇒ Object
- .deterministic(value) ⇒ Object
- .migrate(data) ⇒ Object
- .migrate_v1(old) ⇒ Object
- .scalar?(value) ⇒ Boolean
- .stringify(value) ⇒ Object
- .validate!(data) ⇒ Object
Class Method Details
.default_filesystem ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/bootprint/schema.rb', line 81 def default_filesystem { "path_separator" => File::ALT_SEPARATOR || File::SEPARATOR, "temporary_directory" => { "present" => true, "writable" => true }, "required_directories" => {} } end |
.deterministic(value) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/bootprint/schema.rb', line 97 def deterministic(value) case value when Hash then value.keys.sort.to_h { |key| [key, deterministic(value[key])] } when Array normalized = value.map { |nested| deterministic(nested) } normalized.all? { |item| scalar?(item) } ? normalized.sort_by(&:to_s) : normalized else value end end |
.migrate(data) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bootprint/schema.rb', line 10 def migrate(data) raise InvalidSnapshotError, "Snapshot root must be a JSON object" unless data.is_a?(Hash) version = data.is_a?(Hash) ? data["schema_version"] || data[:schema_version] : nil case version when CURRENT_VERSION then stringify(data) when 1 then migrate_v1(stringify(data)) when nil then raise InvalidSnapshotError, "Snapshot does not declare schema_version" else if version.is_a?(Integer) && version > CURRENT_VERSION raise InvalidSnapshotError, "Snapshot schema #{version} is newer than supported schema #{CURRENT_VERSION}; upgrade Bootprint" end raise InvalidSnapshotError, "Unsupported snapshot schema #{version.inspect}" end end |
.migrate_v1(old) ⇒ Object
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 77 78 79 |
# File 'lib/bootprint/schema.rb', line 50 def migrate_v1(old) known = %w[schema_version metadata runtime toolchain gems libraries environment operating_system rails] unknown = old.except(*known) rails = old["rails"] || {} { "schema_version" => CURRENT_VERSION, "generated_at" => old.dig("metadata", "captured_at") || Time.now.utc.iso8601, "bootprint_version" => VERSION, "environment" => { "name" => old.dig("metadata", "label"), "runtime" => old["runtime"] || {}, "dependencies" => { "toolchain" => old["toolchain"] || {}, "gems" => old.dig("gems", "resolved") || {}, "lockfile" => { "sha256" => old.dig("gems", "lockfile_sha256"), "platforms" => [] }.compact }, "native_libraries" => old["libraries"] || {}, "configuration" => { "environment_variables" => old.dig("environment", "variables") || {}, "required_environment_variables" => [], "optional_environment_variables" => [], "rails" => rails }, "filesystem" => default_filesystem, "operating_system" => old["operating_system"] || {} }.compact, "capture" => { "migrated_from" => 1 }, "extensions" => unknown } end |
.scalar?(value) ⇒ Boolean
107 108 109 |
# File 'lib/bootprint/schema.rb', line 107 def scalar?(value) value.nil? || value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false end |
.stringify(value) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/bootprint/schema.rb', line 89 def stringify(value) case value when Hash then value.to_h { |key, nested| [key.to_s, stringify(nested)] } when Array then value.map { |nested| stringify(nested) } else value end end |
.validate!(data) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bootprint/schema.rb', line 27 def validate!(data) raise InvalidSnapshotError, "Snapshot root must be a JSON object" unless data.is_a?(Hash) raise InvalidSnapshotError, "schema_version must equal #{CURRENT_VERSION}" unless data["schema_version"] == CURRENT_VERSION raise InvalidSnapshotError, "generated_at must be an ISO-8601 string" unless data["generated_at"].is_a?(String) raise InvalidSnapshotError, "bootprint_version must be a string" unless data["bootprint_version"].is_a?(String) environment = data["environment"] raise InvalidSnapshotError, "environment must be an object" unless environment.is_a?(Hash) missing = ENVIRONMENT_SECTIONS - environment.keys raise InvalidSnapshotError, "environment is missing sections: #{missing.join(', ')}" unless missing.empty? invalid = ENVIRONMENT_SECTIONS.reject { |key| environment[key].is_a?(Hash) } raise InvalidSnapshotError, "environment sections must be objects: #{invalid.join(', ')}" unless invalid.empty? variables = environment.dig("configuration", "environment_variables") unless variables.is_a?(Hash) && variables.values.all? { |value| [true, false].include?(value) } raise InvalidSnapshotError, "configuration.environment_variables must map names to booleans" end true end |