baptize 0.1.4 → 0.1.5
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/lib/baptize/application.rb +152 -83
- data/lib/baptize/commands.rb +46 -0
- data/lib/baptize/version.rb +1 -1
- data/lib/baptize.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: ad98ebdbe3b5c570e1b16afd971758d7be6b8c94
         | 
| 4 | 
            +
              data.tar.gz: f10151aab4e40be681ebf14c6e66f4881f63fbd0
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 2f737a1d1f690fafe84ccf3141ba1e4691b19416895079a1093f9b245c9135ee8c1148c69fbadd2388b7a48cec68d54dc84af7cbf32a23b41e418b2e04ca53f5
         | 
| 7 | 
            +
              data.tar.gz: 194aa44252afe3379155932a781dbb81440cecb3d426dec36a6365f86bc59211b7d5162b33b7cc22c737f283c85b1be43b8fa10284a8bf858b37cb79ccc4d280
         | 
    
        data/lib/baptize/application.rb
    CHANGED
    
    | @@ -1,111 +1,180 @@ | |
| 1 | 
            +
            require 'shellwords'
         | 
| 2 | 
            +
            require 'optparse'
         | 
| 3 | 
            +
             | 
| 1 4 | 
             
            module Baptize
         | 
| 2 | 
            -
              class Application < Rake::Application
         | 
| 3 5 |  | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 6 | 
            +
              class << self
         | 
| 7 | 
            +
                attr_accessor :application
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              class Command
         | 
| 11 | 
            +
                attr_reader :name, :description, :block
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def initialize(name, description=nil, &block)
         | 
| 14 | 
            +
                  @name, @description, @block = name.to_sym, description, block
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def invoke(*args)
         | 
| 18 | 
            +
                  block.call(*args)
         | 
| 9 19 | 
             
                end
         | 
| 10 20 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 21 | 
            +
              end # class Command
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              # Most of this is copy-paste fom Rake::Application
         | 
| 24 | 
            +
              # https://github.com/ruby/rake/blob/master/lib/rake/application.rb
         | 
| 25 | 
            +
              class Application
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                attr_accessor :commands
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def initialize
         | 
| 30 | 
            +
                  @original_dir = Dir.pwd
         | 
| 31 | 
            +
                  @bapfiles = %w(bapfile Bapfile bapfile.rb Bapfile.rb)
         | 
| 32 | 
            +
                  @commands = {}
         | 
| 33 | 
            +
                  Baptize.application = self
         | 
| 34 | 
            +
                  load 'baptize/commands.rb'
         | 
| 13 35 | 
             
                end
         | 
| 14 36 |  | 
| 15 | 
            -
                def  | 
| 16 | 
            -
                  super
         | 
| 37 | 
            +
                def run
         | 
| 17 38 | 
             
                  standard_exception_handling do
         | 
| 18 | 
            -
                     | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
                        define_task(Rake::Task, package.name.to_s) do
         | 
| 22 | 
            -
                          puts "Invoke package: #{package.name}"
         | 
| 23 | 
            -
                          Baptize::Registry.policies.keys.each do |role|
         | 
| 24 | 
            -
                            on roles(role), in: :parallel do |host|
         | 
| 25 | 
            -
                              Baptize::Registry.execution_scope.set :current_host, host
         | 
| 26 | 
            -
                              Baptize::Registry.execution_scope.set :current_ssh_connection, ssh_connection
         | 
| 27 | 
            -
                              package.execute
         | 
| 28 | 
            -
                            end
         | 
| 29 | 
            -
                          end
         | 
| 30 | 
            -
                        end
         | 
| 31 | 
            -
                      end
         | 
| 32 | 
            -
                    end
         | 
| 39 | 
            +
                    parse_input
         | 
| 40 | 
            +
                    load_bapfile
         | 
| 41 | 
            +
                    dispatch
         | 
| 33 42 | 
             
                  end
         | 
| 34 43 | 
             
                end
         | 
| 35 44 |  | 
| 36 | 
            -
                def  | 
| 37 | 
            -
                   | 
| 45 | 
            +
                def define_command(name, description=nil, &block)
         | 
| 46 | 
            +
                  Command.new(name, description, &block).tap do |command|
         | 
| 47 | 
            +
                    @commands[command.name] = command
         | 
| 48 | 
            +
                  end
         | 
| 38 49 | 
             
                end
         | 
| 39 50 |  | 
| 40 | 
            -
                def  | 
| 41 | 
            -
                   | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
                  OptionParser.new do |opts|
         | 
| 45 | 
            -
                    opts.banner = "Baptize prepares your servers"
         | 
| 46 | 
            -
                    opts.separator ""
         | 
| 47 | 
            -
                    opts.separator "Show available tasks:"
         | 
| 48 | 
            -
                    opts.separator "    bundle exec baptize -T"
         | 
| 51 | 
            +
                def parse_input
         | 
| 52 | 
            +
                  @arguments = OptionParser.new do |opts|
         | 
| 53 | 
            +
                    opts.banner = "baptize [OPTIONS] COMMAND"
         | 
| 49 54 | 
             
                    opts.separator ""
         | 
| 50 | 
            -
                    opts.separator " | 
| 51 | 
            -
             | 
| 55 | 
            +
                    opts.separator "COMMANDS are ..."
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    width = [commands.values.map(&:name).map(&:length), 31].flatten.max
         | 
| 58 | 
            +
                    commands.values.each do |command|
         | 
| 59 | 
            +
                      opts.separator sprintf("    %-#{width}s  %s\n",
         | 
| 60 | 
            +
                        command.name,
         | 
| 61 | 
            +
                        command.description)
         | 
| 62 | 
            +
                    end
         | 
| 63 | 
            +
             | 
| 52 64 | 
             
                    opts.separator ""
         | 
| 53 | 
            -
                    opts.separator " | 
| 65 | 
            +
                    opts.separator "OPTIONS are ..."
         | 
| 54 66 |  | 
| 55 67 | 
             
                    opts.on_tail("-h", "--help", "-H", "Display this help message.") do
         | 
| 56 68 | 
             
                      puts opts
         | 
| 57 69 | 
             
                      exit
         | 
| 58 70 | 
             
                    end
         | 
| 59 71 |  | 
| 60 | 
            -
                     | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 72 | 
            +
                    opts.on('--bapfile', '-f [FILENAME]', "Use FILENAME as the bapfile to search for.") do |value|
         | 
| 73 | 
            +
                      value ||= ''
         | 
| 74 | 
            +
                      @bapfiles.clear
         | 
| 75 | 
            +
                      @bapfiles << value
         | 
| 76 | 
            +
                    end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                    opts.on('--verbose', '-v', "Log message to standard output.") do |value|
         | 
| 79 | 
            +
                      Registry.execution_scope.set :verbose, true
         | 
| 80 | 
            +
                    end
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                    opts.on('--version', '-V', "Display the program version.") do |value|
         | 
| 83 | 
            +
                      puts "baptize, version #{Baptize::VERSION}"
         | 
| 84 | 
            +
                      exit
         | 
| 85 | 
            +
                    end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                  end.parse(ARGV)
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                def dispatch
         | 
| 91 | 
            +
                  args = @arguments.dup
         | 
| 92 | 
            +
                  command_name = args.shift&.to_sym
         | 
| 93 | 
            +
                  fail "No command given. Try baptize --help" if command_name.nil?
         | 
| 94 | 
            +
                  command = commands[command_name]
         | 
| 95 | 
            +
                  fail "Invalid command #{command_name}" unless command
         | 
| 96 | 
            +
                  command.invoke(*args)
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                def load_bapfile
         | 
| 100 | 
            +
                  standard_exception_handling do
         | 
| 101 | 
            +
                    raw_load_bapfile
         | 
| 102 | 
            +
                  end
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                def raw_load_bapfile # :nodoc:
         | 
| 106 | 
            +
                  bapfile, location = find_bapfile_location
         | 
| 107 | 
            +
                  fail "No Bapfile found (looking for: #{@bapfiles.join(', ')})" if bapfile.nil?
         | 
| 108 | 
            +
                  @bapfile = bapfile
         | 
| 109 | 
            +
                  Dir.chdir(location)
         | 
| 110 | 
            +
                  print_bapfile_directory(location)
         | 
| 111 | 
            +
                  load(File.expand_path(@bapfile)) if @bapfile && @bapfile != ''
         | 
| 63 112 | 
             
                end
         | 
| 64 113 |  | 
| 114 | 
            +
                def print_bapfile_directory(location) # :nodoc:
         | 
| 115 | 
            +
                  $stderr.puts "(in #{Dir.pwd})" unless @original_dir == location
         | 
| 116 | 
            +
                end
         | 
| 65 117 |  | 
| 66 | 
            -
                def  | 
| 67 | 
            -
                   | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 118 | 
            +
                def find_bapfile_location # :nodoc:
         | 
| 119 | 
            +
                  here = Dir.pwd
         | 
| 120 | 
            +
                  until (fn = have_bapfile)
         | 
| 121 | 
            +
                    Dir.chdir("..")
         | 
| 122 | 
            +
                    return nil if Dir.pwd == here # || options.nosearch
         | 
| 123 | 
            +
                    here = Dir.pwd
         | 
| 124 | 
            +
                  end
         | 
| 125 | 
            +
                  [fn, here]
         | 
| 126 | 
            +
                ensure
         | 
| 127 | 
            +
                  Dir.chdir(@original_dir)
         | 
| 128 | 
            +
                end
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                # True if one of the files in BAPFILES is in the current directory.
         | 
| 131 | 
            +
                # If a match is found, it is copied into @bapfile.
         | 
| 132 | 
            +
                def have_bapfile # :nodoc:
         | 
| 133 | 
            +
                  @bapfiles.each do |fn|
         | 
| 134 | 
            +
                    if File.exist?(fn)
         | 
| 135 | 
            +
                      others = Dir.glob(fn, File::FNM_CASEFOLD).sort
         | 
| 136 | 
            +
                      return others.size == 1 ? others.first : fn
         | 
| 137 | 
            +
                    elsif fn == ''
         | 
| 138 | 
            +
                      return fn
         | 
| 72 139 | 
             
                    end
         | 
| 73 | 
            -
                    trace "(Backtrace restricted to imported tasks)"
         | 
| 74 140 | 
             
                  end
         | 
| 75 | 
            -
                   | 
| 76 | 
            -
                end
         | 
| 77 | 
            -
             | 
| 78 | 
            -
                 | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
                   | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
                   | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
                   | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 141 | 
            +
                  return nil
         | 
| 142 | 
            +
                end
         | 
| 143 | 
            +
             | 
| 144 | 
            +
                def bapfile_location(backtrace=caller) # :nodoc:
         | 
| 145 | 
            +
                  backtrace.map { |t| t[/([^:]+):/, 1] }
         | 
| 146 | 
            +
             | 
| 147 | 
            +
                  re = /^#{@bapfile}$/
         | 
| 148 | 
            +
                  re = /#{re.source}/i if windows?
         | 
| 149 | 
            +
             | 
| 150 | 
            +
                  backtrace.find { |str| str =~ re } || ''
         | 
| 151 | 
            +
                end
         | 
| 152 | 
            +
             | 
| 153 | 
            +
                # Provide standard exception handling for the given block.
         | 
| 154 | 
            +
                def standard_exception_handling # :nodoc:
         | 
| 155 | 
            +
                  yield
         | 
| 156 | 
            +
                rescue SystemExit
         | 
| 157 | 
            +
                  # Exit silently with current status
         | 
| 158 | 
            +
                  raise
         | 
| 159 | 
            +
                rescue OptionParser::InvalidOption => ex
         | 
| 160 | 
            +
                  $stderr.puts ex.message
         | 
| 161 | 
            +
                  exit(false)
         | 
| 162 | 
            +
                rescue Exception => ex
         | 
| 163 | 
            +
                  # Exit with error message
         | 
| 164 | 
            +
                  display_error_message(ex)
         | 
| 165 | 
            +
                  exit_because_of_exception(ex)
         | 
| 166 | 
            +
                end
         | 
| 167 | 
            +
             | 
| 168 | 
            +
                # Display the error message that caused the exception.
         | 
| 169 | 
            +
                def display_error_message(ex) # :nodoc:
         | 
| 170 | 
            +
                  $stderr.puts "Error during processing: #{$!}"
         | 
| 171 | 
            +
                  $stderr.puts ex.backtrace.join("\n\t")
         | 
| 172 | 
            +
                end
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                # Exit the program because of an unhandled exception.
         | 
| 175 | 
            +
                # (may be overridden by subclasses)
         | 
| 176 | 
            +
                def exit_because_of_exception(ex) # :nodoc:
         | 
| 177 | 
            +
                  exit(false)
         | 
| 109 178 | 
             
                end
         | 
| 110 179 |  | 
| 111 180 | 
             
              end
         | 
| @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            Baptize.application.define_command :list, "Shows list of all available packages" do
         | 
| 2 | 
            +
              puts "# Packages"
         | 
| 3 | 
            +
              packages = Baptize::Registry.packages.values
         | 
| 4 | 
            +
              width = packages.map(&:name).map(&:length).max
         | 
| 5 | 
            +
              packages.each do |p|
         | 
| 6 | 
            +
                printf("%-#{width}s  # %s\n",
         | 
| 7 | 
            +
                  p.name,
         | 
| 8 | 
            +
                  p.description)
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Baptize.application.define_command :policies, "Shows configured policies" do
         | 
| 13 | 
            +
              puts "# Policies"
         | 
| 14 | 
            +
              registry = Baptize::Registry
         | 
| 15 | 
            +
              registry.policies.each do |role, packages|
         | 
| 16 | 
            +
                puts "#{role}:"
         | 
| 17 | 
            +
                packages.each do |package|
         | 
| 18 | 
            +
                  puts "  #{package}"
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
            Baptize.application.define_command :servers, "Show list of configured servers" do
         | 
| 24 | 
            +
              puts "# Servers"
         | 
| 25 | 
            +
              registry = Baptize::Registry
         | 
| 26 | 
            +
              registry.servers.values.each do |server|
         | 
| 27 | 
            +
                puts server.inspect
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
            end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
            Baptize.application.define_command :deploy, "Deploys the configured policies to remote servers" do
         | 
| 32 | 
            +
              registry = Baptize::Registry
         | 
| 33 | 
            +
              registry.policies.keys.each do |role|
         | 
| 34 | 
            +
                registry.for_role role, in: :parallel do |host|
         | 
| 35 | 
            +
                  registry.apply_policy role, host, self
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            Baptize.application.define_command :info, "Shows configuration" do
         | 
| 41 | 
            +
              Baptize.application.commands[:list].invoke
         | 
| 42 | 
            +
              puts
         | 
| 43 | 
            +
              Baptize.application.commands[:policies].invoke
         | 
| 44 | 
            +
              puts
         | 
| 45 | 
            +
              Baptize.application.commands[:servers].invoke
         | 
| 46 | 
            +
            end
         | 
    
        data/lib/baptize/version.rb
    CHANGED
    
    
    
        data/lib/baptize.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: baptize
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.5
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Troels Knak-Nielsen
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2016-01- | 
| 11 | 
            +
            date: 2016-01-08 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: sshkit
         | 
| @@ -51,6 +51,7 @@ files: | |
| 51 51 | 
             
            - bin/baptize
         | 
| 52 52 | 
             
            - lib/baptize.rb
         | 
| 53 53 | 
             
            - lib/baptize/application.rb
         | 
| 54 | 
            +
            - lib/baptize/commands.rb
         | 
| 54 55 | 
             
            - lib/baptize/dsl.rb
         | 
| 55 56 | 
             
            - lib/baptize/execution_scope.rb
         | 
| 56 57 | 
             
            - lib/baptize/package_definition.rb
         |