class Snapsync::SnapperConfig

A snapper configuration

Attributes

fstype[R]

The filesystem type

name[R]

The configuration name

subvolume[R]

Path to the subvolume

@return [Pathname]

Public Class Methods

default_config_dir() click to toggle source
# File lib/snapsync/snapper_config.rb, line 32
def self.default_config_dir
    Pathname.new('/etc/snapper/configs')
end
each_in_dir(path = default_config_dir) { |config| ... } click to toggle source

Enumerates the valid snapper configurations present in a directory

# File lib/snapsync/snapper_config.rb, line 37
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.message}"
            e.backtrace.each do |line|
                Snapsync.debug "  #{line}"
            end
            next
        end

        yield(config)
    end
end
load(path, name: path.basename.to_s) click to toggle source

Create a SnapperConfig object from the data in a configuration file

@param [#readlines] path the file @param [String] name the configuration name @return [SnapperConfig] @raise (see load)

# File lib/snapsync/snapper_config.rb, line 84
def self.load(path, name: path.basename.to_s)
    config = new(name)
    config.load(path)
    config
end
new(name) click to toggle source
# File lib/snapsync/snapper_config.rb, line 15
def initialize(name)
    @name = name.to_str
    @subvolume, @fstype = nil
end

Public Instance Methods

create(type: 'single', description: '', user_data: Hash.new) click to toggle source

Create a new snapshot

@return [Snapshot]

# File lib/snapsync/snapper_config.rb, line 61
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) click to toggle source

Delete one of this configuration's snapshots

# File lib/snapsync/snapper_config.rb, line 74
def delete(snapshot)
    system("snapper", "-c", name, "delete", snapshot.num.to_s)
end
each_snapshot(&block) click to toggle source

Enumerate the valid snapshots in this configuration

@yieldparam [Snapshot] snapshot

# File lib/snapsync/snapper_config.rb, line 28
def each_snapshot(&block)
    Snapshot.each(snapshot_dir, &block)
end
load(path) click to toggle source

Load the information from a configuration file into this object

@see SnapperConfig.load

# File lib/snapsync/snapper_config.rb, line 110
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) click to toggle source

@api private

Extract the key and value from a snapper configuration file

@return [(String,String)] the key and value pair, or nil if it is an

empty or comment line
# File lib/snapsync/snapper_config.rb, line 96
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() click to toggle source

The directory containing the snapshots

# File lib/snapsync/snapper_config.rb, line 21
def snapshot_dir
    subvolume + ".snapshots"
end