-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from tknerr/feature/update-vm-optimizations
update-vm.sh cleanup and optimizations + update to ChefDK 1.3.40 stable
- Loading branch information
Showing
6 changed files
with
58 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
require 'spec_helper' | ||
|
||
describe 'update-vm.sh' do | ||
# Serverspec examples can be found at | ||
# http://serverspec.org/resource_types.html | ||
|
||
it 'installs git' do | ||
expect(package('git')).to be_installed | ||
expect(command('git --version').exit_status).to eq 0 | ||
it 'installs chefdk 1.3.40' do | ||
expect(file('/opt/chefdk/version-manifest.txt')).to contain 'chefdk 1.3.40' | ||
end | ||
|
||
it 'installs chefdk 1.3.32' do | ||
expect(command('chef --version').stdout).to contain 'Chef Development Kit Version: 1.3.32' | ||
it 'symlinks the update-vm script to /usr/local/bin/' do | ||
expect(file('/usr/local/bin/update-vm')).to be_linked_to "#{vm_user_home}/vm-setup/scripts/update-vm.sh" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,121 +1,89 @@ | ||
#!/bin/bash | ||
set -e -o pipefail | ||
|
||
CHEFDK_VERSION=1.3.32 | ||
TARGET_DIR=/tmp/vagrant-cache/wget | ||
CHEFDK_VERSION=1.3.40 | ||
DOWNLOAD_DIR=/tmp/vagrant-cache/wget | ||
REPO_ROOT=~/vm-setup | ||
CMD_LINE_FLAG=$1 | ||
|
||
big_step() { | ||
echo "" | ||
echo "=====================================" | ||
echo ">>>>>> $1" | ||
echo "=====================================" | ||
echo "" | ||
} | ||
|
||
step() { | ||
echo "" | ||
echo "" | ||
echo ">>>>>> $1" | ||
echo "-------------------------------------" | ||
echo "" | ||
main() { | ||
setup_chefdk | ||
if [[ "$CMD_LINE_FLAG" == "--verify-only" ]]; then | ||
verify_vm | ||
else | ||
copy_repo_and_symlink_self | ||
[[ "$CMD_LINE_FLAG" == "--pull" ]] && update_repo | ||
update_vm | ||
[[ "$CMD_LINE_FLAG" == "--provision-only" ]] || verify_vm | ||
fi | ||
} | ||
|
||
check_chefdk() { | ||
big_step "Checking ChefDK..." | ||
if [[ $(chef -v | grep $CHEFDK_VERSION) ]]; then | ||
setup_chefdk() { | ||
big_step "Setting up ChefDK..." | ||
if [[ $(head -n1 /opt/chefdk/version-manifest.txt 2>/dev/null | grep "chefdk $CHEFDK_VERSION") ]]; then | ||
echo "ChefDK $CHEFDK_VERSION already installed" | ||
else | ||
step "Downloading and installing ChefDK $CHEFDK_VERSION" | ||
mkdir -p $TARGET_DIR | ||
mkdir -p $DOWNLOAD_DIR | ||
local CHEFDK_DEB=chefdk_$CHEFDK_VERSION-1_amd64.deb | ||
local CHEFDK_URL=https://packages.chef.io/files/current/chefdk/$CHEFDK_VERSION/ubuntu/16.04/$CHEFDK_DEB | ||
[[ -f $TARGET_DIR/$CHEFDK_DEB ]] || wget -O $TARGET_DIR/$CHEFDK_DEB $CHEFDK_URL | ||
sudo dpkg -i $TARGET_DIR/$CHEFDK_DEB | ||
fi | ||
} | ||
|
||
check_git() { | ||
big_step "Checking Git..." | ||
if [[ $(which git) ]]; then | ||
echo "Git already installed" | ||
else | ||
step "Installing Git" | ||
sudo apt-get update | ||
sudo apt-get install git -y | ||
local CHEFDK_URL=https://packages.chef.io/files/stable/chefdk/$CHEFDK_VERSION/ubuntu/16.04/$CHEFDK_DEB | ||
[[ -f $DOWNLOAD_DIR/$CHEFDK_DEB ]] || sudo wget --no-verbose -O $DOWNLOAD_DIR/$CHEFDK_DEB $CHEFDK_URL | ||
sudo dpkg -i $DOWNLOAD_DIR/$CHEFDK_DEB | ||
fi | ||
# initialize the shell, adding ChefDK binaries to the PATH | ||
eval "$(chef shell-init bash)" | ||
} | ||
|
||
copy_repo_and_symlink_self() { | ||
big_step "Copying repo into the VM..." | ||
if mountpoint -q /vagrant; then | ||
step "Copy /vagrant to $REPO_ROOT" | ||
sudo rm -rf $REPO_ROOT | ||
sudo cp -r /vagrant $REPO_ROOT | ||
sudo chown -R $USER:$USER $REPO_ROOT | ||
step "Symlinking 'update-vm' script" | ||
sudo ln -sf $REPO_ROOT/scripts/update-vm.sh /usr/local/bin/update-vm | ||
echo "Copied repo to $REPO_ROOT and symlinked the 'update-vm' script" | ||
else | ||
echo "Skipped because /vagrant not mounted" | ||
fi | ||
} | ||
|
||
shell_init() { | ||
step "init the shell" | ||
set -e | ||
eval "$(chef shell-init bash)" | ||
} | ||
|
||
update_repo() { | ||
big_step "Pulling latest changes from git..." | ||
cd $REPO_ROOT | ||
git pull | ||
} | ||
|
||
update_vm() { | ||
big_step "Updating the VM via Chef..." | ||
|
||
shell_init | ||
cd $REPO_ROOT/cookbooks/vm | ||
|
||
# install cookbook dependencies | ||
step "install cookbook dependencies" | ||
rm -rf ./cookbooks | ||
berks vendor ./cookbooks | ||
berks vendor --delete ./cookbooks | ||
|
||
# disable vmware ohai plugin because it's so painfully slow | ||
sudo find /opt/chefdk/embedded/ -wholename *ohai* -name vmware.rb -exec mv {} {}.disabled \; | ||
|
||
# converge the system via chef-zero | ||
step "trigger the chef-zero run" | ||
step "update the system via chef-zero" | ||
sudo -H chef-client --config-option node_path=/root/.chef/nodes --local-mode --format=doc --force-formatter --log_level=warn --color --runlist=vm | ||
} | ||
|
||
verify_vm() { | ||
big_step "Verifying the VM..." | ||
|
||
shell_init | ||
cd $REPO_ROOT/cookbooks/vm | ||
|
||
# run lint checks | ||
step "run codestyle checks" | ||
step "run foodcritic linting checks" | ||
foodcritic -f any . | ||
|
||
# run integration tests | ||
step "run integration tests" | ||
step "run serverspec integration tests" | ||
rspec --require rspec_junit_formatter --format doc --color --tty --format RspecJunitFormatter --out test/junit-report.xml --format html --out test/test-report.html | ||
} | ||
|
||
# | ||
# main flow | ||
# | ||
if [[ "$1" == "--verify-only" ]]; then | ||
verify_vm | ||
else | ||
check_git | ||
check_chefdk | ||
copy_repo_and_symlink_self | ||
[[ "$1" == "--pull" ]] && update_repo | ||
update_vm | ||
[[ "$1" == "--provision-only" ]] || verify_vm | ||
fi | ||
update_repo() { | ||
big_step "Pulling latest changes from git..." | ||
cd $REPO_ROOT | ||
git pull | ||
} | ||
|
||
big_step() { | ||
echo -e "\n=====================================\n>>>>>> $1\n=====================================\n" | ||
} | ||
step() { | ||
echo -e "\n\n>>>>>> $1\n-------------------------------------\n" | ||
} | ||
|
||
# run it! | ||
main |