Skip to content

Instantly share code, notes, and snippets.

@jmealo
Last active December 21, 2015 01:59
Show Gist options
  • Save jmealo/6231713 to your computer and use it in GitHub Desktop.
Save jmealo/6231713 to your computer and use it in GitHub Desktop.
Script to prepare environment in SmartOS Zone or Ubuntu VM before installing software
#!/bin/bash
# To run:
# source <(curl -s https://gist.github.com/jmealo/6231713/raw)
# note: running it this way renders the network configuration tests superfluous
if [[ "$(uname)" = "SunOS" ]] ; then
OS="SMARTOS"
elif [[ -f /etc/debian_version ]]; then
OS="DEBIAN"
fi
function install_pkg {
if [[ $OS == 'DEBIAN' ]]; then
apt-get -y install $1
elif [[ $OS == "SMARTOS" ]]; then
pkgin -y in $1
fi
#if the package isn't in the repository this won't catch the error
rc=$?
if [[ $rc != 0 ]]; then
warn 'Failed to install $1'
exit $rc
fi
}
function warn {
echo -e "\e[01;31m$1!\e[0m"
}
#make sure user running script is root
if [[ $EUID -ne 0 ]]; then
warn "You must be a root user to run this script, try 'sudo su'" 2>&1
exit 1
fi
echo "Checking network connectivity... this can take a minute."
#check to make sure internet is working
if [[ OS == "SMARTOS" ]]; then
ping 8.8.8.8 64 1 >/dev/null
else
ping -c1 8.8.8.8 >/dev/null
fi
rc=$?
if [[ $rc != 0 ]] ; then
warn 'Unable to connect to internet... check network settings:\n'
ifconfig -a
exit $rc
fi
#check to make sure DNS is working
wget -q --tries=10 --timeout=5 http://www.google.com -O /tmp/index.google &> /dev/null
if [ ! -s /tmp/index.google ];then
warn "DNS failed to resolve google.com"
exit 1
fi
echo -e '\nDo you need to compile software? [y/n]:'
read COMPILE
if [[ "$(head -1 ~/.bashrc)" != 'export HISTFILESIZE=3000' ]]; then
echo "Increasing BASH history file size..."
#increase command line history to 3000
echo "$(echo 'export HISTFILESIZE=3000' | cat - ~/.bashrc)" > ~/.bashrc
fi
#reload settings
source ~/.bashrc
#prep for install (update packages, install nano)
if [[ $OS = "SMARTOS" ]] ; then
echo "Updating packages..."
pkgin -y update
echo "Upgrading packages..."
pkgin -y upgrade
if [[ $COMPILE == 'y' ]]; then
warn 'Do not build software in a zone other than the build node!'
fi
elif [ $OS == 'DEBIAN' ]; then
apt-get -y update
#update list of packages
apt-get -y upgrade
#upgrade packages
if [[ $COMPILE == 'y' ]]; then
install_pkg 'build-essential automake checkinstall'
fi
#make sure timezone is configured
dpkg-reconfigure tzdata
fi
echo "Installing nano..."
install_pkg 'nano'
echo "Installing unzip..."
install_pkg 'unzip'
echo "Enabling syntax highlighting in nano"
/bin/ls /opt/local/share/nano/*.nanorc | xargs -I {} echo 'include "{}"' >> ~/.nanorc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment