poise-javascript 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.kitchen.travis.yml +9 -0
  4. data/.kitchen.yml +8 -0
  5. data/.travis.yml +21 -0
  6. data/.yardopts +7 -0
  7. data/Berksfile +28 -0
  8. data/CHANGELOG.md +6 -0
  9. data/Gemfile +33 -0
  10. data/LICENSE +201 -0
  11. data/README.md +332 -0
  12. data/Rakefile +17 -0
  13. data/chef/attributes/default.rb +23 -0
  14. data/chef/recipes/default.rb +19 -0
  15. data/lib/poise_javascript.rb +24 -0
  16. data/lib/poise_javascript/cheftie.rb +18 -0
  17. data/lib/poise_javascript/error.rb +23 -0
  18. data/lib/poise_javascript/javascript_command_mixin.rb +56 -0
  19. data/lib/poise_javascript/javascript_providers.rb +40 -0
  20. data/lib/poise_javascript/javascript_providers/base.rb +97 -0
  21. data/lib/poise_javascript/javascript_providers/dummy.rb +77 -0
  22. data/lib/poise_javascript/javascript_providers/iojs.rb +64 -0
  23. data/lib/poise_javascript/javascript_providers/nodejs.rb +64 -0
  24. data/lib/poise_javascript/javascript_providers/scl.rb +79 -0
  25. data/lib/poise_javascript/javascript_providers/system.rb +71 -0
  26. data/lib/poise_javascript/resources.rb +29 -0
  27. data/lib/poise_javascript/resources/javascript_execute.rb +83 -0
  28. data/lib/poise_javascript/resources/javascript_runtime.rb +85 -0
  29. data/lib/poise_javascript/resources/javascript_runtime_test.rb +226 -0
  30. data/lib/poise_javascript/resources/node_package.rb +254 -0
  31. data/lib/poise_javascript/resources/npm_install.rb +94 -0
  32. data/lib/poise_javascript/version.rb +20 -0
  33. data/poise-javascript.gemspec +41 -0
  34. data/test/cookbooks/poise-javascript_test/metadata.rb +18 -0
  35. data/test/cookbooks/poise-javascript_test/recipes/default.rb +49 -0
  36. data/test/gemfiles/chef-12.gemfile +19 -0
  37. data/test/gemfiles/master.gemfile +23 -0
  38. data/test/integration/default/serverspec/default_spec.rb +107 -0
  39. data/test/spec/javascript_command_mixin_spec.rb +59 -0
  40. data/test/spec/javascript_providers/base_spec.rb +89 -0
  41. data/test/spec/javascript_providers/dummy_spec.rb +62 -0
  42. data/test/spec/javascript_providers/iojs_spec.rb +77 -0
  43. data/test/spec/javascript_providers/nodejs_spec.rb +82 -0
  44. data/test/spec/javascript_providers/scl_spec.rb +68 -0
  45. data/test/spec/javascript_providers/system_spec.rb +73 -0
  46. data/test/spec/resources/javascript_execute_spec.rb +67 -0
  47. data/test/spec/resources/node_package_spec.rb +324 -0
  48. data/test/spec/resources/npm_install_spec.rb +118 -0
  49. data/test/spec/spec_helper.rb +19 -0
  50. metadata +169 -0
@@ -0,0 +1,254 @@
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/json_compat'
18
+ require 'chef/provider/package'
19
+ require 'chef/resource/package'
20
+ require 'poise'
21
+
22
+ require 'poise_javascript/error'
23
+ require 'poise_javascript/javascript_command_mixin'
24
+
25
+
26
+ module PoiseJavascript
27
+ module Resources
28
+ # (see NodePackage::Resource)
29
+ # @since 1.0.0
30
+ module NodePackage
31
+ # A `node_package` resource to manage Node.js packages using npm.
32
+ #
33
+ # @provides node_package
34
+ # @action install
35
+ # @action upgrade
36
+ # @action uninstall
37
+ # @example
38
+ # node_package 'express' do
39
+ # javascript '0.10'
40
+ # version '1.8.3'
41
+ # end
42
+ class Resource < Chef::Resource::Package
43
+ include PoiseJavascript::JavascriptCommandMixin
44
+ provides(:node_package)
45
+ # Manually create matchers because #actions is unreliable.
46
+ %i{install upgrade remove}.each do |action|
47
+ Poise::Helpers::ChefspecMatchers.create_matcher(:node_package, action)
48
+ end
49
+
50
+ # @!attribute group
51
+ # System group to install the package.
52
+ # @return [String, Integer, nil]
53
+ attribute(:group, kind_of: [String, Integer, NilClass])
54
+ # @!attribute path
55
+ # Path to install the package in to. If unset install using --global.
56
+ # @return [String, nil, false]
57
+ attribute(:path, kind_of: [String, NilClass, FalseClass])
58
+ # @!attribute unsafe_perm
59
+ # Enable --unsafe-perm.
60
+ # @return [Boolean, nil]
61
+ attribute(:unsafe_perm, equal_to: [true, false, nil], default: true)
62
+ # @!attribute user
63
+ # System user to install the package.
64
+ # @return [String, Integer, nil]
65
+ attribute(:user, kind_of: [String, Integer, NilClass])
66
+
67
+ def initialize(*args)
68
+ super
69
+ # For older Chef.
70
+ @resource_name = :node_package
71
+ # We don't have these actions.
72
+ @allowed_actions.delete(:purge)
73
+ @allowed_actions.delete(:reconfig)
74
+ end
75
+
76
+ # Upstream attribute we don't support. Sets are an error and gets always
77
+ # return nil.
78
+ #
79
+ # @api private
80
+ # @param arg [Object] Ignored
81
+ # @return [nil]
82
+ def response_file(arg=nil)
83
+ raise NoMethodError if arg
84
+ end
85
+
86
+ # (see #response_file)
87
+ def response_file_variables(arg=nil)
88
+ raise NoMethodError if arg
89
+ end
90
+ end
91
+
92
+ # The default provider for the `node_package` resource.
93
+ #
94
+ # @see Resource
95
+ class Provider < Chef::Provider::Package
96
+ include PoiseJavascript::JavascriptCommandMixin
97
+ provides(:node_package)
98
+
99
+ # Load current and candidate versions for all needed packages.
100
+ #
101
+ # @api private
102
+ # @return [Chef::Resource]
103
+ def load_current_resource
104
+ @current_resource = new_resource.class.new(new_resource.name, run_context)
105
+ current_resource.package_name(new_resource.package_name)
106
+ check_package_versions(current_resource)
107
+ current_resource
108
+ end
109
+
110
+ # Populate current and candidate versions for all needed packages.
111
+ #
112
+ # @api private
113
+ # @param resource [PoiseJavascript::Resources::NodePackage::Resource]
114
+ # Resource to load for.
115
+ # @return [void]
116
+ def check_package_versions(resource)
117
+ version_data = Hash.new {|hash, key| hash[key] = {current: nil, candidate: nil} }
118
+ # Get the version for everything currently installed.
119
+ list_args = npm_version?('>= 1.4.16') ? %w{--depth 0} : []
120
+ npm_shell_out!('list', list_args).fetch('dependencies', {}).each do |pkg_name, pkg_data|
121
+ version_data[pkg_name][:current] = pkg_data['version']
122
+ end
123
+ # If any requested packages are currently installed, run npm outdated
124
+ # to look for candidate versions. Older npm doesn't support --json
125
+ # here so you get slow behavior, sorry.
126
+ requested_packages = Set.new(Array(resource.package_name))
127
+ if npm_version?('>= 1.3.16') && version_data.any? {|pkg_name, _pkg_vers| requested_packages.include?(pkg_name) }
128
+ outdated = npm_shell_out!('outdated') || {}
129
+ version_data.each do |pkg_name, pkg_vers|
130
+ pkg_vers[:candidate] = if outdated.include?(pkg_name)
131
+ outdated[pkg_name]['wanted']
132
+ else
133
+ # If it was already installed and not listed in outdated, it
134
+ # must have been up to date already.
135
+ pkg_vers[:current]
136
+ end
137
+ end
138
+ end
139
+ # Check for candidates for anything else we didn't get from outdated.
140
+ requested_packages.each do |pkg_name|
141
+ version_data[pkg_name][:candidate] ||= npm_shell_out!('show', [pkg_name])['version']
142
+ end
143
+ # Populate the current resource and candidate versions. Youch this is
144
+ # a gross mix of data flow.
145
+ if(resource.package_name.is_a?(Array))
146
+ @candidate_version = []
147
+ versions = []
148
+ [resource.package_name].flatten.each do |name|
149
+ ver = version_data[name.downcase]
150
+ versions << ver[:current]
151
+ @candidate_version << ver[:candidate]
152
+ end
153
+ resource.version(versions)
154
+ else
155
+ ver = version_data[resource.package_name.downcase]
156
+ resource.version(ver[:current])
157
+ @candidate_version = ver[:candidate]
158
+ end
159
+ end
160
+
161
+ # Install package(s) using npm.
162
+ #
163
+ # @param name [String, Array<String>] Name(s) of package(s).
164
+ # @param version [String, Array<String>] Version(s) of package(s).
165
+ # @return [void]
166
+ def install_package(name, version)
167
+ args = []
168
+ # Set --unsafe-perm unless the property is nil.
169
+ unless new_resource.unsafe_perm.nil?
170
+ args << '--unsafe-perm'
171
+ args << new_resource.unsafe_perm.to_s
172
+ end
173
+ # Build up the actual package install args.
174
+ if new_resource.source
175
+ args << new_resource.source
176
+ else
177
+ Array(name).zip(Array(version)) do |pkg_name, pkg_ver|
178
+ args << "#{pkg_name}@#{pkg_ver}"
179
+ end
180
+ end
181
+ npm_shell_out!('install', args, parse_json: false)
182
+ end
183
+
184
+ # Upgrade and install are the same for NPM.
185
+ alias_method :upgrade_package, :install_package
186
+
187
+ # Uninstall package(s) using npm.
188
+ #
189
+ # @param name [String, Array<String>] Name(s) of package(s).
190
+ # @param version [String, Array<String>] Version(s) of package(s).
191
+ # @return [void]
192
+ def remove_package(name, version)
193
+ npm_shell_out!('uninstall', [name].flatten, parse_json: false)
194
+ end
195
+
196
+ private
197
+
198
+ # Run an npm command.
199
+ #
200
+ # @param subcmd [String] Subcommand to run.
201
+ # @param args [Array<String>] Command arguments.
202
+ # @param parse_json [Boolean] Parse the JSON on stdout.
203
+ # @return [Hash]
204
+ def npm_shell_out!(subcmd, args=[], parse_json: true)
205
+ cmd = [new_resource.npm_binary, subcmd, '--json']
206
+ # If path is nil, we are in global mode.
207
+ cmd << '--global' unless new_resource.path
208
+ # Add the rest.
209
+ cmd.concat(args)
210
+ # If we are in global mode, cwd will be nil so probably just fine. Add
211
+ # the directory for the node binary to $PATH for post-install stuffs.
212
+ new_path = [::File.dirname(new_resource.javascript), ENV['PATH'].to_s].join(::File::PATH_SEPARATOR)
213
+ out = javascript_shell_out!(cmd, cwd: new_resource.path, group: new_resource.group, user: new_resource.user, environment: {'PATH' => new_path})
214
+ if parse_json
215
+ # Parse the JSON.
216
+ if out.stdout.strip.empty?
217
+ {}
218
+ else
219
+ Chef::JSONCompat.parse(out.stdout)
220
+ end
221
+ else
222
+ out
223
+ end
224
+ end
225
+
226
+ # Find the version of the current npm binary.
227
+ #
228
+ # @return [Gem::Version]
229
+ def npm_version
230
+ @npm_version ||= begin
231
+ out = javascript_shell_out!([new_resource.npm_binary, 'version'])
232
+ # Older NPM doesn't support --json here we get to regex!
233
+ # The line we want looks like:
234
+ # npm: '2.12.1'
235
+ if out.stdout =~ /npm: '([^']+)'/
236
+ Gem::Version.new($1)
237
+ else
238
+ raise PoiseJavascript::Error.new("Unable to parse NPM version from #{out.stdout.inspect}")
239
+ end
240
+ end
241
+ end
242
+
243
+ # Check the NPM version against a requirement.
244
+ #
245
+ # @param req [String] Requirement string in Gem::Requirement format.
246
+ # @return [Boolean]
247
+ def npm_version?(req)
248
+ Gem::Requirement.new(req).satisfied_by?(npm_version)
249
+ end
250
+
251
+ end
252
+ end
253
+ end
254
+ end
@@ -0,0 +1,94 @@
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/provider'
18
+ require 'chef/resource'
19
+ require 'poise'
20
+
21
+ require 'poise_javascript/javascript_command_mixin'
22
+
23
+
24
+ module PoiseJavascript
25
+ module Resources
26
+ # (see NpmInstall::Resource)
27
+ # @since 1.0.0
28
+ module NpmInstall
29
+ # A `npm_install` resource to install NPM packages based on a package.json.
30
+ #
31
+ # @provides npm_install
32
+ # @action install
33
+ # @example
34
+ # npm_install '/opt/myapp'
35
+ class Resource < Chef::Resource
36
+ include PoiseJavascript::JavascriptCommandMixin
37
+ provides(:npm_install)
38
+ actions(:install)
39
+
40
+ # @!attribute path
41
+ # Directory to run `npm install` from.
42
+ # @return [String]
43
+ attribute(:path, kind_of: String, name_attribute: true)
44
+ # @!attribute group
45
+ # System group to install the packages.
46
+ # @return [String, Integer, nil]
47
+ attribute(:group, kind_of: [String, Integer, NilClass])
48
+ # @!attribute production
49
+ # Enable production install mode.
50
+ # @return [Boolean]
51
+ attribute(:production, equal_to: [true, false], default: true)
52
+ # @!attribute unsafe_perm
53
+ # Enable --unsafe-perm.
54
+ # @return [Boolean, nil]
55
+ attribute(:unsafe_perm, equal_to: [true, false, nil], default: true)
56
+ # @!attribute user
57
+ # System user to install the packages.
58
+ # @return [String, Integer, nil]
59
+ attribute(:user, kind_of: [String, Integer, NilClass])
60
+ end
61
+
62
+ # The default provider for `npm_install`.
63
+ #
64
+ # @see Resource
65
+ # @provides npm_install
66
+ class Provider < Chef::Provider
67
+ include Poise
68
+ include PoiseJavascript::JavascriptCommandMixin
69
+ provides(:npm_install)
70
+
71
+ # The `install` action for the `npm_install` resource.
72
+ #
73
+ # @return [void]
74
+ def action_install
75
+ cmd = [new_resource.npm_binary, 'install']
76
+ cmd << '--production' if new_resource.production
77
+ # Set --unsafe-perm unless the property is nil.
78
+ unless new_resource.unsafe_perm.nil?
79
+ cmd << '--unsafe-perm'
80
+ cmd << new_resource.unsafe_perm.to_s
81
+ end
82
+ # Add the directory for the node binary to $PATH for post-install stuffs.
83
+ new_path = [::File.dirname(new_resource.javascript), ENV['PATH'].to_s].join(::File::PATH_SEPARATOR)
84
+ output = javascript_shell_out!(cmd, cwd: new_resource.path, user: new_resource.user, group: new_resource.group, environment: {'PATH' => new_path}).stdout
85
+ unless output.strip.empty?
86
+ # Any output means it did something.
87
+ new_resource.updated_by_last_action(true)
88
+ end
89
+ end
90
+
91
+ end
92
+ end
93
+ end
94
+ 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 PoiseJavascript
19
+ VERSION = '1.0.0'
20
+ 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_javascript/version'
20
+
21
+ Gem::Specification.new do |spec|
22
+ spec.name = 'poise-javascript'
23
+ spec.version = PoiseJavascript::VERSION
24
+ spec.authors = ['Noah Kantrowitz']
25
+ spec.email = %w{noah@coderanger.net}
26
+ spec.description = "A Chef cookbook for managing Node.js and io.js installations."
27
+ spec.summary = spec.description
28
+ spec.homepage = 'https://github.com/poise/poise-javascript'
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
+ spec.add_dependency 'poise-languages', '~> 1.2'
39
+
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-javascript_test'
18
+ depends 'poise-javascript'
@@ -0,0 +1,49 @@
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_javascript/resources/javascript_runtime_test'
18
+
19
+ # Install lsb-release because Debian 6 doesn't by default and serverspec requires it
20
+ package 'lsb-release' if platform?('debian') && node['platform_version'].start_with?('6')
21
+
22
+ javascript_runtime_test 'nodejs'
23
+
24
+ javascript_runtime_test 'nodejs-0.10' do
25
+ test_yo false
26
+ end
27
+
28
+ javascript_runtime_test 'iojs'
29
+
30
+ if platform_family?('rhel')
31
+ javascript_runtime_test 'scl' do
32
+ version ''
33
+ runtime_provider :scl
34
+ test_yo false
35
+ end
36
+ else
37
+ file '/no_scl'
38
+ end
39
+
40
+ # npm in 12.04 seems to be busted.
41
+ if !platform_family?('rhel') && !(platform?('ubuntu') && node['platform_version'] == '12.04')
42
+ javascript_runtime_test 'system' do
43
+ version ''
44
+ runtime_provider :system
45
+ test_yo false
46
+ end
47
+ else
48
+ file '/no_system'
49
+ end