diff options
| author | elijah <elijah@riseup.net> | 2014-06-27 17:19:51 -0700 | 
|---|---|---|
| committer | elijah <elijah@riseup.net> | 2014-06-27 17:19:51 -0700 | 
| commit | 09a82209f3a40e75caf966ba41b17da1a9ced146 (patch) | |
| tree | 586cfdff7b8d42736ab6559c917b9d2fce4b130c | |
| parent | 6da0270db08f734f2d586a0fc957875e86485549 (diff) | |
leap list improvements: lazy evaluation; don't bomb on ConfigError; remove requirements.rb
| -rw-r--r-- | Rakefile | 27 | ||||
| -rwxr-xr-x | bin/leap | 21 | ||||
| -rw-r--r-- | lib/leap_cli.rb | 1 | ||||
| -rw-r--r-- | lib/leap_cli/commands/list.rb | 9 | ||||
| -rw-r--r-- | lib/leap_cli/commands/pre.rb | 18 | ||||
| -rw-r--r-- | lib/leap_cli/config/manager.rb | 10 | ||||
| -rw-r--r-- | lib/leap_cli/config/object.rb | 75 | ||||
| -rw-r--r-- | lib/leap_cli/exceptions.rb | 3 | ||||
| -rw-r--r-- | lib/leap_cli/requirements.rb | 19 | 
9 files changed, 74 insertions, 109 deletions
| @@ -87,33 +87,6 @@ end  task :default => :test  ## -## CODE GENERATION -## - -desc "Updates the list of required configuration options for this version of LEAP CLI" -task 'update-requirements' do -  Dir.chdir($base_dir) do -    required_configs = `find -name '*.rb' | xargs grep -R 'assert_config!'`.split("\n").collect{|line| -      if line =~ /def/ || line =~ /pre\.rb/ -        nil -      else -        line.sub(/.*assert_config! ["'](.*?)["'].*/,'"\1"') -      end -    }.compact -    File.open("#{$base_dir}/lib/leap_cli/requirements.rb", 'w') do |f| -      f.puts "# run 'rake update-requirements' to generate this file." -      f.puts "module LeapCli" -      f.puts "  REQUIREMENTS = [" -      f.puts "    " + required_configs.join(",\n    ") -      f.puts "  ]" -      f.puts "end" -    end -    puts "updated #{$base_dir}/lib/leap_cli/requirements.rb" -    #puts `cat '#{$base_dir}/lib/leap_cli/requirements.rb'` -  end -end - -##  ## DOCUMENTATION  ## @@ -78,9 +78,26 @@ module LeapCli::Commands      exit(0)    end +  # disable GLI error catching +  ENV['GLI_DEBUG'] = "true" +  def error_message(msg) +  end +    # load commands and run    commands_from('leap_cli/commands')    ORIGINAL_ARGV = ARGV.dup -  exit_status = run(ARGV) -  exit(LeapCli::Util.exit_status || exit_status) +  begin +    exit_status = run(ARGV) +    exit(LeapCli::Util.exit_status || exit_status) +  rescue StandardError => exc +    if LeapCli.log_level < 2 +      if exc.respond_to? :log +        exc.log +      else +        puts "%s: %s" % [exc.class, exc.message] +      end +    else +      raise exc +    end +  end  end diff --git a/lib/leap_cli.rb b/lib/leap_cli.rb index 18a15a1..aa17655 100644 --- a/lib/leap_cli.rb +++ b/lib/leap_cli.rb @@ -9,7 +9,6 @@ require 'leap/platform'  require 'leap_cli/version'  require 'leap_cli/constants' -require 'leap_cli/requirements'  require 'leap_cli/exceptions'  require 'leap_cli/leapfile' diff --git a/lib/leap_cli/commands/list.rb b/lib/leap_cli/commands/list.rb index 5b84113..be9163b 100644 --- a/lib/leap_cli/commands/list.rb +++ b/lib/leap_cli/commands/list.rb @@ -15,15 +15,15 @@ module LeapCli; module Commands      c.flag 'print', :desc => 'What attributes to print (optional)'      c.switch 'disabled', :desc => 'Include disabled nodes in the list.', :negatable => false      c.action do |global_options,options,args| +      # don't rely on default manager(), because we want to pass custom options to load() +      manager = LeapCli::Config::Manager.new        if global_options[:color]          colors = ['cyan', 'white']        else          colors = [nil, nil]        end        puts -      if options['disabled'] -        manager.load(:include_disabled => true) # reload, with disabled nodes -      end +      manager.load(:include_disabled => options['disabled'], :continue_on_error => true)        if options['print']          print_node_properties(manager.filter(args), options['print'])        else @@ -45,7 +45,6 @@ module LeapCli; module Commands      properties = properties.split(',')      max_width = nodes.keys.inject(0) {|max,i| [i.size,max].max}      nodes.each_node do |node| -      node.evaluate        value = properties.collect{|prop|          if node[prop].nil?            "null" @@ -68,7 +67,7 @@ module LeapCli; module Commands        @colors = colors      end      def run -      tags = @tag_list.keys.sort +      tags = @tag_list.keys.select{|tag| tag !~ /^_/}.sort # sorted list of tags, excluding _partials        max_width = [20, (tags+[@heading]).inject(0) {|max,i| [i.size,max].max}].max        table :border => false do          row :color => @colors[0]  do diff --git a/lib/leap_cli/commands/pre.rb b/lib/leap_cli/commands/pre.rb index 4b62b5b..835eada 100644 --- a/lib/leap_cli/commands/pre.rb +++ b/lib/leap_cli/commands/pre.rb @@ -32,11 +32,6 @@ module LeapCli; module Commands      # set verbosity      #      LeapCli.log_level = global[:verbose].to_i -    if LeapCli.log_level > 1 -      ENV['GLI_DEBUG'] = "true" -    else -      ENV['GLI_DEBUG'] = "false" -    end      #      # load Leapfile @@ -68,18 +63,7 @@ module LeapCli; module Commands      log_version      LeapCli.log_in_color = global[:color] -    # -    # load all the nodes everything -    # -    manager - -    # -    # check requirements -    # -    REQUIREMENTS.each do |key| -      assert_config! key -    end - +    true    end    private diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index 1831de7..21dafd1 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -131,7 +131,15 @@ module LeapCli          # apply control files          @nodes.each do |name, node|            control_files(node).each do |file| -            node.instance_eval File.read(file), file, 1 +            begin +              node.eval_file file +            rescue ConfigError => exc +              if options[:continue_on_error] +                exc.log +              else +                raise exc +              end +            end            end          end        end diff --git a/lib/leap_cli/config/object.rb b/lib/leap_cli/config/object.rb index ef66757..cfa07cb 100644 --- a/lib/leap_cli/config/object.rb +++ b/lib/leap_cli/config/object.rb @@ -202,6 +202,10 @@ module LeapCli          end        end +      def eval_file(filename) +        evaluate_ruby(filename, File.read(filename)) +      end +        protected        # @@ -242,45 +246,42 @@ module LeapCli        # (`key` is just passed for debugging purposes)        #        def evaluate_ruby(key, value) -        result = nil -        if LeapCli.log_level >= 2 -          result = self.instance_eval(value) -        else -          begin -            result = self.instance_eval(value) -          rescue SystemStackError => exc -            Util::log 0, :error, "while evaluating node '#{self.name}'" -            Util::log 0, "offending key: #{key}", :indent => 1 -            Util::log 0, "offending string: #{value}", :indent => 1 -            Util::log 0, "STACK OVERFLOW, BAILING OUT. There must be an eval loop of death (variables with circular dependencies).", :indent => 1 -            raise SystemExit.new(1) -          rescue FileMissing => exc -            Util::bail! do -              if exc.options[:missing] -                Util::log :missing, exc.options[:missing].gsub('$node', self.name).gsub('$file', exc.path) -              else -                Util::log :error, "while evaluating node '#{self.name}'" -                Util::log "offending key: #{key}", :indent => 1 -                Util::log "offending string: #{value}", :indent => 1 -                Util::log "error message: no file '#{exc}'", :indent => 1 -              end -            end -          rescue AssertionFailed => exc -            Util.bail! do -              Util::log :failed, "assertion while evaluating node '#{self.name}'" -              Util::log 'assertion: %s' % exc.assertion, :indent => 1 -              Util::log "offending key: #{key}", :indent => 1 -            end -          rescue SyntaxError, StandardError => exc -            Util::bail! do -              Util::log :error, "while evaluating node '#{self.name}'" -              Util::log "offending key: #{key}", :indent => 1 -              Util::log "offending string: #{value}", :indent => 1 -              Util::log "error message: #{exc.inspect}", :indent => 1 -            end +        self.instance_eval(value, key, 1) +      rescue ConfigError => exc +        raise exc # pass through +      rescue SystemStackError => exc +        Util::log 0, :error, "while evaluating node '#{self.name}'" +        Util::log 0, "offending key: #{key}", :indent => 1 +        Util::log 0, "offending string: #{value}", :indent => 1 +        Util::log 0, "STACK OVERFLOW, BAILING OUT. There must be an eval loop of death (variables with circular dependencies).", :indent => 1 +        raise SystemExit.new(1) +      rescue FileMissing => exc +        Util::bail! do +          if exc.options[:missing] +            Util::log :missing, exc.options[:missing].gsub('$node', self.name).gsub('$file', exc.path) +          else +            Util::log :error, "while evaluating node '#{self.name}'" +            Util::log "offending key: #{key}", :indent => 1 +            Util::log "offending string: #{value}", :indent => 1 +            Util::log "error message: no file '#{exc}'", :indent => 1            end +          raise exc if LeapCli.log_level >= 2 +        end +      rescue AssertionFailed => exc +        Util.bail! do +          Util::log :failed, "assertion while evaluating node '#{self.name}'" +          Util::log 'assertion: %s' % exc.assertion, :indent => 1 +          Util::log "offending key: #{key}", :indent => 1 +          raise exc if LeapCli.log_level >= 2 +        end +      rescue SyntaxError, StandardError => exc +        Util::bail! do +          Util::log :error, "while evaluating node '#{self.name}'" +          Util::log "offending key: #{key}", :indent => 1 +          Util::log "offending string: #{value}", :indent => 1 +          Util::log "error message: #{exc.inspect}", :indent => 1 +          raise exc if LeapCli.log_level >= 2          end -        return result        end        private diff --git a/lib/leap_cli/exceptions.rb b/lib/leap_cli/exceptions.rb index 27993c2..24a0fa7 100644 --- a/lib/leap_cli/exceptions.rb +++ b/lib/leap_cli/exceptions.rb @@ -6,6 +6,9 @@ module LeapCli        @node = node        super(msg)      end +    def log +      Util.log(0, :error, "in node `#{@node.name}`: " + self.message) +    end    end    class FileMissing < StandardError diff --git a/lib/leap_cli/requirements.rb b/lib/leap_cli/requirements.rb deleted file mode 100644 index f1f0952..0000000 --- a/lib/leap_cli/requirements.rb +++ /dev/null @@ -1,19 +0,0 @@ -# run 'rake update-requirements' to generate this file. -module LeapCli -  REQUIREMENTS = [ -    "provider.ca.name", -    "provider.ca.server_certificates.bit_size", -    "provider.ca.server_certificates.digest", -    "provider.ca.server_certificates.life_span", -    "common.x509.use", -    "provider.domain", -    "provider.name", -    "provider.ca.server_certificates.bit_size", -    "provider.ca.server_certificates.digest", -    "provider.ca.name", -    "provider.ca.bit_size", -    "provider.ca.life_span", -    "provider.ca.client_certificates.unlimited_prefix", -    "provider.ca.client_certificates.limited_prefix" -  ] -end | 
