diff options
-rw-r--r-- | Rakefile | 4 | ||||
-rw-r--r-- | lib/leap_cli.rb | 6 | ||||
-rw-r--r-- | lib/leap_cli/bootstrap.rb | 15 | ||||
-rw-r--r-- | lib/leap_cli/commands/common.rb | 61 | ||||
-rw-r--r-- | lib/leap_cli/commands/new.rb | 2 | ||||
-rw-r--r-- | lib/leap_cli/config/environment.rb | 4 | ||||
-rw-r--r-- | lib/leap_cli/config/manager.rb | 5 | ||||
-rw-r--r-- | lib/leap_cli/leapfile.rb | 12 | ||||
-rw-r--r-- | lib/leap_cli/log.rb | 81 | ||||
-rw-r--r-- | lib/leap_cli/path.rb | 8 | ||||
-rw-r--r-- | lib/leap_cli/remote/leap_plugin.rb | 2 | ||||
-rw-r--r-- | lib/leap_cli/ssh_key.rb | 4 | ||||
-rw-r--r-- | lib/leap_cli/util.rb | 20 | ||||
-rw-r--r-- | lib/leap_cli/util/remote_command.rb | 6 | ||||
-rw-r--r-- | test/unit/command_line_test.rb | 2 | ||||
-rw-r--r-- | test/unit/config_object_list_test.rb | 1 |
16 files changed, 78 insertions, 155 deletions
@@ -37,7 +37,7 @@ end desc "Build #{$spec.name}-#{$spec.version}.gem into the pkg directory" task 'build' do FileUtils.mkdir_p(File.join($base_dir, 'pkg')) - FileUtils.rm($gem_path) if File.exists?($gem_path) + FileUtils.rm($gem_path) if File.exist?($gem_path) run "gem build -V '#{$spec_path}'" file_name = File.basename(built_gem_path) FileUtils.mv(built_gem_path, 'pkg') @@ -46,7 +46,7 @@ end desc "Install #{$spec.name}-#{$spec.version}.gem into either system-wide or user gems" task 'install' do - if !File.exists?($gem_path) + if !File.exist?($gem_path) puts("Could not file #{$gem_path}. Try running 'rake build'") else options = '--verbose --conservative --no-rdoc --no-ri' diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index 0563327..fc8ab2b 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -55,7 +55,7 @@ require 'leap_cli/markdown_document_listener' # allow everyone easy access to log() command. # module LeapCli - Util.send(:extend, LeapCli::Log) - Config::Manager.send(:include, LeapCli::Log) - extend LeapCli::Log + Util.send(:extend, LeapCli::LogCommand) + Config::Manager.send(:include, LeapCli::LogCommand) + extend LeapCli::LogCommand end diff --git a/lib/leap_cli/bootstrap.rb b/lib/leap_cli/bootstrap.rb index b7bc8e9..a6b1759 100644 --- a/lib/leap_cli/bootstrap.rb +++ b/lib/leap_cli/bootstrap.rb @@ -5,7 +5,6 @@ module LeapCli module Bootstrap - extend LeapCli::Log extend self # @@ -36,7 +35,7 @@ module LeapCli # called from leap executable. # def load_libraries(app) - if LeapCli.log_level >= 2 + if LeapCli.logger.log_level >= 2 log_version end load_commands(app) @@ -72,14 +71,14 @@ module LeapCli options = parse_logging_options(argv) verbose = (options[:verbose] || 1).to_i if verbose - LeapCli.set_log_level(verbose) + LeapCli.logger.log_level = verbose end if options[:log] - LeapCli.log_file = options[:log] - LeapCli::Util.log_raw(:log) { $0 + ' ' + argv.join(' ')} + LeapCli.logger.log_file = options[:log] + LeapCli.logger.log_raw(:log) { $0 + ' ' + argv.join(' ')} end unless options[:color].nil? - LeapCli.log_in_color = options[:color] + LeapCli.logger.log_in_color = options[:color] end end @@ -97,8 +96,8 @@ module LeapCli if !Path.platform || !File.directory?(Path.platform) bail! { log :missing, "platform directory '#{Path.platform}'" } end - if LeapCli.log_file.nil? && LeapCli.leapfile.log - LeapCli.log_file = LeapCli.leapfile.log + if LeapCli.logger.log_file.nil? && LeapCli.leapfile.log + LeapCli.logger.log_file = LeapCli.leapfile.log end elsif !leapfile_optional?(argv) puts diff --git a/lib/leap_cli/commands/common.rb b/lib/leap_cli/commands/common.rb deleted file mode 100644 index 7bf49db..0000000 --- a/lib/leap_cli/commands/common.rb +++ /dev/null @@ -1,61 +0,0 @@ -# -# Some common helpers available to all LeapCli::Commands -# -# This also includes utility methods, and makes all instance -# methods available as class methods. -# - -module LeapCli - module Commands - - extend self - extend LeapCli::Log - extend LeapCli::Util - extend LeapCli::Util::RemoteCommand - - protected - - def path(name) - Path.named_path(name) - end - - # - # keeps prompting the user for a numbered choice, until they pick a good one or bail out. - # - # block is yielded and is responsible for rendering the choices. - # - def numbered_choice_menu(msg, items, &block) - while true - say("\n" + msg + ':') - items.each_with_index &block - say("q. quit") - index = ask("number 1-#{items.length}> ") - if index.empty? - next - elsif index =~ /q/ - bail! - else - i = index.to_i - 1 - if i < 0 || i >= items.length - bail! - else - return i - end - end - end - end - - def parse_node_list(nodes) - if nodes.is_a? Config::Object - Config::ObjectList.new(nodes) - elsif nodes.is_a? Config::ObjectList - nodes - elsif nodes.is_a? String - manager.filter!(nodes) - else - bail! "argument error" - end - end - - end -end
\ No newline at end of file diff --git a/lib/leap_cli/commands/new.rb b/lib/leap_cli/commands/new.rb index 838b80e..5c9fd74 100644 --- a/lib/leap_cli/commands/new.rb +++ b/lib/leap_cli/commands/new.rb @@ -54,7 +54,7 @@ module LeapCli; module Commands unless directory && directory.any? help! "Directory name is required." end - unless File.exists?(directory) + unless File.exist?(directory) if global[:yes] || agree("Create directory #{directory}? ") ensure_dir directory else diff --git a/lib/leap_cli/config/environment.rb b/lib/leap_cli/config/environment.rb index df4b56c..398fd02 100644 --- a/lib/leap_cli/config/environment.rb +++ b/lib/leap_cli/config/environment.rb @@ -106,7 +106,7 @@ module LeapCli; module Config # def template(template) path = Path.named_path([:template_config, template], Path.provider_base) - if File.exists?(path) + if File.exist?(path) return load_json(path, Config::Object) else return nil @@ -133,7 +133,7 @@ module LeapCli; module Config end def load_json(filename, object_class, options={}) - if !File.exists?(filename) + if !File.exist?(filename) return object_class.new(self) end diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 5af046f..80ccbad 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -258,7 +258,7 @@ module LeapCli # yields each node, in sorted order # def each_node(&block) - env.nodes.each_node &block + env.nodes.each_node(&block) end def reload_node!(node) @@ -294,7 +294,6 @@ module LeapCli # def apply_inheritance(node, throw_exceptions=false) new_node = Config::Node.new(nil) - name = node.name node_env = guess_node_env(node) new_node.set_environment(node_env, new_node) @@ -409,7 +408,7 @@ module LeapCli [['services', :service_config], ['tags', :tag_config]].each do |attribute, path_sym| node[attribute].each do |attr_value| path = Path.named_path([path_sym, "#{attr_value}.rb"], provider_dir).sub(/\.json$/,'') - if File.exists?(path) + if File.exist?(path) files << path end end diff --git a/lib/leap_cli/leapfile.rb b/lib/leap_cli/leapfile.rb index 9164d0a..ac40237 100644 --- a/lib/leap_cli/leapfile.rb +++ b/lib/leap_cli/leapfile.rb @@ -62,7 +62,7 @@ module LeapCli # load the platform # platform_file = "#{@platform_directory_path}/platform.rb" - unless File.exists?(platform_file) + unless File.exist?(platform_file) Util.bail! "ERROR: The file `#{platform_file}` does not exist. Please check the value of `@platform_directory_path` in `Leapfile` or `~/.leaprc`." end require "#{@platform_directory_path}/platform.rb" @@ -73,12 +73,6 @@ module LeapCli "You need either leap command #{Leap::Platform.compatible_cli.first} to #{Leap::Platform.compatible_cli.last} or " + "platform version #{LeapCli::COMPATIBLE_PLATFORM_VERSION.first} to #{LeapCli::COMPATIBLE_PLATFORM_VERSION.last}" end - unless @allow_production_deploy.nil? - Util::log 0, :warning, "in Leapfile: @allow_production_deploy is no longer supported." - end - unless @platform_branch.nil? - Util::log 0, :warning, "in Leapfile: @platform_branch is no longer supported." - end @valid = true return @valid end @@ -105,7 +99,7 @@ module LeapCli def edit_leaprc(property, value=nil) file_path = leaprc_path lines = [] - if File.exists?(file_path) + if File.exist?(file_path) regexp = /self\.#{Regexp.escape(property)} = .*? if @provider_directory_path == '#{Regexp.escape(@provider_directory_path)}'/ File.readlines(file_path).each do |line| unless line =~ regexp @@ -128,7 +122,7 @@ module LeapCli end def read_settings(file) - if File.exists? file + if File.exist? file Util::log 2, :read, file instance_eval(File.read(file), file) validate(file) diff --git a/lib/leap_cli/log.rb b/lib/leap_cli/log.rb index 6589ad4..0497275 100644 --- a/lib/leap_cli/log.rb +++ b/lib/leap_cli/log.rb @@ -3,56 +3,50 @@ require 'paint' ## ## LOGGING ## -## Ugh. This class does not work well with multiple threads! -## module LeapCli - extend self - - attr_accessor :log_in_color - - # logging options - def log_level - @log_level ||= 1 - end - def set_log_level(value) - @log_level = value - end - - def indent_level - @indent_level ||= 0 - end - def indent_level=(value) - @indent_level = value - end + module LogCommand + def log(*args) + logger.log(*args) + end - def log_file - @log_file - end - def log_file=(value) - @log_file = value - if @log_file - if !File.directory?(File.dirname(@log_file)) - Util.bail!('Invalid log file "%s", directory "%s" does not exist' % [@log_file, File.dirname(@log_file)]) - end - @log_output_stream = File.open(@log_file, 'a') + def log_raw(*args) + logger.log(*args) end - end - def log_output_stream - @log_output_stream + def logger + @logger ||= LeapCli::LeapLogger.new + end end - end module LeapCli - module Log + class LeapLogger # # these are log titles typically associated with files # FILE_TITLES = [:updated, :created, :removed, :missing, :nochange, :loading] + attr_reader :log_output_stream, :log_file + attr_accessor :indent_level, :log_level, :log_in_color + + def initialize() + @log_level = 1 + @indent_level = 0 + @log_file = nil + @log_output_stream = nil + end + + def log_file=(value) + @log_file = value + if @log_file + if !File.directory?(File.dirname(@log_file)) + Util.bail!('Invalid log file "%s", directory "%s" does not exist' % [@log_file, File.dirname(@log_file)]) + end + @log_output_stream = File.open(@log_file, 'a') + end + end # # master logging function. @@ -65,13 +59,12 @@ module LeapCli # [:error, :warning, :info, :updated, :created, :removed, :no_change, :missing] # * Hash: a hash of options. so far, only :indent is supported. # - def log(*args) level = args.grep(Integer).first || 1 title = args.grep(Symbol).first message = args.grep(String).first options = args.grep(Hash).first || {} - unless message && LeapCli.log_level >= level + unless message && @log_level >= level return end @@ -117,7 +110,7 @@ module LeapCli end log_raw(:log, nil) { [clear_prefix, message].join } - if LeapCli.log_in_color + if @log_in_color log_raw(:stdout, options[:indent]) { [colored_prefix, message].join } else log_raw(:stdout, options[:indent]) { [clear_prefix, message].join } @@ -125,9 +118,9 @@ module LeapCli # run block, if given if block_given? - LeapCli.indent_level += 1 + @indent_level += 1 yield - LeapCli.indent_level -= 1 + @indent_level -= 1 end end @@ -142,20 +135,20 @@ module LeapCli def log_raw(mode, indent=nil, &block) # NOTE: print message (using 'print' produces better results than 'puts' when multiple threads are logging) if mode == :log - if LeapCli.log_output_stream + if @log_output_stream messages = [yield].compact.flatten if messages.any? timestamp = Time.now.strftime("%b %d %H:%M:%S") messages.each do |message| - LeapCli.log_output_stream.print("#{timestamp} #{message}\n") + @log_output_stream.print("#{timestamp} #{message}\n") end - LeapCli.log_output_stream.flush + @log_output_stream.flush end end elsif mode == :stdout messages = [yield].compact.flatten if messages.any? - indent ||= LeapCli.indent_level + indent ||= @indent_level indent_str = "" indent_str += " " * indent.to_i if indent.to_i > 0 diff --git a/lib/leap_cli/path.rb b/lib/leap_cli/path.rb index fd2e3fc..11fc0f1 100644 --- a/lib/leap_cli/path.rb +++ b/lib/leap_cli/path.rb @@ -40,14 +40,14 @@ module LeapCli; module Path [Path.provider, Path.provider_base].each do |base| if arg.is_a?(Symbol) || arg.is_a?(Array) named_path(arg, base).tap {|path| - return path if File.exists?(path) + return path if File.exist?(path) } else File.join(base, arg).tap {|path| - return path if File.exists?(path) + return path if File.exist?(path) } File.join(base, 'files', arg).tap {|path| - return path if File.exists?(path) + return path if File.exist?(path) } end end @@ -83,7 +83,7 @@ module LeapCli; module Path end def self.exists?(name, provider_dir=nil) - File.exists?(named_path(name, provider_dir)) + File.exist?(named_path(name, provider_dir)) end def self.defined?(name) diff --git a/lib/leap_cli/remote/leap_plugin.rb b/lib/leap_cli/remote/leap_plugin.rb index b48f433..ee33cea 100644 --- a/lib/leap_cli/remote/leap_plugin.rb +++ b/lib/leap_cli/remote/leap_plugin.rb @@ -148,7 +148,7 @@ module LeapCli; module Remote; module LeapPlugin ssh_failures = [] exitcode_failures = [] succeeded = [] - task = LeapCli.log_level > 1 ? :standard_task : :skip_errors_task + task = LeapCli.logger.log_level > 1 ? :standard_task : :skip_errors_task with_task(task) do log :querying, 'facts' do progress " " diff --git a/lib/leap_cli/ssh_key.rb b/lib/leap_cli/ssh_key.rb index 138f444..2570557 100644 --- a/lib/leap_cli/ssh_key.rb +++ b/lib/leap_cli/ssh_key.rb @@ -30,7 +30,7 @@ module LeapCli if arg1 =~ /^ssh-/ type, data = arg1.split(' ') key = SshKey.new load_from_data(data, type) - elsif File.exists? arg1 + elsif File.exist? arg1 key = SshKey.new load_from_file(arg1) key.filename = arg1 else @@ -38,7 +38,7 @@ module LeapCli end end return key - rescue StandardError => exc + rescue StandardError end def self.load_from_file(filename) diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb index 5014238..248a59c 100644 --- a/lib/leap_cli/util.rb +++ b/lib/leap_cli/util.rb @@ -38,7 +38,7 @@ module LeapCli # def bail!(*message) if block_given? - LeapCli.set_log_level(3) + LeapCli.logger.log_level = 3 yield elsif message log 0, *message @@ -119,7 +119,7 @@ module LeapCli base = options[:base] || Path.provider file_list = files.collect { |file_path| file_path = Path.named_path(file_path, base) - File.exists?(file_path) ? Path.relative_path(file_path, base) : nil + File.exist?(file_path) ? Path.relative_path(file_path, base) : nil }.compact if file_list.length > 1 bail! do @@ -138,7 +138,7 @@ module LeapCli options = files.last.is_a?(Hash) ? files.pop : {} file_list = files.collect { |file_path| file_path = Path.named_path(file_path) - !File.exists?(file_path) ? Path.relative_path(file_path) : nil + !File.exist?(file_path) ? Path.relative_path(file_path) : nil }.compact if file_list.length > 1 bail! do @@ -157,7 +157,7 @@ module LeapCli def file_exists?(*files) files.each do |file_path| file_path = Path.named_path(file_path) - if !File.exists?(file_path) + if !File.exist?(file_path) return false end end @@ -233,7 +233,7 @@ module LeapCli # def replace_file!(filepath, &block) filepath = Path.named_path(filepath) - if !File.exists?(filepath) + if !File.exist?(filepath) content = yield(nil) unless content.nil? write_file!(filepath, content) @@ -258,7 +258,7 @@ module LeapCli def remove_file!(filepath) filepath = Path.named_path(filepath) - if File.exists?(filepath) + if File.exist?(filepath) if File.directory?(filepath) remove_directory!(filepath) else @@ -298,7 +298,7 @@ module LeapCli def write_file!(filepath, contents) filepath = Path.named_path(filepath) ensure_dir File.dirname(filepath) - existed = File.exists?(filepath) + existed = File.exist?(filepath) if existed if file_content_equals?(filepath, contents) log :nochange, filepath, 2 @@ -320,11 +320,11 @@ module LeapCli def rename_file!(oldpath, newpath) oldpath = Path.named_path(oldpath) newpath = Path.named_path(newpath) - if File.exists? newpath + if File.exist? newpath log :skipping, "#{Path.relative_path(newpath)}, file already exists" return end - if !File.exists? oldpath + if !File.exist? oldpath log :skipping, "#{Path.relative_path(oldpath)}, file is missing" return end @@ -429,7 +429,7 @@ module LeapCli Dir.chdir(dir) do branch = `git symbolic-ref HEAD 2>/dev/null`.strip if branch.chars.any? - branch.sub /^refs\/heads\//, '' + branch.sub(/^refs\/heads\//, '') else nil end diff --git a/lib/leap_cli/util/remote_command.rb b/lib/leap_cli/util/remote_command.rb index 10a5ca8..c2f1ace 100644 --- a/lib/leap_cli/util/remote_command.rb +++ b/lib/leap_cli/util/remote_command.rb @@ -13,7 +13,7 @@ module LeapCli; module Util; module RemoteCommand node_list = parse_node_list(nodes) cap = new_capistrano - cap.logger = LeapCli::Logger.new(:level => [LeapCli.log_level,3].min) + cap.logger = LeapCli::Logger.new(:level => [LeapCli.logger.log_level,3].min) user = options[:user] || 'root' cap.set :user, user cap.set :ssh_options, ssh_options # ssh options common to all nodes @@ -85,7 +85,7 @@ module LeapCli; module Util; module RemoteCommand def net_ssh_log_level if DEBUG - case LeapCli.log_level + case LeapCli.logger.log_level when 1 then 3 when 2 then 2 when 3 then 1 @@ -145,7 +145,7 @@ module LeapCli; module Util; module RemoteCommand opts[:keys] = [vagrant_ssh_key_file] opts[:keys_only] = true # only use the keys specified above, and ignore whatever keys the ssh-agent is aware of. opts[:paranoid] = false # we skip host checking for vagrant nodes, because fingerprint is different for everyone. - if LeapCli::log_level <= 1 + if LeapCli.logger.log_level <= 1 opts[:verbose] = :error # suppress all the warnings about adding host keys to known_hosts, since it is not actually doing that. end end diff --git a/test/unit/command_line_test.rb b/test/unit/command_line_test.rb index 0b57ed0..ea0d1cc 100644 --- a/test/unit/command_line_test.rb +++ b/test/unit/command_line_test.rb @@ -21,7 +21,7 @@ class CommandLineTest < Minitest::Test cleanup_files('nodes/banana.json', 'files/nodes/banana') output = leap_bin("node add banana tags:production "+ "services:openvpn ip_address:1.1.1.1 openvpn.gateway_address:2.2.2.2") - assert_match /created nodes\/banana\.json/, output + assert_match(/created nodes\/banana\.json/, output) end end diff --git a/test/unit/config_object_list_test.rb b/test/unit/config_object_list_test.rb index 9b6e09f..c4f37d8 100644 --- a/test/unit/config_object_list_test.rb +++ b/test/unit/config_object_list_test.rb @@ -9,7 +9,6 @@ class ConfigObjectListTest < Minitest::Test end def test_complex_node_search - domain = provider.domain nodes = manager.nodes['location.country_code' => 'US'] assert nodes.size != manager.nodes.size, 'should not return all nodes' assert_equal 2, nodes.size, 'should be some nodes' |