diff --git a/.gitignore b/.gitignore index 0b4bcc7..5cfa85c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ vendor/bundle build/* .kitchen/ *.lock +.kitchen.local.yml diff --git a/.kitchen.yml b/.kitchen.yml index 965f121..4c002a2 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -28,4 +28,13 @@ suites: cmake: install_method: source source: - version: 2.8.12.2 + version: 3.2.3 + checksum: "a1ebcaf6d288eb4c966714ea457e3b9677cdfde78820d0f088712d7320850297" + - name: binary + run_list: + - recipe[cmake] + attributes: + cmake: + install_method: binary + binary: + prefix: "/opt/local" diff --git a/README.md b/README.md index 4f1c98b..d74c8af 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,13 @@ This cookbook installs the cmake components if not present, and pulls updates if ## Attributes ```ruby -default["cmake"]["install_method"] = "package" # `package` or `source` +default["cmake"]["install_method"] = "package" # `package`, `source`, or `binary` -default["cmake"]["source"]["version"] = "2.8.12.2" -default["cmake"]["source"]["checksum"] = "8c6574e9afabcb9fc66f463bb1f2f051958d86c85c37fccf067eb1a44a120e5e" +default["cmake"]["source"]["version"] = "3.2.3" +default["cmake"]["source"]["checksum"] = "e7be87a6cfc403785ab9d5846a70be2c27b17fda6a9b7442e9fc7af45c077d63" + +default["cmake"]["binary"]["version"] = "LATEST" +default["cmake"]["binary"]["prefix"] = "/usr/local" ``` diff --git a/attributes/binary.rb b/attributes/binary.rb new file mode 100644 index 0000000..686366b --- /dev/null +++ b/attributes/binary.rb @@ -0,0 +1,27 @@ +# +# Cookbook:: cmake +# Attributes:: binary +# + +# have to defer getting latest version until after git is installed. +# Ideally, git would be run on the host rather than the guest so that +# git didn't have to be installed +default["cmake"]["binary"]["version"] = "LATEST" +default["cmake"]["binary"]["repo"] = "https://github.com/Kitware/CMake.git" +default["cmake"]["binary"]["prefix"] = "/usr/local" + +case node["kernel"]["machine"] +when "amd64", "x86_64" + default["cmake"]["binary"]["architecture"] = "x86_64" +when "i386", "i686" + default["cmake"]["binary"]["architecture"] = "i386" +else + fail "Unsupported processor: '#{node['kernel']['machine']}'" +end + +case node["platform"] +when "debian", "ubuntu", "redhat", "centos", "fedora" + default["cmake"]["binary"]["platform"] = "Linux" +else + fail "Unsupported platform: '#{node['platform']}'" +end diff --git a/attributes/default.rb b/attributes/default.rb index 6088c51..fd22d45 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -3,4 +3,5 @@ # Attributes:: default # -default["cmake"]["install_method"] = "package" # `package` or `source` +# `package`, `source`, or `binary` +default["cmake"]["install_method"] = "package" diff --git a/attributes/source.rb b/attributes/source.rb index 025897a..b408a5a 100644 --- a/attributes/source.rb +++ b/attributes/source.rb @@ -3,5 +3,6 @@ # Attributes:: source # -default["cmake"]["source"]["version"] = "2.8.12.2" -default["cmake"]["source"]["checksum"] = "8c6574e9afabcb9fc66f463bb1f2f051958d86c85c37fccf067eb1a44a120e5e" +# TODO: make this use the "LATEST", like `binary` +default["cmake"]["source"]["version"] = "3.2.3" +default["cmake"]["source"]["checksum"] = "a1ebcaf6d288eb4c966714ea457e3b9677cdfde78820d0f088712d7320850297" diff --git a/metadata.rb b/metadata.rb index 403aafc..c33eec8 100644 --- a/metadata.rb +++ b/metadata.rb @@ -14,3 +14,4 @@ supports "fedora" suggests "build-essential" +suggests "git" diff --git a/recipes/_binary.rb b/recipes/_binary.rb new file mode 100644 index 0000000..3980fc1 --- /dev/null +++ b/recipes/_binary.rb @@ -0,0 +1,51 @@ +# +# Cookbook:: cmake +# Recipe:: _binary +# + +cache_dir = Chef::Config[:file_cache_path] +cmake_platform = node["cmake"]["binary"]["platform"] +cmake_arch = node["cmake"]["binary"]["architecture"] +cmake_repo = node["cmake"]["binary"]["repo"] +cmake_prefix = node["cmake"]["binary"]["prefix"] + +package "git" do + action :install + only_if { node["cmake"]["binary"]["version"] == "LATEST" } +end + +ruby_block "get latest" do + block do + if node["cmake"]["binary"]["version"] == "LATEST" + vers = [] + IO.popen("git ls-remote -t #{cmake_repo}").each do |line| + m = line.match(%r{^\S+\s+refs\/tags\/v(\d+.\d+.\d+)$}) + if m + vers.push(Gem::Version.new(m[1])) # sort semver-ly + end + end + vers.sort + node.set["cmake"]["binary"]["version"] = vers[-1].to_s + end + node.set["cmake"]["binary"]["sh"] = "cmake-#{node['cmake']['binary']['version']}-#{cmake_platform}-#{cmake_arch}.sh" # rubocop:disable LineLength + end +end + +remote_file "get sh" do + path lazy { "#{cache_dir}/#{node['cmake']['binary']['sh']}" } + source lazy { "http://www.cmake.org/files/v#{node['cmake']['binary']['version'][/^\d\.\d/, 0]}/#{node['cmake']['binary']['sh']}" } # rubocop:disable LineLength +end + +directory "prefix" do + path lazy { cmake_prefix } + owner 'root' + group 'root' + mode '0755' + action :create +end + +execute "cmake install" do + command lazy { "bash #{cache_dir}/#{node['cmake']['binary']['sh']} --prefix=#{cmake_prefix} --exclude-subdir" } # rubocop:disable LineLength + cwd "/tmp" + creates "#{cmake_prefix}/bin/cmake" +end diff --git a/spec/binary_spec.rb b/spec/binary_spec.rb new file mode 100644 index 0000000..8c4f6f1 --- /dev/null +++ b/spec/binary_spec.rb @@ -0,0 +1,10 @@ +require "spec_helper" + +describe "cmake::_binary" do + let(:chef_run) do + ChefSpec::Runner.new do |node| + node.set["cmake"]["install_method"] = "binary" + end.converge("git", described_recipe) + end + +end