Creates, saves and restores snapshots.

master
Luka Vandervelden 2020-11-28 21:21:01 +01:00
parent 4d4c248ec2
commit 6e651a53fa
1 changed files with 61 additions and 18 deletions

View File

@ -14,9 +14,7 @@ class Baguette::Configuration
end end
end end
module Baguette::Base module Baguette::Log
getter configuration : Configuration
def info(text) def info(text)
STDOUT << (":: ".colorize :blue) << (text.colorize :white) << "\n" STDOUT << (":: ".colorize :blue) << (text.colorize :white) << "\n"
end end
@ -26,10 +24,16 @@ module Baguette::Base
end end
def error(text) def error(text)
STDERR << (":: ".colorize :red) << (text.colorize :white) << "\n" STDERR << (":: ".colorize :red) << (text.colorize :red) << "\n"
end end
end end
module Baguette::Base
getter configuration : Configuration
include Baguette::Log
end
Colorize.on_tty_only! Colorize.on_tty_only!
# CAUTION # CAUTION
@ -48,7 +52,15 @@ class RootFS::Context
commands = { commands = {
"new" => ->(args : Array(String)){ "new" => ->(args : Array(String)){
} RootFS.new args[1], args[0]
},
"save" => ->(args : Array(String)){
RootFS.load(args[0]).save(args[1])
},
"restore" => ->(args : Array(String)){
RootFS.load(args[0]).restore(args[1])
},
# FIXME: This needs access to the list of commands.
"help" => ->(args : Array(String)){ "help" => ->(args : Array(String)){
STDOUT << options_parser << "\n" STDOUT << options_parser << "\n"
} }
@ -64,6 +76,11 @@ class RootFS::Context
Colorize.enabled = true Colorize.enabled = true
end end
parser.on "-h", "--help", "Displays this help message." do
STDOUT << parser << "\n"
exit 0
end
# FIXME: This leads to unusual behaviors when using the CLI. # FIXME: This leads to unusual behaviors when using the CLI.
# Replace by dedicated executables. # Replace by dedicated executables.
parser.unknown_args do |args| parser.unknown_args do |args|
@ -71,7 +88,7 @@ class RootFS::Context
raise "no command provided" if args.size < 1 raise "no command provided" if args.size < 1
command = args.pop command = args.shift
arguments = args arguments = args
end end
end end
@ -91,6 +108,8 @@ class RootFS::Context
end end
class RootFS::RootFS class RootFS::RootFS
include Baguette::Log
getter directory : String getter directory : String
def initialize(@directory) def initialize(@directory)
@ -103,13 +122,37 @@ class RootFS::RootFS
instance instance
end end
protected def execute(command : String)
info "Executing: #{command}"
r = Process.new(
"sh", ["-c", command],
input: Process::Redirect::Inherit,
output: Process::Redirect::Inherit,
error: Process::Redirect::Inherit
).wait
raise "Command returned #{r.exit_status}." unless r.success?
end
# FIXME: Abstract process calls.
# FIXME: REmove the sudos.
protected def create!(template : String) protected def create!(template : String)
show! # FIXME: Alternate backends.
puts "UNIMPLEMENTED: creating rootfs here" # FIXME: template string parsing.
is_btrfs = true # FIXME: do the actual detection
unless is_btrfs
raise "can only create rootfs in a btrfs filesystem"
end
execute "btrfs subvolume create '#{@directory}'"
execute "debootstrap --arch amd64 '#{template}' '#{@directory}'"
end end
def self.load(directory) def self.load(directory)
new directory, nil new directory
end end
def delete def delete
@ -117,28 +160,28 @@ class RootFS::RootFS
puts "UNIMPLEMENTED: removing rootfs directory here" puts "UNIMPLEMENTED: removing rootfs directory here"
end end
def save def save(snapshot_directory)
show! execute "btrfs subvolume snapshot '#{@directory}' '#{snapshot_directory}'"
puts "UNIMPLEMENTED: updating snapshot here"
end end
def restore def restore(snapshot_directory)
show! unmount
puts "UNIMPLEMENTED: restoring from snapshot here" execute "btrfs subvolume delete '#{@directory}'"
execute "btrfs subvolume snapshot '#{snapshot_directory}' '#{@directory}'"
end end
def mount def mount(directory = nil)
show! show!
puts "UNIMPLEMENTED: binding file systems here" puts "UNIMPLEMENTED: binding file systems here"
end end
def unmount def unmount(directory = nil)
show! show!
puts "UNIMPLEMENTED: unbinding file systems here" puts "UNIMPLEMENTED: unbinding file systems here"
end end
def show! def show!
puts "#{@template}, #{@directory}" puts "#{@directory}"
end end
end end