From 09b1fdd5fe26503dade4ac3e93a1bd28f3d87f11 Mon Sep 17 00:00:00 2001 From: Bryan MacFarlane Date: Wed, 11 May 2016 23:02:30 -0400 Subject: [PATCH] config.sh for nix --- .../Configuration/ConfigurationManager.cs | 3 +- src/Misc/layoutbin/darwin.svc.sh.template | 51 +++++++++++++------ src/Misc/layoutbin/systemd.svc.sh.template | 8 +-- src/Misc/layoutroot/config.sh | 10 ++++ src/dev.sh | 7 +++ 5 files changed, 56 insertions(+), 23 deletions(-) create mode 100755 src/Misc/layoutroot/config.sh diff --git a/src/Agent.Listener/Configuration/ConfigurationManager.cs b/src/Agent.Listener/Configuration/ConfigurationManager.cs index 93d6e1e55d..906e8c65c4 100644 --- a/src/Agent.Listener/Configuration/ConfigurationManager.cs +++ b/src/Agent.Listener/Configuration/ConfigurationManager.cs @@ -267,7 +267,8 @@ public async Task ConfigureAsync(CommandSettings command) // chown/chmod the _diag and settings files to the current user, if we started with sudo. // Also if we started with sudo, the _diag will be owned by root. Change this to current login user - if (Constants.Agent.Platform == Constants.OSPlatform.Linux) + if (Constants.Agent.Platform == Constants.OSPlatform.Linux || + Constants.Agent.Platform == Constants.OSPlatform.OSX) { string uidValue = Environment.GetEnvironmentVariable("SUDO_UID"); string gidValue = Environment.GetEnvironmentVariable("SUDO_GID"); diff --git a/src/Misc/layoutbin/darwin.svc.sh.template b/src/Misc/layoutbin/darwin.svc.sh.template index a2d1ba7d92..de1b9c2f46 100755 --- a/src/Misc/layoutbin/darwin.svc.sh.template +++ b/src/Misc/layoutbin/darwin.svc.sh.template @@ -3,10 +3,18 @@ SVC_NAME="{{SvcNameVar}}" SVC_DESCRIPTION="{{SvcDescription}}" +user_id=`id -u` + +if [ $user_id -ne 0 ]; then + echo "Must run as sudo" + exit 1 +fi + SVC_CMD=$1 AGENT_ROOT=`pwd` -PLIST_PATH=${HOME}/Library/LaunchAgents/${SVC_NAME}.plist +LAUNCH_PATH="${HOME}/Library/LaunchAgents" +PLIST_PATH="${LAUNCH_PATH}/${SVC_NAME}.plist" TEMPLATE_PATH=./bin/vsts.agent.plist.template TEMP_PATH=./bin/vsts.agent.plist.temp CONFIG_PATH=.Service @@ -25,47 +33,59 @@ fi function install() { echo "Creating launch agent in ${PLIST_PATH}" + + if [ ! -d "${LAUNCH_PATH}" ]; then + failed "${LAUNCH_PATH} does not exist. OSX system dir expected" + fi + if [ -f "${PLIST_PATH}" ]; then failed "error: exists ${PLIST_PATH}" fi - if [ -f "" ]; then + if [ -f "${TEMP_PATH}" ]; then rm "${TEMP_PATH}" fi log_path="${HOME}/Library/Logs/${SVC_NAME}" - echo "creating ${log_path}" + echo "Creating ${log_path}" mkdir -p "${log_path}" - chown "${USER}" "${log_path}" + chown ${SUDO_UID:-$UID}:${SUDO_GID} "${log_path}" - sed "s/{{User}}/$USER/g; s/{{SvcName}}/$SVC_NAME/g; s@{{AgentRoot}}@${AGENT_ROOT}@g; s@{{UserHome}}@$HOME@g;" "${TEMPLATE_PATH}" > "${TEMP_PATH}" || failed "failed to create replacement temp file" + echo Creating ${PLIST_PATH} + sed "s/{{User}}/${SUDO_USER:-$USER}/g; s/{{SvcName}}/$SVC_NAME/g; s@{{AgentRoot}}@${AGENT_ROOT}@g; s@{{UserHome}}@$HOME@g;" "${TEMPLATE_PATH}" > "${TEMP_PATH}" || failed "failed to create replacement temp file" cp "${TEMP_PATH}" "${PLIST_PATH}" || "failed to copy plist" + chown ${SUDO_UID:-$UID}:${SUDO_GID} "${PLIST_PATH}" + + # Since we started with sudo, runsvc.sh will be owned by root. Change this to current login user. + echo Creating runsvc.sh cp ./bin/runsvc.sh ./runsvc.sh + chown ${SUDO_UID:-$UID}:${SUDO_GID} ./runsvc.sh chmod 755 ./runsvc.sh - echo ' - { - "RunAsService": true, - "ServiceName": "'${SVC_NAME}'", - "ServiceDisplayName": "'${SVC_DESCRIPTION}'" - } - ' > ${CONFIG_PATH} + echo Creating ${CONFIG_PATH} + echo "${PLIST_PATH}" > ${CONFIG_PATH} + chown ${SUDO_UID:-$UID}:${SUDO_GID} ${CONFIG_PATH} + + echo "svc install complete" } function start() { - launchctl load -w "${PLIST_PATH}" || "failed to load ${PLIST_PATH}" + echo "starting ${SVC_NAME}" + sudo -u "${SUDO_USER}" launchctl load -w "${PLIST_PATH}" || "failed to load ${PLIST_PATH}" status } function stop() { - launchctl unload "${PLIST_PATH}" || "failed to unload ${PLIST_PATH}" + echo "stopping ${SVC_NAME}" + sudo -u "${SUDO_USER}" launchctl unload "${PLIST_PATH}" || "failed to unload ${PLIST_PATH}" status } function uninstall() { + echo "uninstalling ${SVC_NAME}" stop rm "${PLIST_PATH}" if [ -f "${CONFIG_PATH}" ]; then @@ -75,6 +95,7 @@ function uninstall() function status() { + echo "status ${SVC_NAME}:" if [ -f "${PLIST_PATH}" ]; then echo echo "${PLIST_PATH}" @@ -86,7 +107,7 @@ function status() fi echo - status_out=`launchctl list | grep "${SVC_NAME}"` + status_out=`sudo -u "${SUDO_USER}" launchctl list | grep "${SVC_NAME}"` if [ ! -z "$status_out" ]; then echo Started: echo $status_out diff --git a/src/Misc/layoutbin/systemd.svc.sh.template b/src/Misc/layoutbin/systemd.svc.sh.template index 467c713b24..6b702277b0 100644 --- a/src/Misc/layoutbin/systemd.svc.sh.template +++ b/src/Misc/layoutbin/systemd.svc.sh.template @@ -47,13 +47,7 @@ function install() systemctl enable ${SVC_NAME} || "failed to enable ${SVC_NAME}" - echo ' - { - "RunAsService": true, - "ServiceName": "'${SVC_NAME}'", - "ServiceDisplayName": "'${SVC_DESCRIPTION}'" - } - ' > ${CONFIG_PATH} + echo "${SVC_NAME}" > ${CONFIG_PATH} } function start() diff --git a/src/Misc/layoutroot/config.sh b/src/Misc/layoutroot/config.sh new file mode 100755 index 0000000000..becf913b1a --- /dev/null +++ b/src/Misc/layoutroot/config.sh @@ -0,0 +1,10 @@ +user_id=`id -u` + +if [ $user_id -eq 0 ]; then + echo "Must not run with sudo" + exit 1 +fi + +# user_name=`id -nu $user_id` + +sudo ./bin/Agent.Listener configure $* \ No newline at end of file diff --git a/src/dev.sh b/src/dev.sh index 7e2627e50a..8a442430eb 100755 --- a/src/dev.sh +++ b/src/dev.sh @@ -170,6 +170,13 @@ function layout () cp -Rf ./Misc/layoutroot/* ${LAYOUT_DIR} cp -Rf ./Misc/layoutbin/* ${LAYOUT_DIR}/bin + # clean up files not meant for platform + if [[ ("$PLATFORM_NAME" == "Linux") || ("$PLATFORM_NAME" == "Darwin") ]]; then + rm ${LAYOUT_DIR}/run.cmd + else + rm ${LAYOUT_DIR}/*.sh + fi + heading Externals ... bash ./Misc/externals.sh