Skip to content

Commit 1b511b2

Browse files
committed
TUN-5881: Clarify success (or lack thereof) of (un)installing cloudflared service
1 parent 05b903a commit 1b511b2

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

cmd/cloudflared/linux_service.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ import (
1919
func runApp(app *cli.App, graceShutdownC chan struct{}) {
2020
app.Commands = append(app.Commands, &cli.Command{
2121
Name: "service",
22-
Usage: "Manages the Cloudflare Tunnel system service",
22+
Usage: "Manages the cloudflared system service",
2323
Subcommands: []*cli.Command{
2424
{
2525
Name: "install",
26-
Usage: "Install Cloudflare Tunnel as a system service",
26+
Usage: "Install cloudflared as a system service",
2727
Action: cliutil.ConfiguredAction(installLinuxService),
2828
},
2929
{
3030
Name: "uninstall",
31-
Usage: "Uninstall the Cloudflare Tunnel service",
31+
Usage: "Uninstall the cloudflared service",
3232
Action: cliutil.ConfiguredAction(uninstallLinuxService),
3333
},
3434
},
@@ -49,7 +49,7 @@ var systemdTemplates = []ServiceTemplate{
4949
{
5050
Path: "/etc/systemd/system/cloudflared.service",
5151
Content: `[Unit]
52-
Description=Cloudflare Tunnel
52+
Description=cloudflared
5353
After=network.target
5454
5555
[Service]
@@ -66,7 +66,7 @@ WantedBy=multi-user.target
6666
{
6767
Path: "/etc/systemd/system/cloudflared-update.service",
6868
Content: `[Unit]
69-
Description=Update Cloudflare Tunnel
69+
Description=Update cloudflared
7070
After=network.target
7171
7272
[Service]
@@ -76,7 +76,7 @@ ExecStart=/bin/bash -c '{{ .Path }} update; code=$?; if [ $code -eq 11 ]; then s
7676
{
7777
Path: "/etc/systemd/system/cloudflared-update.timer",
7878
Content: `[Unit]
79-
Description=Update Cloudflare Tunnel
79+
Description=Update cloudflared
8080
8181
[Timer]
8282
OnCalendar=daily
@@ -93,16 +93,16 @@ var sysvTemplate = ServiceTemplate{
9393
Content: `#!/bin/sh
9494
# For RedHat and cousins:
9595
# chkconfig: 2345 99 01
96-
# description: Cloudflare Tunnel agent
96+
# description: cloudflared
9797
# processname: {{.Path}}
9898
### BEGIN INIT INFO
9999
# Provides: {{.Path}}
100100
# Required-Start:
101101
# Required-Stop:
102102
# Default-Start: 2 3 4 5
103103
# Default-Stop: 0 1 6
104-
# Short-Description: Cloudflare Tunnel
105-
# Description: Cloudflare Tunnel agent
104+
# Short-Description: cloudflared
105+
# Description: cloudflared agent
106106
### END INIT INFO
107107
name=$(basename $(readlink -f $0))
108108
cmd="{{.Path}} --pidfile /var/run/$name.pid --autoupdate-freq 24h0m0s{{ range .ExtraArgs }} {{ . }}{{ end }}"
@@ -212,11 +212,16 @@ func installLinuxService(c *cli.Context) error {
212212
switch {
213213
case isSystemd():
214214
log.Info().Msgf("Using Systemd")
215-
return installSystemd(&templateArgs, log)
215+
err = installSystemd(&templateArgs, log)
216216
default:
217217
log.Info().Msgf("Using SysV")
218-
return installSysv(&templateArgs, log)
218+
err = installSysv(&templateArgs, log)
219219
}
220+
221+
if err == nil {
222+
log.Info().Msg("Linux service for cloudflared installed successfully")
223+
}
224+
return err
220225
}
221226

222227
func buildArgsForConfig(c *cli.Context, log *zerolog.Logger) ([]string, error) {
@@ -271,7 +276,7 @@ func installSystemd(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) erro
271276
log.Err(err).Msg("systemctl start cloudflared-update.timer error")
272277
return err
273278
}
274-
log.Info().Msg("systemctl daemon-reload")
279+
log.Info().Msg("running systemctl daemon-reload")
275280
return runCommand("systemctl", "daemon-reload")
276281
}
277282

@@ -301,14 +306,20 @@ func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error {
301306
func uninstallLinuxService(c *cli.Context) error {
302307
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
303308

309+
var err error
304310
switch {
305311
case isSystemd():
306312
log.Info().Msg("Using Systemd")
307-
return uninstallSystemd(log)
313+
err = uninstallSystemd(log)
308314
default:
309315
log.Info().Msg("Using SysV")
310-
return uninstallSysv(log)
316+
err = uninstallSysv(log)
317+
}
318+
319+
if err == nil {
320+
log.Info().Msg("Linux service for cloudflared uninstalled successfully")
311321
}
322+
return err
312323
}
313324

314325
func uninstallSystemd(log *zerolog.Logger) error {
@@ -326,7 +337,6 @@ func uninstallSystemd(log *zerolog.Logger) error {
326337
return err
327338
}
328339
}
329-
log.Info().Msgf("Successfully uninstalled cloudflared service from systemd")
330340
return nil
331341
}
332342

@@ -345,6 +355,5 @@ func uninstallSysv(log *zerolog.Logger) error {
345355
continue
346356
}
347357
}
348-
log.Info().Msgf("Successfully uninstalled cloudflared service from sysv")
349358
return nil
350359
}

cmd/cloudflared/macos_service.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ const (
2121
func runApp(app *cli.App, graceShutdownC chan struct{}) {
2222
app.Commands = append(app.Commands, &cli.Command{
2323
Name: "service",
24-
Usage: "Manages the Cloudflare Tunnel launch agent",
24+
Usage: "Manages the cloudflared launch agent",
2525
Subcommands: []*cli.Command{
2626
{
2727
Name: "install",
28-
Usage: "Install Cloudflare Tunnel as an user launch agent",
28+
Usage: "Install cloudflared as an user launch agent",
2929
Action: cliutil.ConfiguredAction(installLaunchd),
3030
},
3131
{
3232
Name: "uninstall",
33-
Usage: "Uninstall the Cloudflare Tunnel launch agent",
33+
Usage: "Uninstall the cloudflared launch agent",
3434
Action: cliutil.ConfiguredAction(uninstallLaunchd),
3535
},
3636
},
@@ -114,12 +114,12 @@ func installLaunchd(c *cli.Context) error {
114114
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
115115

116116
if isRootUser() {
117-
log.Info().Msg("Installing Cloudflare Tunnel client as a system launch daemon. " +
118-
"Cloudflare Tunnel client will run at boot")
117+
log.Info().Msg("Installing cloudflared client as a system launch daemon. " +
118+
"cloudflared client will run at boot")
119119
} else {
120-
log.Info().Msg("Installing Cloudflare Tunnel client as an user launch agent. " +
121-
"Note that Cloudflare Tunnel client will only run when the user is logged in. " +
122-
"If you want to run Cloudflare Tunnel client at boot, install with root permission. " +
120+
log.Info().Msg("Installing cloudflared client as an user launch agent. " +
121+
"Note that cloudflared client will only run when the user is logged in. " +
122+
"If you want to run cloudflared client at boot, install with root permission. " +
123123
"For more information, visit https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/run-tunnel/run-as-service")
124124
}
125125
etPath, err := os.Executable()
@@ -163,16 +163,20 @@ func installLaunchd(c *cli.Context) error {
163163
}
164164

165165
log.Info().Msgf("Outputs are logged to %s and %s", stderrPath, stdoutPath)
166-
return runCommand("launchctl", "load", plistPath)
166+
err = runCommand("launchctl", "load", plistPath)
167+
if err == nil {
168+
log.Info().Msg("MacOS service for cloudflared installed successfully")
169+
}
170+
return err
167171
}
168172

169173
func uninstallLaunchd(c *cli.Context) error {
170174
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
171175

172176
if isRootUser() {
173-
log.Info().Msg("Uninstalling Cloudflare Tunnel as a system launch daemon")
177+
log.Info().Msg("Uninstalling cloudflared as a system launch daemon")
174178
} else {
175-
log.Info().Msg("Uninstalling Cloudflare Tunnel as an user launch agent")
179+
log.Info().Msg("Uninstalling cloudflared as a user launch agent")
176180
}
177181
installPath, err := installPath()
178182
if err != nil {
@@ -194,10 +198,13 @@ func uninstallLaunchd(c *cli.Context) error {
194198
}
195199
err = runCommand("launchctl", "unload", plistPath)
196200
if err != nil {
197-
log.Err(err).Msg("error unloading")
201+
log.Err(err).Msg("error unloading launchd")
198202
return err
199203
}
200204

201-
log.Info().Msgf("Outputs are logged to %s and %s", stderrPath, stdoutPath)
202-
return launchdTemplate.Remove()
205+
err = launchdTemplate.Remove()
206+
if err == nil {
207+
log.Info().Msg("Launchd for cloudflared was uninstalled successfully")
208+
}
209+
return err
203210
}

cmd/cloudflared/windows_service.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import (
2626

2727
const (
2828
windowsServiceName = "Cloudflared"
29-
windowsServiceDescription = "Cloudflare Tunnel agent"
30-
windowsServiceUrl = "https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/run-tunnel/run-as-service#windows"
29+
windowsServiceDescription = "Cloudflared agent"
30+
windowsServiceUrl = "https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/run-tunnel/as-a-service/windows/"
3131

3232
recoverActionDelay = time.Second * 20
3333
failureCountResetPeriod = time.Hour * 24
@@ -46,16 +46,16 @@ const (
4646
func runApp(app *cli.App, graceShutdownC chan struct{}) {
4747
app.Commands = append(app.Commands, &cli.Command{
4848
Name: "service",
49-
Usage: "Manages the Cloudflare Tunnel Windows service",
49+
Usage: "Manages the cloudflared Windows service",
5050
Subcommands: []*cli.Command{
5151
{
5252
Name: "install",
53-
Usage: "Install Cloudflare Tunnel as a Windows service",
53+
Usage: "Install cloudflared as a Windows service",
5454
Action: cliutil.ConfiguredAction(installWindowsService),
5555
},
5656
{
5757
Name: "uninstall",
58-
Usage: "Uninstall the Cloudflare Tunnel service",
58+
Usage: "Uninstall the cloudflared service",
5959
Action: cliutil.ConfiguredAction(uninstallWindowsService),
6060
},
6161
},
@@ -177,7 +177,7 @@ func (s *windowsService) Execute(serviceArgs []string, r <-chan svc.ChangeReques
177177
func installWindowsService(c *cli.Context) error {
178178
zeroLogger := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
179179

180-
zeroLogger.Info().Msg("Installing Cloudflare Tunnel Windows service")
180+
zeroLogger.Info().Msg("Installing cloudflared Windows service")
181181
exepath, err := os.Executable()
182182
if err != nil {
183183
return errors.Wrap(err, "Cannot find path name that start the process")
@@ -206,7 +206,7 @@ func installWindowsService(c *cli.Context) error {
206206
return errors.Wrap(err, "Cannot install service")
207207
}
208208
defer s.Close()
209-
log.Info().Msg("Cloudflare Tunnel agent service is installed")
209+
log.Info().Msg("cloudflared agent service is installed")
210210
err = eventlog.InstallAsEventCreate(windowsServiceName, eventlog.Error|eventlog.Warning|eventlog.Info)
211211
if err != nil {
212212
s.Delete()
@@ -219,38 +219,42 @@ func installWindowsService(c *cli.Context) error {
219219
log.Info().Msgf("See %s to manually configure service recovery actions", windowsServiceUrl)
220220
}
221221

222-
return s.Start()
222+
err = s.Start()
223+
if err == nil {
224+
log.Info().Msg("Agent service for cloudflared installed successfully")
225+
}
226+
return err
223227
}
224228

225229
func uninstallWindowsService(c *cli.Context) error {
226230
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog).
227231
With().
228232
Str(LogFieldWindowsServiceName, windowsServiceName).Logger()
229233

230-
log.Info().Msg("Uninstalling Cloudflare Tunnel Windows Service")
234+
log.Info().Msg("Uninstalling cloudflared agent service")
231235
m, err := mgr.Connect()
232236
if err != nil {
233237
return errors.Wrap(err, "Cannot establish a connection to the service control manager")
234238
}
235239
defer m.Disconnect()
236240
s, err := m.OpenService(windowsServiceName)
237241
if err != nil {
238-
return fmt.Errorf("Service %s is not installed", windowsServiceName)
242+
return fmt.Errorf("Agent service %s is not installed, so it could not be uninstalled", windowsServiceName)
239243
}
240244
defer s.Close()
241245

242246
if status, err := s.Query(); err == nil && status.State == svc.Running {
243-
log.Info().Msg("Stopping Cloudflare Tunnel agent service")
247+
log.Info().Msg("Stopping cloudflared agent service")
244248
if _, err := s.Control(svc.Stop); err != nil {
245-
log.Info().Err(err).Msg("Failed to stop Cloudflare Tunnel agent service, you may need to stop it manually to complete uninstall.")
249+
log.Info().Err(err).Msg("Failed to stop cloudflared agent service, you may need to stop it manually to complete uninstall.")
246250
}
247251
}
248252

249253
err = s.Delete()
250254
if err != nil {
251-
return errors.Wrap(err, "Cannot delete service")
255+
return errors.Wrap(err, "Cannot delete agent service")
252256
}
253-
log.Info().Msg("Cloudflare Tunnel agent service is uninstalled")
257+
log.Info().Msg("Agent service for cloudflared was uninstalled successfully")
254258
err = eventlog.Remove(windowsServiceName)
255259
if err != nil {
256260
return errors.Wrap(err, "Cannot remove event logger")

0 commit comments

Comments
 (0)