poise-service 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.kitchen.travis.yml +4 -0
  4. data/.kitchen.yml +9 -0
  5. data/.travis.yml +23 -0
  6. data/.yardopts +6 -0
  7. data/Berksfile +27 -0
  8. data/Gemfile +33 -0
  9. data/LICENSE +201 -0
  10. data/README.md +381 -0
  11. data/Rakefile +17 -0
  12. data/chef/attributes/default.rb +19 -0
  13. data/chef/templates/systemd.service.erb +13 -0
  14. data/chef/templates/sysvinit.sh.erb +177 -0
  15. data/chef/templates/upstart.conf.erb +37 -0
  16. data/lib/poise_service.rb +25 -0
  17. data/lib/poise_service/cheftie.rb +18 -0
  18. data/lib/poise_service/error.rb +20 -0
  19. data/lib/poise_service/resources.rb +28 -0
  20. data/lib/poise_service/resources/poise_service.rb +161 -0
  21. data/lib/poise_service/resources/poise_service_test.rb +200 -0
  22. data/lib/poise_service/resources/poise_service_user.rb +136 -0
  23. data/lib/poise_service/service_mixin.rb +192 -0
  24. data/lib/poise_service/service_providers.rb +37 -0
  25. data/lib/poise_service/service_providers/base.rb +193 -0
  26. data/lib/poise_service/service_providers/dummy.rb +100 -0
  27. data/lib/poise_service/service_providers/systemd.rb +63 -0
  28. data/lib/poise_service/service_providers/sysvinit.rb +86 -0
  29. data/lib/poise_service/service_providers/upstart.rb +117 -0
  30. data/lib/poise_service/utils.rb +45 -0
  31. data/lib/poise_service/version.rb +19 -0
  32. data/poise-service.gemspec +41 -0
  33. data/test/cookbooks/poise-service_test/metadata.rb +18 -0
  34. data/test/cookbooks/poise-service_test/providers/mixin.rb +43 -0
  35. data/test/cookbooks/poise-service_test/recipes/default.rb +47 -0
  36. data/test/cookbooks/poise-service_test/recipes/mixin.rb +34 -0
  37. data/test/cookbooks/poise-service_test/resources/mixin.rb +26 -0
  38. data/test/gemfiles/chef-12.gemfile +19 -0
  39. data/test/gemfiles/master.gemfile +22 -0
  40. data/test/integration/default/serverspec/Gemfile +19 -0
  41. data/test/integration/default/serverspec/default_spec.rb +45 -0
  42. data/test/integration/default/serverspec/mixin_spec.rb +37 -0
  43. data/test/spec/resources/poise_service_spec.rb +235 -0
  44. data/test/spec/resources/poise_service_user_spec.rb +119 -0
  45. data/test/spec/service_mixin_spec.rb +164 -0
  46. data/test/spec/service_providers/base_spec.rb +231 -0
  47. data/test/spec/service_providers/dummy_spec.rb +91 -0
  48. data/test/spec/service_providers/systemd_spec.rb +98 -0
  49. data/test/spec/service_providers/sysvinit_spec.rb +96 -0
  50. data/test/spec/service_providers/upstart_spec.rb +300 -0
  51. data/test/spec/spec_helper.rb +50 -0
  52. data/test/spec/utils_spec.rb +44 -0
  53. metadata +172 -0
@@ -0,0 +1,117 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # Used in the template.
18
+ require 'shellwords'
19
+
20
+ require 'chef/mixin/shell_out'
21
+
22
+ require 'poise_service/error'
23
+ require 'poise_service/service_providers/base'
24
+
25
+
26
+ module PoiseService
27
+ module ServiceProviders
28
+ class Upstart < Base
29
+ include Chef::Mixin::ShellOut
30
+ provides(:upstart)
31
+
32
+ def self.provides_auto?(node, resource)
33
+ # Don't allow upstart under docker, it won't work.
34
+ return false if node['virtualization'] && %w{docker lxc}.include?(node['virtualization']['system'])
35
+ service_resource_hints.include?(:upstart)
36
+ end
37
+
38
+ def action_reload
39
+ return if options['never_reload']
40
+ if !upstart_features[:reload_signal] && new_resource.reload_signal != 'HUP'
41
+ if options[:reload_shim]
42
+ Process.kill(new_resource.reload_signal, pid)
43
+ else
44
+ check_reload_signal!
45
+ end
46
+ else
47
+ super
48
+ end
49
+ end
50
+
51
+ def pid
52
+ cmd = shell_out(%w{initctl status} + [new_resource.service_name])
53
+ if !cmd.error? && md = cmd.stdout.match(/process (\d+)/)
54
+ md[1].to_i
55
+ else
56
+ nil
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def service_resource
63
+ super.tap do |r|
64
+ r.provider(Chef::Provider::Service::Upstart)
65
+ end
66
+ end
67
+
68
+ def create_service
69
+ check_reload_signal!
70
+ # Set features so it will be a closure below.
71
+ features = upstart_features
72
+ service_template("/etc/init/#{new_resource.service_name}.conf", 'upstart.conf.erb') do
73
+ variables.update(
74
+ upstart_features: features,
75
+ )
76
+ end
77
+ end
78
+
79
+ def destroy_service
80
+ file "/etc/init/#{new_resource.service_name}.conf" do
81
+ action :delete
82
+ end
83
+ end
84
+
85
+ def upstart_version
86
+ cmd = shell_out(%w{initctl --version})
87
+ if !cmd.error? && md = cmd.stdout.match(/upstart ([^)]+)\)/)
88
+ md[1]
89
+ else
90
+ '0'
91
+ end
92
+ end
93
+
94
+ def upstart_features
95
+ @upstart_features ||= begin
96
+ upstart_ver = Gem::Version.new(upstart_version)
97
+ versions_added = {
98
+ kill_signal: '1.3',
99
+ reload_signal: '1.10',
100
+ setuid: '1.4',
101
+ }
102
+ versions_added.inject({}) do |memo, (feature, version)|
103
+ memo[feature] = Gem::Requirement.create(">= #{version}").satisfied_by?(upstart_ver)
104
+ memo
105
+ end
106
+ end
107
+ end
108
+
109
+ def check_reload_signal!
110
+ if !options['reload_shim'] && !upstart_features[:reload_signal] && new_resource.reload_signal != 'HUP'
111
+ raise Error.new("Upstart #{upstart_version} only supports HUP for reload, to use the shim please set the 'reload_shim' options for #{new_resource.to_s}")
112
+ end
113
+ end
114
+
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,45 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'pathname'
18
+
19
+
20
+ module PoiseService
21
+ # Utility methods for PoiseService.
22
+ #
23
+ # @api public
24
+ # @since 1.0.0
25
+ module Utils
26
+ # Methods are also available as module-level methods as well as a mixin.
27
+ extend self
28
+
29
+ # Common segments to ignore
30
+ COMMON_SEGMENTS = %w{var www current etc}.inject({}) {|memo, seg| memo[seg] = true; memo }
31
+
32
+ # Parse the service name from a path. Look at the last component of the
33
+ # path, ignoring some common names.
34
+ #
35
+ # @param path [String] Path to parse.
36
+ # @return [String]
37
+ # @example
38
+ # attribute(:service_name, kind_of: String, default: lazy { PoiseService::Utils.parse_service_name(path) })
39
+ def parse_service_name(path)
40
+ parts = Pathname.new(path).each_filename.to_a.reverse!
41
+ # Find the last segment not in common segments, fall back to the last segment.
42
+ parts.find {|seg| !COMMON_SEGMENTS[seg] } || parts.first
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ module PoiseService
18
+ VERSION = '1.0.0'
19
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ lib = File.expand_path('../lib', __FILE__)
18
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
19
+ require 'poise_service/version'
20
+
21
+ Gem::Specification.new do |spec|
22
+ spec.name = 'poise-service'
23
+ spec.version = PoiseService::VERSION
24
+ spec.authors = ['Noah Kantrowitz']
25
+ spec.email = %w{noah@coderanger.net}
26
+ spec.description = "A Chef cookbook for managing system services."
27
+ spec.summary = spec.description
28
+ spec.homepage = 'https://github.com/poise/poise-service'
29
+ spec.license = 'Apache 2.0'
30
+
31
+ spec.files = `git ls-files`.split($/)
32
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
33
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
34
+ spec.require_paths = %w{lib}
35
+
36
+ spec.add_dependency 'halite', '~> 1.0'
37
+ spec.add_dependency 'poise', '~> 2.0'
38
+
39
+ spec.add_development_dependency 'kitchen-rackspace', '~> 0.14'
40
+ spec.add_development_dependency 'poise-boiler', '~> 1.0'
41
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ name 'poise-service_test'
18
+ depends 'poise-service'
@@ -0,0 +1,43 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_service/service_mixin'
18
+
19
+ include PoiseService::ServiceMixin
20
+
21
+ def action_enable
22
+ notifying_block do
23
+ file "/usr/bin/poise_mixin_#{new_resource.service_name}" do
24
+ owner 'root'
25
+ group 'root'
26
+ mode '755'
27
+ content <<-EOH
28
+ #!/opt/chef/embedded/bin/ruby
29
+ require 'webrick'
30
+ server = WEBrick::HTTPServer.new(Port: #{new_resource.port})
31
+ server.mount_proc '/' do |req, res|
32
+ res.body = #{new_resource.message.inspect}
33
+ end
34
+ server.start
35
+ EOH
36
+ end
37
+ end
38
+ super
39
+ end
40
+
41
+ def service_options(resource)
42
+ resource.command("/usr/bin/poise_mixin_#{new_resource.service_name}")
43
+ end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # Create the various services.
18
+ poise_service_test 'default' do
19
+ base_port 5000
20
+ end
21
+
22
+ if node['platform_family'] == 'rhel' && node['platform_version'].start_with?('7')
23
+ file '/no_sysvinit'
24
+ file '/no_upstart'
25
+
26
+ poise_service_test 'systemd' do
27
+ service_provider :systemd
28
+ base_port 8000
29
+ end
30
+ else
31
+ file '/no_systemd'
32
+
33
+ poise_service_test 'sysvinit' do
34
+ service_provider :sysvinit
35
+ base_port 6000
36
+ end
37
+
38
+ poise_service_test 'upstart' do
39
+ service_provider :upstart
40
+ base_port 7000
41
+ end
42
+ end
43
+
44
+ poise_service_test 'dummy' do
45
+ service_provider :dummy
46
+ base_port 9000
47
+ end
@@ -0,0 +1,34 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ # Integration tests for the mixin.
18
+ poise_service_test_mixin 'default' do
19
+ service_name 'poise_mixin_default'
20
+ message 'Hello world!'
21
+ port 4000
22
+ end
23
+
24
+ poise_service_test_mixin 'update' do
25
+ service_name 'poise_mixin_update'
26
+ message 'first'
27
+ port 4001
28
+ end
29
+
30
+ poise_service_test_mixin 'update again' do
31
+ service_name 'poise_mixin_update'
32
+ message 'second'
33
+ port 4001
34
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ require 'poise_service/service_mixin'
18
+
19
+ include PoiseService::ServiceMixin
20
+
21
+ attribute :message
22
+ attribute :port
23
+
24
+ def after_created
25
+ notifies(:restart, self)
26
+ end
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ eval_gemfile File.expand_path('../../../Gemfile', __FILE__)
18
+
19
+ gem 'chef', '~> 12.0'
@@ -0,0 +1,22 @@
1
+ #
2
+ # Copyright 2015, Noah Kantrowitz
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+
17
+ eval_gemfile File.expand_path('../../../Gemfile', __FILE__)
18
+
19
+ gem 'chef', github: 'chef/chef'
20
+ gem 'halite', github: 'poise/halite'
21
+ gem 'poise', github: 'poise/poise'
22
+ gem 'poise-boiler', github: 'poise/poise-boiler'