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

Install chef via apt repo on ubuntu #79

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 78 additions & 50 deletions ChefExtensionHandler/bin/chef-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,6 @@ get_script_dir(){

chef_extension_root=$(get_script_dir)/../

# returns chef-client bebian installer
get_deb_installer(){
pkg_file="$chef_extension_root/installer/chef-client-latest.deb"
echo "${pkg_file}"
}

get_rpm_installer(){
pkg_file="$chef_extension_root/installer/chef-client-latest.rpm"
echo "${pkg_file}"
}

# install_file TYPE FILENAME
# TYPE is "deb"
install_file() {
echo "Installing Chef $version"
case "$1" in
"deb")
echo "[$(date)] Installing with dpkg...$2"
dpkg -i "$2"
;;
"rpm")
echo "[$(date)] Installing with rpm...$2"
rpm -i "$2"
;;
*)
echo "Unknown filetype: $1"
exit 1
;;
esac
if test $? -ne 0; then
echo "[$(date)] Chef Client installation failed"
exit 1
else
echo "[$(date)] Chef Client Package installation succeeded!"
fi
}

# install azure chef extension gem
install_chef_extension_gem(){
echo "[$(date)] Installing Azure Chef Extension gem"
Expand Down Expand Up @@ -88,19 +51,17 @@ curl_check (){
echo "Detected curl..."
else
echo "Installing curl..."
yum install -d0 -e0 -y curl
if [ "$1" == "centos" ]; then
yum install -d0 -e0 -y curl
else
apt-get install -q -y curl
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems indentation is not proper

fi
}

install_from_local_package(){
# install chef
chef_client_installer=$(get_deb_installer)
installer_type="deb"
install_file $installer_type "$chef_client_installer"
}

install_from_repo(){
curl_check
install_from_repo_centos(){
platform="centos"
curl_check $platform
get_hostname
yum_repo_path=/etc/yum.repos.d/chef_stable.repo
yum_repo_config_url="https://packagecloud.io/install/repositories/chef/stable/config_file.repo?os=el&dist=5&name=${host}"
Expand Down Expand Up @@ -145,7 +106,74 @@ install_from_repo(){
yum -y install chef
echo "Package Installed successfully ..."
}


install_from_repo_ubuntu() {
platform="ubuntu"
curl_check $platform

# Need to first run apt-get update so that apt-transport-https can be installed
echo -n "Running apt-get update... "
apt-get update
echo "done."

echo -n "Installing apt-transport-https... "
apt-get install -y apt-transport-https
echo "done."

apt_config_url="https://packagecloud.io/install/repositories/chef/stable/config_file.list?os=ubuntu&dist=trusty&source=script"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NimishaS Could please verify this with ubuntu 12 precise distro ?
Thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddheshwar-more, verified with ubuntu 12, dist=trusty works fine

apt_source_path="/etc/apt/sources.list.d/chef_stable.list"

echo -n "Installing $apt_source_path..."

# create an apt config file for this repository
curl -sSf "${apt_config_url}" > $apt_source_path
curl_exit_code=$?

if [ "$curl_exit_code" = "22" ]; then
echo -n "Unable to download repo config from: "
echo "${apt_config_url}"
echo
echo "Please contact support@packagecloud.io and report this."
[ -e $apt_source_path ] && rm $apt_source_path
exit 1
elif [ "$curl_exit_code" = "35" ]; then
echo "curl is unable to connect to packagecloud.io over TLS when running: "
echo " curl ${apt_config_url}"
echo "This is usually due to one of two things:"
echo
echo " 1.) Missing CA root certificates (make sure the ca-certificates package is installed)"
echo " 2.) An old version of libssl. Try upgrading libssl on your system to a more recent version"
echo
echo "Contact support@packagecloud.io with information about your system for help."
[ -e $apt_source_path ] && rm $apt_source_path
exit 1
elif [ "$curl_exit_code" -gt "0" ]; then
echo
echo "Unable to run: "
echo " curl ${apt_config_url}"
echo
echo "Double check your curl installation and try again."
[ -e $apt_source_path ] && rm $apt_source_path
exit 1
else
echo "done."
fi

echo -n "Importing packagecloud gpg key... "
# import the gpg key
curl https://packagecloud.io/gpg.key | sudo apt-key add -
echo "done."

echo -n "Running apt-get update... "
# update apt on this system
apt-get update
echo "done."

echo "Installing chef-client package"
apt-get install chef

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NimishaS
Just to confirm how about checking command exit status here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NimishaS we can refactor this code since we are using same logic for centos to check curl command status

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@siddheshwar-more, added check_installation_status to check status.
@Vasu1105 , refactored code for checking curl status.

echo "Package Installed successfully ..."
}

get_linux_distributor(){
#### Using python -mplatform command to get distributor name #####
if python -mplatform | grep centos > /dev/null; then
Expand All @@ -169,11 +197,11 @@ else
case $linux_distributor in
"ubuntu")
echo "Linux Distributor: ${linux_distributor}"
install_from_local_package
install_from_repo_ubuntu
;;
"centos")
echo "Linux Distributor: ${linux_distributor}"
install_from_repo
install_from_repo_centos
;;
*)
echo "No Linux Distributor detected ... exiting..."
Expand Down
2 changes: 1 addition & 1 deletion ChefExtensionHandler/bin/chef-uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ uninstall_ubuntu_chef_package(){

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NimishaS The above code is really necessary now ? Since, now we are using apt-get here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed code for status check

if test "$install_status" = "$dpkg_installed" ; then
echo "[$(date)] Uninstalling package $pkg_name ..."
dpkg -P $pkg_name
apt-get purge $pkg_name
check_uninstallation_status
else
echo "No Package found to uninstall!!!"
Expand Down
82 changes: 1 addition & 81 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,62 +60,6 @@ def windows?
end
end

def download_chef(download_url, target)
puts "Downloading from url [#{download_url}]"
uri = URI(download_url)
Net::HTTP.start(uri.host) do |http|
begin
file = open(target, 'wb')
http.request_get(uri.request_uri) do |response|
case response
when Net::HTTPSuccess then
file = open(target, 'wb')
response.read_body do |segment|
file.write(segment)
end
when Net::HTTPRedirection then
location = response['location']
puts "WARNING: Redirected to #{location}"
download_chef(location, target)
else
puts "ERROR: Download failed. Http response code: #{response.code}"
end
end
ensure
file.close if file
end
end
end

def load_build_environment(platform, version)
# Parse the version to form the correct string
major_minor_version = version.split(/[.-]/)

if major_minor_version.length == 4 && major_minor_version[0].to_i >= 1100
version = major_minor_version[1] + '.' + major_minor_version[2] + '.' + (major_minor_version[3].to_i / 1000).to_s
else
version = major_minor_version.join('.')
end

if platform == "windows"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + "&prerelease=false&nightlies=false&p=windows&pv=7&m=x86_64")
elsif platform == "ubuntu"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + '&prerelease=false&nightlies=false&p=ubuntu&pv=14.04&m=x86_64')
elsif platform == "centos"
url = URI.parse('http://opscode.com/chef/metadata?v=' + version + '&prerelease=false&nightlies=false&p=centos&pv=7&m=x86_64')
end
request = Net::HTTP::Get.new(url.to_s)
response = Net::HTTP.start(url.host, url.port) {|http|
http.request(request)
}
if response.kind_of? Net::HTTPOK
download_url = response.body.split(' ')[1]
download_url
else
error_and_exit! "ERROR: Invalid chef-client version"
end
end

def error_and_exit!(message)
puts "\nERROR: #{message}\n"
exit
Expand Down Expand Up @@ -256,17 +200,6 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem

assert_git_state

download_url = load_build_environment(args.target_type,args.extension_version)
unless download_url.nil?
puts <<-CONFIRMATION

**********************************************
Downloading specific chef-client version using
#{download_url}.
Please confirm the correct chef-client version in url.
**********************************************
CONFIRMATION

# Get user confirmation if we are downloading correct version.
if args.confirmation_required == "true"
confirm!("build")
Expand All @@ -277,7 +210,6 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
FileUtils.mkdir_p CHEF_BUILD_DIR
FileUtils.mkdir_p "#{CHEF_BUILD_DIR}/bin"
FileUtils.mkdir_p "#{CHEF_BUILD_DIR}/gems"
FileUtils.mkdir_p "#{CHEF_BUILD_DIR}/installer"

# Copy platform specific files to package dir
puts "Copying #{args.target_type} scripts to package directory..."
Expand All @@ -298,24 +230,12 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
end
end

puts "\nDownloading chef installer..."
target_chef_pkg = case args.target_type
when "ubuntu"
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.deb"
when "centos"
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.rpm"
else
"#{CHEF_BUILD_DIR}/installer/chef-client-latest.msi"
end

date_tag = Date.today.strftime("%Y%m%d")

# Write a release tag file to zip. This will help during testing
# to check if package was synced in PIR.
FileUtils.touch "#{CHEF_BUILD_DIR}/version_#{args.extension_version}_#{date_tag}_#{args.target_type}"

download_chef(download_url, target_chef_pkg)

puts "\nCreating a zip package..."
puts "#{PACKAGE_NAME}_#{args.extension_version}_#{date_tag}_#{args.target_type}.zip\n\n"

Expand All @@ -324,9 +244,9 @@ task :build, [:target_type, :extension_version, :confirmation_required] => [:gem
zipfile.add(file.sub("#{CHEF_BUILD_DIR}/", ''), file)
end
end
end
end


desc "Cleans up the package sandbox"
task :clean do
puts "Cleaning Chef Package..."
Expand Down