Skip to content

Commit 09b1fdd

Browse files
config.sh for nix
1 parent 18076ea commit 09b1fdd

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

src/Agent.Listener/Configuration/ConfigurationManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ public async Task ConfigureAsync(CommandSettings command)
267267

268268
// chown/chmod the _diag and settings files to the current user, if we started with sudo.
269269
// Also if we started with sudo, the _diag will be owned by root. Change this to current login user
270-
if (Constants.Agent.Platform == Constants.OSPlatform.Linux)
270+
if (Constants.Agent.Platform == Constants.OSPlatform.Linux ||
271+
Constants.Agent.Platform == Constants.OSPlatform.OSX)
271272
{
272273
string uidValue = Environment.GetEnvironmentVariable("SUDO_UID");
273274
string gidValue = Environment.GetEnvironmentVariable("SUDO_GID");

src/Misc/layoutbin/darwin.svc.sh.template

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33
SVC_NAME="{{SvcNameVar}}"
44
SVC_DESCRIPTION="{{SvcDescription}}"
55

6+
user_id=`id -u`
7+
8+
if [ $user_id -ne 0 ]; then
9+
echo "Must run as sudo"
10+
exit 1
11+
fi
12+
613
SVC_CMD=$1
714
AGENT_ROOT=`pwd`
815

9-
PLIST_PATH=${HOME}/Library/LaunchAgents/${SVC_NAME}.plist
16+
LAUNCH_PATH="${HOME}/Library/LaunchAgents"
17+
PLIST_PATH="${LAUNCH_PATH}/${SVC_NAME}.plist"
1018
TEMPLATE_PATH=./bin/vsts.agent.plist.template
1119
TEMP_PATH=./bin/vsts.agent.plist.temp
1220
CONFIG_PATH=.Service
@@ -25,47 +33,59 @@ fi
2533
function install()
2634
{
2735
echo "Creating launch agent in ${PLIST_PATH}"
36+
37+
if [ ! -d "${LAUNCH_PATH}" ]; then
38+
failed "${LAUNCH_PATH} does not exist. OSX system dir expected"
39+
fi
40+
2841
if [ -f "${PLIST_PATH}" ]; then
2942
failed "error: exists ${PLIST_PATH}"
3043
fi
3144

32-
if [ -f "" ]; then
45+
if [ -f "${TEMP_PATH}" ]; then
3346
rm "${TEMP_PATH}"
3447
fi
3548

3649
log_path="${HOME}/Library/Logs/${SVC_NAME}"
37-
echo "creating ${log_path}"
50+
echo "Creating ${log_path}"
3851
mkdir -p "${log_path}"
39-
chown "${USER}" "${log_path}"
52+
chown ${SUDO_UID:-$UID}:${SUDO_GID} "${log_path}"
4053

41-
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"
54+
echo Creating ${PLIST_PATH}
55+
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"
4256
cp "${TEMP_PATH}" "${PLIST_PATH}" || "failed to copy plist"
57+
chown ${SUDO_UID:-$UID}:${SUDO_GID} "${PLIST_PATH}"
58+
59+
# Since we started with sudo, runsvc.sh will be owned by root. Change this to current login user.
60+
echo Creating runsvc.sh
4361
cp ./bin/runsvc.sh ./runsvc.sh
62+
chown ${SUDO_UID:-$UID}:${SUDO_GID} ./runsvc.sh
4463
chmod 755 ./runsvc.sh
4564

46-
echo '
47-
{
48-
"RunAsService": true,
49-
"ServiceName": "'${SVC_NAME}'",
50-
"ServiceDisplayName": "'${SVC_DESCRIPTION}'"
51-
}
52-
' > ${CONFIG_PATH}
65+
echo Creating ${CONFIG_PATH}
66+
echo "${PLIST_PATH}" > ${CONFIG_PATH}
67+
chown ${SUDO_UID:-$UID}:${SUDO_GID} ${CONFIG_PATH}
68+
69+
echo "svc install complete"
5370
}
5471

5572
function start()
5673
{
57-
launchctl load -w "${PLIST_PATH}" || "failed to load ${PLIST_PATH}"
74+
echo "starting ${SVC_NAME}"
75+
sudo -u "${SUDO_USER}" launchctl load -w "${PLIST_PATH}" || "failed to load ${PLIST_PATH}"
5876
status
5977
}
6078

6179
function stop()
6280
{
63-
launchctl unload "${PLIST_PATH}" || "failed to unload ${PLIST_PATH}"
81+
echo "stopping ${SVC_NAME}"
82+
sudo -u "${SUDO_USER}" launchctl unload "${PLIST_PATH}" || "failed to unload ${PLIST_PATH}"
6483
status
6584
}
6685

6786
function uninstall()
6887
{
88+
echo "uninstalling ${SVC_NAME}"
6989
stop
7090
rm "${PLIST_PATH}"
7191
if [ -f "${CONFIG_PATH}" ]; then
@@ -75,6 +95,7 @@ function uninstall()
7595

7696
function status()
7797
{
98+
echo "status ${SVC_NAME}:"
7899
if [ -f "${PLIST_PATH}" ]; then
79100
echo
80101
echo "${PLIST_PATH}"
@@ -86,7 +107,7 @@ function status()
86107
fi
87108

88109
echo
89-
status_out=`launchctl list | grep "${SVC_NAME}"`
110+
status_out=`sudo -u "${SUDO_USER}" launchctl list | grep "${SVC_NAME}"`
90111
if [ ! -z "$status_out" ]; then
91112
echo Started:
92113
echo $status_out

src/Misc/layoutbin/systemd.svc.sh.template

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ function install()
4747

4848
systemctl enable ${SVC_NAME} || "failed to enable ${SVC_NAME}"
4949

50-
echo '
51-
{
52-
"RunAsService": true,
53-
"ServiceName": "'${SVC_NAME}'",
54-
"ServiceDisplayName": "'${SVC_DESCRIPTION}'"
55-
}
56-
' > ${CONFIG_PATH}
50+
echo "${SVC_NAME}" > ${CONFIG_PATH}
5751
}
5852

5953
function start()

src/Misc/layoutroot/config.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
user_id=`id -u`
2+
3+
if [ $user_id -eq 0 ]; then
4+
echo "Must not run with sudo"
5+
exit 1
6+
fi
7+
8+
# user_name=`id -nu $user_id`
9+
10+
sudo ./bin/Agent.Listener configure $*

src/dev.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@ function layout ()
170170
cp -Rf ./Misc/layoutroot/* ${LAYOUT_DIR}
171171
cp -Rf ./Misc/layoutbin/* ${LAYOUT_DIR}/bin
172172

173+
# clean up files not meant for platform
174+
if [[ ("$PLATFORM_NAME" == "Linux") || ("$PLATFORM_NAME" == "Darwin") ]]; then
175+
rm ${LAYOUT_DIR}/run.cmd
176+
else
177+
rm ${LAYOUT_DIR}/*.sh
178+
fi
179+
173180
heading Externals ...
174181
bash ./Misc/externals.sh
175182

0 commit comments

Comments
 (0)