daptiv-chef-ci 0.0.15 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/daptiv-chef-ci.gemspec +21 -20
  3. data/lib/daptiv-chef-ci/clone_environment_task.rb +69 -0
  4. data/lib/daptiv-chef-ci/logger.rb +9 -10
  5. data/lib/daptiv-chef-ci/raketask_helper.rb +5 -6
  6. data/lib/daptiv-chef-ci/shell.rb +50 -28
  7. data/lib/daptiv-chef-ci/upload_cookbook_task.rb +62 -0
  8. data/lib/daptiv-chef-ci/vagrant_destroy_task.rb +11 -12
  9. data/lib/daptiv-chef-ci/vagrant_driver.rb +47 -56
  10. data/lib/daptiv-chef-ci/vagrant_provision_task.rb +12 -15
  11. data/lib/daptiv-chef-ci/vagrant_task.rb +34 -57
  12. data/lib/daptiv-chef-ci/vagrant_up_task.rb +14 -16
  13. data/lib/daptiv-chef-ci/version_cookbook_task.rb +69 -0
  14. data/lib/daptiv-chef-ci/virtualbox_driver.rb +11 -12
  15. data/spec/daptiv-chef-ci/logger_spec.rb +11 -15
  16. data/spec/daptiv-chef-ci/shell_spec.rb +23 -27
  17. data/spec/daptiv-chef-ci/vagrant_destroy_task_spec.rb +5 -7
  18. data/spec/daptiv-chef-ci/vagrant_driver_spec.rb +34 -72
  19. data/spec/daptiv-chef-ci/vagrant_provision_task_spec.rb +9 -13
  20. data/spec/daptiv-chef-ci/vagrant_task_spec.rb +29 -21
  21. data/spec/daptiv-chef-ci/vagrant_up_task_spec.rb +9 -12
  22. data/spec/daptiv-chef-ci/virtualbox_driver_spec.rb +21 -16
  23. data/spec/shared_contexts/rake.rb +7 -7
  24. metadata +21 -14
  25. data/lib/daptiv-chef-ci/basebox_builder_factory.rb +0 -19
  26. data/lib/daptiv-chef-ci/virtualbox_basebox_builder.rb +0 -33
  27. data/lib/daptiv-chef-ci/vmware_basebox_builder.rb +0 -103
  28. data/spec/daptiv-chef-ci/basebox_builder_factory_spec.rb +0 -25
  29. data/spec/daptiv-chef-ci/virtualbox_basebox_builder_spec.rb +0 -20
  30. data/spec/daptiv-chef-ci/vmware_basebox_builder_spec.rb +0 -20
@@ -1,92 +1,83 @@
1
1
  require 'log4r'
2
2
  require 'mixlib/shellout/exceptions'
3
- require_relative 'basebox_builder_factory'
4
3
  require_relative 'shell'
5
4
 
6
5
  module DaptivChefCI
6
+ # Drives Vagrant via the command shell
7
7
  class VagrantDriver
8
-
9
8
  # Constructs a new Vagrant management instance
10
9
  #
11
- # @param [String] The name of the Vagrant virtualization provider: virtualbox, vmware_fusion
12
- # defaults to :virtualbox
10
+ # @param [String] The name of the Vagrant virtualization provider:
11
+ # => virtualbox (default), vmware_fusion
13
12
  # @param [Shell] The CLI, optional
14
- # @param [BaseBoxBuilderFactory] The base box builder factory instance, optional
15
- def initialize(provider = :virtualbox, shell = nil, basebox_builder_factory = nil)
16
- @logger = Log4r::Logger.new("daptiv_chef_ci::vagrant")
17
- @shell = shell || DaptivChefCI::Shell.new()
18
- @basebox_builder_factory = basebox_builder_factory || DaptivChefCI::BaseBoxBuilderFactory.new()
13
+ def initialize(provider = :virtualbox, shell = nil)
14
+ @logger = Log4r::Logger.new('daptiv_chef_ci::vagrant')
15
+ @shell = shell || DaptivChefCI::Shell.new
19
16
  @provider = provider
20
17
  end
21
-
22
- def destroy(opts={})
18
+
19
+ def destroy(opts = {})
23
20
  opts = {
24
- :cmd_timeout_in_seconds => 180,
25
- :retry_attempts => 2,
26
- :retry_wait_in_seconds => 20
21
+ cmd_timeout_in_seconds: 180,
22
+ retry_attempts: 2,
23
+ retry_wait_in_seconds: 20,
24
+ continue_on_error: true
27
25
  }.merge(opts)
28
26
  exec_cmd_with_retry('vagrant destroy -f', opts)
29
27
  end
30
-
31
- def halt(opts={})
28
+
29
+ def halt(opts = {})
32
30
  opts = {
33
- :cmd_timeout_in_seconds => 180,
34
- :retry_attempts => 2,
35
- :retry_wait_in_seconds => 20
31
+ cmd_timeout_in_seconds: 180,
32
+ retry_attempts: 2,
33
+ retry_wait_in_seconds: 20
36
34
  }.merge(opts)
37
35
  exec_cmd_with_retry('vagrant halt', opts)
38
36
  end
39
-
40
- def up(opts={})
37
+
38
+ def up(opts = {})
41
39
  opts = {
42
- :cmd_timeout_in_seconds => 7200,
43
- :retry_attempts => 0
40
+ cmd_timeout_in_seconds: 7200,
41
+ retry_attempts: 0
44
42
  }.merge(opts)
45
43
  cmd = 'vagrant up'
46
- cmd += ' --provider=' + @provider.to_s if @provider != :virtualbox
44
+ cmd += ' --provider=' + @provider.to_s
47
45
  exec_cmd_with_retry(cmd, opts)
48
46
  end
49
-
50
- def provision(opts={})
47
+
48
+ def provision(opts = {})
51
49
  opts = {
52
- :cmd_timeout_in_seconds => 7200,
53
- :retry_attempts => 0
50
+ cmd_timeout_in_seconds: 7200,
51
+ retry_attempts: 0
54
52
  }.merge(opts)
55
53
  exec_cmd_with_retry('vagrant provision', opts)
56
54
  end
57
-
58
- def reload(opts={})
59
- opts = {
60
- :cmd_timeout_in_seconds => 180,
61
- :retry_attempts => 0
62
- }.merge(opts)
63
- exec_cmd_with_retry('vagrant reload', opts)
64
- end
65
-
66
- def package(opts={})
67
- base_dir = opts[:base_dir] || Dir.pwd
68
- box_name = opts[:box_name] || File.basename(base_dir)
69
- box_name += '.box' unless box_name.end_with?('.box')
70
-
71
- builder = @basebox_builder_factory.create(@shell, @provider, base_dir)
72
- builder.build(box_name)
73
- end
74
-
75
-
55
+
76
56
  private
77
-
78
- def exec_cmd_with_retry(cmd, opts)
57
+
58
+ def exec_cmd_with_retry(cmd, options)
79
59
  attempt ||= 1
80
- environment ||= opts[:environment] || {}
81
- timeout ||= opts[:cmd_timeout_in_seconds] || 600
82
- @shell.exec_cmd(cmd, timeout, environment)
60
+ opts ||= ensure_defaults(options)
61
+ @shell.exec_cmd(cmd, opts[:cmd_timeout_in_seconds], opts[:environment])
83
62
  rescue Mixlib::ShellOut::ShellCommandFailed => e
84
63
  @logger.warn("#{cmd} failed with error: #{e.message}")
85
- raise if attempt > (opts[:retry_attempts] || 0)
64
+ if attempt > opts[:retry_attempts]
65
+ return if opts[:continue_on_error]
66
+ raise
67
+ end
86
68
  attempt += 1
87
- sleep(opts[:retry_wait_in_seconds] || 10)
69
+ sleep(opts[:retry_wait_in_seconds])
88
70
  retry
89
71
  end
90
-
72
+
73
+ def ensure_defaults(opts)
74
+ {
75
+ environment: {},
76
+ cmd_timeout_in_seconds: 600,
77
+ retry_attempts: 0,
78
+ retry_wait_in_seconds: 10,
79
+ continue_on_error: false
80
+ }.merge(opts)
81
+ end
91
82
  end
92
- end
83
+ end
@@ -6,7 +6,6 @@ require_relative 'raketask_helper'
6
6
  require_relative 'logger'
7
7
 
8
8
  class VagrantProvision
9
-
10
9
  # Example usage, provisions a vmware box (box must already be up):
11
10
  #
12
11
  # VagrantProvision::RakeTask.new 'provision' do |t|
@@ -18,14 +17,15 @@ class VagrantProvision
18
17
  class RakeTask < ::Rake::TaskLib
19
18
  include ::Rake::DSL if defined? ::Rake::DSL
20
19
  include DaptivChefCI::RakeTaskHelpers
21
-
20
+
22
21
  attr_accessor :vagrant_driver
23
22
  attr_accessor :provision_timeout_in_seconds
24
23
  attr_accessor :environment
25
-
24
+
26
25
  # @param [String] name The task name.
27
26
  # @param [String] desc Description of the task.
28
- # @param [String] provider vagrant provider to use if other than the default virtualbox provider
27
+ # @param [String] provider vagrant provider to use if other than the default
28
+ # virtualbox provider
29
29
  def initialize(name = 'vagrant_provision', desc = 'Vagrant provision task')
30
30
  @name, @desc = name, desc
31
31
  @provision_timeout_in_seconds = 7200
@@ -33,25 +33,22 @@ class VagrantProvision
33
33
  yield self if block_given?
34
34
  define_task
35
35
  end
36
-
36
+
37
37
  private
38
38
 
39
39
  def define_task
40
40
  desc @desc
41
41
  task @name do
42
- execute {
43
- vagrant_driver.provision({
44
- :cmd_timeout_in_seconds => @provision_timeout_in_seconds,
45
- :environment => @environment
46
- })
47
- }
42
+ execute do
43
+ vagrant_driver.provision(
44
+ cmd_timeout_in_seconds: @provision_timeout_in_seconds,
45
+ environment: @environment)
46
+ end
48
47
  end
49
48
  end
50
49
 
51
- def vagrant_driver()
52
- @vagrant_driver ||= DaptivChefCI::VagrantDriver.new()
50
+ def vagrant_driver
51
+ @vagrant_driver ||= DaptivChefCI::VagrantDriver.new
53
52
  end
54
-
55
53
  end
56
54
  end
57
-
@@ -5,14 +5,12 @@ require_relative 'vagrant_driver'
5
5
  require_relative 'raketask_helper'
6
6
  require_relative 'logger'
7
7
 
8
+ # Drives Vagrant through Rake tasks
8
9
  class Vagrant
9
-
10
10
  # Example usage, creates a vmware base box:
11
11
  #
12
12
  # Vagrant::RakeTask.new 'vagrant_fusion' do |t|
13
13
  # t.provider = :vmware_fusion
14
- # t.create_box = true
15
- # t.box_name = 'windows-server-vmwarefusion.box'
16
14
  # t.up_timeout_in_seconds = 3600
17
15
  # t.environment = { :ENV_VAR1 => 'val1', :ENV_VAR2 => 'val2' }
18
16
  # end
@@ -21,28 +19,25 @@ class Vagrant
21
19
  class RakeTask < ::Rake::TaskLib
22
20
  include ::Rake::DSL if defined? ::Rake::DSL
23
21
  include DaptivChefCI::RakeTaskHelpers
24
-
22
+
25
23
  attr_accessor :vagrant_driver
26
24
  attr_accessor :provider
27
- attr_accessor :create_box
28
25
  attr_accessor :vagrantfile_dir
29
- attr_accessor :box_name
30
26
  attr_accessor :up_timeout_in_seconds
31
27
  attr_accessor :halt_timeout_in_seconds
32
28
  attr_accessor :destroy_timeout_in_seconds
33
29
  attr_accessor :destroy_retry_attempts
34
30
  attr_accessor :halt_retry_attempts
35
31
  attr_accessor :environment
36
-
32
+
37
33
  # @param [String] name The task name.
38
34
  # @param [String] desc Description of the task.
39
- # @param [String] provider vagrant provider to use if other than the default virtualbox provider
40
- def initialize(name = 'vagrant', desc = 'Vagrant up, halt, destroy, package task')
35
+ # @param [String] provider vagrant provider to use if other than the default
36
+ # virtualbox provider
37
+ def initialize(name = 'vagrant', desc = 'Vagrant up, halt, destroy task')
41
38
  @name, @desc = name, desc
42
39
  @provider = :virtualbox
43
- @create_box = false
44
40
  @vagrantfile_dir = Dir.pwd
45
- @box_name = nil
46
41
  @up_timeout_in_seconds = 7200
47
42
  @halt_timeout_in_seconds = 180
48
43
  @destroy_timeout_in_seconds = 180
@@ -52,65 +47,47 @@ class Vagrant
52
47
  yield self if block_given?
53
48
  define_task
54
49
  end
55
-
50
+
56
51
  private
57
52
 
58
53
  def define_task
59
54
  desc @desc
60
55
  task @name do
61
- execute_vagrant_run()
56
+ execute_vagrant_run
62
57
  end
63
58
  end
64
-
65
- def execute_vagrant_run()
66
- execute { destroy() }
67
- try_vagrant_up()
68
- end
69
-
70
- def try_vagrant_up()
71
- begin
72
- execute do
73
- up()
74
- package() if @create_box
75
- end
76
- ensure
77
- destroy()
59
+
60
+ def execute_vagrant_run
61
+ execute do
62
+ destroy(0)
63
+ up
78
64
  end
65
+ ensure
66
+ destroy
79
67
  end
80
-
81
- def up()
82
- vagrant_driver.up({
83
- :cmd_timeout_in_seconds => @up_timeout_in_seconds,
84
- :environment => @environment
85
- })
86
- end
87
-
88
- def package()
89
- halt()
90
- vagrant_driver.package({
91
- :base_dir => @vagrantfile_dir,
92
- :box_name => @box_name,
93
- :environment => @environment })
68
+
69
+ def up
70
+ vagrant_driver.up(
71
+ cmd_timeout_in_seconds: @up_timeout_in_seconds,
72
+ environment: @environment)
94
73
  end
95
-
96
- def destroy()
97
- vagrant_driver.destroy({
98
- :cmd_timeout_in_seconds => @destroy_timeout_in_seconds,
99
- :retry_attempts => @destroy_retry_attempts,
100
- :environment => @environment })
74
+
75
+ def destroy(destroy_retry_attempts = @destroy_retry_attempts)
76
+ vagrant_driver.destroy(
77
+ cmd_timeout_in_seconds: @destroy_timeout_in_seconds,
78
+ retry_attempts: destroy_retry_attempts,
79
+ environment: @environment)
101
80
  end
102
-
103
- def halt()
104
- vagrant_driver.halt({
105
- :cmd_timeout_in_seconds => @halt_timeout_in_seconds,
106
- :retry_attempts => @halt_retry_attempts,
107
- :environment => @environment })
81
+
82
+ def halt
83
+ vagrant_driver.halt(
84
+ cmd_timeout_in_seconds: @halt_timeout_in_seconds,
85
+ retry_attempts: @halt_retry_attempts,
86
+ environment: @environment)
108
87
  end
109
-
110
- def vagrant_driver()
88
+
89
+ def vagrant_driver
111
90
  @vagrant_driver ||= DaptivChefCI::VagrantDriver.new(@provider)
112
91
  end
113
-
114
92
  end
115
93
  end
116
-
@@ -6,8 +6,8 @@ require_relative 'raketask_helper'
6
6
  require_relative 'logger'
7
7
 
8
8
  class VagrantUp
9
-
10
- # Example usage, ups and provisions a Vagrant box without halting or destroying it.
9
+ # Example usage, ups and provisions a Vagrant box without halting or
10
+ # destroying it.
11
11
  #
12
12
  # VagrantUp::RakeTask.new 'up' do |t|
13
13
  # t.provider = :vmware_fusion
@@ -19,15 +19,16 @@ class VagrantUp
19
19
  class RakeTask < ::Rake::TaskLib
20
20
  include ::Rake::DSL if defined? ::Rake::DSL
21
21
  include DaptivChefCI::RakeTaskHelpers
22
-
22
+
23
23
  attr_accessor :vagrant_driver
24
24
  attr_accessor :provider
25
25
  attr_accessor :up_timeout_in_seconds
26
26
  attr_accessor :environment
27
-
27
+
28
28
  # @param [String] name The task name.
29
29
  # @param [String] desc Description of the task.
30
- # @param [String] provider vagrant provider to use if other than the default virtualbox provider
30
+ # @param [String] provider vagrant provider to use if other than the default
31
+ # virtualbox provider
31
32
  def initialize(name = 'vagrant_up', desc = 'Vagrant up task')
32
33
  @name, @desc = name, desc
33
34
  @provider = :virtualbox
@@ -36,25 +37,22 @@ class VagrantUp
36
37
  yield self if block_given?
37
38
  define_task
38
39
  end
39
-
40
+
40
41
  private
41
42
 
42
43
  def define_task
43
44
  desc @desc
44
45
  task @name do
45
- execute {
46
- vagrant_driver.up({
47
- :cmd_timeout_in_seconds => @up_timeout_in_seconds,
48
- :environment => @environment
49
- })
50
- }
46
+ execute do
47
+ vagrant_driver.up(
48
+ cmd_timeout_in_seconds: @up_timeout_in_seconds,
49
+ environment: @environment)
50
+ end
51
51
  end
52
52
  end
53
-
54
- def vagrant_driver()
53
+
54
+ def vagrant_driver
55
55
  @vagrant_driver ||= DaptivChefCI::VagrantDriver.new(@provider)
56
56
  end
57
-
58
57
  end
59
58
  end
60
-
@@ -0,0 +1,69 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+ require 'rake/dsl_definition'
4
+ require 'versionomy'
5
+ require_relative 'raketask_helper'
6
+
7
+ class VersionCookbook
8
+ # Example usage, versions a cookbook to 1.0.x for usage in CI
9
+ #
10
+ # VersionCookbook::RakeTask.new 'version_cookbook' do |t|
11
+ # t.bump = false
12
+ # t.version = "1.0.#{ENV['BUILD_NUMBER']}"
13
+ # t.version_file = 'version.txt'
14
+ # end
15
+ #
16
+ # This class lets you define Rake tasks to manage a cookbook
17
+ # version.txt file
18
+ class RakeTask < ::Rake::TaskLib
19
+ include ::Rake::DSL if defined? ::Rake::DSL
20
+ include DaptivChefCI::RakeTaskHelpers
21
+
22
+ attr_accessor :bump
23
+ attr_accessor :version
24
+ attr_accessor :version_file
25
+
26
+ # @param [String] name The task name.
27
+ # @param [String] desc Description of the task.
28
+ def initialize(name = 'version_cookbook', desc = 'Version cookbook task')
29
+ @logger = Log4r::Logger.new('daptiv_chef_ci::version_cookbook_task')
30
+ @name, @desc = name, desc
31
+ @version_file = File.join(Dir.pwd, 'version.txt')
32
+ @bump = true
33
+ yield self if block_given?
34
+ define_task
35
+ end
36
+
37
+ private
38
+
39
+ def define_task
40
+ desc @desc
41
+ task @name do
42
+ execute do
43
+ create_or_update_version_file
44
+ end
45
+ end
46
+ end
47
+
48
+ def create_or_update_version_file
49
+ version = ::Versionomy.parse(start_version)
50
+ version = version.bump(:tiny) if @bump
51
+ write_version(version)
52
+ end
53
+
54
+ def start_version
55
+ raw_version = @version
56
+ if !raw_version && File.exist?(@version_file)
57
+ raw_version = IO.read(@version_file).chomp
58
+ end
59
+ raw_version = '0.0.1' unless raw_version
60
+ @logger.debug("Found starting cookbook version: #{raw_version}")
61
+ raw_version
62
+ end
63
+
64
+ def write_version(version)
65
+ @logger.debug("Setting cookbook version: #{version}")
66
+ IO.write(@version_file, version.to_s)
67
+ end
68
+ end
69
+ end