Class: Snapsync::SnapperConfig
- Inherits:
-
Object
- Object
- Snapsync::SnapperConfig
- Defined in:
- lib/snapsync/snapper_config.rb
Overview
A snapper configuration
Instance Attribute Summary collapse
-
#fstype ⇒ String
readonly
The filesystem type.
-
#name ⇒ Object
readonly
The configuration name.
-
#subvolume ⇒ Pathname
readonly
Path to the subvolume.
Class Method Summary collapse
- .default_config_dir ⇒ Object
-
.each_in_dir(path = default_config_dir) ⇒ Object
Enumerates the valid snapper configurations present in a directory.
-
.load(path, name: path.basename.to_s) ⇒ SnapperConfig
Create a SnapperConfig object from the data in a configuration file.
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#create(type: 'single', description: '', user_data: Hash.new) ⇒ Snapshot
Create a new snapshot.
-
#delete(snapshot) ⇒ Object
Delete one of this configuration’s snapshots.
-
#each_snapshot {|snapshot| ... } ⇒ Object
Enumerate the valid snapshots in this configuration.
-
#initialize(name) ⇒ SnapperConfig
constructor
A new instance of SnapperConfig.
-
#load(path) ⇒ Object
Load the information from a configuration file into this object.
-
#parse_line(line) ⇒ (String,String)
private
Extract the key and value from a snapper configuration file.
-
#snapshot_dir ⇒ Object
The directory containing the snapshots.
Constructor Details
#initialize(name) ⇒ SnapperConfig
Returns a new instance of SnapperConfig.
16 17 18 19 |
# File 'lib/snapsync/snapper_config.rb', line 16 def initialize(name) @name = name.to_str @subvolume, @fstype = nil end |
Instance Attribute Details
#fstype ⇒ String (readonly)
The filesystem type
14 15 16 |
# File 'lib/snapsync/snapper_config.rb', line 14 def fstype @fstype end |
#name ⇒ Object (readonly)
The configuration name
5 6 7 |
# File 'lib/snapsync/snapper_config.rb', line 5 def name @name end |
#subvolume ⇒ Pathname (readonly)
Path to the subvolume
10 11 12 |
# File 'lib/snapsync/snapper_config.rb', line 10 def subvolume @subvolume end |
Class Method Details
.default_config_dir ⇒ Object
33 34 35 |
# File 'lib/snapsync/snapper_config.rb', line 33 def self.default_config_dir Pathname.new('/etc/snapper/configs') end |
.each_in_dir(path = default_config_dir) ⇒ Object
Enumerates the valid snapper configurations present in a directory
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/snapsync/snapper_config.rb', line 38 def self.each_in_dir(path = default_config_dir) path.each_entry do |config_file| config_name = config_file.to_s config_file = path + config_file next if !config_file.file? begin config = SnapperConfig.load(config_file) rescue Interrupt raise rescue Exception => e Snapsync.warn "cannot load #{config_file}: #{e.}" e.backtrace.each do |line| Snapsync.debug " #{line}" end next end yield(config) end end |
.load(path, name: path.basename.to_s) ⇒ SnapperConfig
Create a SnapperConfig object from the data in a configuration file
90 91 92 93 94 |
# File 'lib/snapsync/snapper_config.rb', line 90 def self.load(path, name: path.basename.to_s) config = new(name) config.load(path) config end |
Instance Method Details
#cleanup ⇒ Object
79 80 81 82 |
# File 'lib/snapsync/snapper_config.rb', line 79 def cleanup Snapsync.debug "SnapperConfig.cleanup" system('snapper', '-c', name, 'cleanup', 'all') end |
#create(type: 'single', description: '', user_data: Hash.new) ⇒ Snapshot
Create a new snapshot
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/snapsync/snapper_config.rb', line 62 def create(type: 'single', description: '', user_data: Hash.new) user_data = user_data.map { |k,v| "#{k}=#{v}" }.join(",") snapshot_id = IO.popen(["snapper", "-c", name, "create", "--type", type, "--print-number", "--description", description, "--userdata", user_data]) do |io| Integer(io.read.strip) end Snapshot.new(snapshot_dir + snapshot_id.to_s) end |
#delete(snapshot) ⇒ Object
Delete one of this configuration’s snapshots
75 76 77 |
# File 'lib/snapsync/snapper_config.rb', line 75 def delete(snapshot) system("snapper", "-c", name, "delete", snapshot.num.to_s) end |
#each_snapshot {|snapshot| ... } ⇒ Object
Enumerate the valid snapshots in this configuration
29 30 31 |
# File 'lib/snapsync/snapper_config.rb', line 29 def each_snapshot(&block) Snapshot.each(snapshot_dir, &block) end |
#load(path) ⇒ Object
Load the information from a configuration file into this object
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/snapsync/snapper_config.rb', line 116 def load(path) path.readlines.each do |line| key, value = parse_line(line) case key when NilClass then next else instance_variable_set("@#{key.downcase}", value) end end if @subvolume @subvolume = Pathname.new(subvolume) end end |
#parse_line(line) ⇒ (String,String)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extract the key and value from a snapper configuration file
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/snapsync/snapper_config.rb', line 102 def parse_line(line) line = line.strip.gsub(/#.*/, '') if !line.empty? if line =~ /^(\w+)="?([^"]*)"?$/ return $1, $2 else raise ArgumentError, "cannot parse #{line}" end end end |
#snapshot_dir ⇒ Object
The directory containing the snapshots
22 23 24 |
# File 'lib/snapsync/snapper_config.rb', line 22 def snapshot_dir subvolume + ".snapshots" end |