Skip to content

Commit 9a7f233

Browse files
committed
Add wrapper resources for python_execute and python_package.
1 parent b0da493 commit 9a7f233

6 files changed

Lines changed: 234 additions & 0 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,34 @@ end
258258

259259
All actions and properties are the same as the [`python_runtime` resource](https://github.com/poise/poise-python#python_runtime).
260260

261+
### `application_python_execute`
262+
263+
The `application_python_execute` resource runs Python commands for the deployment.
264+
265+
```ruby
266+
application '/srv/myapp' do
267+
python_execute 'setup.py install'
268+
end
269+
```
270+
271+
All actions and properties are the same as the [`python_execute` resource](https://github.com/poise/poise-python#python_execute),
272+
except that the `cwd`, `environment`, `group`, and `user` properties default to
273+
the application-level data if not specified.
274+
275+
### `application_python_package`
276+
277+
The `application_python_package` resource installs Python packages for the deployment.
278+
279+
```ruby
280+
application '/srv/myapp' do
281+
python_package 'requests'
282+
end
283+
```
284+
285+
All actions and properties are the same as the [`python_package` resource](https://github.com/poise/poise-python#python_package),
286+
except that the `group` and `user` properties default to the application-level
287+
data if not specified.
288+
261289
### `application_virtualenv`
262290

263291
The `application_virtualenv` resource creates a Python virtualenv for the

lib/poise_application_python/resources.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
require 'poise_application_python/resources/gunicorn'
2222
require 'poise_application_python/resources/pip_requirements'
2323
require 'poise_application_python/resources/python'
24+
require 'poise_application_python/resources/python_execute'
25+
require 'poise_application_python/resources/python_package'
2426
require 'poise_application_python/resources/virtualenv'
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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_python/resources/python_execute'
18+
19+
require 'poise_application_python/app_mixin'
20+
21+
22+
module PoiseApplicationPython
23+
module Resources
24+
# (see PythonExecute::Resource)
25+
# @since 4.0.0
26+
module PythonExecute
27+
# An `application_python_execute` resource to run Python commands inside an
28+
# Application cookbook deployment.
29+
#
30+
# @provides application_python_execute
31+
# @provides application_python_python_execute
32+
# @action run
33+
# @example
34+
# application '/srv/myapp' do
35+
# python_execute 'setup.py install'
36+
# end
37+
class Resource < PoisePython::Resources::PythonExecute::Resource
38+
include PoiseApplicationPython::AppMixin
39+
provides(:application_python_execute)
40+
provides(:application_python_python_execute)
41+
42+
def initialize(*args)
43+
super
44+
# Clear some instance variables so my defaults work.
45+
remove_instance_variable(:@cwd)
46+
remove_instance_variable(:@group)
47+
remove_instance_variable(:@user)
48+
end
49+
50+
# #!attribute cwd
51+
# Override the default directory to be the app path if unspecified.
52+
# @return [String]
53+
attribute(:cwd, kind_of: [String, NilClass, FalseClass], default: lazy { parent && parent.path })
54+
55+
# #!attribute group
56+
# Override the default group to be the app group if unspecified.
57+
# @return [String, Integer]
58+
attribute(:group, kind_of: [String, Integer, NilClass, FalseClass], default: lazy { parent && parent.group })
59+
60+
# #!attribute user
61+
# Override the default user to be the app owner if unspecified.
62+
# @return [String, Integer]
63+
attribute(:user, kind_of: [String, Integer, NilClass, FalseClass], default: lazy { parent && parent.owner })
64+
end
65+
66+
# The default provider for `application_python_execute`.
67+
#
68+
# @see Resource
69+
# @provides application_python_execute
70+
# @provides application_python_python_execute
71+
class Provider < PoisePython::Resources::PythonExecute::Provider
72+
provides(:application_python_execute)
73+
provides(:application_python_python_execute)
74+
75+
private
76+
77+
# Override environment to add the application envivonrment instead.
78+
#
79+
# @return [Hash]
80+
def environment
81+
super.tap do |environment|
82+
# Don't use the app_state_environment_python because we already have
83+
# those values in place.
84+
environment.update(new_resource.app_state_environment)
85+
# Re-apply the resource environment for correct ordering.
86+
environment.update(new_resource.environment) if new_resource.environment
87+
end
88+
end
89+
end
90+
91+
end
92+
end
93+
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_python/resources/python_package'
18+
19+
require 'poise_application_python/app_mixin'
20+
21+
22+
module PoiseApplicationPython
23+
module Resources
24+
# (see PythonPackage::Resource)
25+
# @since 4.0.0
26+
module PythonPackage
27+
# An `application_python_package` resource to install Python
28+
# packages inside an Application cookbook deployment.
29+
#
30+
# @provides application_python_package
31+
# @provides application_python_python_package
32+
# @action install
33+
# @action upgrade
34+
# @action remove
35+
# @example
36+
# application '/srv/myapp' do
37+
# python_package 'requests'
38+
# end
39+
class Resource < PoisePython::Resources::PythonPackage::Resource
40+
include PoiseApplicationPython::AppMixin
41+
provides(:application_python_package)
42+
provides(:application_python_python_package)
43+
subclass_providers!
44+
45+
def initialize(*args)
46+
super
47+
# For older Chef.
48+
@resource_name = :application_python_package
49+
end
50+
51+
# #!attribute group
52+
# Override the default group to be the app group if unspecified.
53+
# @return [String, Integer]
54+
attribute(:group, kind_of: [String, Integer, NilClass], default: lazy { parent && parent.group })
55+
56+
# #!attribute user
57+
# Override the default user to be the app owner if unspecified.
58+
# @return [String, Integer]
59+
attribute(:user, kind_of: [String, Integer, NilClass], default: lazy { parent && parent.owner })
60+
end
61+
62+
end
63+
end
64+
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 'spec_helper'
18+
19+
describe PoiseApplicationPython::Resources::PythonExecute do
20+
step_into(:application_python_execute)
21+
recipe do
22+
application '/srv/myapp' do
23+
owner 'myuser'
24+
group 'mygroup'
25+
environment ENVKEY: 'ENVVALUE'
26+
27+
python('') { provider :dummy }
28+
python_execute 'myapp.py'
29+
end
30+
end
31+
32+
it do
33+
expect_any_instance_of(described_class::Provider).to receive(:shell_out!).with(
34+
'/python myapp.py',
35+
user: 'myuser',
36+
group: 'mygroup',
37+
cwd: '/srv/myapp',
38+
timeout: 3600,
39+
returns: 0,
40+
environment: {'ENVKEY' => 'ENVVALUE'},
41+
log_level: :info,
42+
log_tag: 'application_python_execute[myapp.py]',
43+
)
44+
is_expected.to run_application_python_execute('myapp.py').with(user: 'myuser', group: 'mygroup', cwd: '/srv/myapp')
45+
end
46+
end

test/spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616

1717
require 'poise_boiler/spec_helper'
1818
require 'poise_application_python'
19+
require 'poise_application/cheftie'

0 commit comments

Comments
 (0)