diff options
Diffstat (limited to 'lib/leap_cli/util.rb')
-rw-r--r-- | lib/leap_cli/util.rb | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb index 67fca8d..6095b2b 100644 --- a/lib/leap_cli/util.rb +++ b/lib/leap_cli/util.rb @@ -1,6 +1,14 @@ require 'md5' module LeapCli + + class FileMissing < Exception + attr_reader :file_path + def initialize(file_path) + @file_path = file_path + end + end + module Util extend self @@ -50,6 +58,18 @@ module LeapCli assert! `which #{cmd_name}`.strip.any?, "Sorry, bailing out, the command '%s' is not installed." % cmd_name end + # + # assert that the command is run without an error. + # if successful, return output. + # + def assert_run!(cmd, message) + log2(" * run: #{cmd}") + cmd = cmd + " 2>&1" + output = `#{cmd}` + assert!($?.success?, message) + return output + end + ## ## FILES AND DIRECTORIES ## @@ -67,7 +87,7 @@ module LeapCli end def progress_nochange(path) - progress 'no change %s' % relative_path(path) + progress2 'no change %s' % relative_path(path) end def progress_removed(path) @@ -93,23 +113,43 @@ module LeapCli NAMED_PATHS = { :user_ssh => 'users/#{arg}/#{arg}_ssh.pub', - :user_pgp => 'users/#{arg}/#{arg}_pgp.pub' + :user_pgp => 'users/#{arg}/#{arg}_pgp.pub', + :hiera => 'hiera/#{arg}.yaml', + :node_ssh_pub_key => 'files/nodes/#{arg}/#{arg}_ssh_key.pub', + :known_hosts => 'files/ssh/known_hosts', + :authorized_keys => 'files/ssh/authorized_keys' } - # - # read a file, exit if the file doesn't exist. - # - def read_file!(file_path) - if !File.exists?(file_path) - bail!("File '%s' does not exist." % file_path) - else - File.readfile(file_path) + def read_file!(*args) + begin + try_to_read_file!(*args) + rescue FileMissing => exc + bail!("File '%s' does not exist." % exc.file_path) + end + end + + def read_file(*args) + begin + try_to_read_file!(*args) + rescue FileMissing => exc + return nil end end + # + # Three ways to call: + # + # - write_file!(file_path, file_contents) + # - write_file!(named_path, file_contents) + # - write_file!(named_path, file_contents, argument) -- deprecated + # - write_file!([named_path, argument], file_contents) + # + # def write_file!(*args) if args.first.is_a? Symbol write_named_file!(*args) + elsif args.first.is_a? Array + write_named_file!(args.first[0], args.last, args.first[1]) else write_to_path!(*args) end @@ -123,15 +163,17 @@ module LeapCli end # - # saves a named file + # saves a named file. # - def write_named_file!(name, arg, contents) - assert!(NAMED_PATHS[name], "Error, I don't know the path for #{arg}") + def write_named_file!(name, contents, arg=nil) + fullpath = named_path(name, arg) + write_to_path!(fullpath, contents) + end + def named_path(name, arg=nil) + assert!(NAMED_PATHS[name], "Error, I don't know the path for :#{name} (with argument '#{arg}')") filename = eval('"' + NAMED_PATHS[name] + '"') fullpath = Path.provider + '/' + filename - - write_to_path!(fullpath, contents) end def write_to_path!(filepath, contents) @@ -166,6 +208,24 @@ module LeapCli end end + # + # trys to read a file, raise exception if the file doesn't exist. + # + def try_to_read_file!(*args) + if args.first.is_a? Symbol + file_path = named_path(args.first) + elsif args.first.is_a? Array + file_path = named_path(*args.first) + else + file_path = args.first + end + if !File.exists?(file_path) + raise FileMissing.new(file_path) + else + File.read(file_path) + end + end + end end |