diff --git a/attributes/install.rb b/attributes/install.rb new file mode 100644 index 00000000..8fc4492e --- /dev/null +++ b/attributes/install.rb @@ -0,0 +1,2 @@ +default['chef-client']['version'] = :latest +default['chef-client']['install_method'] = "omnibus" diff --git a/attributes/install_omnibus.rb b/attributes/install_omnibus.rb new file mode 100644 index 00000000..2d26a41c --- /dev/null +++ b/attributes/install_omnibus.rb @@ -0,0 +1,7 @@ +include_attribute "chef-client::install" + +default['chef-client']['omnibus']['version'] = node['chef-client']['version'] +default['chef-client']['omnibus']['prereleases'] = false +default['chef-client']['omnibus']['nightlies'] = false +default['chef-client']['omnibus']['package_file'] = nil +default['chef-client']['omnibus']['package_checksum'] = nil diff --git a/libraries/omnitruck_client.rb b/libraries/omnitruck_client.rb new file mode 100644 index 00000000..9652a6ab --- /dev/null +++ b/libraries/omnitruck_client.rb @@ -0,0 +1,62 @@ +# +# Copyright:: Copyright (c) 2012 Opscode, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require 'uri' + +class OmnitruckClientClient + + attr_reader :platform, :platform_version, :machine_architecture + + def initialize(node) + @platform = node['platform_family'] == "rhel" ? "el" : node['platform'] + @platform_version = node['platform_family'] == "rhel" ? node['platform_version'].to_i : node['platform_version'] + @machine_architecture = node['kernel']['machine'] + end + + def package_for_version(version, prerelease=false, nightly=false) + url = "http://www.opscode.com/chef/download" + url << "?p=#{platform}" + url << "&pv=#{platform_version}" + url << "&m=#{machine_architecture}" + url << "&v=#{version}" if version + url << "&prerelease=#{prerelease}" + url << "&nightlies=#{nightly}" + Chef::Log.info("Omnitruck download-clien request: #{url}") + target = redirect_target(url) + Chef::Log.info("Downloading chef-client package from: #{target}") if target + target + end + + private + + def redirect_target(url) + url = URI.parse(url) + http = Net::HTTP.new(url.host, url.port) + if url.scheme == "https" + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + response = http.get(url.request_uri, {}) + case response + when Net::HTTPRedirection + response['location'] + else + nil + end + end + +end diff --git a/recipes/install.rb b/recipes/install.rb new file mode 100644 index 00000000..08a7db25 --- /dev/null +++ b/recipes/install.rb @@ -0,0 +1,20 @@ +# +# Cookbook Name:: chef-client +# Recipe:: install +# +# Copyright 2010, Opscode, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +include_recipe "chef-client::install_#{node['chef-client']['install_method']}" diff --git a/recipes/install_omnibus.rb b/recipes/install_omnibus.rb new file mode 100644 index 00000000..9a732b0a --- /dev/null +++ b/recipes/install_omnibus.rb @@ -0,0 +1,65 @@ +# +# Cookbook Name:: chef-client +# Recipe:: install_omnibus +# +# Copyright 2010, Opscode, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# Acquire the chef-client Omnibus package +if node['chef-client']['package_file'].nil? || node['chef-client']['package_file'].empty? + omnibus_package = OmnitruckClientClient.new(node).package_for_version(node['chef-client']['version'], + node['chef-client']['prereleases'], + node['chef-client']['nightlies']) + unless omnibus_package + err_msg = "Could not locate chef-client" + err_msg << " pre-release" if node['chef-client']['prereleases'] + err_msg << " nightly" if node['chef-client']['nightlies'] + err_msg << " package matching version '#{node['chef-client']['version']}' for node." + raise err_msg + end +else + omnibus_package = node['chef-client']['package_file'] +end + +package_name = ::File.basename(omnibus_package) +package_local_path = "#{Chef::Config[:file_cache_path]}/#{package_name}" + +# omnibus_package is remote (ie a URI) let's download it +if ::URI.parse(omnibus_package).absolute? + remote_file package_local_path do + source omnibus_package + if node['chef-client']['package_checksum'] + checksum node['chef-client']['package_checksum'] if node['chef-client']['package_checksum'] + action :create + else + action :create_if_missing + end + end +# else we assume it's on the local machine +else + package_local_path = omnibus_package +end + +# install the platform package +package package_name do + source package_local_path + provider case node["platform_family"] + when "debian"; Chef::Provider::Package::Dpkg + when "rhel"; Chef::Provider::Package::Rpm + else + raise RuntimeError("I don't know how to install chef-client packages for platform family '#{node["platform_family"]}'!") + end + action :install +end