consul-template-generator 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/bin/consul-template-generator +46 -11
- data/lib/consul/template/generator.rb +1 -0
- data/lib/consul/template/generator/cmd.rb +17 -3
- data/lib/consul/template/generator/configuration.rb +3 -1
- data/lib/consul/template/generator/graphite.rb +21 -0
- data/lib/consul/template/generator/version.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +3 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8bc42e4ac6ae599136ce6b8c458d8a72bc1db9c2
         | 
| 4 | 
            +
              data.tar.gz: eaa801befcdbc1196ad2bc753c7ec5e0ff3960e1
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 5f036b92be9660f0478e4c6dd514fffa2ce65bb89f25e3ebd8818d3d4bcb5982d333d9a6fab02dc7fb8cb7dc9488278f1298c082479b72a2a1b9b1b45cb350be
         | 
| 7 | 
            +
              data.tar.gz: ed9399d2f4c696d89b7e30a69dfd7280066517ea7fabf578c3b35615967116468e556aff9e8814f4d5a2c0a3ff3fad5790443b8a33c3f54412997e97405dce7e
         | 
    
        data/Gemfile.lock
    CHANGED
    
    
| @@ -5,9 +5,28 @@ require 'consul/template/generator/cmd' | |
| 5 5 | 
             
            include Consul::Template::Generator
         | 
| 6 6 |  | 
| 7 7 | 
             
            def verify_opts(opts)
         | 
| 8 | 
            -
              !(opts[:templates].empty? || opts[:templates].any? { |k,v| v.nil? })
         | 
| 8 | 
            +
              templates_supplied = !(opts[:templates].empty? || opts[:templates].any? { |k,v| v.nil? })
         | 
| 9 | 
            +
              return false unless templates_supplied
         | 
| 10 | 
            +
              unless opts[:graphite_host].nil?
         | 
| 11 | 
            +
                return false if opts[:graphite_paths].empty?
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
              templates_supplied
         | 
| 9 14 | 
             
            end
         | 
| 10 15 |  | 
| 16 | 
            +
            def process_templates(t_string)
         | 
| 17 | 
            +
              Hash[t_string.split(',').collect do |tmpl|
         | 
| 18 | 
            +
                parts = tmpl.split(':')
         | 
| 19 | 
            +
                [parts[0], parts[1]]
         | 
| 20 | 
            +
              end]
         | 
| 21 | 
            +
            end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            def process_graphite_paths(t_string)
         | 
| 24 | 
            +
              return {} if t_string.split(',')[0].split(':').length < 3
         | 
| 25 | 
            +
              Hash[t_string.split(',').collect do |tmpl|
         | 
| 26 | 
            +
                parts = tmpl.split(':')
         | 
| 27 | 
            +
                [parts[0], parts[2]]
         | 
| 28 | 
            +
              end]
         | 
| 29 | 
            +
            end
         | 
| 11 30 |  | 
| 12 31 | 
             
            options = {}
         | 
| 13 32 | 
             
            opt_parser = OptionParser.new do |opts|
         | 
| @@ -27,8 +46,15 @@ EOC | |
| 27 46 | 
             
              end
         | 
| 28 47 |  | 
| 29 48 | 
             
              options[:templates] = {}
         | 
| 30 | 
            -
               | 
| 31 | 
            -
             | 
| 49 | 
            +
              options[:graphite_paths] = {}
         | 
| 50 | 
            +
              opts.on('-t TEMPLATES',
         | 
| 51 | 
            +
                      '--templates TEMPLATES',
         | 
| 52 | 
            +
                      'Comma separated list of consul-template ctmpl file and keys to monitor, tmple.ctmpl:tmple-key,tmple2.ctmpl:templ-key2  (required)',
         | 
| 53 | 
            +
                      'If \'--graphite-host\' is supplied, target graphite path must also be supplied, e.g.',
         | 
| 54 | 
            +
                      '    tmple.ctmpl:tmple-key:consul.template.HOSTNAME.<template name>,tmple2.ctmpl:tmple-key2:consul.template.HOSTNAME.<template name>',
         | 
| 55 | 
            +
                      'where \'HOSTNAME\' will be replaced as one might expect.') do |t|
         | 
| 56 | 
            +
                options[:templates] = process_templates(t)
         | 
| 57 | 
            +
                options[:graphite_paths] = process_graphite_paths(t)
         | 
| 32 58 | 
             
              end
         | 
| 33 59 |  | 
| 34 60 | 
             
              options[:session_key] = 'consul-template-generator'
         | 
| @@ -36,11 +62,6 @@ EOC | |
| 36 62 | 
             
                options[:session_key] = l
         | 
| 37 63 | 
             
              end
         | 
| 38 64 |  | 
| 39 | 
            -
              options[:proxy] = nil
         | 
| 40 | 
            -
              opts.on('-p PROXY_URL', '--proxy PROXY_URL', 'Proxy URL if required (e.g. http://proxy.example.com:3128)') do |p|
         | 
| 41 | 
            -
                options[:proxy] = p
         | 
| 42 | 
            -
              end
         | 
| 43 | 
            -
             | 
| 44 65 | 
             
              options[:unset_proxy] = false
         | 
| 45 66 | 
             
              opts.on(nil, '--unset-proxy', "Use if 'http_proxy' is set in your environment, but you don't want to use it...") do |u|
         | 
| 46 67 | 
             
                options[:unset_proxy] = true
         | 
| @@ -60,13 +81,27 @@ EOC | |
| 60 81 | 
             
              opts.on(nil, '--lock-sleep LOCK_SLEEP', "Sleep interval in seconds between each attempt to obtain a session lock [default: 1.0]") do |s|
         | 
| 61 82 | 
             
                options[:cycle_sleep] = s.to_f
         | 
| 62 83 | 
             
              end
         | 
| 84 | 
            +
             | 
| 85 | 
            +
              options[:graphite_host] = nil
         | 
| 86 | 
            +
              opts.on('-g GRAPHITE_HOST', '--graphite-host GRAPHITE_HOST', "Graphite host to post template update events to (optional)") do |g|
         | 
| 87 | 
            +
                if g.split(':').length != 2
         | 
| 88 | 
            +
                  STDERR.puts "GRAPHITE_HOST must be defined as <host>:<port> pair\n\n"
         | 
| 89 | 
            +
                  puts opt_parser
         | 
| 90 | 
            +
                  exit(1)
         | 
| 91 | 
            +
                end
         | 
| 92 | 
            +
                options[:graphite_host] = g
         | 
| 93 | 
            +
              end
         | 
| 63 94 | 
             
            end
         | 
| 64 95 |  | 
| 96 | 
            +
            opt_parser.version = VERSION
         | 
| 65 97 | 
             
            opt_parser.parse!
         | 
| 66 98 |  | 
| 67 | 
            -
             | 
| 68 99 | 
             
            unless verify_opts(options)
         | 
| 69 | 
            -
               | 
| 100 | 
            +
              if options[:graphite_host].nil?
         | 
| 101 | 
            +
                STDERR.puts "'--templates' must be provided with <template>:<key> pairs\n\n"
         | 
| 102 | 
            +
              else
         | 
| 103 | 
            +
                STDERR.puts "'--templates' must be provided with <template>:<key>:<graphite path> triplets\n\n"
         | 
| 104 | 
            +
              end
         | 
| 70 105 | 
             
              puts opt_parser
         | 
| 71 106 | 
             
              exit(1)
         | 
| 72 107 | 
             
            end
         | 
| @@ -75,7 +110,7 @@ if options[:unset_proxy] | |
| 75 110 | 
             
              ENV['http_proxy'] = nil
         | 
| 76 111 | 
             
            end
         | 
| 77 112 |  | 
| 78 | 
            -
            CMD.configure(options[:consul], options[:templates], options[:session_key], options[:log_level], options[: | 
| 113 | 
            +
            CMD.configure(options[:consul], options[:templates], options[:session_key], options[:log_level], options[:graphite_host], options[:graphite_paths])
         | 
| 79 114 |  | 
| 80 115 | 
             
            ec = 1
         | 
| 81 116 | 
             
            cmd = ARGV[0]
         | 
| @@ -2,6 +2,7 @@ require_relative 'generator/cmd' | |
| 2 2 | 
             
            require_relative 'generator/ct'
         | 
| 3 3 | 
             
            require_relative 'generator/configuration'
         | 
| 4 4 | 
             
            require_relative 'generator/error'
         | 
| 5 | 
            +
            require_relative 'generator/graphite'
         | 
| 5 6 | 
             
            require_relative 'generator/init'
         | 
| 6 7 | 
             
            require_relative 'generator/key_value'
         | 
| 7 8 | 
             
            require_relative 'generator/run'
         | 
| @@ -7,12 +7,14 @@ module Consul | |
| 7 7 | 
             
                    include Consul::Template::Generator
         | 
| 8 8 | 
             
                    class << self
         | 
| 9 9 |  | 
| 10 | 
            -
                      def configure(consul_host, templates, session_key, log_level,  | 
| 10 | 
            +
                      def configure(consul_host, templates, session_key, log_level, graphite_host = nil, graphite_paths = nil)
         | 
| 11 11 | 
             
                        Consul::Template::Generator.configure do |config|
         | 
| 12 12 | 
             
                          config.log_level = log_level
         | 
| 13 13 | 
             
                          config.templates = templates
         | 
| 14 14 | 
             
                          config.session_key = session_key
         | 
| 15 15 | 
             
                          config.consul_host = consul_host
         | 
| 16 | 
            +
                          config.graphite_host = graphite_host
         | 
| 17 | 
            +
                          config.graphite_paths = graphite_paths || {}
         | 
| 16 18 | 
             
                        end
         | 
| 17 19 | 
             
                        @config = Consul::Template::Generator.config
         | 
| 18 20 | 
             
                      end
         | 
| @@ -20,6 +22,7 @@ module Consul | |
| 20 22 | 
             
                      def configure_signal_handlers
         | 
| 21 23 | 
             
                        Signal.trap("INT") do
         | 
| 22 24 | 
             
                          @config.logger.error "Received INT signal..."
         | 
| 25 | 
            +
                          @terminated = true
         | 
| 23 26 | 
             
                          @interrupted = true
         | 
| 24 27 | 
             
                        end
         | 
| 25 28 | 
             
                        Signal.trap("TERM") do
         | 
| @@ -43,7 +46,13 @@ module Consul | |
| 43 46 | 
             
                            @config.logger.info "Session lock acquired..."
         | 
| 44 47 | 
             
                            begin
         | 
| 45 48 | 
             
                              @config.templates.each do |template,template_key|
         | 
| 46 | 
            -
                                 | 
| 49 | 
            +
                                new_hash = runner.run(template, template_key, uploaded_hashes[template])
         | 
| 50 | 
            +
                                unless new_hash.nil?
         | 
| 51 | 
            +
                                  uploaded_hashes[template] =  new_hash
         | 
| 52 | 
            +
                                  if @config.graphite_paths.include? template
         | 
| 53 | 
            +
                                    runner.post_graphite_event @config.graphite_paths[template]
         | 
| 54 | 
            +
                                  end
         | 
| 55 | 
            +
                                end
         | 
| 47 56 | 
             
                                sleep cycle_sleep
         | 
| 48 57 | 
             
                              end
         | 
| 49 58 | 
             
                            rescue ConsulSessionExpired
         | 
| @@ -70,7 +79,12 @@ module Consul | |
| 70 79 | 
             
                        begin
         | 
| 71 80 | 
             
                          @config.templates.each do |template,template_key|
         | 
| 72 81 | 
             
                            runner = CTRunner.new
         | 
| 73 | 
            -
                            result = runner.run | 
| 82 | 
            +
                            result = runner.run(template, template_key)
         | 
| 83 | 
            +
                            unless result.nil?
         | 
| 84 | 
            +
                              if @config.graphite_paths.include? template
         | 
| 85 | 
            +
                                runner.post_graphite_event @config.graphite_paths[template]
         | 
| 86 | 
            +
                              end
         | 
| 87 | 
            +
                            end
         | 
| 74 88 | 
             
                          end
         | 
| 75 89 | 
             
                        rescue Exception => e
         | 
| 76 90 | 
             
                          @config.logger.error "An unexpected error occurred, unable to process template: #{e.message}"
         | 
| @@ -39,7 +39,7 @@ module Consul | |
| 39 39 | 
             
                  end
         | 
| 40 40 |  | 
| 41 41 | 
             
                  class Configuration
         | 
| 42 | 
            -
                    attr_accessor :templates, :session_key, :consul_template_binary, :logger, :log_level
         | 
| 42 | 
            +
                    attr_accessor :templates, :session_key, :consul_template_binary, :logger, :log_level, :graphite_host, :graphite_paths
         | 
| 43 43 | 
             
                    attr_accessor :consul_host, :node, :client_options
         | 
| 44 44 |  | 
| 45 45 | 
             
                    def initialize
         | 
| @@ -47,9 +47,11 @@ module Consul | |
| 47 47 | 
             
                      @node = nil
         | 
| 48 48 | 
             
                      @consul_host = nil
         | 
| 49 49 | 
             
                      @templates = {}
         | 
| 50 | 
            +
                      @graphite_paths = {}
         | 
| 50 51 | 
             
                      @session_key = 'consul-template-generator'
         | 
| 51 52 | 
             
                      @client_options = {}
         | 
| 52 53 | 
             
                      @logger = Consul::Template::Generator::STDLogger
         | 
| 54 | 
            +
                      @graphite_host = nil
         | 
| 53 55 | 
             
                    end
         | 
| 54 56 |  | 
| 55 57 | 
             
                    def lock_key(key)
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            require 'socket'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Consul
         | 
| 4 | 
            +
              module Template
         | 
| 5 | 
            +
                module Generator
         | 
| 6 | 
            +
                  class CTRunner
         | 
| 7 | 
            +
                    def post_graphite_event(path)
         | 
| 8 | 
            +
                      @config.logger.debug "Posting event to graphite. Server: #{@config.graphite_host}, Path: #{path}"
         | 
| 9 | 
            +
                      host, port = @config.graphite_host.split(':')
         | 
| 10 | 
            +
                      begin
         | 
| 11 | 
            +
                        s = TCPSocket.open(host, port)
         | 
| 12 | 
            +
                        s.write("#{path} 1 #{Time.new.to_i}\n")
         | 
| 13 | 
            +
                        s.close
         | 
| 14 | 
            +
                      rescue Exception => e
         | 
| 15 | 
            +
                        @config.logger.error "An unknown error occurred while posting data to graphite: #{e.message}"
         | 
| 16 | 
            +
                      end
         | 
| 17 | 
            +
                    end
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    | @@ -101,7 +101,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ | |
| 101 101 | 
             
              SimpleCov::Formatter::HTMLFormatter,
         | 
| 102 102 | 
             
              SimpleCov::Formatter::Console
         | 
| 103 103 | 
             
            ]
         | 
| 104 | 
            -
            SimpleCov.minimum_coverage( | 
| 104 | 
            +
            SimpleCov.minimum_coverage(80)
         | 
| 105 105 | 
             
            SimpleCov.start
         | 
| 106 106 |  | 
| 107 107 | 
             
            def capture_stdout(&block)
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: consul-template-generator
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.3. | 
| 4 | 
            +
              version: 0.3.3
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Brian Oldfield
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2015-08- | 
| 11 | 
            +
            date: 2015-08-04 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         | 
| @@ -161,6 +161,7 @@ files: | |
| 161 161 | 
             
            - lib/consul/template/generator/configuration.rb
         | 
| 162 162 | 
             
            - lib/consul/template/generator/ct.rb
         | 
| 163 163 | 
             
            - lib/consul/template/generator/error.rb
         | 
| 164 | 
            +
            - lib/consul/template/generator/graphite.rb
         | 
| 164 165 | 
             
            - lib/consul/template/generator/init.rb
         | 
| 165 166 | 
             
            - lib/consul/template/generator/key_value.rb
         | 
| 166 167 | 
             
            - lib/consul/template/generator/run.rb
         |