Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download Source and Binary Files to Chef Cache #67

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions 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
35 changes: 17 additions & 18 deletions recipes/install_from_binary.rb
Expand Up @@ -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
23 changes: 18 additions & 5 deletions recipes/install_from_source.rb
Expand Up @@ -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"
Expand All @@ -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