Class: Bootprint::Snapshot
- Inherits:
-
Object
- Object
- Bootprint::Snapshot
- Defined in:
- lib/bootprint/snapshot.rb
Constant Summary collapse
- SCHEMA_VERSION =
Schema::CURRENT_VERSION
- NON_SEMANTIC_PATHS =
%w[generated_at capture.duration_ms capture.plugins].freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .capture(label: nil, privacy: Bootprint.configuration.privacy) ⇒ Object
- .configuration_data(warnings) ⇒ Object
- .dependency_data(warnings) ⇒ Object
- .load(path) ⇒ Object
- .safely_collect(collector, warnings) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #environment ⇒ Object
-
#initialize(data) ⇒ Snapshot
constructor
A new instance of Snapshot.
- #name ⇒ Object
- #semantic_data ⇒ Object
- #write(path) ⇒ Object
Constructor Details
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/bootprint/snapshot.rb', line 20 def data @data end |
Class Method Details
.capture(label: nil, privacy: Bootprint.configuration.privacy) ⇒ 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 |
# File 'lib/bootprint/snapshot.rb', line 22 def self.capture(label: nil, privacy: Bootprint.configuration.privacy) started = Process.clock_gettime(Process::CLOCK_MONOTONIC) Bootprint.configuration.privacy = privacy.to_sym warnings = [] environment = { "name" => label, "runtime" => safely_collect(Collectors::Runtime, warnings), "dependencies" => dependency_data(warnings), "native_libraries" => safely_collect(Collectors::Libraries, warnings), "configuration" => configuration_data(warnings), "filesystem" => safely_collect(Collectors::Filesystem, warnings), "operating_system" => safely_collect(Collectors::OperatingSystem, warnings) }.compact plugin_data = if defined?(Bootprint::Plugins) Bootprint::Plugins.capture(warnings:, strict: Bootprint.configuration.plugin_strict) else {} end environment["plugins"] = plugin_data unless plugin_data.empty? duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1_000 new({ "schema_version" => SCHEMA_VERSION, "generated_at" => Time.now.utc.iso8601, "bootprint_version" => VERSION, "environment" => Sanitizer.recursive(environment, privacy:), "capture" => { "duration_ms" => duration.round(3), "privacy" => privacy.to_s, "warnings" => warnings } }) end |
.configuration_data(warnings) ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bootprint/snapshot.rb', line 85 def self.configuration_data(warnings) environment = safely_collect(Collectors::Environment, warnings) rails = safely_collect(Collectors::Rails, warnings) { "environment_variables" => environment["variables"] || {}, "required_environment_variables" => Bootprint.configuration.required_environment_names.sort, "optional_environment_variables" => Bootprint.configuration.optional_environment_names.sort, "rails" => rails || {} } end |
.dependency_data(warnings) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/bootprint/snapshot.rb', line 76 def self.dependency_data(warnings) gems = safely_collect(Collectors::Gems, warnings) { "toolchain" => safely_collect(Collectors::Toolchain, warnings), "gems" => gems["resolved"] || {}, "lockfile" => gems.except("resolved") } end |
.load(path) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/bootprint/snapshot.rb', line 56 def self.load(path) parsed = JSON.parse(File.read(path, encoding: "UTF-8")) new(parsed) rescue JSON::ParserError => error raise InvalidSnapshotError, "#{File.(path)} is not valid JSON: #{error.}" rescue Errno::ENOENT raise InvalidSnapshotError, "Snapshot not found: #{File.(path)}" end |
.safely_collect(collector, warnings) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/bootprint/snapshot.rb', line 65 def self.safely_collect(collector, warnings) collector.capture rescue StandardError => error warnings << { "collector" => collector.key, "message" => Sanitizer.text("#{error.class}: #{error.}"), "severity" => "warning" } { "capture_error" => Sanitizer.text("#{error.class}: #{error.}") } end |
Instance Method Details
#[](key) ⇒ Object
109 |
# File 'lib/bootprint/snapshot.rb', line 109 def [](key) = data[key.to_s] |
#environment ⇒ Object
110 |
# File 'lib/bootprint/snapshot.rb', line 110 def environment = data["environment"] |
#name ⇒ Object
111 |
# File 'lib/bootprint/snapshot.rb', line 111 def name = environment["name"] || "unnamed" |
#semantic_data ⇒ Object
113 114 115 116 117 118 119 120 |
# File 'lib/bootprint/snapshot.rb', line 113 def semantic_data Marshal.load(Marshal.dump(data)).tap do |copy| copy.delete("generated_at") copy.delete("bootprint_version") copy["capture"]&.delete("duration_ms") copy["capture"]&.delete("warnings") end end |
#write(path) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/bootprint/snapshot.rb', line 102 def write(path) directory = File.dirname(File.(path)) FileUtils.mkdir_p(directory) File.write(path, "#{JSON.pretty_generate(data)}\n") path end |