Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mac:(launchd) mange the WS App as a service #153

Merged
merged 6 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions assets/io.chef.chef-workstation.app.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>io.chef.chef-workstation.app</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/Chef Workstation App.app/Contents/MacOS/Chef Workstation App</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
</dict>
</dict>
</plist>
117 changes: 117 additions & 0 deletions assets/scripts/chef_workstation_app_launcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/sh
#
# Copyright:: Copyright 2020 Chef Software, Inc.
# Author:: Salim Afiune <afiune@chef.io>
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -eou pipefail

PROGNAME=$(basename "$0")
PRODUCTNAME="Chef Workstation App"
SERVICENAME="io.chef.chef-workstation.app"
PLISTFILE="${SERVICENAME}.plist"

usage() {
cat <<DOC
Usage: ${PROGNAME} <subcommand>

Controls the ${PRODUCTNAME} launcher behavior for macOS systems.

Subcommands:
show Show launchd information about the ${PRODUCTNAME} service.
load Bootstraps the ${PRODUCTNAME} service.
remove Removes the ${PRODUCTNAME} service.
DOC
}

main()
{
if ! is_darwin; then
error_exit "Launcher is only available for macOS systems"
fi

if [ $# -eq 0 ]; then
usage
exit 1
fi

case "$1" in
"-h"|"--help")
usage
;;
"show")
launchctl_show
;;
"load")
launchctl_load
;;
"remove")
launchctl_remove
;;
*)
error_exit "invalid option '$1'.\\nTry '--help' for more information."
esac
}

error_exit()
{
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}

is_darwin()
{
uname -v | grep "^Darwin" >/dev/null 2>&1
}

launchctl_load()
{
if [ ! -d "/Applications/Chef Workstation App.app/Contents" ]; then
error_exit "${PRODUCTNAME} not found in /Applications folder. The application needs to be installed first."
fi

launchctl_remove

# we let launchd to process the removal of the service
sleep 1

cp "/Applications/Chef Workstation App.app/Contents/Resources/assets/${PLISTFILE}" "$HOME/Library/LaunchAgents"
( cd "$HOME/Library/LaunchAgents" || error_exit "unable to enter LaunchAgents directory"
launchctl load ${PLISTFILE}
)
}

launchctl_remove()
{
if launchctl list "$SERVICENAME" >/dev/null 2>&1; then
launchctl remove "$SERVICENAME"
fi

if [ -f "$HOME/Library/LaunchAgents/${PLISTFILE}" ]; then
rm -rf "$HOME/Library/LaunchAgents/${PLISTFILE}"
fi
}

launchctl_show()
{
if launchctl list "$SERVICENAME" >/dev/null 2>&1; then
launchctl list "$SERVICENAME"
else
error_exit "$PRODUCTNAME is not yet loaded.\\nTry using the subcommand 'load'."
fi
}

main "$@"