ors 0.2.10 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.markdown +71 -0
  2. data/bin/ors +1 -1
  3. data/lib/ors.rb +21 -2
  4. data/lib/ors/base.rb +30 -0
  5. data/lib/ors/commands/base.rb +56 -12
  6. data/lib/ors/commands/changes.rb +21 -7
  7. data/lib/ors/commands/console.rb +16 -18
  8. data/lib/ors/commands/deploy.rb +24 -13
  9. data/lib/ors/commands/env.rb +9 -9
  10. data/lib/ors/commands/exec.rb +14 -7
  11. data/lib/ors/commands/help.rb +9 -11
  12. data/lib/ors/commands/logs.rb +16 -14
  13. data/lib/ors/commands/migrate.rb +8 -9
  14. data/lib/ors/commands/restart.rb +9 -11
  15. data/lib/ors/commands/ruby.rb +8 -6
  16. data/lib/ors/commands/runner.rb +29 -25
  17. data/lib/ors/commands/setup.rb +16 -16
  18. data/lib/ors/commands/start.rb +9 -11
  19. data/lib/ors/commands/stop.rb +9 -11
  20. data/lib/ors/commands/symlink.rb +31 -0
  21. data/lib/ors/commands/timestamps.rb +27 -0
  22. data/lib/ors/commands/update.rb +15 -11
  23. data/lib/ors/config.rb +88 -58
  24. data/lib/ors/helpers.rb +107 -76
  25. data/lib/ors/log_unifier.rb +2 -2
  26. data/lib/ors/version.rb +2 -2
  27. data/spec/ors/{command_spec.rb → base_spec.rb} +11 -8
  28. data/spec/ors/commands/base_spec.rb +17 -9
  29. data/spec/ors/commands/deploy_spec.rb +4 -3
  30. data/spec/ors/commands/runner_spec.rb +8 -27
  31. data/spec/ors/commands/timestamps_spec.rb +16 -0
  32. data/spec/ors/commands/update_spec.rb +8 -3
  33. data/spec/ors/config_spec.rb +56 -61
  34. data/spec/ors/helpers_spec.rb +6 -2
  35. data/spec/ors/log_unifier_spec.rb +2 -2
  36. metadata +82 -57
  37. data/README +0 -52
  38. data/lib/ors/command.rb +0 -46
  39. data/lib/ors/commands/check.rb +0 -27
  40. data/lib/ors/commands/sync.rb +0 -27
  41. data/lib/ors/core_ext.rb +0 -53
  42. data/spec/ors/commands/check_spec.rb +0 -15
data/README.markdown ADDED
@@ -0,0 +1,71 @@
1
+ # Set Up
2
+
3
+ Required Files:
4
+
5
+ * .rvmrc
6
+ * Gemfile
7
+ * Gemfile.lock
8
+ * config/database.yml
9
+ * .git/config
10
+ * config/unicorn.rb
11
+
12
+
13
+ ### Setting up .git/config
14
+
15
+
16
+ Your .git/config should know about the remotes you will be using to deploy to the server (usually just origin) as it will pull information about the remote from your local config file
17
+
18
+
19
+ ### Setting up RVM
20
+
21
+ On your ruby servers you should create a .rvmrc in the home directory of the user running unicorn.
22
+
23
+ ```bash
24
+ rvm_install_on_use_flag=1
25
+
26
+ rvm_gemset_create_on_use_flag=1
27
+
28
+ rvm_trust_rvmrcs_flag=1
29
+ ```
30
+
31
+
32
+ ### Required gems
33
+
34
+ * rake
35
+ * bundler
36
+ * unicorn
37
+
38
+ ors deploy commands assume you are using unicorn for your servers.
39
+
40
+
41
+
42
+ # Usage
43
+
44
+ run `ors help` for a list of commands to use.
45
+
46
+
47
+ ### Brief Examples
48
+
49
+ Deploying to the staging website
50
+
51
+ ```bash
52
+ bundle exec ors deploy to staging
53
+ ```
54
+
55
+ Deploying a feature branch to the production website
56
+
57
+ ```bash
58
+ bundle exec ors deploy origin/feature_branch
59
+ ```
60
+
61
+ Deploying a feature branch to the staging website
62
+
63
+ ```bash
64
+ bundle exec ors deploy origin/feature_branch to staging
65
+ ```
66
+
67
+ ### Notes
68
+ * By default the environment is assumed to be production and the branch you are deploying is assumed to be origin/#{environment}.
69
+ * You can override settings by implementing a config/deploy.yml file in your repo.
70
+
71
+
data/bin/ors CHANGED
@@ -5,4 +5,4 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) and (not $LOAD_PATH.include? lib
5
5
 
6
6
  require "ors"
7
7
 
8
- ORS::Command.run ARGV
8
+ ORS.new.run ARGV
data/lib/ors.rb CHANGED
@@ -3,9 +3,28 @@ require "git"
3
3
  require "yaml"
4
4
  require "erb"
5
5
 
6
+ require "ors/base"
6
7
  require "ors/version"
7
- require "ors/core_ext"
8
8
  require "ors/log_unifier"
9
9
  require "ors/config"
10
10
  require "ors/helpers"
11
- require "ors/command"
11
+
12
+ # commands
13
+ require "ors/commands/base"
14
+ require "ors/commands/changes"
15
+ require "ors/commands/console"
16
+ require "ors/commands/deploy"
17
+ require "ors/commands/env"
18
+ require "ors/commands/exec"
19
+ require "ors/commands/help"
20
+ require "ors/commands/logs"
21
+ require "ors/commands/migrate"
22
+ require "ors/commands/restart"
23
+ require "ors/commands/ruby"
24
+ require "ors/commands/runner"
25
+ require "ors/commands/setup"
26
+ require "ors/commands/start"
27
+ require "ors/commands/stop"
28
+ require "ors/commands/symlink"
29
+ require "ors/commands/timestamps"
30
+ require "ors/commands/update"
data/lib/ors/base.rb ADDED
@@ -0,0 +1,30 @@
1
+ class ORS
2
+ def self.config(options = [])
3
+ @config ||= ORS::Config.new(options)
4
+ end
5
+
6
+ def run args
7
+ command, *options = args
8
+ klass_string = command.to_s.capitalize
9
+
10
+ # setup initial config
11
+ ORS.config(options)
12
+
13
+ # determine command to use
14
+ if command =~ /-*version/i
15
+ puts "ORS v#{ORS::VERSION}"
16
+ else
17
+ if available_commands.include? klass_string
18
+ ORS::Commands.const_get(klass_string).run
19
+ else
20
+ ORS::Commands::Help.run
21
+ end
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def available_commands
28
+ ORS::Commands.constants.map {|klass| klass.to_s }
29
+ end
30
+ end
@@ -1,18 +1,62 @@
1
- module ORS::Commands
2
- class Base
3
- include ORS::Helpers
1
+ class ORS
2
+ module Commands
3
+ class Base
4
+ include ORS::Helpers
4
5
 
5
- module ClassMethods
6
- def run klass
7
- klass.new.execute
6
+ module ClassMethods
7
+ def run_without_setup
8
+ new.execute
9
+ end
10
+
11
+ def run
12
+ command = new
13
+ command.setup
14
+
15
+ # now that the command has had a chance at the args
16
+ # we let our config finish its setup of variables
17
+ ORS.config.finalize!
18
+
19
+ if ORS.config.valid?
20
+ # now execute the command
21
+ command.execute
22
+ else
23
+ info "ERROR: Invalid options given."
24
+ ORS::Commands::Help.run_without_setup
25
+ end
26
+ end
8
27
  end
9
- end
10
- extend ClassMethods
28
+ extend ClassMethods
11
29
 
12
- def run klass
13
- self.class.run klass
14
- end
30
+ def setup
31
+ end
15
32
 
16
- end
33
+ def usage
34
+ "./ors base [options]"
35
+ end
17
36
 
37
+ def description
38
+ "Base Command Class"
39
+ end
40
+
41
+ def help_options
42
+ <<-END
43
+ === Options
44
+ from|to environment Set which environment to use (default production)
45
+ --pretend (or -p) Don't execute anything, just show me what you're going to do
46
+ --no-gateway (or -ng) Don't use a gateway (if you're inside the firewall)
47
+ END
48
+ end
49
+
50
+ def help
51
+ puts <<-END
52
+ Usage: #{usage}
53
+
54
+ === Description
55
+ #{description}
56
+
57
+ #{help_options}
58
+ END
59
+ end
60
+ end
61
+ end
18
62
  end
@@ -1,13 +1,27 @@
1
- module ORS::Commands
1
+ class ORS
2
+ module Commands
3
+ class Changes < Base
4
+ def setup
5
+ @local_ref = ORS.config[:args].shift
6
+ parse_remote_and_or_branch
7
+ end
2
8
 
3
- class Changes < Base
9
+ def execute
10
+ results = execute_command(ORS.config[:console_server],
11
+ prepare_environment_with_rvm,
12
+ %(git show | head -1),
13
+ :capture => true)
4
14
 
5
- def execute
6
- results = execute_command console_server, prepare_environment, %(git show | head -1), :capture => true
15
+ system('git', 'log', [@local_ref, "remotes/#{ORS.config[:remote]}/#{ORS.config[:branch]}"].join("..")) if results =~ /commit (.*)/
16
+ end
7
17
 
8
- system('git', 'log', [$1, "remotes/origin/#{environment}"].join("..")) if results =~ /commit (.*)/
9
- end
18
+ def usage
19
+ "./ors changes local_ref [remote|remote/branch] [options]"
20
+ end
10
21
 
22
+ def description
23
+ "Detects changes between local commits and what is deployed"
24
+ end
25
+ end # Changes < Base
11
26
  end
12
-
13
27
  end
@@ -1,22 +1,20 @@
1
- module ORS::Commands
2
- class Console < Base
3
- def execute
4
- execute_command console_server, prepare_environment,
5
- %(if [ -f script/rails ]; then bundle exec rails console #{environment}; else ./script/console #{environment}; fi),
6
- :exec => true
7
- end
1
+ class ORS
2
+ module Commands
3
+ class Console < Base
4
+ def execute
5
+ execute_command(ORS.config[:console_server],
6
+ prepare_environment_with_rvm,
7
+ %(if [ -f script/rails ]; then bundle exec rails console #{ORS.config[:environment]}; else ./script/console #{ORS.config[:environment]}; fi),
8
+ :exec => true)
9
+ end
8
10
 
9
- def help
10
- puts <<-END
11
- Usage: ./ors console [environment=production] [options]
11
+ def usage
12
+ "./ors console [options]"
13
+ end
12
14
 
13
- === Description
14
- Replaces current process and runs rails console.
15
-
16
- === Options
17
- --pretend (or -p) Don't execute anything, just show me what you're going to do
18
- --no-gateway (or -ng) Don't use a gateway (if you're inside the firewall)
19
- END
20
- end
15
+ def description
16
+ "Replaces current process and runs rails console."
17
+ end
18
+ end # Console < Base
21
19
  end
22
20
  end
@@ -1,23 +1,34 @@
1
- module ORS::Commands
1
+ class ORS
2
+ module Commands
3
+ class Deploy < Base
2
4
 
3
- class Deploy < Base
5
+ def setup
6
+ parse_remote_and_or_branch
7
+ end
4
8
 
5
- def execute
6
- info "deploying #{name} #{environment}..."
9
+ def execute
10
+ info "deploying #{ORS.config[:name]} #{ORS.config[:environment]}..."
7
11
 
8
- [Update, Migrate].each {|command| run command }
12
+ [Update, Symlink, Migrate].each {|command| command.run_without_setup }
9
13
 
10
- if remote_deploy_hook
11
- execute_in_parallel(app_servers) do |server|
12
- execute_command server, prepare_environment, "RAILS_ENV=#{environment} #{remote_deploy_hook}"
14
+ if ORS.config[:remote_deploy_hook]
15
+ execute_in_parallel(ORS.config[:app_servers]) do |server|
16
+ execute_command server, prepare_environment, "RAILS_ENV=#{ORS.config[:environment]} #{ORS.config[:remote_deploy_hook]}"
17
+ end
13
18
  end
14
- end
15
19
 
16
- run Restart
20
+ Restart.run_without_setup
17
21
 
18
- eval ERB.new(deploy_hook).result(binding) if deploy_hook
19
- end
22
+ eval ERB.new(ORS.config[:deploy_hook]).result(binding) if ORS.config[:deploy_hook]
23
+ end
20
24
 
21
- end
25
+ def usage
26
+ "./ors deploy [remote|remote/branch] [options]"
27
+ end
22
28
 
29
+ def description
30
+ "Deploys given branch (origin/environment by default) to given environment"
31
+ end
32
+ end # Deploy < Base
33
+ end
23
34
  end
@@ -1,15 +1,15 @@
1
- module ORS::Commands
1
+ class ORS
2
+ module Commands
3
+ class Env < Base
2
4
 
3
- class Env < Base
5
+ def execute
6
+ puts "ORS v#{ORS::VERSION} configuration:\n\n"
4
7
 
5
- def execute
6
- puts "ORS v#{ORS::VERSION} configuration:\n\n"
7
8
 
8
- [:name, :environment, :use_gateway, :pretending, :log_lines, :rails2, :gateway, :deploy_user, :repo,
9
- :base_path, :deploy_hook, :remote_deploy_hook, :web_servers, :app_servers, :migration_server, :console_server, :cron_server].each do |config_variable|
10
- puts "%20s: %-40s" % [config_variable, ORS::Config.send(config_variable).inspect]
9
+ ORS.config._options.each_pair do |key, value|
10
+ puts "%20s: %-40s" % [key, value.inspect]
11
+ end
11
12
  end
12
- end
13
-
13
+ end # Env < Base
14
14
  end
15
15
  end
@@ -1,13 +1,20 @@
1
- module ORS::Commands
1
+ class ORS
2
+ module Commands
3
+ class Exec < Base
2
4
 
3
- class Exec < Base
5
+ def setup
6
+ @command = ORS.config[:args].shift
7
+ end
4
8
 
5
- def execute
6
- info "executing command for #{name} #{environment}..."
9
+ def execute
10
+ info "executing command for #{ORS.config[:name]} #{ORS.config[:environment]}..."
7
11
 
8
- execute_command migration_server, prepare_environment, %(bundle exec #{ENV["CMD"]})
9
- end
12
+ execute_command migration_server, prepare_environment, %(bundle exec #{@command})
13
+ end
10
14
 
15
+ def usage
16
+ "./ors exec command [options]"
17
+ end
18
+ end # Exec < Base
11
19
  end
12
-
13
20
  end
@@ -1,9 +1,9 @@
1
- module ORS::Commands
1
+ class ORS
2
+ module Commands
3
+ class Help < Base
2
4
 
3
- class Help < Base
4
-
5
- def execute
6
- puts <<-END
5
+ def execute
6
+ puts <<-END
7
7
  Usage: ./ors <action> [environment=production] [options]
8
8
 
9
9
  === Actions
@@ -25,11 +25,9 @@ update Updates the code on all servers
25
25
  Must be one of: production demo staging
26
26
  Defaults to production.
27
27
 
28
- === Options
29
- --pretend (or -p) Don't execute anything, just show me what you're going to do (default: false)
30
- --no-gateway (or -ng) Don't use a gateway (if you're inside the firewall) (default: true)
31
- END
32
- end
33
-
28
+ #{help_options}
29
+ END
30
+ end
31
+ end # Help < Base
34
32
  end
35
33
  end
@@ -1,18 +1,20 @@
1
- module ORS::Commands
2
- class Logs < Base
1
+ class ORS
2
+ module Commands
3
+ class Logs < Base
3
4
 
4
- def execute
5
- all_logs = app_servers.map do |server|
6
- [
7
- server,
8
- execute_command(server, prepare_environment,
9
- %(tail -n #{log_lines} log/#{environment}.log),
10
- :capture => true)
11
- ]
12
- end
13
-
14
- puts ORS::LogUnifier.new(all_logs).unify unless pretending
15
- end
5
+ def execute
6
+ all_logs = ORS.config[:app_servers].map do |server|
7
+ [
8
+ server,
9
+ execute_command(server,
10
+ prepare_environment,
11
+ %(tail -n #{ORS.config[:log_lines]} log/#{ORS.config[:environment]}.log),
12
+ :capture => true)
13
+ ]
14
+ end
16
15
 
16
+ puts ORS::LogUnifier.new(all_logs).unify unless ORS.config[:pretending]
17
+ end
18
+ end # Logs < Base
17
19
  end
18
20
  end