chef_instance 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ require 'chef_instance/install'
2
+
3
+ module ChefInstance
4
+ module Install
5
+ # A Superclass for installing softare that already exists on the system.
6
+ # Useful for software copied on to the system from the outside, or
7
+ # available on the local disk.
8
+ class Existing < ChefInstance::Install::Template
9
+ # Construct a new Existing object.
10
+ # @param new_resource [Chef::Resource]
11
+ # @param run_context [Chef::RunContext]
12
+ def initialize(new_resource, run_context = nil)
13
+ super
14
+ @new_resource = new_resource
15
+ @run_context = run_context
16
+ end
17
+
18
+ # We don't really install since its already existing.
19
+ def install
20
+ end
21
+
22
+ # We don't really uninstall since its already existing.
23
+ def uninstall
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,66 @@
1
+ require 'chef/resource/package'
2
+ require 'chef_instance/install'
3
+
4
+ module ChefInstance
5
+ module Install
6
+ # A Superclass template for installing packages.
7
+ class Package < ChefInstance::Install::Template
8
+ # Construct a new ChefInstance::Install::Package.
9
+ # @param new_resource [Chef::Resource]
10
+ # @param run_context [Chef::RunContext]
11
+ def initialize(new_resource, run_context = nil)
12
+ super
13
+ end
14
+
15
+ # Installs a package.
16
+ #
17
+ # - Sets up a memoized [Chef::Resource::Package]
18
+ # - Passes install_options to the memoized resource.
19
+ # - Performs a `run_action(:install)` on the resource.
20
+ def install
21
+ package_resource
22
+ package_options
23
+ package_action(:install)
24
+ end
25
+
26
+ # Uninstalls a package.
27
+ #
28
+ # - Sets up a memoized [Chef::Resource::Package]
29
+ # - Performs a `run_action(:remove)` on the resource.
30
+ def uninstall
31
+ package_resource
32
+ package_options
33
+ package_action(:remove)
34
+ end
35
+
36
+ private
37
+
38
+ # Sets up a memoized [Chef::Resource::Package] based on package_name
39
+ # @return [Chef::Resource::Package]
40
+ def package_resource
41
+ @package ||= Chef::Resource::Package.new(package_name, @run_context)
42
+ end
43
+
44
+ # Iterates over options[:package] to send each key as a argument
45
+ # to the [Chef::Resource::Package]
46
+ def package_options
47
+ options[:package].each do |k, v|
48
+ package_resource.send(k, v)
49
+ end
50
+ end
51
+
52
+ # Performs action on the resource.
53
+ # @param action [Symbol] The action to perform.
54
+ def package_action(action)
55
+ package_resource.run_action(action)
56
+ end
57
+
58
+ # Fetches the package_name from options.
59
+ # TODO: Set a default for fetch that raises an error.
60
+ # @return [String] the package name
61
+ def package_name
62
+ options.fetch(package_name)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,82 @@
1
+ require 'chef/provider'
2
+
3
+ module ChefInstance
4
+ module Provider
5
+ # Provides a Superclass Chef Provider to help with Factories.
6
+ class Instance < Chef::Provider
7
+ # Construct a new ChefInstance::Provider::Instance.
8
+ # @param new_resource [Chef::Resource]
9
+ # @param run_context [Chef::RunContext]
10
+ def initialize(new_resource, run_context = nil)
11
+ super
12
+ @new_resource = new_resource
13
+ @run_context = run_context
14
+ @namespace = @new_resource.namespace
15
+ end
16
+
17
+ # http://docs.opscode.com/essentials_nodes_why_run.html
18
+ def whyrun_supported?
19
+ false
20
+ end
21
+
22
+ # Determines the state of the resource _currently_
23
+ # @return [Resource] @current_resource with name of @new_resource
24
+ def load_current_resource
25
+ @current_resource = Chef::Resource::Instance.new(
26
+ @new_resource, @run_context)
27
+ end
28
+
29
+ # Installs the `install_type`
30
+ # Creates the `service_type`
31
+ def action_create
32
+ instance(@new_resource.install_type, :install)
33
+ instance(@new_resource.service_type, :create)
34
+ end
35
+
36
+ # Destroy the `service_type`
37
+ # Uninstall the `install_type`
38
+ def action_destroy
39
+ end
40
+
41
+ # Disable the `service_type`
42
+ def action_disable
43
+ instance(@new_resource.service_type, :disable)
44
+ end
45
+
46
+ # Enable the `service_type`
47
+ def action_enable
48
+ instance(@new_resource.service_type, :enable)
49
+ end
50
+
51
+ private
52
+
53
+ # Lookup Class, Construct, and send an action.
54
+ # @param type [String] The install or service type being used.
55
+ # @param action [Symbol] The action being performed.
56
+ # @return [TrueClass, FalseClass]
57
+ def instance(type, action)
58
+ instance_class = instance_sub_class(type)
59
+ i = instance_class.new(@new_resource, @run_context)
60
+ i.send(action)
61
+ end
62
+
63
+ # Lookup the CONSTANT name of a class
64
+ # ChefInstance::Service::Init == BaseName::Type::Name
65
+ # @param type [String] The type to lookup.
66
+ # @param name [String] The name of the type to lookup.
67
+ # @return [Constant] The class handling type.
68
+ def instance_sub_class(type, name)
69
+ klass = "#{ @namespace }::#{ type.capitalize }::#{ name.capitalize }"
70
+ klass.split('::').reduce(Object) { |a, e| a.const_get(e) }
71
+ end
72
+
73
+ # Determine the @current_resource service provider.
74
+ def discover_service
75
+ end
76
+
77
+ # Determine how the @current_resource is installed.
78
+ def discover_install
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,63 @@
1
+ require 'chef/resource'
2
+
3
+ module ChefInstance
4
+ module Resource
5
+ # Provides a Superclass Chef Resource to help with Factories.
6
+ class Instance < Chef::Resource
7
+ # Construct a new ChefInstance::Resource::Instance.
8
+ # @param new_resource [Chef::Resource]
9
+ # @param run_context [Chef::RunContext]
10
+ def initialize(new_resource, run_context = nil)
11
+ super
12
+ @provider = ChefInstance::Provider::Instance
13
+ @action = :create
14
+ @allowed_actions = [:create, :destroy, :enable, :disable, :nothing]
15
+ end
16
+
17
+ # A hash passed to the install type.
18
+ # Used to pass end-user overrides to the installer.
19
+ # @param arg [Hash]
20
+ def install_options(arg = nil)
21
+ set_or_return(:install_options, arg, kind_of: Hash)
22
+ end
23
+
24
+ # The type of installation you are doing.
25
+ # @param arg [Symbol]
26
+ def install_type(arg = nil)
27
+ set_or_return(:install_type,
28
+ arg,
29
+ kind_of: [Symbol],
30
+ required: true,
31
+ equal_to: [:package, :existing])
32
+ end
33
+
34
+ # A hash passed to the service type.
35
+ # Used to pass end-user overrides to the service.
36
+ # @param arg [Hash]
37
+ def service_options(arg = nil)
38
+ set_or_return(:service_options, arg, kind_of: Hash)
39
+ end
40
+
41
+ # The type of service manager you are using.
42
+ # @param arg [Symbol]
43
+ def service_type(arg = nil)
44
+ set_or_return(:service_type,
45
+ arg,
46
+ kind_of: Symbol,
47
+ required: true,
48
+ equal_to: [:init, :runit])
49
+ end
50
+
51
+ # Provide the namespace for lookups.
52
+ # Because the provider subclasses Chef::Provider
53
+ # Its `self.class` points us in the wrong direction.
54
+ # @param arg [String] namespace to use for lookups.
55
+ def namespace(arg = nil)
56
+ set_or_return(:namespace,
57
+ arg,
58
+ kind_of: String,
59
+ default: 'ChefInstance')
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,64 @@
1
+ module ChefInstance
2
+ module Service
3
+ # A Superclass template for service managers.
4
+ class Template
5
+ # Construct a new ChefInstance::Service::Template.
6
+ # @param new_resource [Chef::Resource]
7
+ # @param run_context [Chef::RunContext]
8
+ def initialize(new_resource, run_context = nil)
9
+ @new_resource = new_resource
10
+ @run_context = run_context
11
+ end
12
+
13
+ # Create and place the service script.
14
+ def create
15
+ create_service_script
16
+ end
17
+
18
+ # Destroy the service script.
19
+ def destroy
20
+ destroy_service
21
+ end
22
+
23
+ # If enabled, disable the service.
24
+ def disable
25
+ disable_service
26
+ end
27
+
28
+ # Enable the service.
29
+ def enable
30
+ enable_service
31
+ end
32
+
33
+ private
34
+
35
+ # Iterates over options[:service] to send each key as a argument
36
+ # to the [Chef::Resource::Service]
37
+ def service_options
38
+ options[:service].each do |k, v|
39
+ service_resource.send(k, v)
40
+ end
41
+ end
42
+
43
+ # Performs action on the resource.
44
+ # @param action [Symbol] The action to perform.
45
+ def service_action(action)
46
+ service_resource.run_action(action)
47
+ end
48
+
49
+ # Sets up a memoized [Chef::Resource::Service] based on package_name
50
+ # @return [Chef::Resource::Service]
51
+ def service_resource
52
+ @service ||= Chef::Resource::Service.new(service_name, @run_context)
53
+ end
54
+
55
+ def service_name
56
+ new_resource.name
57
+ end
58
+
59
+ def options
60
+ @options ||= @new_resource.service_options
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,23 @@
1
+ require 'chef/resource/template'
2
+ require 'chef_instance/service'
3
+
4
+ module ChefInstance
5
+ module Service
6
+ # A Superclass managing Init.
7
+ class Init < ChefInstance::Service::Template
8
+ # Construct a new ChefInstance::Service::Init.
9
+ # @param new_resource [Chef::Resource]
10
+ # @param run_context [Chef::RunContext]
11
+ def initialize(new_resource, run_context = nil)
12
+ super
13
+ end
14
+
15
+ private
16
+
17
+ # Uses a template to create a Init script for the named service.
18
+ def create_service_script
19
+ #r = Chef::Resource::Template.new
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ require 'chef_instance/service'
2
+
3
+ module ChefInstance
4
+ module Service
5
+ # A Superclass managing Runit.
6
+ class Runit < ChefInstance::Service::Template
7
+ # Construct a new ChefInstance::Service::Runit.
8
+ # @param new_resource [Chef::Resource]
9
+ # @param run_context [Chef::RunContext]
10
+ def initialize(new_resource, run_context = nil)
11
+ super
12
+ end
13
+
14
+ private
15
+
16
+ # Sets up a memoized [Chef::Resource::RunitService] based on package_name
17
+ # @return [Chef::Resource::RunitService]
18
+ def service_resource
19
+ @service ||=
20
+ Chef::Resource::RunitService.new(service_name, @run_context)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module ChefInstance
2
+ # The version of the chef_instance library.
3
+ # This is semver
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,45 @@
1
+ require_relative '../spec_helper.rb'
2
+ require 'chef_instance/install/existing'
3
+
4
+ describe ChefInstance::Install::Existing, '' do
5
+ let(:node) do
6
+ node = Chef::Node.new
7
+ node.automatic[:platform] = 'ubuntu'
8
+ node.automatic[:platform_version] = '12.04'
9
+ node
10
+ end
11
+
12
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
13
+ let(:run_context) { Chef::RunContext.new(node, {}, events) }
14
+ let(:resource) { 'package_test' }
15
+ let(:install) do
16
+ ChefInstance::Install::Existing.new(resource, run_context)
17
+ end
18
+
19
+ before do
20
+ @install_res = install
21
+ end
22
+
23
+ describe 'Object Ancestry Checks' do
24
+ it 'Is a ChefInstance::Install::Template?' do
25
+ @install_res.must_be_kind_of(ChefInstance::Install::Template)
26
+ end
27
+
28
+ it 'Is a instance of Init' do
29
+ @install_res.must_be_instance_of(ChefInstance::Install::Existing)
30
+ end
31
+ end
32
+
33
+ describe 'It adhears to the Install::Template contract.' do
34
+
35
+ describe 'It should..' do
36
+ it 'Create.' do
37
+ @install_res.must_respond_to(:install)
38
+ end
39
+
40
+ it 'Destroy.' do
41
+ @install_res.must_respond_to(:uninstall)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,76 @@
1
+ require_relative '../spec_helper.rb'
2
+ require 'chef_instance/install/package'
3
+
4
+ describe ChefInstance::Install::Package, '' do
5
+ let(:node) do
6
+ node = Chef::Node.new
7
+ node.automatic[:platform] = 'ubuntu'
8
+ node.automatic[:platform_version] = '12.04'
9
+ node
10
+ end
11
+ let(:events) { Chef::EventDispatch::Dispatcher.new }
12
+ let(:run_context) { Chef::RunContext.new(node, {}, events) }
13
+ let(:instance_name) { 'test' }
14
+ let(:install_options) do
15
+ {
16
+ package: {
17
+ package_name: 'test',
18
+ version: '1.0.0'
19
+ }
20
+ }
21
+ end
22
+ let(:instance) do
23
+ MiniTest::Mock.new
24
+ end
25
+ let(:package_res) do
26
+ ChefInstance::Install::Package.new(instance, run_context)
27
+ end
28
+
29
+ before do
30
+ @package_res = package_res
31
+ @install_options = install_options
32
+ end
33
+
34
+ describe 'Object Ancestry Checks' do
35
+ it 'Is a ChefInstance::Install::Template?' do
36
+ @package_res.must_be_kind_of(ChefInstance::Install::Template)
37
+ end
38
+
39
+ it 'Is a instance of Init' do
40
+ @package_res.must_be_instance_of(ChefInstance::Install::Package)
41
+ end
42
+ end
43
+
44
+ describe 'It adhears to the Install::Template contract.' do
45
+ describe 'It should..' do
46
+ before :each do
47
+ @chef_package = MiniTest::Mock.new
48
+ @chef_package.expect(:package_name, true, %w(test))
49
+ @chef_package.expect(:version, true, %w(1.0.0))
50
+ instance.expect(:install_options, install_options)
51
+ instance.expect(:install_type, :package)
52
+ end
53
+
54
+ it 'install.' do
55
+ @chef_package.expect(:run_action, true, [:install])
56
+
57
+ @package_res.must_respond_to(:install)
58
+ @package_res.stub(:package_resource, @chef_package) do
59
+ @package_res.install.must_equal(true)
60
+ end
61
+
62
+ @chef_package.verify.must_equal(true)
63
+ end
64
+
65
+ it 'uninstall.' do
66
+ @chef_package.expect(:run_action, true, [:remove])
67
+ @package_res.must_respond_to(:uninstall)
68
+ @package_res.stub(:package_resource, @chef_package) do
69
+ @package_res.uninstall.must_equal(true)
70
+ end
71
+
72
+ @chef_package.verify.must_equal(true)
73
+ end
74
+ end
75
+ end
76
+ end