puppetbox 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6199b6d382c20f729a392c12218a9a5986dc4ac3
4
- data.tar.gz: 4c8dcbc077105f11ff0b49c46657402ccebd2d27
3
+ metadata.gz: ad288284d9a66b09a591260219ab1c3ad31c9562
4
+ data.tar.gz: 810ebf10d5f24b32eb3ba98d01b45db454805bb8
5
5
  SHA512:
6
- metadata.gz: c9cce6b05a5971072fadfa7a1e99e5e3e8d752cd34bcf8cab83ec2bc06ecd712470849e8ba9aa89201c9ad8ab87f5b1018c8fa9bef9b4e59e93136981a4d836f
7
- data.tar.gz: 371fa53666addeff1d83b6e0aa5a8a6860e030f8ff1431d0fe3f5c0482076b4a62be625110219f5e2cc28dcb1d8bc01b854252e9c174f70b0c88203393ea71dc
6
+ metadata.gz: 35b2473e845e2aa9ad67d4012434a81ecaff79266d1a11823e29333eab3cb7cb78ffb8322f9e112b523967b142f7a19a29608c0e52d070fa8f5bb64558695745
7
+ data.tar.gz: 84b43a030638aad37d350bd7179882e1860f9bcd59ca324380ad7d45d2652cdb0df46276fd1384c77436bb90cf5a24aa43f69c8c6222c1e091ff8602a9c80968
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
 
11
11
  # rspec failure tracking
12
12
  .rspec_status
13
+ *.gem
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in puppetbox.gemspec
4
+ gem 'derelict', :git => 'https://github.com/GeoffWilliams/derelict', :ref => 'preserve_real_status'
5
+
4
6
  gemspec
data/README.md CHANGED
@@ -25,9 +25,15 @@ TODO: Write usage instructions here
25
25
 
26
26
  ## Development
27
27
 
28
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+ ### Additional vagrant boxes
29
+ Many vagrant boxes that would be useful can't be shared for legal reasons, eg windows, suse, etc. These requirements can be worked around by users producing their own Vagrant boxes for internal use.
29
30
 
30
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+ A really good source of build scripts is [bento](https://github.com/chef/bento) from Chef.
32
+
33
+ PuppetBox requires each box:
34
+ * Has puppet installed
35
+ * Has all puppet executables in `$PATH`
36
+ * Has the ability to work with vagrant shared folders. This usually means guestbox additions must be installed, although rsync can work in some situations. When using rsync, remember that your code will only be synced as your VM is reloaded
31
37
 
32
38
  ## Contributing
33
39
 
@@ -1,6 +1,82 @@
1
+ require 'vagrantomatic/vagrantomatic'
2
+ require 'puppetbox/result'
3
+ require 'fileutils'
4
+ require "puppetbox/logger"
5
+
1
6
  module PuppetBox
2
7
  module Driver
3
8
  class Vagrant
9
+ # fixme - seems abandoned, might need to make my own :(
10
+ DEFAULT_VAGRANT_BOX = "puppetlabs/centos-7.2-64-puppet"
11
+ PUPPET_CODE_MOUNT = "/etc/puppetlabs/code/environments/production"
12
+
13
+ def initialize(name, codedir, keep_vm:true, working_dir:nil, config:{'box'=> DEFAULT_VAGRANT_BOX}, logger: nil)
14
+ @name = name
15
+ @keep_vm = keep_vm
16
+ @working_dir = working_dir || File.join(Dir.home, '.puppetbox')
17
+ @config = config
18
+ @result = PuppetBox::Result.new
19
+ @logger = PuppetBox::Logger.new(logger).logger
20
+
21
+ # Add the code dir to the config has so that it will automatically become
22
+ # a shared folder when the VM boots
23
+ @config["folders"] = "#{codedir}:#{PUPPET_CODE_MOUNT}"
24
+ @logger.debug "instance #{name} initialised"
25
+ end
26
+
27
+ def result
28
+ @result
29
+ end
30
+
31
+ # convert a derelelict (vagrant library used by vagrantomatic) exectutor to
32
+ # a result object as used by puppetbox
33
+ #
34
+ # Puppet exit status codes:
35
+ # 0: The run succeeded with no changes or failures; the system was already in the desired state.
36
+ # 1: The run failed, or wasn't attempted due to another run already in progress.
37
+ # 2: The run succeeded, and some resources were changed.
38
+ # 4: The run succeeded, and some resources failed.
39
+ # 6: The run succeeded, and included both changes and failures.
40
+ def run_puppet(puppet_class)
41
+ run_hash = @vm.run("sudo -i puppet apply --detailed-exitcodes -e 'include #{puppet_class}'")
42
+ @result.report(run_hash[:status], run_hash[:messages])
43
+ end
44
+
45
+ # Open a connection to a box (eg start a vm, ssh to a host etc)
46
+ def open()
47
+ # make sure working dir exists...
48
+ FileUtils.mkdir_p(@working_dir)
49
+ vom = Vagrantomatic::Vagrantomatic.new(vagrant_vm_dir: @working_dir)
50
+
51
+ @logger.debug("reading instance metadata for #{@name}")
52
+ @vm = vom.instance(@name)
53
+
54
+ @logger.debug("...setting instance config and saving")
55
+
56
+ @vm.config=(@config)
57
+ @vm.save
58
+ @logger.debug("Instance saved and ready for starting")
59
+ @vm.start
60
+ end
61
+
62
+ # Close a connection to a box (eg stop a vm, probaly doesn't need to do
63
+ # anything on SSH...)
64
+ def close()
65
+ if ! @keep_vm
66
+ @vm.purge
67
+ end
68
+ end
69
+
70
+ # Test that a VM is operational and able to run puppet
71
+ def self_test()
72
+ @vm.run("sudo -i puppet --version")[:status] == 0
73
+ end
74
+
75
+ def run_puppet_x2(puppet_class)
76
+ # fixme - link module
77
+ run_puppet(puppet_class)
78
+ run_puppet(puppet_class)
79
+ end
4
80
 
5
81
  end
6
82
  end
@@ -1,31 +1,20 @@
1
1
  module PuppetBox
2
- module Driver
3
- # Open a connection to a box (eg start a vm, ssh to a host etc)
4
- def self.open()
5
-
6
- end
7
-
8
- # Close a connection to a box (eg stop a vm, probaly doesn't need to do
9
- # anything on SSH...)
10
- def self.close()
11
-
12
- end
13
-
14
- # Test that a VM is operational and able to run puppet
15
- def self.selftest()
16
-
17
- end
18
-
19
- def self.puppet_apply_x2(puppet_class)
20
-
21
- end
22
-
23
- def self.log_error()
24
- end
25
-
26
- def self.log_debug()
27
- end
28
-
2
+ # New drivers must implement the methods in this class (via ductyping)
3
+ class Driver
4
+ # def initialize(name, keep_vm=true)
5
+ # end
6
+ #
7
+ # def self.open()
8
+ # end
9
+ #
10
+ # def self.close()
11
+ # end
12
+ #
13
+ # def self.selftest()
14
+ # end
15
+ #
16
+ # def self.puppet_apply_x2(puppet_class)
17
+ # end
29
18
  end
30
19
 
31
20
  end
@@ -1,3 +1,4 @@
1
+ require "logger"
1
2
  module PuppetBox
2
3
  # Logger is a class to allow for stateful and separate logs between instances
3
4
  # by passing in individual loggers on initialisation
@@ -7,7 +8,10 @@ module PuppetBox
7
8
  if logger
8
9
  @logger = logger
9
10
  else
10
- logger = Logger.new(STDOUT)
11
+ @logger = ::Logger.new(STDOUT)
12
+ @logger.formatter = proc do |severity, datetime, progname, msg|
13
+ "#{severity}: #{msg}"
14
+ end
11
15
  end
12
16
  end
13
17
 
@@ -18,7 +18,25 @@ module PuppetBox
18
18
  @report = []
19
19
  end
20
20
 
21
- def report(status, messages)
21
+ # 0: The run succeeded with no changes or failures; the system was already in the desired state.
22
+ # 1: The run failed, or wasn't attempted due to another run already in progress.
23
+ # 2: The run succeeded, and some resources were changed.
24
+ # 4: The run succeeded, and some resources failed.
25
+ # 6: The run succeeded, and included both changes and failures.
26
+ def report(status_code, messages)
27
+ status = PS_ERROR
28
+ if @report.empty?
29
+ # first run
30
+ if status_code == 0 or status_code == 2
31
+ status = PS_OK
32
+ end
33
+ else
34
+ if status_code == 0
35
+ status = PS_OK
36
+ elsif status_code == 2
37
+ status = PS_NOT_IDEMPOTENT
38
+ end
39
+ end
22
40
  @report.push({:status => status, :messages => messages})
23
41
  end
24
42
 
@@ -1,3 +1,3 @@
1
1
  module PuppetBox
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/puppetbox.gemspec CHANGED
@@ -22,4 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "bundler", "~> 1.14"
23
23
  spec.add_development_dependency "rake", "~> 10.0"
24
24
  spec.add_development_dependency "rspec", "~> 3.0"
25
+
26
+ spec.add_dependency "vagrantomatic"
25
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puppetbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Williams
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-09 00:00:00.000000000 Z
11
+ date: 2017-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vagrantomatic
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - geoff@geoffwilliams.me.uk