poise-application 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,116 @@
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 'chef/resource'
18
+ require 'chef/provider'
19
+ require 'poise/utils'
20
+ require 'poise_service/service_mixin'
21
+ require 'poise_service/utils'
22
+
23
+ require 'poise_application/app_mixin'
24
+ require 'poise_application/utils'
25
+
26
+
27
+ module PoiseApplication
28
+ # Mixin for application services. This is any resource that will be part of
29
+ # an application deployment and involves running a persistent service.
30
+ #
31
+ # @api public
32
+ # @since 5.0.0
33
+ # @example
34
+ # module MyApp
35
+ # class Resource < Chef::Resource
36
+ # include Poise
37
+ # provides(:my_app)
38
+ # include PoiseApplication::ServiceMixin
39
+ # end
40
+ #
41
+ # class Provider < Chef::Provider
42
+ # include Poise
43
+ # provides(:my_app)
44
+ # include PoiseApplication::ServiceMixin
45
+ #
46
+ # def action_enable
47
+ # notifying_block do
48
+ # template '/etc/myapp.conf' do
49
+ # # ...
50
+ # end
51
+ # end
52
+ # super
53
+ # end
54
+ #
55
+ # def service_options(r)
56
+ # super
57
+ # r.command('myapp --serve')
58
+ # end
59
+ # end
60
+ # end
61
+ module ServiceMixin
62
+ include Poise::Utils::ResourceProviderMixin
63
+
64
+ # Mixin for application service resources.
65
+ #
66
+ # @see ServiceMixin
67
+ module Resource
68
+ include PoiseService::ServiceMixin::Resource
69
+ include PoiseApplication::AppMixin::Resource
70
+
71
+ module ClassMethods
72
+ # @api private
73
+ def included(klass)
74
+ super
75
+ klass.extend(ClassMethods)
76
+ klass.class_exec do
77
+ attribute(:path, kind_of: String, name_attribute: true)
78
+ # Redefines from the PoiseService version so we get a better default.
79
+ attribute(:service_name, kind_of: String, default: lazy { PoiseService::Utils.parse_service_name(path) })
80
+ attribute(:user, kind_of: [String, Integer], default: lazy { parent ? parent.owner : 'root' })
81
+ end
82
+ end
83
+ end
84
+
85
+ extend ClassMethods
86
+ end
87
+
88
+ # Mixin for application service providers.
89
+ #
90
+ # @see ServiceMixin
91
+ module Provider
92
+ include PoiseService::ServiceMixin::Provider
93
+ include PoiseApplication::AppMixin::Provider
94
+
95
+ private
96
+
97
+ # Abstract hook to set parameters on {#service_resource} when it is
98
+ # created. This is required to set at least `resource.command`.
99
+ #
100
+ # @api public
101
+ # @param resource [Chef::Resource] Resource instance to set parameters on.
102
+ # @return [void]
103
+ # @example
104
+ # def service_options(resource)
105
+ # super
106
+ # resource.command('myapp --serve')
107
+ # end
108
+ def service_options(resource)
109
+ super
110
+ resource.directory(new_resource.path)
111
+ resource.user(new_resource.user)
112
+ resource.environment.update(new_resource.app_state_environment) if new_resource.parent
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,51 @@
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 'etc'
18
+
19
+
20
+ module PoiseApplication
21
+ # Utility methods for PoiseApplication.
22
+ #
23
+ # @api public
24
+ # @since 5.0.0
25
+ module Utils
26
+ # Methods are also available as module-level methods as well as a mixin.
27
+ extend self
28
+
29
+ # Try to find the primary group name for a given user.
30
+ #
31
+ # @param user [String, Integer] User to check, if given as an integer this
32
+ # is used as a UID, otherwise it is the username.
33
+ # @return [String]
34
+ # @example
35
+ # attribute(:group, kind_of: [String, Integer], default: lazy { PoiseApplication::Utils.primary_group_for(user) })
36
+ def primary_group_for(user)
37
+ # Force a reload in case any users were created earlier in the run.
38
+ Etc.endpwent
39
+ Etc.endgrent
40
+ user = if user.is_a?(Integer)
41
+ Etc.getpwuid(user)
42
+ else
43
+ Etc.getpwnam(user.to_s)
44
+ end
45
+ Etc.getgrgid(user.gid).name
46
+ rescue ArgumentError
47
+ # One of the get* calls exploded. ¯\_(ツ)_/¯
48
+ user.to_s
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,20 @@
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
+
18
+ module PoiseApplication
19
+ VERSION = '5.0.0'
20
+ end
@@ -0,0 +1,42 @@
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_application/version'
20
+
21
+ Gem::Specification.new do |spec|
22
+ spec.name = 'poise-application'
23
+ spec.version = PoiseApplication::VERSION
24
+ spec.authors = ['Noah Kantrowitz']
25
+ spec.email = %w{noah@coderanger.net}
26
+ spec.description = "A Chef cookbook for deploying application code."
27
+ spec.summary = spec.description
28
+ spec.homepage = 'https://github.com/poise/application'
29
+ spec.license = 'Apache 2.0'
30
+ spec.metadata['halite_name'] = 'application'
31
+
32
+ spec.files = `git ls-files`.split($/)
33
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
34
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
35
+ spec.require_paths = %w{lib}
36
+
37
+ spec.add_dependency 'halite', '~> 1.0'
38
+ spec.add_dependency 'poise', '~> 2.4'
39
+ spec.add_dependency 'poise-service', '~> 1.0'
40
+
41
+ spec.add_development_dependency 'poise-boiler', '~> 1.0'
42
+ 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 'application_test'
18
+ depends 'application'
@@ -0,0 +1,25 @@
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
+ include Poise
18
+
19
+ action :run do
20
+ notifying_block do
21
+ file ::File.join(new_resource.parent.path, 'plugin') do
22
+ content new_resource.content
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,61 @@
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
+ application '/home/app' do
18
+ test_plugin do
19
+ content 'test plugin'
20
+ end
21
+ end
22
+
23
+ # Restart behavior test.
24
+ directory '/opt/restarter' do
25
+ owner 'root'
26
+ group 'root'
27
+ mode '755'
28
+ end
29
+ file '/opt/restarter/main.rb' do
30
+ content <<-EOH
31
+ require 'webrick'
32
+ server = WEBrick::HTTPServer.new(Port: 2000)
33
+ trap('INT') { server.shutdown }
34
+ server.mount_proc '/' do |req, res|
35
+ res.body = 'One'
36
+ end
37
+ server.start
38
+ EOH
39
+ end
40
+ poise_service 'restarter' do
41
+ command '/opt/chef/embedded/bin/ruby /opt/restarter/main.rb'
42
+ end
43
+
44
+ application '/opt/restarter' do
45
+ file '/opt/restarter/main.rb2' do
46
+ path '/opt/restarter/main.rb'
47
+ content <<-EOH
48
+ require 'webrick'
49
+ server = WEBrick::HTTPServer.new(Port: 2000)
50
+ trap('INT') { server.shutdown }
51
+ server.mount_proc '/' do |req, res|
52
+ res.body = 'Two'
53
+ end
54
+ server.start
55
+ EOH
56
+ end
57
+ poise_service 'restarter2' do
58
+ service_name 'restarter'
59
+ command '/opt/chef/embedded/bin/ruby /opt/restarter/main.rb'
60
+ end
61
+ end
@@ -0,0 +1,20 @@
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
+ include Poise(parent: :application)
18
+
19
+ default_action :run
20
+ attribute :content
@@ -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.2.0'
@@ -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.3.0'
@@ -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,23 @@
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'
23
+ gem 'poise-service', github: 'poise/poise-service'