#!/usr/bin/env bash ### end common-components/bash-shebang # Welcome to the thoughtbot laptop script! Be prepared to turn your laptop (or # desktop, no haters here) into an awesome development machine. # This file is auto-generated. Do not edit it by hand unless you know what # you're doing. Please see 'CONTRIBUTING.md' for more information. ### end common-components/header trap 'ret=$?; test $ret -ne 0 && printf "failed\n\n" >&2; exit $ret' EXIT set -e ### end common-components/exit-trap if [[ ! -d "$HOME/.bin/" ]]; then mkdir "$HOME/.bin" fi if [ ! -f "$HOME/.zshrc" ]; then touch $HOME/.zshrc fi if [[ ":$PATH:" != *":$HOME/.bin:"* ]]; then printf 'export PATH="$HOME/.bin:$PATH"\n' >> ~/.zshrc export PATH="$HOME/.bin:$PATH" fi ### end common-components/check-home-bin fancy_echo() { printf "\n%b\n" "$1" } fancy_line() { fancy_echo "............................................." } fancy_para() { fancy_echo "#############################################" fancy_echo " " fancy_echo " " } ### end common-components/shared-functions brew_install_or_upgrade() { if brew_is_installed "$1"; then if brew_is_upgradable "$1"; then echo "$1 is installed. Upgrading..." brew upgrade $1 fi #fancy_echo "$1 is installed. Ignore..." else brew install "$@" fi } brew_is_installed() { local NAME=$(brew_expand_alias "$1") brew list -1 | grep -Fqx "$NAME" } brew_is_upgradable() { local NAME=$(brew_expand_alias "$1") brew outdated --quiet "$NAME" >/dev/null [[ $? -ne 0 ]] } brew_expand_alias() { brew info "$1" 2>/dev/null | head -1 | awk '{gsub(/:/, ""); print $1}' } brew_launchctl_restart() { local NAME=$(brew_expand_alias "$1") local DOMAIN="homebrew.mxcl.$NAME" local PLIST="$DOMAIN.plist" mkdir -p ~/Library/LaunchAgents ln -sfv /usr/local/opt/$NAME/$PLIST ~/Library/LaunchAgents if [ "$2" == "sudo" ]; then sudo chown root ~/Library/LaunchAgents/$PLIST if launchctl list | grep -q $DOMAIN; then sudo launchctl unload /Users/$(whoami)/Library/LaunchAgents/$PLIST >/dev/null fi sudo launchctl load /Users/$(whoami)/Library/LaunchAgents/$PLIST >/dev/null else if launchctl list | grep -q $DOMAIN; then launchctl unload ~/Library/LaunchAgents/$PLIST >/dev/null fi launchctl load ~/Library/LaunchAgents/$PLIST >/dev/null fi } ### end mac-components/mac-functions while [[ $# > 1 ]] do key="$1" shift case $key in -d|--dir) DIRECTORY="$1" shift ;; -h|--hostname) DOMAIN="$1" shift ;; -p|--package) PACKAGE="$1" shift ;; -u|--yourname) YOURUSERNAME="$1" shift ;; --default) DEFAULT=YES shift ;; *) # unknown option ;; esac done REPOMAIN="https://raw.githubusercontent.com/listia/laptop/master/" if [[ -n $1 ]]; then echo "Last line of file specified as non-opt/last argument:" tail -1 $1 exit fi if [[ "$PACKAGE" ]]; then fancy_echo "..................................................." fancy_echo "Single package install mode. Will install: $PACKAGE..." fi ### end mac-components/argument if ( echo "mac-components/filevault" | grep -q "$PACKAGE" ); then fancy_echo "Checking Filevault status..." if [ "$(sudo fdesetup status)" == "FileVault is Off." ]; then fancy_echo "Please turn on Filevault. https://github.com/listia/listia/wiki/Computer-Security" exit fi fi ### end mac-components/filevault if ( echo "mac-components/github" | grep -q "$PACKAGE" ); then github_add_key() { result=$(curl -H "Authorization: token $1" -d "{\"title\":\"Listia\",\"key\":\"$2\"}" https://api.github.com/user/keys > /dev/null 2>&1) } fancy_echo "Generating SSH KEY" if [ ! -f ~/.ssh/id_rsa ]; then ssh-keygen -t rsa -b 4096 else fancy_echo "A SSH key pair is exsited. Will not generate." fi read -p "Auto import your SSH key to Github? (Y/n) " autoimport if [ "$autoimport" == "Y" ]; then fancy_echo "Enter your github username/password. Your SSH public key will be imported automatically" valid_password="" while [ "$valid_password" == "" ] do confirm="" while [ ! "$confirm" == "Y" ] do read -p "Username: " username echo "Your username is: $username." read -p "Is this correct? (Y/n) " confirm done read -s -p "Password: " password printf "\n" # Check 2-factor login, get the otp token if needed, then send other request to grab the token perm="{\"note\":\"Listia Laptop: $(date)\",\"scopes\":[\"read:public_key\", \"write:public_key\"]}" basicauth="$username:$password" authorization=$(curl -i -d "$perm" -u "$basicauth" https://api.github.com/authorizations 2>/dev/null) if [ "$(echo $authorization | grep 'Must specify two-factor authentication')" == "" ]; then if [ "$(echo $authorization | grep 'Listia Laptop:')" == "" ]; then echo "Wrong password" else valid_password=1 payload="{\"title\":\"Listia\",\"key\":\"$(cat ~/.ssh/id_rsa.pub)\"}" curl -u "$basicauth" -d "$payload" https://api.github.com/user/keys > /dev/null 2>&1 fi else valid_password=1 valid_otptoken="" while [ "$valid_otptoken" == "" ] do read -p "Enter your 2-factor token: " otptoken authorization=$(curl -H "X-GitHub-OTP: $otptoken" -d "$perm" -u "$basicauth" https://api.github.com/authorizations 2>/dev/null) token=$(echo "$authorization" | grep "token" | awk '{print $2}' | sed "s/[^a-zA-Z0-9]//g") if [ "$token" == "" ]; then echo "Wrong OTP token. Please check your SMS." else valid_otptoken=1 github_add_key "$token" "$(cat ~/.ssh/id_rsa.pub)" fi done fi done fi fi ### end mac-components/github if ( echo "mac-components/homebrew" | grep -q "$PACKAGE" ); then if ! command -v brew >/dev/null; then fancy_echo "Installing Homebrew, a good OS X package manager ..." ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" if ! grep -qs "recommended by brew doctor" ~/.zshrc; then fancy_echo "Put Homebrew location earlier in PATH ..." printf '\n# recommended by brew doctor\n' >> ~/.zshrc printf 'export PATH="/usr/local/bin:$PATH"\n' >> ~/.zshrc export PATH="/usr/local/bin:$PATH" fi else fancy_echo "Homebrew already installed. Skipping ..." fi fancy_echo "Updating Homebrew formulas ..." brew update fi ### end mac-components/homebrew if ( echo "mac-components/packages" | grep -q "$PACKAGE" ); then fancy_echo "Installing watch, to execute a program periodically and show the output ..." brew_install_or_upgrade 'watch' fi ### end mac-components/packages if ( echo "mac-components/imagemagick" | grep -q "$PACKAGE" ); then fancy_echo "Installing ImageMagick, to crop and resize images ..." brew_install_or_upgrade 'imagemagick' fi ### end mac-components/imagemagick if ( echo "mac-components/rvm" | grep -q "$PACKAGE" ); then fancy_echo "Installing rvm, to change Ruby versions ..." rvmdetect="$(which rvm1 || true)" if [[ -z "$rvmdetect" ]]; then echo "Installing rvm..." curl -L https://get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm else echo "Found an rvm" fi fi ### end mac-components/rvm fancy_echo "Install common libraries and utilities" fancy_line fancy_echo "Checking: Upgrading and linking OpenSSL ..." if (brew install 'openssl' > /dev/null 2>&1); then echo "Installed OpenSSL" fi brew unlink openssl > /dev/null 2>&1 && brew link openssl --force > /dev/null 2>&1 fancy_echo "Checking: Upgrading and linking wget ..." if (brew install 'wget' > /dev/null 2>&1); then echo "Install WGET" fi fancy_echo "Finish. Start to install packages." fancy_para ### end mac-components/compiler-and-libraries if ( echo "common-components/ruby-environment" | grep -q "$PACKAGE" ); then ruby_version="ruby-2.0.0-p645" fancy_echo "Installing Ruby $ruby_version ..." if [ -n "$(rvm list | grep "$ruby_version")" ]; then echo "Ruby $ruby_version is installed. Ignore..." else rvm get head rvm install "$ruby_version" --disable-binary --patch railsexpress rvm-exec "$ruby_version@global" gem install bundler -v 1.9.4 fi #fancy_echo "Setting $ruby_version as global default Ruby ..." # rvm "$ruby_version@listia" fi ### end common-components/ruby-environment if ( echo "mac-components/bundler" | grep -q "$PACKAGE" ); then fancy_echo "Installing Bundler to install project-specific Ruby gems ..." gem install bundler --no-document --pre fancy_echo "Configuring Bundler for faster, parallel gem installation ..." number_of_cores=$(sysctl -n hw.ncpu) bundle config --global jobs $((number_of_cores - 1)) fi ### end mac-components/bundler if ( echo "mac-components/anvil" | grep -q "$PACKAGE" ); then fancy_echo "Installing Anvil..." if [ ! -d "/Applications/Anvil.app" ]; then wget http://sparkler.herokuapp.com/apps/3/download -O /tmp/Anvil.app.zip #if [ "$(md5 -r /tmp/Anvil.app.zip)" == "d41d8cd98f00b204e9800998ecf8427e" ]; then echo "Enter your password to install Anvil into /Applications " sudo unzip -o /tmp/Anvil.app.zip -d /Applications/ #rm -f /tmp/Anvil.app.zip #fi else fancy_echo "Ignore" fi fi ### end mac-components/anvil if ( echo "mac-components/hipchat" | grep -q "$PACKAGE" ); then fancy_echo "Installing Hipchat..." if [ ! -d "/Applications/HipChat.app" ]; then brew cask install hipchat fancy_echo "*** Done" else fancy_echo "*** Installed. Ignore" fi fi ### end mac-components/hipchat if ( echo "mac-components/imageoptim" | grep -q "$PACKAGE" ); then fancy_echo "Installing ImageOptim..." if [ ! -d "/Applications/ImageOptim.app" ]; then wget https://imageoptim.com/ImageOptim.tbz2 -O /tmp/ImageOptim.tbz2 sudo tar xvf /tmp/ImageOptim.tbz2 -C /Applications rm -rf /tmp/ImageOptim.tbz2 else fancy_echo "Ignore." fi fi ### end mac-components/imageoptim if ( echo "mac-components/mysql" | grep -q "$PACKAGE" ); then fancy_echo "Installing MySQL 5.7 ..." fancy_echo "Updating brew formula..." brew_install_or_upgrade mysql brew link mysql --force >> /tmp/laptop.log 2>&1 brew services start mysql echo -e "Relax default setting of MySQL57:\n" cat > /usr/local/etc/my.cnf.d/relax.cnf <<- EOM [mysqld] sql-mode="NO_ENGINE_SUBSTITUTION" EOM cat /usr/local/etc/my.cnf.d/relax.cnf fi ### end mac-components/mysql if ( echo "mac-components/sphinx" | grep -q "$PACKAGE" ); then fancy_echo "Installing Sphinx..." if [ "$(brew tap | grep dupes)" == "" ]; then brew tap homebrew/dupes fi if [ "$(brew tap | grep versions)" == "" ]; then brew tap homebrew/versions fi brew_install_or_upgrade apple-gcc42 if ( brew list | grep -q sphinx ); then if ( brew list sphinx | grep -q 0.9.9 ); then fancy_echo "Sphinx 0.9.9 is installed. Will remove to prepare 2.2.8 upgrade." brew unlink sphinx fi else brew install https://raw.githubusercontent.com/listia/laptop/master/formula/sphinx.rb --mysql fi if brew_is_installed "Sphinx"; then fancy_echo "Sphinx 2.2.8 is installed. Ignore." else brew install https://raw.githubusercontent.com/listia/laptop/master/formula/sphinx.rb --mysql fi fi ### end mac-components/sphinx if ( echo "mac-components/nginx" | grep -q "$PACKAGE" ); then fancy_echo "Installing Nginx, to server SSL connections ..." brew_install_or_upgrade nginx fancy_echo "Creating config file..." mkdir -p /usr/local/etc/nginx/sites-enabled mkdir -p /usr/local/etc/nginx/sites-available #cat "templates/nginx.conf" > /usr/local/etc/nginx/nginx.conf #cat "templates/app.dev" | sed "s/DOMAIN/$DOMAIN/g" | sed "s/DIRECTORY/$(echo $DIRECTORY | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" > /usr/local/etc/nginx/sites-available/"$DOMAIN".dev curl -0 $REPOMAIN"/templates/nginx.conf" > /usr/local/etc/nginx/nginx.conf 2>/dev/null curl -0 $REPOMAIN"/templates/app.dev" 2>/dev/null | sed "s/DOMAIN/$DOMAIN/g" | sed "s/DIRECTORY/$(echo $DIRECTORY | sed -e 's/\\/\\\\/g' -e 's/\//\\\//g' -e 's/&/\\\&/g')/g" > /usr/local/etc/nginx/sites-available/"$DOMAIN".dev fancy_echo "Enabling the SSL site..." if [ -f /usr/local/etc/nginx/sites-enabled/"$DOMAIN".dev ]; then unlink /usr/local/etc/nginx/sites-enabled/"$DOMAIN".dev fi ln -s /usr/local/etc/nginx/sites-available/"$DOMAIN".dev /usr/local/etc/nginx/sites-enabled/"$DOMAIN".dev fancy_echo "Creating self-sign certificate..." openssl genrsa -out "$DOMAIN".dev.key 2048 openssl req -new -key "$DOMAIN".dev.key -out "$DOMAIN".dev.csr -subj '/C=US/ST=CA/L=MT/O=MyCompany/OU=IT/CN="$DOMAIN"' cp "$DOMAIN".dev.key "$DOMAIN".dev.key.bak openssl rsa -in "$DOMAIN".dev.key.bak -out "$DOMAIN".dev.key openssl x509 -req -days 365 -in "$DOMAIN".dev.csr -signkey "$DOMAIN".dev.key -out "$DOMAIN".dev.crt mkdir -p /usr/local/etc/nginx/ssl mv "$DOMAIN".dev.* /usr/local/etc/nginx/ssl fancy_echo "Creating log dir in /var/log/nginx..." if [ ! -d "/var/log/nginx" ]; then sudo mkdir /var/log/nginx/ sudo chown $(whoami) /var/log/nginx fi fancy_echo "Starting nginx service with sudo to bind on port 443..." brew_launchctl_restart nginx sudo fi ### end mac-components/nginx if ( echo "mac-components/pumadev" | grep -q "$PACKAGE" ); then fancy_echo "Installing Pumadev ..." if [ ! -f /usr/local/bin/puma-dev ]; then brew install puma/puma/puma-dev sudo puma-dev -setup puma-dev -install else fancy_echo "Ignore." fi fi ### end mac-components/pumadev if [[ ! "$PACKAGE" ]]; then fancy_echo "Re starting MySQL ..." brew_launchctl_restart mysql fi ### end mac-components/start-services if ( echo "common-components/personal-additions" | grep -q "$PACKAGE" ); then fancy_echo "Installing your personal additions from ~/.laptop.local ..." if [[ -f ~/.laptop.local ]]; then source ~/.laptop.local fi fi ### end common-components/personal-additions