baptize 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21ee15f5fa163ebaa24036c18ed5f7aed06e4ba5
4
- data.tar.gz: b6606c765e138843fc0cce48d87a0c76f686db24
3
+ metadata.gz: ad98ebdbe3b5c570e1b16afd971758d7be6b8c94
4
+ data.tar.gz: f10151aab4e40be681ebf14c6e66f4881f63fbd0
5
5
  SHA512:
6
- metadata.gz: bc396d66c705cfd836ddbb777e1cb0d577216075e0124635ec120f2ffc24ce622c787eea5047cb928957c02c72a2719bf90a0e56d48c3f4df59349f041825d39
7
- data.tar.gz: 54ef22187f9def94e2b44b825de22f4507fa63384b5efb8876d2d729ef63c169e453859348a03999432c5ff01f92d32e13b92630eb355e3fc822c0e7b1593b36
6
+ metadata.gz: 2f737a1d1f690fafe84ccf3141ba1e4691b19416895079a1093f9b245c9135ee8c1148c69fbadd2388b7a48cec68d54dc84af7cbf32a23b41e418b2e04ca53f5
7
+ data.tar.gz: 194aa44252afe3379155932a781dbb81440cecb3d426dec36a6365f86bc59211b7d5162b33b7cc22c737f283c85b1be43b8fa10284a8bf858b37cb79ccc4d280
@@ -1,111 +1,180 @@
1
+ require 'shellwords'
2
+ require 'optparse'
3
+
1
4
  module Baptize
2
- class Application < Rake::Application
3
5
 
4
- def initialize
5
- super
6
- @rakefiles = %w(bapfile Bapfile bapfile.rb Bapfile.rb)
7
- Rake.application = self
8
- require 'baptize/rake'
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
- def name
12
- "baptize"
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 load_rakefile
16
- super
37
+ def run
17
38
  standard_exception_handling do
18
- in_namespace :packages do
19
- Baptize::Registry.packages.values.each do |package|
20
- @last_description = package.description
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 sort_options(options)
37
- super.push(version, dry_run, roles)
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 handle_options
41
- options.rakelib = ['rakelib']
42
- options.trace_output = $stderr
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 "Invoke (or simulate invoking) a task:"
51
- opts.separator " bundle exec baptize [--dry-run] TASK"
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 "Advanced options:"
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
- standard_rake_options.each { |args| opts.on(*args) }
61
- opts.environment('RAKEOPT')
62
- end.parse!
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 display_error_message(ex)
67
- unless options.backtrace
68
- if (loc = Rake.application.find_rakefile_location)
69
- whitelist = (@imported.dup << loc[0]).map{|f| File.absolute_path(f, loc[1])}
70
- pattern = %r@^(?!#{whitelist.map{|p| Regexp.quote(p)}.join('|')})@
71
- Rake.application.options.suppress_backtrace_pattern = pattern
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
- super
76
- end
77
-
78
- private
79
-
80
- def version
81
- ['--version', '-V',
82
- "Display the program version.",
83
- lambda { |_value|
84
- require 'capistrano/version'
85
- puts "Baptize Version: #{Baptize::VERSION} (Capistrano Version: #{Capistrano::VERSION}, Rake Version: #{RAKEVERSION})"
86
- exit
87
- }
88
- ]
89
- end
90
-
91
- def dry_run
92
- ['--dry-run', '-n',
93
- "Do a dry run without executing actions",
94
- lambda { |_value|
95
- raise "TODO: Port this"
96
- Configuration.env.set(:sshkit_backend, SSHKit::Backend::Printer)
97
- }
98
- ]
99
- end
100
-
101
- def roles
102
- ['--roles ROLES', '-r',
103
- "Run SSH commands only on hosts matching these roles",
104
- lambda { |value|
105
- raise "TODO: Port this"
106
- Configuration.env.add_cmdline_filter(:role, value)
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
@@ -1,3 +1,3 @@
1
1
  module Baptize
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end # module Baptize
data/lib/baptize.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'rake'
1
+ # require 'rake'
2
2
  require 'sshkit'
3
3
  require 'baptize/version'
4
4
  require 'baptize/registry'
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
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-07 00:00:00.000000000 Z
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