From f16c4ece80cc369d4aaf4086fd3443c0eaeb88ed Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Fri, 31 Jan 2020 15:13:14 -0700 Subject: [PATCH 1/5] mac:(launchd) mange the app as a service This change is adding a new `scripts/` folder with a shell script called `chef_workstation_app_launcher` that will control the app launcher behavior for MacOS systems. Load the app as a service: ``` $ chef_workstation_app_launcher load ``` Remove the app as a service: ``` $ chef_workstation_app_launcher remove ``` Signed-off-by: Salim Afiune --- assets/chef.workstation.app.plist | 19 ++++ assets/scripts/chef_workstation_app_launcher | 94 ++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 assets/chef.workstation.app.plist create mode 100755 assets/scripts/chef_workstation_app_launcher diff --git a/assets/chef.workstation.app.plist b/assets/chef.workstation.app.plist new file mode 100644 index 00000000..6e865731 --- /dev/null +++ b/assets/chef.workstation.app.plist @@ -0,0 +1,19 @@ + + + + + Label + chef.workstation.app + ProgramArguments + + /Applications/Chef Workstation App.app/Contents/MacOS/Chef Workstation App + + RunAtLoad + + KeepAlive + + SuccessfulExit + + + + diff --git a/assets/scripts/chef_workstation_app_launcher b/assets/scripts/chef_workstation_app_launcher new file mode 100755 index 00000000..16a2e406 --- /dev/null +++ b/assets/scripts/chef_workstation_app_launcher @@ -0,0 +1,94 @@ +#!/bin/sh +# +# Copyright:: Copyright 2020 Chef Software, Inc. +# Author:: Salim Afiune +# 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="chef.workstation.app" +PLISTFILE="${SERVICENAME}.plist" + +usage() { + cat < + +Controls the ${PRODUCTNAME} launcher behavior for MacOS systems. + +Subcommands: + load Bootstraps the ${PRODUCTNAME} service. + remove Removes the ${PRODUCTNAME} service. +DOC +} + +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 +} + +if ! is_darwin; then + error_exit "Launcher is only available for MacOS systems" +fi + +case "${1:-}" in + "-h"|"--help") + usage + ;; + "load") + launchctl_load + ;; + "remove") + launchctl_remove + ;; + *) + error_exit "invalid option '$1'.\\nTry '--help' for more information." +esac From 0ec15c9aa0f5ec3306dcd0955a479ba03b489ac9 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Mon, 3 Feb 2020 15:22:26 -0700 Subject: [PATCH 2/5] chef_workstation_app_launcher:(show) launchd info Show launchd information about the Chef Workstation App service. Signed-off-by: Salim Afiune --- assets/scripts/chef_workstation_app_launcher | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/assets/scripts/chef_workstation_app_launcher b/assets/scripts/chef_workstation_app_launcher index 16a2e406..a74034d3 100755 --- a/assets/scripts/chef_workstation_app_launcher +++ b/assets/scripts/chef_workstation_app_launcher @@ -31,6 +31,7 @@ Usage: ${PROGNAME} 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 @@ -75,6 +76,15 @@ launchctl_remove() 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 +} + if ! is_darwin; then error_exit "Launcher is only available for MacOS systems" fi @@ -83,6 +93,9 @@ case "${1:-}" in "-h"|"--help") usage ;; + "show") + launchctl_show + ;; "load") launchctl_load ;; From e28bc5b5864d6e1e082571133b9e27cb2b1a9b87 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Tue, 4 Feb 2020 14:25:08 -0700 Subject: [PATCH 3/5] chef_workstation_app_launcher:(sh) enforce arguments Signed-off-by: Salim Afiune --- assets/scripts/chef_workstation_app_launcher | 50 ++++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/assets/scripts/chef_workstation_app_launcher b/assets/scripts/chef_workstation_app_launcher index a74034d3..1d62f05a 100755 --- a/assets/scripts/chef_workstation_app_launcher +++ b/assets/scripts/chef_workstation_app_launcher @@ -37,6 +37,35 @@ Subcommands: 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 @@ -85,23 +114,4 @@ launchctl_show() fi } -if ! is_darwin; then - error_exit "Launcher is only available for MacOS systems" -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 +main "$@" From 2e8c4b130da28b82c2c7c759e87c700e32cf58d2 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Tue, 4 Feb 2020 15:10:36 -0700 Subject: [PATCH 4/5] rename:(plist) use domain io.chef Signed-off-by: Salim Afiune --- ...workstation.app.plist => io.chef.chef-workstation.app.plist} | 2 +- assets/scripts/chef_workstation_app_launcher | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename assets/{chef.workstation.app.plist => io.chef.chef-workstation.app.plist} (90%) diff --git a/assets/chef.workstation.app.plist b/assets/io.chef.chef-workstation.app.plist similarity index 90% rename from assets/chef.workstation.app.plist rename to assets/io.chef.chef-workstation.app.plist index 6e865731..4a3b9b7f 100644 --- a/assets/chef.workstation.app.plist +++ b/assets/io.chef.chef-workstation.app.plist @@ -3,7 +3,7 @@ Label - chef.workstation.app + io.chef.chef-workstation.app ProgramArguments /Applications/Chef Workstation App.app/Contents/MacOS/Chef Workstation App diff --git a/assets/scripts/chef_workstation_app_launcher b/assets/scripts/chef_workstation_app_launcher index 1d62f05a..5475e75f 100755 --- a/assets/scripts/chef_workstation_app_launcher +++ b/assets/scripts/chef_workstation_app_launcher @@ -21,7 +21,7 @@ set -eou pipefail PROGNAME=$(basename "$0") PRODUCTNAME="Chef Workstation App" -SERVICENAME="chef.workstation.app" +SERVICENAME="io.chef.chef-workstation.app" PLISTFILE="${SERVICENAME}.plist" usage() { From d0d9a3edd392bfb94550e8747981214767c10c65 Mon Sep 17 00:00:00 2001 From: Salim Afiune Date: Wed, 5 Feb 2020 16:33:46 -0700 Subject: [PATCH 5/5] review:(typos) correct macOS Signed-off-by: Salim Afiune --- assets/scripts/chef_workstation_app_launcher | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/scripts/chef_workstation_app_launcher b/assets/scripts/chef_workstation_app_launcher index 5475e75f..62f93af9 100755 --- a/assets/scripts/chef_workstation_app_launcher +++ b/assets/scripts/chef_workstation_app_launcher @@ -28,7 +28,7 @@ usage() { cat < -Controls the ${PRODUCTNAME} launcher behavior for MacOS systems. +Controls the ${PRODUCTNAME} launcher behavior for macOS systems. Subcommands: show Show launchd information about the ${PRODUCTNAME} service. @@ -40,7 +40,7 @@ DOC main() { if ! is_darwin; then - error_exit "Launcher is only available for MacOS systems" + error_exit "Launcher is only available for macOS systems" fi if [ $# -eq 0 ]; then