Skip to content

Instantly share code, notes, and snippets.

@cyupa
Last active August 17, 2017 14:06
Show Gist options
  • Save cyupa/076d77f9d0a9396ab82e1023ef8b53e2 to your computer and use it in GitHub Desktop.
Save cyupa/076d77f9d0a9396ab82e1023ef8b53e2 to your computer and use it in GitHub Desktop.
#!/bin/bash
function showMessage() { echo "⏳`tput setaf 12` $1 `tput op`"; }
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
function installCommandLineTools() {
showMessage "Installing Xcode command line tools."
# create the placeholder file that's checked by CLI updates' .dist code
# in Apple's SUS catalog
touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
# find the CLI Tools update
PROD=$(softwareupdate -l | grep "\*.*Command Line" | head -n 1 | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n')
# install it
softwareupdate -i "$PROD" --verbose
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
}
function cleanUp() {
showMessage "Revoking passwordless sudo for '$CI_USERNAME'"
sudo -S bash -c "cp /etc/sudoers.orig /etc/sudoers"
}
function reloadShell() {
source ~/.profile
}
CI_USERNAME=$(whoami)
# Check if the user is root
[ "$CI_USERNAME" = "root" ] && abort "Do not run under root user."
# Check if a password was provided
[[ "$CI_PASSWORD" == "" ]] && abort "Set PASSWORD env variable with the password of the $CI_USERNAME."
# setup passwordless sudo for current user
echo "$CI_PASSWORD" | sudo -S bash -c "cp /etc/sudoers /etc/sudoers.orig; echo '${CI_USERNAME} ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers"
# Install xcode comand line tools
installCommandLineTools
trap cleanUp SIGHUP SIGINT SIGTERM EXIT
showMessage "Enabling Developer Mode"
sudo /usr/sbin/DevToolsSecurity --enable
sudo /usr/sbin/dseditgroup -o edit -t group -a staff _developer
showMessage "Disabling Sleep Mode"
sudo pmset sleep 0
showMessage "Disabling Screensaver"
defaults -currentHost write com.apple.screensaver idleTime -int 0
showMessage "Disabling Gatekeeper"
sudo spctl --master-disable
showMessage "Automatic Restart on System Freeze"
sudo systemsetup -setrestartfreeze on
showMessage "Installing Apple WWDRCA certificate"
curl -O -L http://developer.apple.com/certificationauthority/AppleWWDRCA.cer
security import AppleWWDRCA.cer -k ~/Library/Keychains/login.keychain -P "" -A
showMessage "Injecting environment variables"
echo 'export LC_ALL=en_US.UTF-8' > ~/.profile
echo 'export ANDROID_HOME=/usr/local/opt/android-sdk' >> ~/.profile
echo 'export NDK_HOME=/usr/local/opt/android-ndk' >> ~/.profile
echo 'export GOPATH=/usr/local/opt/go/libexec' >> ~/.profile
echo 'export FINDBUGS_HOME=/usr/local/Cellar/findbugs/3.0.1/libexec' >> ~/.profile
echo 'export SONAR_RUNNER_HOME=/usr/local/Cellar/sonar-runner/2.5/libexec' >> ~/.profile
echo 'export M2_HOME=/usr/local/Cellar/maven30/3.0.5/libexec' >> ~/.profile
echo 'export M2=/usr/local/Cellar/maven30/3.0.5/libexec/bin' >> ~/.profile
echo 'export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:$ANDROID_HOME/bin:$PATH:$GOPATH:$GOPATH/bin' >> ~/.profile
echo 'export JENV_ROOT=/usr/local/var/jenv' >> ~/.profile
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.profile
echo 'if which jenv > /dev/null; then eval "$(jenv init -)"; fi' >> ~/.profile
source ~/.profile
# make Jenkins slave load .profile env variables
[[ -f ~/.bashrc ]] && mv ~/.bashrc ~/.bashrc.old
ln -s ~/.profile ~/.bashrc
# Create Jenkins slabe folder
showMessage "Creating CI folder at '/opt/ci/jenkins'"
sudo mkdir -p "/opt/ci/jenkins"
sudo chown -R "$(whoami)" "/opt/ci"
# Install Homebrew
showMessage "Installing Homebrew and taps"
echo "" | ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Update Ruby version
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | sudo gem update -p
showMessage "Installing rbenv 2.2.3"
brew install rbenv ruby-build
eval "$(rbenv init -)"
rbenv install 2.2.3
rbenv global 2.2.3
reloadShell
# Install Homebrew taps
brew doctor
brew tap homebrew/versions
brew tap caskroom/cask
brew tap caskroom/versions
brew install caskroom/cask/brew-cask
brew update
brew upgrade
reloadShell
showMessage "Installing rbenv Gems"
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | gem update -p
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | gem install bundler \
ocunit2junit nomad-cli cocoapods xcpretty xcode-install slather cloc \
fastlane deliver snapshot frameit pem sigh produce match cert codes spaceship pilot gym jazzy
reloadShell
# Install the latest Xcode versions
export XCODE_INSTALL_USER="$CI_APPLE_USERNAME"
export XCODE_INSTALL_PASSWORD="$CI_APPLE_PASSWORD"
xcversion update
#get the latest xcode version (non beta)
xcode_latest_installed_version=$(xcversion installed | grep -v beta | tail -n 1 | cut -f1)
#get the latest xcode version (non beta)
xcode_version_install=$(xcversion list | grep -v beta | tail -n 1 | cut -d" " -f1)
if [ x"$xcode_version_install" != x"" ]; then
if version_gt $xcode_version_install $xcode_latest_installed_version; then
showMessage "Xcode $xcode_version:"
xcversion install "$xcode_version_install"
sudo xcodebuild -license accept
installCommandLineTools
fi
fi
# Install brew formulas
showMessage "Installing Brew formulas"
brew cask install oclint java java7
brew install jenv
eval "$(jenv init -)"
for java_home in $(/usr/libexec/java_home -V 2>&1 | uniq | grep -v Matching | grep "Java SE" | cut -f3 | sort)
do
( sleep 1 && while [ 1 ]; do sleep 1; echo y; done ) | jenv add "$java_home"
done
jenv global 1.7
brew install \
lcov gcovr ios-sim \
node go xctool swiftlint carthage\
android-sdk android-ndk findbugs sonar-runner maven31 ant gradle
brew install appledoc
APPLEDOCVERSION=$(appledoc --version | cut -d' ' -f3)
mkdir ~/Library/Application\ Support/appledoc
ln -s /usr/local/Cellar/appledoc/${APPLEDOCVERSION}/Templates ~/Library/Application\ Support/appledoc
ln -s /usr/local/Cellar/appledoc/${APPLEDOCVERSION}/Templates ~/.appledoc
sudo easy_install lizard
sudo easy_install jira
sudo easy_install pip
go get github.com/aktau/github-release
showMessage "Installing additional Android SDK components. \
Except x86 and MIPS Emulators, Documentation, Sources, Obsolete packages, Web Driver, Glass and Android TV"
packages="1"
for package in $(android list sdk --all | \
grep -v -e "Obsolete" -e "Sources" -e "x86" -e "Samples" \
-e "Documentation" -e "MIPS" -e "Android TV" \
-e "Glass" -e "XML" -e "URL" -e "Packages available" \
-e "Fetch" -e "Web Driver" -e "GPU Debugging" -e "Android Auto" | \
cut -d'-' -f1)
do
if [ $package != "1" ]; then
packages=$(printf "${packages},${package}")
fi
done
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | android update sdk --all --no-ui --filter "$packages"
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) | android update sdk --all --no-ui --filter platform-tools
# Install SSDH agent
showMessage "Installing custom SSHD agent"
if [ ! -f sshd_rsa_key.pub ]; then
showActionMessage "Generating custom SSHD agent SSH key pair."
showActionMessage "Make sure that you save these generated keys."
ssh-keygen -t rsa -f sshd_rsa_key -P ""
fi
customsshd install sshd_rsa_key.pub
showActionMessage "Installing Websocketd info service"
info-service-helper install
showMessage "Build machine is ready ! 🔧 Now connect a Jenkins agent to this machine with '$CI_USERNAME' at port 50111 and 🔑 sshd_rsa_key using workspace /opt/ci/jenkins 🚀"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment