Skip to content

Commit

Permalink
Fixed creation of default files
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmorley committed Oct 24, 2016
1 parent 7e72871 commit 41ecefa
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 196 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
AllCops:
Exclude:
- 'omnibus/**/*'
inherit_from:
- '.rubocop_todo.yml'
69 changes: 14 additions & 55 deletions bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,22 @@ Function Add-ToPath {
}
}

Write-Host 'Bootstrapping workup'
Write-Host 'Bootstrapping Workup'

$WORKUP_URL = 'https://raw.githubusercontent.com/cvent/workup/master'
$WORKUP_VERSION = "0.1.1"
$WORKUP_URL = "https://github.com/cvent/workup/releases/download/v${WORKUP_VERSION}/workup.msi"
$WORKUP_DIR = Join-Path ${HOME} '.workup'

$CHEFDK_URL = 'https://omnitruck.chef.io/install.ps1'
$CHEFDK_VERSION = '0.17.17'

$wc = New-Object System.Net.WebClient

Write-Host -NoNewLine "Checking for ChefDK >= v${CHEFDK_VERSION}... "
If (Get-Command chef -ErrorAction SilentlyContinue) {
$current_chef_version = (chef --version | select -first 1) -replace 'Chef Development Kit Version: ',''

$install_chef = (([System.Version] ${current_chef_version}) -lt ([System.Version] ${CHEFDK_VERSION}))

Write-Host -NoNewLine "v${current_chef_version} found "

If ($install_chef) {
Write-Host -ForegroundColor 'Yellow' 'Too old'
} Else {
Write-Host -ForegroundColor 'Green' 'OK'
}
} Else {
Write-Host -ForegroundColor 'Yellow' 'Not found'
$install_chef = $true
}

If (${install_chef}) {
Get-WmiObject `
-Class Win32_Product `
-Filter "Name LIKE 'Chef Development Kit%'" |% {
Write-Host -NoNewLine "Uninstalling ChefDK v$($_.Version)... "
$_.Uninstall() | Out-Null
Write-Host -ForegroundColor 'Green' 'OK'
}

Write-Host -NoNewLine "Installing ChefDK v${CHEFDK_VERSION}... "
. { $wc.DownloadString(${CHEFDK_URL}) } | iex | Out-Null
install -project 'chefdk' -version ${CHEFDK_VERSION} | Out-Null
Get-WmiObject `
-Class Win32_Product `
-Filter "Name LIKE 'Workup%'" |% {
Write-Host -NoNewLine "Uninstalling Workup v$($_.Version)... "
$_.Uninstall() | Out-Null
Write-Host -ForegroundColor 'Green' 'OK'
}

# ChefDK binaries
Add-ToPath 'C:\opscode\chefdk\bin'

# Chef gem binaries
Add-ToPath (Join-Path ${ENV:LOCALAPPDATA} 'chefdk\gem\ruby\2.1.0\bin')

Write-Host -NoNewLine 'Installing latest workup... '
chef gem list -i workup
If ($LASTEXITCODE -eq 0) { chef gem update workup }
Else { chef gem install workup }
Write-Host -ForegroundColor 'Green' 'OK'

If (Test-Path ${WORKUP_DIR} -PathType 'Container') {
Write-Host "~/.workup directory already exists"
Write-Information "~/.workup directory already exists"
} ElseIf (Test-Path ${WORKUP_DIR} -PathType 'Leaf') {
Throw "~/.workup is unexpectedly a file. Please resolve this and try again"
} Else {
Expand All @@ -91,10 +50,10 @@ If (Test-Path ${WORKUP_DIR} -PathType 'Container') {
Write-Host -ForegroundColor 'Green' 'OK'
}

@('Policyfile.rb', 'client.rb') |% {
Write-Host -NoNewLine "Fetching new ${_}... "
$wc.DownloadFile("${WORKUP_URL}/files/${_}", (Join-Path ${WORKUP_DIR} ${_}))
Write-Host -ForegroundColor 'Green' 'OK'
}
Write-Host -NoNewLine "Installing Workup v${WORKUP_VERSION}... "
$installer = Join-Path $WORKUP_DIR 'workup.msi'
(New-Object System.Net.WebClient).DownloadFile($WORKUP_URL, $installer)
cmd /c start '' /wait msiexec /i $installer /qn
Write-Host -ForegroundColor 'Green' 'OK'

Write-Host 'You are ready to run workup'
106 changes: 19 additions & 87 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,103 +8,35 @@ if [[ $EUID -ne 0 ]]; then
exit 1
fi

echo 'Bootstrapping workup'
echo_success() {
printf "\033[1;32m%s\033[0m\n" "$1"
}

WORKUP_BRANCH='master' # Useful for testing
WORKUP_URL="https://raw.githubusercontent.com/cvent/workup/${WORKUP_BRANCH}"
echo_warning() {
printf "\033[1;33m%s\033[0m\n" "$1"
}

WORKUP_DIR="${HOME}/.workup"
WORKUP_BINS="${WORKUP_DIR}/bin"

CHEFDK_URL='https://omnitruck.chef.io/install.sh'
CHEFDK_VERSION='0.17.17'

printf "Checking for ChefDK >= v${CHEFDK_VERSION}... "
if command -v '/usr/local/bin/chef' > /dev/null; then
# From https://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

chefdk_version_regex="^Chef Development Kit Version: (.+)$"
if [[ "$(/usr/local/bin/chef env -v)" =~ $chefdk_version_regex ]]; then
vercomp "${BASH_REMATCH[1]}" "${CHEFDK_VERSION}"
if [[ "${?}" == '-1' ]]; then install_chef='true'; else install_chef='false'; fi

printf "v${BASH_REMATCH[1]} found "
else
install_chef=true
fi

if ${install_chef}; then
printf "\033[1;33mToo old\033[0m\n"
else
printf "\033[1;32mOK\033[0m\n"
fi
else
printf "\033[1;33mNot found\033[0m\n"
install_chef='true'
fi

if ${install_chef}; then
printf "Installing ChefDK v${CHEFDK_VERSION}... "
curl -Ls "${CHEFDK_URL}" | sudo bash -s -- -P 'chefdk' -v "${CHEFDK_VERSION}" > /dev/null
eval "$(/usr/local/bin/chef shell-init bash)"
printf "\033[1;32mOK\033[0m\n"
fi
echo 'Bootstrapping Workup'

printf 'Installing latest workup... '
if chef gem list -i workup; then
chef gem update workup --bindir '/usr/local/bin'
else
chef gem install workup --bindir '/usr/local/bin'
fi
printf "\033[1;32mOK\033[0m\n"
WORKUP_VERSION="0.1.1"
WORKUP_URL="https://github.com/cvent/workup/releases/download/v${WORKUP_VERSION}/workup.pkg"
WORKUP_DIR="${HOME}/.workup"

printf 'Creating workup directory... '
printf 'Creating ~/.workup directory... '
[ -d "${WORKUP_DIR}" ] || mkdir "${WORKUP_DIR}"
printf "\033[1;32mOK\033[0m\n"

printf 'Fetching new Policyfile... '
curl -Lsko "${WORKUP_DIR}/Policyfile.rb" "${WORKUP_URL}/files/Policyfile.rb"
printf "\033[1;32mOK\033[0m\n"
echo_success "OK"

printf 'Fetching new client.rb... '
curl -Lsko "${WORKUP_DIR}/client.rb" "${WORKUP_URL}/files/client.rb"
printf "\033[1;32mOK\033[0m\n"
printf "Installing Workup v%s... " "${WORKUP_VERSION}"
installer="${WORKUP_DIR}/workup.pkg"
curl -Ls "${WORKUP_URL}" -o "${installer}"
/usr/sbin/installer -target / -pkg "${installer}"
echo_success 'OK'

printf 'Checking PATH for /usr/local/bin... '
local_regex='(^|:)/usr/local/bin/?($|:)'
if [[ "${PATH}" =~ $local_regex ]]; then
printf "\033[1;32mOK\033[0m\n"
echo_success 'OK'
echo 'You are ready to run workup'
else
printf "\033[1;33mNot found\033[0m\n"
echo_warning 'Not found'
fi
52 changes: 9 additions & 43 deletions lib/workup/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

require 'chef-dk/command/update'
require 'chef-dk/command/export'
require 'mixlib/shellout'
require 'thor'

module Workup
Expand All @@ -40,43 +39,20 @@ def initialize(*args)
log
end

Workup::Helpers.check_user!

Workup::Helpers.check_user

super(*args)
end

no_commands do
def execute(*cmd, **args)
command = Gem.win_platform? ? cmd.join(' ') : cmd

shell_out = Mixlib::ShellOut.new(command, **args)
shell_out.run_command

if shell_out.error?
log.error "Error\n"
log.error shell_out.inspect
log.error shell_out.stdout
log.error shell_out.stderr
exit shell_out.exitstatus
else
log.debug "OK\n"
end
end
end

desc 'default', 'Default task'
def default
log.info "Starting workup\n"
chef_zero
chef_client
workup
end

desc 'chef_zero', 'Create the chef-zero directory'
def chef_zero
unless File.exist?(options[:workup_dir])
Workup::Helpers.initialize_files!(options[:workup_dir])
end
Workup::Helpers.initialize_files(options[:workup_dir])

policy_path = File.join(options[:workup_dir], 'Policyfile.rb')
chefzero_path = File.join(options[:workup_dir], 'chef-zero')
Expand All @@ -92,22 +68,12 @@ def chef_zero
log.debug "OK\n"
end

desc 'chef_client', 'Run chef-client'
def chef_client
unless File.exist?(options[:workup_dir])
Workup::Helpers.initialize_files!(options[:workup_dir])
end

client_cmd = [
"#{'/opt' unless Gem.win_platform?}/workup/embedded/bin/chef-client",
'--no-fork',
'--config',
File.join(options[:workup_dir], 'client.rb')
]
client_cmd << '-A' if Gem.win_platform?
client_cmd << '--why-run' if options[:dry_run]

execute(*client_cmd, live_stdout: STDOUT, live_stderr: STDERR)
desc 'workup', 'Run workup'
def workup
log.info "Starting workup\n"
Workup::Helpers.initialize_files(options[:workup_dir])
Workup::Helpers.chef_client(File.join(options[:workup_dir], 'client.rb'),
options[:dry_run])
end

default_task :default
Expand Down
56 changes: 48 additions & 8 deletions lib/workup/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,67 @@
# limitations under the License.
#

require 'mixlib/shellout'

module Workup
class Helpers
class << self
def check_user!
def check_user
user = ENV['SUDO_USER'] || ENV['USER']
raise 'You cannot run workup as root directly' if user == 'root'
end

def initialize_files!(workup_dir)
files_dir = File.join(File.dirname(File.expand_path(__FILE__)), '../../files')

Dir.mkdir workup_dir unless File.directory? workup_dir
FileUtils.cp_r "#{files_dir}/.", workup_dir
end

def silence
$stdout = StringIO.new
yield
ensure
$stdout = STDOUT
end

def execute(*cmd, **args)
command = Gem.win_platform? ? cmd.join(' ') : cmd
Mixlib::ShellOut.new(command, **args).run_command
end

def chef_bin(executable)
[
"#{'/opt' unless Gem.win_platform?}/workup/embedded/bin/#{executable}",
"#{Gem.win_platform? ? '/opscode' : '/opt'}/chefdk/bin/#{executable}",
"#{Gem.win_platform? ? '/opscode' : '/opt'}/chef/bin/#{executable}"
].find(-> { raise "#{executable} not found" }) { |path| File.exist? path }
end

def chef_client(client_rb, dry_run = false)
cmd = [chef_bin('chef-client'), '--no-fork', '--config', client_rb]
cmd << '-A' if Gem.win_platform?
cmd << '--why-run' if dry_run

execute(*cmd, live_stdout: STDOUT, live_stderr: STDERR)
end

def chef_apply(recipe, dry_run = false)
cmd = [chef_bin('chef-apply'),
'--log_level', 'fatal',
'--minimal-ohai',
'--execute', recipe]
cmd << '--why-run' if dry_run

execute(*cmd, live_stdout: STDOUT, live_stderr: STDERR)
end

def initialize_files(workup_dir)
chef_apply("directory('#{workup_dir}') { recursive true }")

files_dir = File.join(File.dirname(File.expand_path(__FILE__)), '../../files')

Dir.glob("#{files_dir}/*")
.each do |f|
chef_apply %(file '#{File.join(workup_dir, File.basename(f))}' do
action :create_if_missing
content %q(#{IO.read(f)})
end)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/workup/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
#

module Workup
VERSION = '0.1.1'
VERSION = '0.1.2'
end
2 changes: 1 addition & 1 deletion omnibus/.kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ verifier:
platforms:
- name: macos
driver:
box: jhcook/macos-sierra
box: jhcook/osx-elcapitan-10.11
network:
- ["private_network", {ip: "192.168.33.33"}]
synced_folders:
Expand Down
Loading

0 comments on commit 41ecefa

Please sign in to comment.