aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/util.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-10-21 16:17:05 -0700
committerelijah <elijah@riseup.net>2012-10-21 16:17:05 -0700
commit94dd3b4d99a67679adcc3a9921f9e1fa78783cba (patch)
tree537e0a290801c2783ab24eb5c932c44c0c7f7c86 /lib/leap_cli/util.rb
parent338b3d97b8cb0f5fca0339514ffdfaf30cc65498 (diff)
downloadleap_cli-94dd3b4d99a67679adcc3a9921f9e1fa78783cba.tar.gz
leap_cli-94dd3b4d99a67679adcc3a9921f9e1fa78783cba.tar.bz2
added command init-node
Diffstat (limited to 'lib/leap_cli/util.rb')
-rw-r--r--lib/leap_cli/util.rb90
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