magicmonkey 1.0.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,8 +1,7 @@
1
1
  = Magicmonkey
2
2
 
3
- Magicmonkey is a useful script that can manage your Ruby on Rails applications.
4
- Magicmonkey allows you to build in easy way the follow web server architecture:
5
-
3
+ Magicmonkey is a useful script that can help you to manage your Ruby on Rails applications.
4
+ Magicmonkey allows to build in easy way the follow web server architecture:
6
5
 
7
6
  +---------------------------------------------------------------------------------------------------+
8
7
  | Apache web server |
@@ -29,7 +28,7 @@ Every your web applications have a virtual host like this:
29
28
  ProxyPassReverse / http://127.0.0.1:3000/
30
29
  </VirtualHost>
31
30
 
32
- A proxy that speak with an application server standalone mode (like Phusion Passenger v3) that run with a specific Ruby version.
31
+ A proxy that speak with an application server in standalone mode (like Phusion Passenger v3) that run with a specific Ruby version.
33
32
  To do this you need to have install RVM to manage multiply Ruby versions.
34
33
 
35
34
  == Install
@@ -38,15 +37,15 @@ To do this you need to have install RVM to manage multiply Ruby versions.
38
37
 
39
38
  == Usage
40
39
 
41
- First of all you need to say to Magicmonkey that you want manage an application in this way. To to this use the magicmonkey command 'ADD':
40
+ First of all you need to configure an application. To to this use the magicmonkey command 'configure':
42
41
 
43
- magicmonkey add APP_NAME [options]
42
+ magicmonkey configure APP_NAME [options]
44
43
 
45
- When added, you can start, stop, restart the application
44
+ When configured, you can start, stop, restart the application
46
45
 
47
46
  magicmonkey {start|stop|restart} APP_NAME1, APP_NAME2, ...
48
47
 
49
- If no application is given start, stop, restart all application.
48
+ If no application is given start, stop, restart all applications.
50
49
 
51
50
  You can show the configuration of all your applications with
52
51
 
data/bin/magicmonkey CHANGED
@@ -3,4 +3,4 @@
3
3
  $APP_PATH = File.expand_path("#{File.dirname(__FILE__)}/..")
4
4
  require "#{$APP_PATH}/lib/magicmonkey/magicmonkey"
5
5
 
6
- MagicMonkey.main
6
+ Magicmonkey.main
@@ -1,17 +1,11 @@
1
1
  module Passenger
2
2
 
3
3
  def self.start(args = {})
4
- bundle_exec = 'bundle exec ' if args[:bundle_exec]
5
- return "#{bundle_exec}passenger start -e production -p #{args[:port]} #{args[:app_server_options]} -d"
4
+ ["passenger start -e production -p #{args[:port]} #{args[:app_server_options]} -d"]
6
5
  end
7
6
 
8
7
  def self.stop(args = {})
9
- bundle_exec = 'bundle exec ' if args[:bundle_exec]
10
- return "#{bundle_exec}passenger stop -p #{args[:port]}"
11
- end
12
-
13
- def self.restart(args = {})
14
- [self.stop(args), 'sleep 3', self.start(args)]
8
+ ["passenger stop -p #{args[:port]}"]
15
9
  end
16
10
 
17
11
  end
@@ -1,17 +1,11 @@
1
1
  module Thin
2
2
 
3
3
  def self.start(args = {})
4
- bundle_exec = 'bundle exec ' if args[:bundle_exec]
5
- return "#{bundle_exec}thin start -e production -p #{args[:port]} #{args[:app_server_options]} -d"
4
+ ["thin start -e production -p #{args[:port]} #{args[:app_server_options]} -d"]
6
5
  end
7
6
 
8
7
  def self.stop(args = {})
9
- bundle_exec = 'bundle exec ' if args[:bundle_exec]
10
- return "#{bundle_exec}thin stop -p #{args[:port]}"
11
- end
12
-
13
- def self.restart(args = {})
14
- [self.stop(args), 'sleep 3', self.start(args)]
8
+ ["thin stop -p #{args[:port]}"]
15
9
  end
16
10
 
17
11
  end
@@ -0,0 +1,11 @@
1
+ module UnicornRails
2
+
3
+ def self.start(args = {})
4
+ ["unicorn_rails -E production -p #{args[:port]} -c #{args[:app_path]}/config/unicorn.rb #{args[:app_server_options]} -D"]
5
+ end
6
+
7
+ def self.stop(args = {})
8
+ ["kill `ps ax | grep -v grep | grep -e 'unicorn_rails .*-p #{args[:port]}' | awk 'NR==1{print $1}'`"]
9
+ end
10
+
11
+ end
@@ -16,17 +16,16 @@ EOT
16
16
  DEFAULT = {
17
17
  :app_server => nil,
18
18
  :app_server_options => nil,
19
- :port => nil,
20
19
  :ruby => 'default',
21
- :app_path => '/var/sites/$APP_NAME/current',
22
- :server_name => '$APP_NAME.local',
20
+ :app_path => '/var/sites/$APP/current',
21
+ :bundle_exec => true,
23
22
  :vhost_template => VHT,
24
23
  :vhost_path => '/etc/apache2/sites-available',
25
- :overwrite_files => false,
26
- :create_vhost => true,
24
+ :overwrite_file => false,
27
25
  :enable_site => false,
28
26
  :reload_apache => false,
29
- :bundle_exec => true
27
+ :editor => 'nano',
28
+ :verbose => false
30
29
  }
31
30
 
32
31
  def initialize
@@ -1,26 +1,29 @@
1
1
  require 'optparse'
2
2
  require 'pp'
3
+ require 'tempfile'
3
4
  require 'cocaine'
4
5
  require 'term/ansicolor'
5
6
  require "#{$APP_PATH}/lib/magicmonkey/version"
6
7
  require "#{$APP_PATH}/lib/magicmonkey/configuration"
7
8
  include Term::ANSIColor
8
9
 
9
- module MagicMonkey
10
+ module Magicmonkey
10
11
 
11
- COMMANDS = [:start, :stop, :restart, :add, :remove, :show]
12
+ @o = {}
13
+
14
+ COMMANDS = %w(configure deconfigure show vhost start stop restart)
12
15
 
13
16
  def self.main
14
17
  #Process::UID.change_privilege(Conf[:uid] || Process.uid)
15
18
  raise 'You cannot do this as root' if Process.uid == 0
16
19
 
17
- options = Marshal.load(Marshal.dump(Conf[:default]))
20
+ @o = Marshal.load(Marshal.dump(Conf[:default]))
18
21
  parser = OptionParser.new do |opts|
19
22
  opts.banner = 'Usage: magicmonkey <command> [<args>]'
20
23
  opts.separator ''
21
24
  opts.separator "Commands: #{COMMANDS.join(' ')}"
22
25
  opts.separator 'For more information about a specific command, please type'
23
- opts.separator "'magicmonkey <command> --help', e.g. 'magicmonkey add --help'"
26
+ opts.separator "'magicmonkey <command> --help', e.g. 'magicmonkey configure --help'."
24
27
  opts.separator ''
25
28
  opts.separator 'Options:'
26
29
  opts.on_tail('-v', '--version', 'Print version') { puts Magicmonkey::VERSION; exit }
@@ -28,252 +31,247 @@ module MagicMonkey
28
31
  end
29
32
  begin
30
33
  parser.order!
31
- command = ARGV[0]
32
- ARGV.shift
33
- raise 'Invalid command.' unless COMMANDS.include?(command.to_sym)
34
+ command = (ARGV & COMMANDS).first
35
+ raise 'Invalid command.' unless command
36
+ ARGV.delete(command)
34
37
  rescue => e
35
- rputs e
36
38
  puts parser.help; exit
37
39
  end
40
+ app = ARGV.first
41
+ self.send(command, ARGV)
42
+ end
43
+
44
+ #####################
45
+ # CONFIGURE COMMAND #
46
+ #####################
47
+ def self.configure(args)
48
+ app = self.help('configure', 'DESC', args) do |opts|
49
+ opts.on('-c', '--configuration CONFIG_FILE', 'Use this file as configuration') do |c|
50
+ @o[:configuration] = c
51
+ end
52
+ end
38
53
 
39
- case command
40
- when 'add'
41
- self.add(ARGV, options)
42
- when 'start'
43
- start_stop_restart(:start, ARGV)
44
- when 'stop'
45
- start_stop_restart(:stop, ARGV)
46
- when 'restart'
47
- start_stop_restart(:restart, ARGV)
48
- when 'remove'
49
- remove(ARGV)
50
- when 'show'
51
- show(ARGV)
54
+ app_conf = Conf[app.to_sym]
55
+ unless app_conf
56
+ app_conf = @o.select{|k,v| [:app_server, :app_server_options, :ruby, :app_path, :bundle_exec].include?(k)}
57
+ app_conf[:app_path].gsub!('$APP', app)
58
+ app_conf[:port] = Conf.next_port
52
59
  end
60
+ tmpfile = Tempfile.new("#{app}.yml")
61
+ tmpfile.write(app_conf.to_yaml)
62
+ tmpfile.close
63
+ system("#{@o[:editor]} '#{tmpfile.path}'")
64
+ conf = YAML.load_file(tmpfile.path)
65
+ tmpfile.unlink
66
+
67
+ self.check_port!(conf[:port]) if app_conf[:port] != conf[:port]
68
+ self.check_ruby_version!(conf[:ruby])
69
+ self.check_app_server!(conf[:app_server])
70
+
71
+ Conf[app.to_sym] = conf
72
+ Conf.save
53
73
  end
54
74
 
75
+ #######################
76
+ # DECONFIGURE COMMAND #
77
+ #######################
78
+ def self.deconfigure(args)
79
+ app = help('deconfigure', 'The application will no longer be handled by magicmonkey.', args)
80
+ app_conf = Conf[app.to_sym]
81
+ unless app_conf
82
+ rputs "Application '#{app}' does not configured."
83
+ puts "Use 'magicmonkey configure #{app}' to configure it."
84
+ exit
85
+ else
86
+ Conf.delete(app)
87
+ Conf.save
88
+ end
89
+ end
55
90
 
56
- def self.add(args, o = {})
57
- tmp = args.join('$$').split(/\$\$--\$\$/)
58
- args = tmp[0].split('$$')
59
- o[:app_server_options] = tmp[1].split('$$').join(' ') if tmp[1]
91
+ ################
92
+ # SHOW COMMAND #
93
+ ################
94
+ def self.show(args)
95
+ applications = help2('show', 'Shows the configurations of selected applications', args)
96
+ applications.each do |app|
97
+ puts app.upcase
98
+ pp Conf[app]
99
+ puts '-' * 80
100
+ end
101
+ end
60
102
 
61
- parser = OptionParser.new do |opts|
62
- opts.banner = 'Usage: magicmonkey add APP_NAME [options] -- application_server_options'
63
- opts.separator ''
64
- opts.separator 'Options:'
65
- opts.on('-s', '--app-server APP_SERVER', "Use the given application server (e.g. passenger, thin, unicorn, default: #{o[:app_server]}).") do |s|
66
- o[:app_server] = s
67
- end
68
- opts.on('-p', '--port NUMBER', Integer, "Use the given port number (default: #{Conf.next_port}).") do |p|
69
- o[:port] = p
70
- end
71
- opts.on('-r', '--ruby RUBY_VERSION', "Use the given Ruby version (default: #{o[:ruby]}).") do |r|
72
- o[:ruby] = r
73
- end
74
- opts.on('--app-path APP_PATH', "Use the given application path (default #{o[:app_path]}).") do |path|
75
- o[:app_path] = path
76
- end
77
- opts.on('--server-name SERVER_NAME', "Use the given server name (default: #{o[:server_name]}).") do |name|
78
- o[:server_name] = name
103
+ #################
104
+ # VHOST COMMAND #
105
+ #################
106
+ def self.vhost(args)
107
+ app = self.help('vhost', 'Creates the Apache virtual host file for an application.', args) do |opts|
108
+ opts.on('-s', '--server-name SERVER_NAME', 'The server name') do |s|
109
+ @o[:server_name] = s
79
110
  end
80
- opts.on('--vhost-template TEMPLATE', "Use the given virtual host template file.") do |template|
81
- o[:vhost_template] = template
111
+ opts.on('--path VHOST_PATH', "Use the given virtual host path (default: '#{@o[:vhost_path]}')") do |path|
112
+ @o[:vhost_path] = path
82
113
  end
83
- opts.on('--vhost-path VHOST_PATH', "Use the given virtual host path (default: '#{o[:vhost_path]}').") do |path|
84
- o[:vhost_path] = path
114
+ opts.on('-f', '--[no-]overwrite-file', "Replace exist file (default: #{@o[:overwrite_file]})") do |f|
115
+ @o[:overwrite_file] = f
85
116
  end
86
- opts.on('-f', '--[no-]overwrite-files', "Replace exist files (default: #{o[:overwrite_files]}).") do |f|
87
- o[:overwrite_files] = f
117
+ opts.on('--[no-]enable-site', "Enable Apache virtual host (default: #{@o[:enable_site]})") do |e|
118
+ @o[:enable_site] = e
88
119
  end
89
- opts.on('--[no-]create-vhost', "Create virtual host file from template (default: #{o[:create_vhost]}).") do |c|
90
- o[:create_vhost] = c
120
+ opts.on('--[no-]reload-apache', "Reload apache to load virtual host (default: #{@o[:reload_apache]})") do |r|
121
+ @o[:reload_apache] = r
91
122
  end
92
- opts.on('--[no-]enable-site', "Enable Apache virtual host (default: #{o[:enable_site]}).") do |e|
93
- o[:enable_site] = e
94
- end
95
- opts.on('--[no-]reload-apache', "Reload apache to load virtual host (default: #{o[:reload_apache]}).") do |r|
96
- o[:reload_apache] = r
97
- end
98
- opts.on('--[no-]bundle-exec', "Use bundle exec to run application server (default: #{o[:bundle_exec]}).") do |b|
99
- o[:bundle_exec] = b
100
- end
101
- opts.on_tail('-v', '--version', 'Print version') { puts Magicmonkey::VERSION; exit }
102
- opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
103
- end
104
- begin
105
- args = parser.parse!(args)
106
- raise 'Missing application name.' if args.size != 1
107
- rescue => e
108
- rputs e
109
- puts parser.help; exit
110
123
  end
111
124
 
112
- # Ok goo
113
- app_name = args.first
114
- if Conf.applications.include?(app_name.to_sym)
115
- rputs "Application '#{app_name}' already added. Try to use another name."
125
+ app_conf = Conf[app.to_sym]
126
+ unless app_conf
127
+ rputs "Application '#{app}' does not configured."
128
+ puts "Use 'magicmonkey configure #{app}' to configure it."
116
129
  exit
117
130
  end
118
- o[:app_path].gsub!('$APP_NAME', app_name) if o[:app_path] == Conf[:default][:app_path]
119
- o[:server_name].gsub!('$APP_NAME', app_name) if o[:server_name] == Conf[:default][:server_name]
120
- o[:port] ||= Conf.next_port
121
- if Conf.ports.include?(o[:port])
122
- rputs 'Invalid port number. This port is used by another application or is invalid.'
123
- exit
124
- end
125
- self.check_ruby_version!(o[:ruby])
126
- self.check_app_server!(o[:app_server])
127
- o[:vhost_template].gsub!('$APP_NAME', app_name)
128
- o[:vhost_template].gsub!('$SERVER_NAME', o[:server_name])
129
- o[:vhost_template].gsub!('$PORT', o[:port].to_s)
130
131
 
131
- puts "Configuration for application '#{app_name}' is:"
132
- pp o
133
- print 'Add this application? [Y/n]'
134
- input = STDIN.gets.chop
135
- if input.upcase == 'Y' || input == ''
136
- if o[:create_vhost]
137
- vh_file = "#{o[:vhost_path]}/#{app_name}"
138
- if (!File.exist?(vh_file) || o[:overwrite_files])
139
- begin
140
- Cocaine::CommandLine.new("sudo bash -c \"echo '#{o[:vhost_template]}' > #{vh_file}\"").run
141
- rescue Cocaine::ExitStatusError => e
142
- rputs 'Failed to write virtual host file.'
143
- end
144
- else
145
- puts "Virtual host file '#{vh_file}' already exist. Use option '-f' to replace it. Skip creation."
146
- end
132
+ @o[:vhost_template].gsub!('$SERVER_NAME', @o[:server_name] || app)
133
+ @o[:vhost_template].gsub!('$PORT', app_conf[:port].to_s)
134
+
135
+ vh_file = File.join(@o[:vhost_path], app)
136
+ if (!File.exist?(vh_file) || @o[:overwrite_file])
137
+ begin
138
+ Cocaine::CommandLine.new("sudo echo '#{@o[:vhost_template]}' > '#{vh_file}'").run
139
+ rescue Cocaine::ExitStatusError => e
140
+ rputs 'Failed to write virtual host file.'
147
141
  end
148
- if o[:enable_site]
149
- begin
150
- Cocaine::CommandLine.new("sudo a2ensite '#{app_name}'").run
151
- rescue Cocaine::ExitStatusError => e
152
- rputs 'Failed to enable the site.'
153
- end
142
+ else
143
+ rputs 'Failed to write virtual host file.'
144
+ puts "Virtual host file '#{vh_file}' already exist. Use option '-f' to replace it."
145
+ end
146
+ if @o[:enable_site]
147
+ begin
148
+ Cocaine::CommandLine.new("sudo a2ensite '#{app}'").run
149
+ rescue Cocaine::ExitStatusError => e
150
+ rputs 'Failed to enable the site.'
154
151
  end
155
- if o[:enable_site] && o[:reload_apache]
156
- begin
157
- Cocaine::CommandLine.new('sudo /etc/init.d/apache2 reload').run
158
- rescue Cocaine::ExitStatusError => e
159
- rputs 'Failed to reload Apache.'
160
- end
152
+ end
153
+ if @o[:enable_site] && @o[:reload_apache]
154
+ begin
155
+ Cocaine::CommandLine.new('sudo /etc/init.d/apache2 reload').run
156
+ rescue Cocaine::ExitStatusError => e
157
+ rputs 'Failed to reload Apache.'
161
158
  end
162
- Conf[app_name] = o.select{|k,v| [:ruby, :port, :app_server, :app_server_options, :app_path, :vhost_path, :bundle_exec].include?(k)}
163
- Conf.save
164
-
165
- puts "#{green}Application '#{app_name}' added.#{reset}"
166
- puts "use 'magicmonkey start #{app_name}' to start the application."
167
159
  end
168
160
  end
169
161
 
170
- def self.remove(args)
171
- o = {:remove_vhost => false}
172
- parser = OptionParser.new do |opts|
173
- opts.banner = 'Usage: magicmonkey remove APP_NAME'
174
- opts.separator ''
175
- opts.separator 'Options:'
176
- opts.on('--[no-]remove-vhost', "Remove the virtual host file if exist (default: #{o[:remove_vhost]}).") do |r|
177
- o[:remove_vhost] = r
162
+ #################
163
+ # START COMMAND #
164
+ #################
165
+ def self.start(args)
166
+ applications = help2('start', 'Starts the selected applications.', args)
167
+ applications.each do |app|
168
+ app_conf = Conf[app.to_sym]
169
+ unless app_conf
170
+ rputs "Application '#{app}' does not configured."
171
+ puts "Use 'magicmonkey configure #{app}' to configure it."
172
+ exit
178
173
  end
179
- opts.on_tail('-v', '--version', 'Print version') { puts Magicmonkey::VERSION; exit }
180
- opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
181
- end
182
- begin
183
- args = parser.parse!(args)
184
- raise 'Missing application name.' if args.size != 1
185
- rescue => e
186
- rputs e
187
- puts parser.help; exit
188
- end
189
- app_name = args.first
190
- if Conf[app_name]
191
- self.start_stop_restart(:stop, [app_name])
192
- begin
193
- Cocaine::CommandLine.new("sudo a2dissite '#{app_name}'").run
194
- rescue Cocaine::ExitStatusError => e
195
- rputs 'Failed to disable site.'
174
+ check_ruby_version!(app_conf[:ruby])
175
+ server = check_app_server!(app_conf[:app_server])
176
+ lines = []
177
+ lines << "source '#{Dir.home}/.rvm/scripts/rvm'"
178
+ lines << "rvm use #{app_conf[:ruby]}"
179
+ lines += server.start(app_conf)
180
+ command = "bash -c \"#{lines.join(' && ')}\""
181
+ if app_conf[:bundle_exec]
182
+ command.gsub!(" && #{app_conf[:app_server]} ", " && bundle exec #{app_conf[:app_server]} ")
196
183
  end
197
- vh_file = "#{Conf[app_name][:vhost_path]}/#{app_name}"
198
- if o[:remove_vhost] && File.exist?(vh_file)
199
- begin
200
- Cocaine::CommandLine.new("sudo rm -f '#{vh_file}'").run
201
- rescue Cocaine::ExitStatusError => e
202
- rputs 'Failed to remove virtual host file.'
184
+ begin
185
+ print "Calling #{bold}start#{reset} for '#{app}' application..."
186
+ Dir.chdir(app_conf[:app_path]) do
187
+ Cocaine::CommandLine.new(command).run
203
188
  end
189
+ puts " #{green}#{bold}done#{reset}."
190
+ rescue Cocaine::ExitStatusError => e
191
+ rputs "Failed to start application '#{app}'"
204
192
  end
205
- Conf.delete(app_name)
206
- Conf.save
207
- puts "Application '#{app_name}' removed."
208
- else
209
- rputs "Application '#{app_name}' does not exist."
210
193
  end
211
194
  end
212
195
 
213
- def self.start_stop_restart(ssr, args)
214
- parser = OptionParser.new do |opts|
215
- opts.banner = "Usage: magicmonkey #{ssr} [APP_NAME1 ... APP_NAME2 ...]"
216
- opts.separator "If no application name passed, #{ssr} all applications."
217
- opts.separator ''
218
- opts.separator 'Options:'
219
- opts.on_tail('-v', '--version', 'Print version') { puts Magicmonkey::VERSION; exit }
220
- opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
221
- end
222
- begin
223
- args = parser.parse!(args)
224
- rescue => e
225
- rputs e
226
- puts parser.help; exit
227
- end
228
- applications = args
229
- applications = Conf.applications if applications.empty?
230
- applications.each do |app_name|
231
- o = Conf[app_name.to_sym]
232
- if o
233
- check_ruby_version!(o[:ruby])
234
- server = check_app_server!(o[:app_server])
235
- print "Calling #{bold}#{ssr}#{reset} for '#{app_name}' application..."
236
- STDOUT.flush
237
- begin
238
- output = self.run(o){server.send(ssr, o)}
239
- puts " #{green}#{bold}done#{reset}."
240
- rescue Cocaine::ExitStatusError => e
241
- puts ''
242
- rputs "Failed to #{ssr} application '#{app_name}'"
196
+ ################
197
+ # STOP COMMAND #
198
+ ################
199
+ def self.stop(args)
200
+ applications = help2('stop', 'Stops the selected applications.', args)
201
+ applications.each do |app|
202
+ app_conf = Conf[app.to_sym]
203
+ unless app_conf
204
+ rputs "Application '#{app}' does not configured."
205
+ puts "Use 'magicmonkey configure #{app}' to configure it."
206
+ exit
207
+ end
208
+ check_ruby_version!(app_conf[:ruby])
209
+ server = check_app_server!(app_conf[:app_server])
210
+ lines = []
211
+ lines << "source '#{Dir.home}/.rvm/scripts/rvm'"
212
+ lines << "rvm use #{app_conf[:ruby]}"
213
+ lines += server.stop(app_conf)
214
+ command = "bash -c \"#{lines.join(' && ')}\""
215
+ if app_conf[:bundle_exec]
216
+ command.gsub!(" && #{app_conf[:app_server]} ", " && bundle exec #{app_conf[:app_server]} ")
217
+ end
218
+ begin
219
+ print "Calling #{bold}stop#{reset} for '#{app}' application..."
220
+ Dir.chdir(app_conf[:app_path]) do
221
+ Cocaine::CommandLine.new(command).run
243
222
  end
244
- else
245
- rputs "Application '#{app_name}' is not added."
246
- rputs "use 'magicmonkey add #{app_name}' to add this application."
223
+ puts " #{green}#{bold}done#{reset}."
224
+ rescue Cocaine::ExitStatusError => e
225
+ rputs "Failed to stop application '#{app}'"
247
226
  end
248
227
  end
249
228
  end
250
229
 
251
- def self.show(args)
252
- parser = OptionParser.new do |opts|
253
- opts.banner = "Usage: magicmonkey show [APP_NAME1 ... APP_NAME2 ...]"
254
- opts.separator 'If no application name passed, show the configuration of all applications.'
255
- opts.separator ''
256
- opts.separator 'Options:'
257
- opts.on_tail('-v', '--version', 'Print version') { puts Magicmonkey::VERSION; exit }
258
- opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
259
- end
260
- begin
261
- args = parser.parse!(args)
262
- rescue => e
263
- rputs e
264
- puts parser.help; exit
265
- end
266
- applications = args
267
- applications = Conf.applications if applications.empty?
268
- applications.each do |app_name|
269
- puts app_name.upcase
270
- pp Conf[app_name]
271
- puts '-'*80
230
+ ###################
231
+ # RESTART COMMAND #
232
+ ###################
233
+ def self.restart(args)
234
+ applications = help2('restart', 'Restart the selected applications.', args)
235
+ applications.each do |app|
236
+ app_conf = Conf[app.to_sym]
237
+ unless app_conf
238
+ rputs "Application '#{app}' does not configured."
239
+ puts "Use 'magicmonkey configure #{app}' to configure it."
240
+ exit
241
+ end
242
+ check_ruby_version!(app_conf[:ruby])
243
+ server = check_app_server!(app_conf[:app_server])
244
+ lines = []
245
+ lines << "source '#{Dir.home}/.rvm/scripts/rvm'"
246
+ lines << "rvm use #{app_conf[:ruby]}"
247
+ lines += server.stop(app_conf)
248
+ lines << 'sleep 3'
249
+ lines += server.start(app_conf)
250
+ command = "bash -c \"#{lines.join(' && ')}\""
251
+ if app_conf[:bundle_exec]
252
+ command.gsub!(" && #{app_conf[:app_server]} ", " && bundle exec #{app_conf[:app_server]} ")
253
+ end
254
+ begin
255
+ print "Calling #{bold}restart#{reset} for '#{app}' application..."
256
+ Dir.chdir(app_conf[:app_path]) do
257
+ Cocaine::CommandLine.new(command).run
258
+ end
259
+ puts " #{green}#{bold}done#{reset}."
260
+ rescue Cocaine::ExitStatusError => e
261
+ rputs "Failed to restart application '#{app}'"
262
+ end
272
263
  end
273
264
  end
274
265
 
275
266
  private
276
267
 
268
+ def self.check_port!(port)
269
+ if Conf.ports.include?(port)
270
+ rputs 'Invalid port number. This port is used by another application or is invalid.'
271
+ exit
272
+ end
273
+ end
274
+
277
275
  def self.check_ruby_version!(ruby)
278
276
  rubies = ['default', 'system']
279
277
  begin
@@ -296,11 +294,10 @@ module MagicMonkey
296
294
  def self.check_app_server!(app_server)
297
295
  unless app_server
298
296
  rputs 'You must specify the application server.'
299
- rputs 'Please use --app-server option. e.g. magicmonkey add APP_NAME --app-server=passenger'
300
297
  exit
301
298
  end
302
299
  begin
303
- Cocaine::CommandLine.new(app_server).run
300
+ Cocaine::CommandLine.new("#{app_server} -v").run
304
301
  rescue Cocaine::CommandNotFoundError
305
302
  rputs "The application server '#{app_server}' is not installed in your system."
306
303
  rputs 'Please use a valid and installed application server.'
@@ -309,27 +306,64 @@ module MagicMonkey
309
306
  end
310
307
  begin
311
308
  require "#{$APP_PATH}/lib/magicmonkey/app_servers/#{app_server}"
312
- const_get(app_server.capitalize)
309
+ m = ''
310
+ i = 0
311
+ app_server.each_char do |c|
312
+ app_server[i-1] == '_' ? m << c.upcase : m << c if c != '_'
313
+ i+=1
314
+ end
315
+ m[0] = m[0].upcase
316
+ const_get(m)
313
317
  rescue LoadError, NameError
314
- rputs "No module '#{app_server.capitalize}' found in #{$APP_PATH}/lib/magicmonkey/app_servers/#{app_server}.rb"
318
+ rputs "No module '#{m}' found in #{$APP_PATH}/lib/magicmonkey/app_servers/#{app_server}.rb"
315
319
  rputs "You must create a module called '#{app_server.capitalize}' to tell how to start, stop and restart the server."
316
320
  exit
317
321
  end
318
322
  end
319
323
 
320
- def self.run(options)
321
- lines = []
322
- lines << "source #{Dir.home}/.rvm/scripts/rvm"
323
- lines << "rvm use #{options[:ruby]}"
324
- lines << "cd #{options[:app_path]}"
325
- res = yield
326
- if res.class == Array
327
- lines += res
328
- elsif res.class == String
329
- lines << res
324
+ def self.help(command, desc, args)
325
+ parser = OptionParser.new do |opts|
326
+ opts.banner = "Usage: magicmonkey #{command} APP_NAME"
327
+ opts.separator ''
328
+ if desc
329
+ opts.separator desc
330
+ opts.separator ''
331
+ end
332
+ opts.separator 'Options:'
333
+ yield(opts) if block_given?
334
+ opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
335
+ end
336
+ begin
337
+ args = parser.parse!(args)
338
+ raise 'Missing application name.' if args.size != 1
339
+ rescue => e
340
+ puts parser.help; exit
341
+ end
342
+ return args.first
343
+ end
344
+
345
+ def self.help2(command, desc, args)
346
+ parser = OptionParser.new do |opts|
347
+ opts.banner = "Usage: magicmonkey #{command} [APP_NAME1 APP_NAME2 ...]"
348
+ opts.separator ''
349
+ if desc
350
+ opts.separator desc
351
+ opts.separator "If no application name passed, #{command} all applications."
352
+ opts.separator ''
353
+ end
354
+ opts.separator ''
355
+ opts.separator 'Options:'
356
+ yield(opts) if block_given?
357
+ opts.on_tail('-h', '--help', 'Show this help message') { puts opts; exit }
358
+ end
359
+ begin
360
+ args = parser.parse!(args)
361
+ rescue => e
362
+ puts parser.help; exit
330
363
  end
331
- line = Cocaine::CommandLine.new("bash -c '#{lines.join(' && ')}'")
332
- line.run
364
+ applications = Conf.applications
365
+ applications &= args.map(&:to_sym) unless args.empty?
366
+ return applications
333
367
  end
334
368
 
335
369
  def self.rputs(message)
@@ -1,3 +1,3 @@
1
1
  module Magicmonkey
2
- VERSION = '1.0.8'
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magicmonkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-07 00:00:00.000000000 Z
12
+ date: 2012-06-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cocaine
16
- requirement: &70278634428060 !ruby/object:Gem::Requirement
16
+ requirement: &70117049364340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70278634428060
24
+ version_requirements: *70117049364340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: term-ansicolor
27
- requirement: &70278634427640 !ruby/object:Gem::Requirement
27
+ requirement: &70117049363460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70278634427640
35
+ version_requirements: *70117049363460
36
36
  description: ! 'Manage your Rails applications: different Ruby versions and different
37
37
  application servers.'
38
38
  email:
@@ -50,6 +50,7 @@ files:
50
50
  - bin/magicmonkey
51
51
  - lib/magicmonkey/app_servers/passenger.rb
52
52
  - lib/magicmonkey/app_servers/thin.rb
53
+ - lib/magicmonkey/app_servers/unicorn_rails.rb
53
54
  - lib/magicmonkey/configuration.rb
54
55
  - lib/magicmonkey/magicmonkey.rb
55
56
  - lib/magicmonkey/version.rb