Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Download Source and Binary Files to Chef Cache #67

Closed
wants to merge 2 commits into from

2 participants

@markolson

The application I'm working on has an end-goal of being able to be installed via chef-solo off of a USB key. To that end, we have to make sure that all the packages, tar files, etc.. that our cookbooks download are placed into Chef's Cache (normally /var/chef/cache). This changes the source and binary recipes to do that, as well as pulling out a raise_if_checksum_mismatch method to a helper library so that source and binary can both use it.

I also changed around some of the logic around install_not_needed, changing it to use Mixlib:: ShellOut (foodcritic was complaining about that) and using a notifies block to run the SHA256 code only when necessarily. There's still more opportunity to pull out the checksumming and 'already installed' functionality, but it's Good Enough(tm) right now.

markolson added some commits
@markolson markolson Download source and binary files into Chef's cache c42f065
@markolson markolson Pass foodcritic tests
* Stop using ` to exec system code, using Mixlib::ShellOut instead.
* Raise exceptions if the 'source' download SHA256 doesn't match expected value.
2f9f814
@mdxp
Owner

The development of the nodejs cookbook has been move here: https://github.com/redguide/nodejs
Your PR functionality might have been already included in the new repo. If not, please resubmit it there. Thanks!

@mdxp mdxp closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 24, 2013
  1. @markolson
  2. @markolson

    Pass foodcritic tests

    markolson authored
    * Stop using ` to exec system code, using Mixlib::ShellOut instead.
    * Raise exceptions if the 'source' download SHA256 doesn't match expected value.
This page is out of date. Refresh to see the latest.
View
6 libraries/raise_if_checksum_mismatch.rb
@@ -0,0 +1,6 @@
+def raise_if_checksum_mismatch(path, expected_checksum)
+ calculated_sha256_hash = Digest::SHA256.file(path)
+ if calculated_sha256_hash != expected_checksum
+ raise "SHA256 Hash of #{path} did not match! Expected #{expected_checksum} found #{calculated_sha256_hash}"
+ end
+end
View
35 recipes/install_from_binary.rb
@@ -40,41 +40,40 @@
# Let the user override the source url in the attributes
nodejs_bin_url = "#{node['nodejs']['src_url']}/#{nodejs_tar_path}"
+# Verify the SHA sum of the downloaded file:
+ruby_block "verify_sha_sum" do
+ block do
+ raise_if_checksum_mismatch("#{Chef::Config[:file_cache_path]}/#{nodejs_tar}", expected_checksum)
+ end
+ action :nothing
+end
+already_installed = false
+if File.exists?("#{node['nodejs']['dir']}/bin/node")
+ node_version = Mixlib::ShellOut.new("#{node['nodejs']['dir']}/bin/node --version").run_command
+ already_installed = node_version.stdout.chomp == "v#{node['nodejs']['version']}"
+end
+
# Download it:
-remote_file "/usr/local/src/#{nodejs_tar}" do
+remote_file "#{Chef::Config[:file_cache_path]}/#{nodejs_tar}" do
source nodejs_bin_url
checksum expected_checksum
mode 0644
action :create_if_missing
+ notifies :run, "ruby_block[verify_sha_sum]", :immediately
end
# Where we will install the binaries and libs to (normally /usr/local):
destination_dir = node['nodejs']['dir']
-install_not_needed = File.exists?("#{node['nodejs']['dir']}/bin/node") && `#{node['nodejs']['dir']}/bin/node --version`.chomp == "v#{node['nodejs']['version']}"
-
-# Verify the SHA sum of the downloaded file:
-ruby_block "verify_sha_sum" do
- block do
- require 'digest/sha1'
- calculated_sha256_hash = Digest::SHA256.file("/usr/local/src/#{nodejs_tar}")
- if calculated_sha256_hash != expected_checksum
- raise "SHA256 Hash of #{nodejs_tar} did not match! Expected #{expected_checksum} found #{calculated_sha256_hash}"
- end
- end
- not_if { !node['nodejs']['check_sha'] or install_not_needed }
-end
-
# One hopes that we can trust the contents of the node tarball not to overwrite anything it shouldn't!
execute "install package to system" do
command <<-EOF
- tar xf /usr/local/src/#{nodejs_tar} \
+ tar xf #{Chef::Config[:file_cache_path]}/#{nodejs_tar} \
--strip-components=1 --no-same-owner \
-C #{destination_dir} \
#{package_stub}/bin \
#{package_stub}/lib \
#{package_stub}/share
EOF
-
- not_if { install_not_needed }
+ not_if { already_installed }
end
View
23 recipes/install_from_source.rb
@@ -29,28 +29,41 @@
nodejs_tar = "node-v#{node['nodejs']['version']}.tar.gz"
nodejs_tar_path = nodejs_tar
+
if node['nodejs']['version'].split('.')[1].to_i >= 5
nodejs_tar_path = "v#{node['nodejs']['version']}/#{nodejs_tar_path}"
end
# Let the user override the source url in the attributes
nodejs_src_url = "#{node['nodejs']['src_url']}/#{nodejs_tar_path}"
-remote_file "/usr/local/src/#{nodejs_tar}" do
+ruby_block "verify_sha_sum" do
+ block do
+ raise_if_checksum_mismatch("#{Chef::Config[:file_cache_path]}/#{nodejs_tar}", node['nodejs']['checksum'])
+ end
+ action :nothing
+end
+already_installed = false
+if File.exists?("#{node['nodejs']['dir']}/bin/node")
+ node_version = Mixlib::ShellOut.new("#{node['nodejs']['dir']}/bin/node --version").run_command
+ already_installed = node_version.stdout.chomp == "v#{node['nodejs']['version']}"
+end
+
+remote_file "#{Chef::Config[:file_cache_path]}/#{nodejs_tar}" do
source nodejs_src_url
checksum node['nodejs']['checksum']
mode 0644
action :create_if_missing
+ notifies :run, "ruby_block[verify_sha_sum]", :immediately
end
# --no-same-owner required overcome "Cannot change ownership" bug
# on NFS-mounted filesystem
-execute "tar --no-same-owner -zxf #{nodejs_tar}" do
- cwd "/usr/local/src"
+execute "tar --no-same-owner -zxf #{nodejs_tar} -C /usr/local/src/" do
+ cwd Chef::Config[:file_cache_path]
creates "/usr/local/src/node-v#{node['nodejs']['version']}"
end
bash "compile node.js (on #{node['nodejs']['make_threads']} cpu)" do
- # OSX doesn't have the attribute so arbitrarily default 2
cwd "/usr/local/src/node-v#{node['nodejs']['version']}"
code <<-EOH
PATH="/usr/local/bin:$PATH"
@@ -64,5 +77,5 @@
environment({"PATH" => "/usr/local/bin:/usr/bin:/bin:$PATH"})
command "make install"
cwd "/usr/local/src/node-v#{node['nodejs']['version']}"
- not_if {::File.exists?("#{node['nodejs']['dir']}/bin/node") && `#{node['nodejs']['dir']}/bin/node --version`.chomp == "v#{node['nodejs']['version']}" }
+ not_if { already_installed }
end
Something went wrong with that request. Please try again.