blazing 0.2.7 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,6 +21,7 @@ class Blazing::Recipe
21
21
  end
22
22
 
23
23
  def list
24
+ load_recipes!
24
25
  descendants = []
25
26
  ObjectSpace.each_object(Class) do |k|
26
27
  descendants.unshift k if k < self
@@ -28,21 +29,9 @@ class Blazing::Recipe
28
29
  descendants
29
30
  end
30
31
 
31
- def load_recipes
32
- load_recipe_gems(parse_gemfile)
33
- end
34
-
35
- def parse_gemfile(gemfile = 'Gemfile')
36
- open(gemfile).grep(/blazing-/).map { |l| l.match(/blazing-(\w+)/)[0] }
37
- end
38
-
39
- def load_recipe_gems(gems)
40
- gems.each do |gem|
41
- gem_lib_path = $:.find { |p| p.include? gem }
42
- recipes_path = File.join(gem_lib_path, gem, 'recipes')
43
- recipes = Dir.entries(recipes_path).delete_if { |r| r == '.' || r == '..' }
44
- recipes.each { |recipe| require File.join(gem, 'recipes', recipe) }
45
- end
32
+ def load_recipes!
33
+ gems = $:.select{|p| p.match /blazing-\w+(|-\d\.\d\.\d)\/lib/}.map { |r| r.scan(/blazing-\w+/)[0] }
34
+ gems.each { |gem| require gem }
46
35
  end
47
36
 
48
37
  end
@@ -1,59 +1,79 @@
1
1
  require 'erb'
2
- require 'grit'
2
+ require 'blazing/config'
3
3
 
4
4
  class Blazing::Runner
5
5
 
6
6
  include Blazing::Logger
7
7
 
8
- def initialize(config = nil, target = nil)
9
- if config
10
- @config = config
11
- @target = @config.targets.find { |t| t.name.to_s == target } || @config.default_target
12
- end
8
+ def initialize(target_name, options)
9
+ prepare_config(target_name, options)
13
10
  end
14
11
 
15
- def exec(command)
16
- command_method = "#{command.gsub(':', '_')}_command"
17
- raise "Unknown Command: #{command}" unless self.respond_to? command_method
18
- self.send command_method
19
- end
12
+ #
13
+ # Parse config and set up options
14
+ #
15
+ def prepare_config(target_name, options = {})
16
+ @config ||= Blazing::Config.parse(options[:file])
17
+ @targets = []
20
18
 
21
- def init_command
22
- info "Creating an example config file in #{Blazing::DEFAULT_CONFIG_LOCATION}"
23
- info "Customize it to your needs"
19
+ if target_name == 'all'
20
+ @targets = @config.targets
21
+ else
22
+ @targets << (@config.targets.find { |t| t.name.to_s == target_name.to_s } || @config.default_target)
23
+ end
24
24
 
25
- Dir.mkdir 'config' unless File.exists? 'config'
26
- configuration_file = ERB.new(File.read("#{Blazing::TEMPLATE_ROOT}/config.erb")).result
25
+ @targets.compact!
27
26
 
28
- File.open(Blazing::DEFAULT_CONFIG_LOCATION,"wb") do |f|
29
- f.puts configuration_file
30
- end
27
+ error 'no target given or found' if @targets.empty?
31
28
  end
32
29
 
33
- def setup_git_remotes
34
- repository = Grit::Repo.new(Dir.pwd)
35
- @config.targets.each do |target|
36
- info "Adding new remote #{target.name} pointing to #{target.location}"
37
- repository.config["remote.#{target.name}.url"] = target.location
30
+ def setup
31
+ return if @targets.empty?
32
+
33
+ @targets.each do |target|
34
+ target.setup
38
35
  end
36
+ update
39
37
  end
40
38
 
41
- def setup_command
42
- @target.setup
43
- update_command
44
- end
39
+ def update
40
+ return if @targets.empty?
45
41
 
46
- def update_command
47
- setup_git_remotes
48
- @target.apply_hook
42
+ @targets.each do |t|
43
+ t.setup_git_remote
44
+ t.apply_hook
45
+ end
49
46
  end
50
47
 
51
- def recipes_run_command
52
- @config.recipes.each { |recipe| recipe.run(@target.options) }
48
+ def recipes
49
+ @config.recipes.each { |recipe| recipe.run }
53
50
  end
54
51
 
55
- def recipes_list_command
56
- Blazing::Recipe.list.each { |r| puts r.to_s.demodulize.underscore }
57
- end
52
+ class << self
58
53
 
54
+ include Blazing::Logger
55
+
56
+ #
57
+ # Bootstrap blazing by creating config file
58
+ #
59
+ def init
60
+ info "Creating an example config file in #{Blazing::DEFAULT_CONFIG_LOCATION}"
61
+ info "Customize it to your needs"
62
+
63
+ Dir.mkdir 'config' unless File.exists? 'config'
64
+ configuration_file = ERB.new(File.read("#{Blazing::TEMPLATE_ROOT}/config.erb")).result
65
+
66
+ File.open(Blazing::DEFAULT_CONFIG_LOCATION,"wb") do |f|
67
+ f.puts configuration_file
68
+ end
69
+ end
70
+
71
+ #
72
+ # List available blazing recipes
73
+ #
74
+ def list
75
+ Blazing::Recipe.list.each { |r| puts r.to_s.demodulize.underscore }
76
+ end
77
+
78
+ end
59
79
  end
data/lib/blazing/shell.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  class Blazing::Shell
2
2
 
3
3
  def run(command)
4
- system command
4
+ `#{command}`
5
5
  end
6
6
 
7
7
  end
@@ -1,4 +1,5 @@
1
1
  require 'blazing/shell'
2
+ require 'grit'
2
3
 
3
4
  class Blazing::Target
4
5
 
@@ -12,11 +13,18 @@ class Blazing::Target
12
13
  @config = config
13
14
  @options = options
14
15
  @shell = Blazing::Shell.new
16
+ @target = self
15
17
  end
16
18
 
17
19
  def setup
18
20
  info "Setting up repository for #{name} in #{location}"
19
- @shell.run "ssh #{user}@#{host} '#{init_repository} && #{setup_repository}'"
21
+
22
+ # TODO: Handle case where user is empty
23
+ if host
24
+ @shell.run "ssh #{user}@#{host} '#{init_repository} && #{setup_repository}'"
25
+ else
26
+ @shell.run "#{init_repository} && #{setup_repository}"
27
+ end
20
28
  end
21
29
 
22
30
  def apply_hook
@@ -29,11 +37,25 @@ class Blazing::Target
29
37
 
30
38
  debug "Copying hook for #{name} to #{location}"
31
39
  copy_hook
32
- @shell.run "ssh #{user}@#{host} #{make_hook_executable}"
40
+ if host
41
+ @shell.run "ssh #{user}@#{host} #{make_hook_executable}"
42
+ else
43
+ @shell.run "#{make_hook_executable}"
44
+ end
45
+ end
46
+
47
+ def setup_git_remote
48
+ repository = Grit::Repo.new(Dir.pwd)
49
+ info "Adding new remote #{name} pointing to #{location}"
50
+ repository.config["remote.#{name}.url"] = location
33
51
  end
34
52
 
35
53
  def path
36
- @location.match(/:(.*)$/)[1]
54
+ if host
55
+ @location.match(/:(.*)$/)[1]
56
+ else
57
+ @location
58
+ end
37
59
  end
38
60
 
39
61
  def host
@@ -57,10 +79,16 @@ class Blazing::Target
57
79
 
58
80
  def copy_hook
59
81
  debug "Making hook executable"
60
- @shell.run "scp #{Blazing::TMP_HOOK} #{user}@#{host}:#{path}/.git/hooks/post-receive"
82
+ # TODO: handle missing user?
83
+ if host
84
+ @shell.run "scp #{Blazing::TMP_HOOK} #{user}@#{host}:#{path}/.git/hooks/post-receive"
85
+ else
86
+ @shell.run "cp #{Blazing::TMP_HOOK} #{path}/.git/hooks/post-receive"
87
+ end
61
88
  end
62
89
 
63
90
  def make_hook_executable
91
+ debug "Making hook executable"
64
92
  "chmod +x #{path}/.git/hooks/post-receive"
65
93
  end
66
94
 
@@ -68,4 +96,13 @@ class Blazing::Target
68
96
  "cd #{path} && git config receive.denyCurrentBranch ignore"
69
97
  end
70
98
 
99
+ def rake_command
100
+ rake_config = @config.instance_variable_get("@rake") || {}
101
+ rails_env = "RAILS_ENV=#{@options[:rails_env]}" if @options[:rails_env]
102
+
103
+ if rake_config[:task]
104
+ "#{rake_config[:env]} #{rails_env} bundle exec rake #{rake_config[:task]}"
105
+ end
106
+ end
107
+
71
108
  end
@@ -1,35 +1,61 @@
1
1
  #
2
2
  # Blazing Configuration File
3
3
  #
4
-
4
+ # Sample target definition:
5
+ #
6
+ # target <target_name>, <target_location>, [options]
5
7
  #
6
- # Specify the location of your repository:
8
+ # The options provided in the target definition will override any
9
+ # options provided in the recipe call.
7
10
  #
8
- # repository 'git@github.com:path/to/your/repository'
11
+ # Options recognized by blazing core:
12
+ # rails_env: used when calling the rake task after deployment
13
+
14
+ target :staging, 'screenconcept@ruby:/var/www/vischer.ruby.screenconcept.ch',
15
+ :recipe_specific_option => 'foo', :rails_env => 'production'
9
16
 
17
+
18
+ # Sample rvm setup:
10
19
  #
11
- # Define possible deploy targets:
20
+ # rvm <rvm-string>
12
21
  #
13
- # target :production, 'blazing@production.server.com:/where/your/code/is/shipped/to'
14
- # target :staging, 'blazing@production.server.com:/where/your/code/is/shipped/to', :default => true, :some_option => 'foo' # note: options defined here will override options defined globally for a recipe
22
+ # Setting the rvm string will make sure that the correct rvm ruby and
23
+ # gemset is used before the post-receive hook does anything at all.
24
+ # Use :rvmrc as rvm string if you want blazing to use the rvm
25
+ # environment specified in your project's .rvmrc file.
26
+
27
+ rvm 'ruby-1.9.3@some-gemset'
28
+
15
29
 
30
+ # Sample config for custom rvm location:
16
31
  #
17
- # Optional: RVM handling
32
+ # rvm_scripts <path_to_rvm_scripts>
18
33
  #
19
- # Use Ruby Enterprise Edition and gemset 123
20
- # rvm 'ree@123'
34
+ # If you have installed rvm to a custom location, use this method to
35
+ # specify where the rvm scripts are located.
36
+
37
+ rvm_scripts '/opt/rvm/scripts/rvm'
21
38
 
22
- # Or use the rvmrc
23
- # rvm :rvmrc
24
- #
25
39
 
40
+ # Sample recipe setup:
26
41
  #
27
- # Define Recipes to be used (See the README for available recipes).
28
- # Recipes are run in the order they are specified
42
+ # recipe <recipe_name>, [options]
29
43
  #
30
- # recipe :recipe_name
44
+ # The given recipe will be called with the provided options. Refer to each
45
+ # recipe's documentation for available options. Options provided here
46
+ # may be overridden by target specific options.
47
+ # Recipes will be executed in the order they are defined!yy
48
+
49
+ recipe :precompile_assets, :recipe_specific_option => 'bar'
31
50
 
51
+
52
+ # Sample rake file config:
32
53
  #
33
- # Define a rake task that should be run after deployment. The rake task will run after the recipes have run.
54
+ # rake <task>, [environment variables]
34
55
  #
35
- # rake :a_rake_task_to_call_after_deployment
56
+ # The provided rake task will be run after all recipes have run.
57
+ # Note: you can only call a single rake task. If you need to run several
58
+ # tasks just create one task that wrapps all the others.
59
+
60
+ rake :post_deploy, 'RAILS_ENV=production'
61
+
@@ -66,7 +66,7 @@ bundle --deployment --quiet --without development test
66
66
  bundle exec blazing recipes:run <%= name.to_s %>
67
67
  <% end %>
68
68
 
69
- <% if @config.rake %>
70
- echo "------> [blazing] running rake <%= @config.rake %>"
71
- bundle exec rake <%= @config.rake %>
69
+ <% if @target.rake_command %>
70
+ echo "------> [blazing] running <%= @target.rake_command %>"
71
+ <%= @target.rake_command %>
72
72
  <% end %>
@@ -1,3 +1,7 @@
1
1
  module Blazing
2
- VERSION = "0.2.7"
2
+
3
+ VERSION = "0.2.9"
4
+
5
+ # TODO: remove Config.repository in 0.3
6
+
3
7
  end
@@ -31,23 +31,6 @@ describe Blazing::Config do
31
31
  config.default_target.name.should be :sometarget
32
32
  end
33
33
 
34
- context 'when more than 1 target defined in config' do
35
-
36
- it 'raises an error when none is default' do
37
- config = Blazing::Config.new
38
- config.target :sometarget, 'somewhere'
39
- config.target :someothertarget, 'somewhere'
40
- lambda { config.default_target }.should raise_error
41
- end
42
-
43
- it 'returns the target object with :default => true option if more than 1 target present' do
44
- config = Blazing::Config.new
45
- config.target :sometarget, 'somewhere', :default => true
46
- config.target :someothertarget, 'somewhere'
47
- config.default_target.name.should be :sometarget
48
- end
49
-
50
- end
51
34
  end
52
35
 
53
36
  describe 'DSL' do
@@ -72,18 +55,6 @@ describe Blazing::Config do
72
55
  end
73
56
  end
74
57
 
75
- describe 'repository' do
76
- it 'assigns the repository if a value is given' do
77
- @config.repository('somewhere')
78
- @config.instance_variable_get("@repository").should == 'somewhere'
79
- end
80
-
81
- it 'returns the repository string if no value is given' do
82
- @config.repository('somewhere')
83
- @config.repository.should == 'somewhere'
84
- end
85
- end
86
-
87
58
  describe 'recipe' do
88
59
 
89
60
  before :each do
@@ -117,12 +88,12 @@ describe Blazing::Config do
117
88
 
118
89
  it 'takes the name of the rake task as argument' do
119
90
  @config.rake :post_deploy
120
- @config.instance_variable_get('@rake').should be :post_deploy
91
+ @config.instance_variable_get('@rake').should == { :task => :post_deploy }
121
92
  end
122
93
 
123
- it 'returns the name of the task if no argument given' do
124
- @config.rake :post_deploy
125
- @config.rake.should be :post_deploy
94
+ it 'accepts environment variables to pass along to rake' do
95
+ @config.rake :post_deploy, 'RAILS_ENV=production'
96
+ @config.instance_variable_get('@rake').should == { :task => :post_deploy, :env => 'RAILS_ENV=production' }
126
97
  end
127
98
 
128
99
  end
@@ -6,7 +6,7 @@ describe 'blazing init' do
6
6
 
7
7
  before :each do
8
8
  setup_sandbox
9
- Blazing::Runner.new.exec('init')
9
+ Blazing::Runner.init
10
10
  end
11
11
 
12
12
  after :each do
@@ -1,8 +1,6 @@
1
1
  require 'spec_helper'
2
- require 'blazing/config'
3
- require 'blazing/runner'
4
2
 
5
- describe 'blazing init' do
3
+ describe 'blazing list' do
6
4
 
7
5
  before :each do
8
6
  setup_sandbox
@@ -15,7 +13,7 @@ describe 'blazing init' do
15
13
  it 'prints a list of the available recipes' do
16
14
  class Blazing::Recipe::Dummy < Blazing::Recipe
17
15
  end
18
- capture(:stdout) { Blazing::Runner.new.exec('recipes:list') }.should == "dummy\n"
16
+ capture(:stdout) { Blazing::Runner.list }.should == "dummy\n"
19
17
  end
20
18
 
21
19
  end
@@ -1,18 +1,20 @@
1
1
  require 'spec_helper'
2
- require 'blazing/config'
3
- require 'blazing/runner'
4
2
 
5
- describe 'blazing init' do
3
+ describe 'blazing recipes' do
6
4
 
7
5
  before :each do
8
6
  setup_sandbox
9
7
  class Blazing::Recipe::Dummy < Blazing::Recipe
8
+ def run
9
+ puts 'dummy recipe was run'
10
+ end
10
11
  end
11
12
  @dummy_recipe = Blazing::Recipe::Dummy.new
12
13
  @config = Blazing::Config.new
13
14
  @config.target :production, @production_url
14
15
  @config.instance_variable_set('@recipes', [@dummy_recipe])
15
- @runner = Blazing::Runner.new(@config)
16
+ @cli = Blazing::CLI.new
17
+ Blazing::Config.stub(:parse).and_return @config
16
18
  end
17
19
 
18
20
  after :each do
@@ -20,8 +22,8 @@ describe 'blazing init' do
20
22
  end
21
23
 
22
24
  it 'runs the configured recipes' do
23
- @dummy_recipe.should_receive(:run)
24
- @runner.exec('recipes:run')
25
+ output = capture(:stdout) { @cli.recipes(:production) }
26
+ output.should == "dummy recipe was run\n"
25
27
  end
26
28
 
27
29
  end