Skip to content

Instantly share code, notes, and snippets.

@yyuu
Last active February 3, 2016 07:34
Show Gist options
  • Save yyuu/6c6d17d6d7fe3028c9a4 to your computer and use it in GitHub Desktop.
Save yyuu/6c6d17d6d7fe3028c9a4 to your computer and use it in GitHub Desktop.
ChefDK installer
#!/bin/sh
set -e
# default ChefDK version
chefdk_version="0.10.0-1"
while [ $# -gt 0 ]; do
case "$1" in
"-v" )
chefdk_versoin="$2"
shift 1
;;
esac
shift 1
done
tmp_dir="/tmp/chefdk.$$"
rm -fr "${tmp_dir}"
mkdir -p "${tmp_dir}"
unset machine
unset platform_family
unset platform
unset platform_version
machine="$(uname -m 2>/dev/null || true)"
if [ "${machine}" != "x86_64" ]; then
echo "unsupported architecture: ${machine}" 1>&2
exit 1
fi
if [ -f "/etc/lsb-release" ]; then
platform="$(grep DISTRIB_ID /etc/lsb-release | cut -d '=' -f 2 | tr '[A-Z]' '[a-z]')"
platform_version="$(grep DISTRIB_RELEASE /etc/lsb-release | cut -d '=' -f 2 | tr '[A-Z]' '[a-z]')"
fi
if [ -f "/etc/debian_version" ]; then
if [ -z "${platform_family}" ]; then
platform_family="debian"
fi
if [ -z "${platform}" ]; then
platform="debian"
fi
if [ -z "${platform_version}" ]; then
platform_version="$(cat /etc/debian_version | head -1)"
fi
elif [ -f "/etc/redhat-release" ]; then
if [ -z "${platform_family}" ]; then
platform_family="rhel"
fi
if [ -z "${platform}" ]; then
platform="$(head -1 /etc/redhat-release | sed -e 's/^\(.*\) release \([^ ]*\).*$/\1/' | tr '[A-Z]' '[a-z]')"
fi
if [ -z "${platform_version}" ]; then
platform_version="$(head -1 /etc/redhat-release | sed -e 's/^\(.*\) release \([^ ]*\).*$/\2/' | tr '[A-Z]' '[a-z]')"
fi
fi
unset found_curl
unset found_git
if command -v curl 1>/dev/null 2>&1; then
found_curl=1
fi
if command -v git 1>/dev/null 2>&1; then
found_git=1
fi
case "${platform_family}" in
"debian" )
if [ -z "${found_curl}" ] || [ -z "${found_git}" ]; then
apt-get update -q -y
apt-get install -q -y curl git
fi
;;
"rhel" )
if [ -z "${found_curl}" ] || [ -z "${found_git}" ]; then
yum check-update -q -y || true
yum install -q -y curl git
fi
;;
* )
echo "unknown platform_family: ${platform_family}" 1>&2
exit 1
;;
esac
unset chefdk_url
case "${platform_family}" in
"debian" )
case "${platform}" in
"debian" )
case "${platform_version}" in
"6" | "6."* | "7" | "7."* )
chefdk_url="https://opscode-omnibus-packages.s3.amazonaws.com/debian/6/x86_64/chefdk_${chefdk_version}_amd64.deb"
;;
esac
;;
"ubuntu" )
case "${platform_version}" in
"12.04" | "12.04."* | "13.10" | "13.10."* | "14.04" | "14.04."* )
chefdk_url="https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_${chefdk_version}_amd64.deb"
;;
esac
;;
esac
;;
"rhel" )
case "${platform_version}" in
"6" | "6."* )
chefdk_url="https://opscode-omnibus-packages.s3.amazonaws.com/el/6/x86_64/chefdk-${chefdk_version}.el6.x86_64.rpm"
;;
"7" | "7."* )
chefdk_url="https://opscode-omnibus-packages.s3.amazonaws.com/el/7/x86_64/chefdk-${chefdk_version}.el7.x86_64.rpm"
;;
esac
;;
esac
if [ -z "${chefdk_url}" ]; then
echo "chefdk not available: platform_family=${platform_family}, platform=${platform}, platform_version=${platform_version}" 1>&2
exit 1
fi
chefdk="${tmp_dir}/chefdk.${chefdk_url##*.}"
if curl --retry 5 -fsL "${chefdk_url}" > "${chefdk}"; then
: # FIXME: verify checksum
else
echo "fail to download chefdk from ${chefdk_url}" 1>&2
exit 1
fi
case "${platform_family}" in
"debian" )
dpkg -i "${chefdk}"
;;
"rhel" )
rpm -Uvh --oldpackage --replacepkgs "${chefdk}"
;;
* )
echo "unknown platform_family: ${platform_family}" 1>&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment