blazing 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,11 @@
1
1
  require 'active_support/inflector'
2
2
  require 'blazing'
3
+ # require 'blazing/logger'
3
4
 
4
5
  module Blazing
5
6
  class Recipe
6
7
 
8
+
7
9
  # TODO: provide hooks for recipe to use bundle exec
8
10
 
9
11
  attr_accessor :name, :options
@@ -11,6 +13,7 @@ module Blazing
11
13
  def initialize(name, options = {})
12
14
  @name = name.to_s
13
15
  @options = options
16
+ @logger = options[:_logger] ||= Blazing::Logger.new
14
17
  end
15
18
 
16
19
  def run
@@ -20,20 +23,10 @@ module Blazing
20
23
  def recipe_class
21
24
  ('Blazing::' + (@name.to_s + '_recipe').camelize).constantize
22
25
  rescue NameError
23
- Blazing::LOGGER.error "unable to load #{@name} recipe"
26
+ @logger.log :error, "unable to load #{@name} recipe"
24
27
  return nil
25
28
  end
26
29
 
27
- def fail
28
- raise 'NOT IMPLEMENTED'
29
- # TODO: implement meaningful default behaviour!
30
- end
31
-
32
- def success
33
- raise 'NOT IMPLEMENTED'
34
- # TODO: implement meaningful default behaviour!
35
- end
36
-
37
30
  class << self
38
31
 
39
32
  def load_builtin_recipes
@@ -1,3 +1,5 @@
1
+ require 'blazing/recipe'
2
+
1
3
  module Blazing
2
4
 
3
5
  class BundlerRecipe < Blazing::Recipe
@@ -1,3 +1,5 @@
1
+ require 'blazing/recipe'
2
+
1
3
  module Blazing
2
4
 
3
5
  class RvmRecipe < Blazing::Recipe
@@ -1,62 +1,67 @@
1
+ require 'blazing'
2
+ require 'blazing/config'
3
+
1
4
  module Blazing
2
5
  class Remote
3
- class << self
4
-
5
- def post_receive(target_name)
6
- set_git_dir
7
- target = config.find_target(target_name)
8
-
9
- # TODO: Check if post-hook and blazing versions match before doing anything
10
6
 
11
- if target.recipes.blank?
12
- target.recipes = config.recipes
13
- end
14
-
15
- # TODO: looks stupid. shorter way to do it?
16
- use_rvm = target.recipes.find { |recipe| recipe.name == 'rvm' }
17
- target.recipes.delete_if { |recipe| recipe.name == 'rvm' }
18
-
19
- Blazing::Recipe.load_builtin_recipes
7
+ def post_receive(target_name)
8
+ set_git_dir
9
+ @target = config.find_target(target_name)
10
+ @recipes = @target.recipes
11
+ setup_and_run_recipes
12
+ reset_head!
13
+ end
20
14
 
21
- if use_rvm
22
- use_rvm.run
23
- end
15
+ def gemfile_present?
16
+ File.exists? 'Gemfile'
17
+ end
24
18
 
25
- if gemfile_present?
26
- # TODO: Bundler setup or something
27
- end
19
+ def set_git_dir
20
+ @_env ||= ENV
21
+ @_dir ||= Dir
22
+ if @_env['GIT_DIR'] == '.'
23
+ @_dir.chdir('..')
24
+ @_env['GIT_DIR'] = '.git'
25
+ end
26
+ end
28
27
 
29
- target.recipes.each do |recipe|
30
- recipe.run
31
- end
28
+ def reset_head!
29
+ @runner ||= Blazing::Runner.new
30
+ @runner.run 'git reset --hard HEAD'
31
+ end
32
32
 
33
- reset_head!
34
- end
33
+ def config
34
+ @_config ||= Blazing::Config
35
+ @_config.load
36
+ end
35
37
 
36
- def post_setup(target_name)
37
- # TODO: needed?
38
- end
38
+ def use_rvm?
39
+ @rvm_recipe = @recipes.find { |recipe| recipe.name == 'rvm' }
40
+ @recipes.delete_if { |recipe| recipe.name == 'rvm' }
39
41
 
40
- def gemfile_present?
41
- File.exists? 'Gemfile'
42
- end
42
+ @rvm_recipe
43
+ end
43
44
 
44
- def set_git_dir
45
- if ENV['GIT_DIR'] == '.'
46
- Dir.chdir('..')
47
- ENV['GIT_DIR'] = '.git'
48
- end
49
- end
45
+ def setup_and_run_recipes
46
+ @recipes = config.recipes if @recipes.blank?
47
+ Blazing::Recipe.load_builtin_recipes
48
+ run_recipes
49
+ end
50
50
 
51
- def reset_head!
52
- system 'git reset --hard HEAD'
51
+ def run_recipes
52
+ run_bootstrap_recipes
53
+ @recipes.each do |recipe|
54
+ recipe.run
53
55
  end
56
+ end
54
57
 
55
- def config
56
- Blazing::Config.load
57
- end
58
+ def run_bootstrap_recipes
59
+ @rvm_recipe.run if use_rvm?
58
60
 
61
+ # if gemfile_present?
62
+ # # TODO: Bundler setup or something ?
63
+ # end
59
64
  end
60
-
61
65
  end
66
+
62
67
  end
@@ -0,0 +1,5 @@
1
+ class Blazing::Runner
2
+ def self.run(command)
3
+ system command
4
+ end
5
+ end
@@ -1,49 +1,81 @@
1
+ require 'blazing'
2
+ require 'blazing/object'
3
+
1
4
  module Blazing
2
5
  class Target
3
6
 
4
7
  attr_accessor :name, :recipes
5
8
 
6
- @@configuration_options = [:deploy_to, :host, :user, :path, :default]
9
+ AVAILABLE_SETTINGS = [:deploy_to, :host, :user, :path, :default]
7
10
 
8
11
  def initialize(name, options = {})
9
12
  @name = name.to_s
10
- @@configuration_options.each do |option|
13
+ @logger = options[:_logger] ||= Blazing::Logger.new
14
+ @runner = options[:_runner] ||= Blazing::Runner
15
+ @hook = options[:_hook] ||= Blazing::CLI::Hook
16
+ create_accesors(options)
17
+ end
18
+
19
+ def setup
20
+ clone_repository
21
+ add_target_as_remote
22
+ setup_post_receive_hook
23
+ end
24
+
25
+ def deploy
26
+ @runner.run "git push #{name}"
27
+ end
28
+
29
+ def config
30
+ @_config ||= Blazing::Config
31
+ @_config.load
32
+ end
33
+
34
+ def create_accesors(options)
35
+ assign_settings(options)
36
+ parse_deploy_to_string unless @deploy_to.blank?
37
+ ensure_mandatory_settings
38
+ end
39
+
40
+ def assign_settings(options)
41
+ AVAILABLE_SETTINGS.each do |option|
11
42
  instance_variable_set("@#{option}", options[option])
12
43
  self.class.send(:attr_accessor, option)
13
44
  end
45
+ end
14
46
 
15
- # If the :deploy_to option is given, user, host and path are overriden
16
- unless deploy_to.blank?
17
- @host = deploy_to.scan(/@(.*):/).join
18
- @user = deploy_to.scan(/(.*)@/).join
19
- @path = deploy_to.scan(/:(.*)/).join
20
- end
47
+ # If the :deploy_to option is set, user, host and path are overriden
48
+ def parse_deploy_to_string
49
+ @host = @deploy_to.scan(/@(.*):/).join
50
+ @user = @deploy_to.scan(/(.*)@/).join
51
+ @path = @deploy_to.scan(/:(.*)/).join
52
+ end
21
53
 
22
- # Raise an error if one of the required options is still empty
54
+ # Raise an error if one of the required options is still empty
55
+ def ensure_mandatory_settings
23
56
  [:host, :user, :path].each do |option|
24
57
  raise "#{option} can't be blank!" if instance_variable_get("@#{option}").blank?
25
58
  end
26
59
  end
27
60
 
28
- def setup
29
- clone_command = "if [ -e #{path} ]; then \
30
- echo 'directory exists already'; else \
31
- git clone #{config.repository} #{path} && cd #{path} && git config receive.denyCurrentBranch ignore; fi"
32
-
33
- system "ssh #{user}@#{host} '#{clone_command}'"
34
- system "git remote add #{name} #{user}@#{host}:#{path}"
61
+ def clone_command
62
+ "if [ -e #{@path} ]; then \
63
+ echo 'directory exists already'; else \
64
+ git clone #{config.repository} #{@path} && cd #{@path} && git config receive.denyCurrentBranch ignore; fi"
65
+ end
35
66
 
36
- Blazing::CLI::Hook.new([name]).generate #.generate(target.name)
37
- system "scp /tmp/post-receive #{user}@#{host}:#{path}/.git/hooks/post-receive"
38
- system "ssh #{user}@#{host} 'chmod +x #{path}/.git/hooks/post-receive'"
67
+ def clone_repository
68
+ @runner.run "ssh #{@user}@#{@host} '#{clone_command}'"
39
69
  end
40
70
 
41
- def deploy
42
- system "git push #{name}"
71
+ def add_target_as_remote
72
+ @runner.run "git remote add #{@name} #{@user}@#{@host}:#{@path}"
43
73
  end
44
74
 
45
- def config
46
- Blazing::Config.load
75
+ def setup_post_receive_hook
76
+ @hook.new([@name]).generate
77
+ @runner.run "scp /tmp/post-receive #{@user}@#{@host}:#{@path}/.git/hooks/post-receive"
78
+ @runner.run "ssh #{@user}@#{@host} 'chmod +x #{@path}/.git/hooks/post-receive'"
47
79
  end
48
80
 
49
81
  end
@@ -1,3 +1,3 @@
1
1
  module Blazing
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+ require 'blazing/cli/base'
3
+
4
+ describe Blazing::CLI::Base do
5
+
6
+ before :all do
7
+ Blazing.send(:remove_const, 'CONFIGURATION_FILE')
8
+ Blazing::CONFIGURATION_FILE = 'spec/support/config.rb'
9
+ end
10
+
11
+ before :each do
12
+ @logger = double('logger', :log => nil, :report => nil)
13
+ @runner = double('runner', :run => nil)
14
+ @hook = double('hook', :new => double('template', :generate => nil))
15
+ @base = Blazing::CLI::Base.new(@logger)
16
+ end
17
+
18
+ describe '#init' do
19
+ it 'invokes all CLI::Create tasks' do
20
+ @create_task = double('create task')
21
+ @base.instance_variable_set('@task', @create_task)
22
+ @create_task.should_receive(:invoke_all)
23
+ @base.init
24
+ end
25
+ end
26
+
27
+ describe '#setup' do
28
+
29
+ before :each do
30
+ @some_target_name = 'test_target'
31
+ hook = double('hook', :new => double('template', :generate => nil))
32
+ config = Blazing::Config.new
33
+ config.target(@some_target_name, :deploy_to => 'smoeone@somewhere:/asdasdasd', :_logger => @logger, :_runner => @runner, :_hook => hook)
34
+ @base.instance_variable_set('@config', config)
35
+ end
36
+
37
+ it 'says what target is being setup' do
38
+ @logger.should_receive(:log).with(:info, "setting up target test_target")
39
+ @base.setup(@some_target_name)
40
+ end
41
+
42
+ it 'runs setup on selected target' do
43
+ @target = @base.instance_variable_get('@config').instance_variable_get('@targets').first
44
+ @target.should_receive(:setup)
45
+ @base.setup(@some_target_name)
46
+ end
47
+
48
+ it 'logs a success message if exitstatus of setup was 0' do
49
+ @base.instance_variable_set('@exit_status', 0)
50
+ @logger.should_receive(:log).with(:success, "successfully set up target test_target")
51
+ @base.setup(@some_target_name)
52
+ end
53
+
54
+ it 'logs an error if exitstatus of setup was not 0' do
55
+ @base.instance_variable_set('@exit_status', 1)
56
+ @logger.should_receive(:log).with(:error, "failed setting up target test_target")
57
+ @base.setup(@some_target_name)
58
+ end
59
+ end
60
+
61
+ describe '#deploy' do
62
+
63
+ before :each do
64
+ @some_target_name = 'test_target'
65
+ hook = double('hook', :new => double('template', :generate => nil))
66
+ config = Blazing::Config.new
67
+ config.target(@some_target_name, :deploy_to => 'smoeone@somewhere:/asdasdasd', :_logger => @logger, :_runner => @runner, :_hook => hook)
68
+ @base.instance_variable_set('@config', config)
69
+ end
70
+
71
+ it 'says what target is being deployed' do
72
+ @logger.should_receive(:log).with(:info, "deploying target test_target")
73
+ @base.deploy(@some_target_name)
74
+ end
75
+
76
+ it 'runs setup on selected target' do
77
+ @target = @base.instance_variable_get('@config').instance_variable_get('@targets').first
78
+ @target.should_receive(:deploy)
79
+ @base.deploy(@some_target_name)
80
+ end
81
+
82
+ it 'logs a success message if exitstatus of setup was 0' do
83
+ @base.instance_variable_set('@exit_status', 0)
84
+ @logger.should_receive(:log).with(:success, "successfully deployed target test_target")
85
+ @base.deploy(@some_target_name)
86
+ end
87
+
88
+ it 'logs an error if exitstatus of setup was not 0' do
89
+ @base.instance_variable_set('@exit_status', 1)
90
+ @logger.should_receive(:log).with(:error, "failed deploying on target test_target")
91
+ @base.deploy(@some_target_name)
92
+ end
93
+ end
94
+
95
+ describe '#recipes' do
96
+ it 'prints a list of available recipes' do
97
+ @logger.should_receive(:log).exactly(2).times
98
+ @base.recipes
99
+ end
100
+ end
101
+
102
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'blazing'
3
+ require 'blazing/cli/create'
4
+
5
+ describe Blazing::CLI::Create do
6
+
7
+ before :each do
8
+ @logger = double('logger', :log => nil, :report => nil)
9
+ @config_generator = Blazing::CLI::Create.new(@logger)
10
+ end
11
+
12
+ it 'knows the source root for its tempate' do
13
+ Blazing::CLI::Create.source_root.should == File.dirname(__FILE__).gsub('spec', 'lib')
14
+ end
15
+
16
+ it 'creates an empty directory for the config file' do
17
+ @config_generator.should_receive(:empty_directory).with(Blazing::DIRECTORY)
18
+ @config_generator.create_blazing_dir
19
+ end
20
+
21
+ it 'creates the config file' do
22
+ @config_generator.should_receive(:template).with('templates/blazing.tt', Blazing::CONFIGURATION_FILE)
23
+ @config_generator.create_config_file
24
+ end
25
+
26
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'blazing/cli/hook'
3
+
4
+ describe Blazing::CLI::Hook do
5
+
6
+ it 'knows the source root for its tempate' do
7
+ Blazing::CLI::Hook.source_root.should == File.dirname(__FILE__).gsub('spec', 'lib')
8
+ end
9
+
10
+ it 'genereates the hook template in the correct location' do
11
+ @hook = Blazing::CLI::Hook.new(['test_target'])
12
+ @hook.should_receive(:template).with('templates/post-hook.tt', '/tmp/post-receive')
13
+ @hook.generate
14
+ end
15
+
16
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+ require 'blazing/config'
3
+
4
+ describe Blazing::Config do
5
+
6
+ before :each do
7
+ @config = Blazing::Config.new
8
+ end
9
+
10
+ describe '#target' do
11
+ it 'adds a target to the config object' do
12
+ @config.target(:test_target, :deploy_to => 'smoeone@somewhere:/asdasdasd')
13
+ @config.targets.size.should == 1
14
+ end
15
+ end
16
+
17
+ describe '#use' do
18
+ it 'adds a recipe to the config object' do
19
+ @config.use(:rvm)
20
+ @config.recipes.size.should == 1
21
+ end
22
+ end
23
+
24
+ describe '#find_target' do
25
+ context 'when a target name is given' do
26
+ it 'returns the target with the given name' do
27
+ @config.target(:test_target, :deploy_to => 'smoeone@somewhere:/asdasdasd')
28
+ @config.find_target(:test_target).name.should == 'test_target'
29
+ end
30
+ end
31
+
32
+ context 'when no target name given' do
33
+ it 'returns the target defined in config if there is only one' do
34
+ @config.target(:test_target, :deploy_to => 'smoeone@somewhere:/asdasdasd')
35
+ @config.find_target.name.should == 'test_target'
36
+ end
37
+
38
+ it 'returns the default target if there is one' do
39
+ @config.target(:test_target, :deploy_to => 'smoeone@somewhere:/asdasdasd')
40
+ @config.target(:default_target, :default => true, :deploy_to => 'smoeone@somewhere:/asdasdasd')
41
+ @config.find_target.name.should == 'default_target'
42
+ end
43
+
44
+ it 'raises an error if no target could be determined' do
45
+ lambda { @config.find_target }.should raise_error
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '.dsl_setter' do
51
+ before :each do
52
+ @config.class.class_eval do
53
+ dsl_setter :foo
54
+ end
55
+ end
56
+
57
+ it 'creates a dsl method to set the attribute in the config class' do
58
+ @config.foo 'something'
59
+ @config.instance_variable_get('@foo').should == 'something'
60
+ end
61
+
62
+ it 'creates a dsl method that allows to read the attribute in the config class' do
63
+ @config.foo 'something'
64
+ @config.foo.should == 'something'
65
+ end
66
+ end
67
+
68
+ describe '.load' do
69
+ it 'reads and parses the config file and returns a config object' do
70
+ Blazing.send(:remove_const, 'CONFIGURATION_FILE')
71
+ Blazing::CONFIGURATION_FILE = 'spec/support/config.rb'
72
+ Blazing::Config.load.should be_a Blazing::Config
73
+ end
74
+ end
75
+
76
+ describe '.read' do
77
+ it 'parses the config and returns a config object' do
78
+ @config.class.read {}.should be_a Blazing::Config
79
+ end
80
+ end
81
+
82
+ end