From 092e76eb55b996b4ee11e6574e64be6d6d9e22e4 Mon Sep 17 00:00:00 2001 From: Nuno Diegues Date: Fri, 25 Mar 2022 10:51:15 +0000 Subject: [PATCH] TUN-5954: Start cloudflared service in Linux too similarly to other OSs --- CHANGES.md | 6 +++++- cmd/cloudflared/linux_service.go | 32 +++++++++++++++++++++-------- cmd/cloudflared/service_template.go | 4 ++-- component-tests/test_service.py | 2 -- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 91da8fea..0a09668b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,10 +4,14 @@ achievable with: `cloudflared tunnel token --cred-file /path/to/file.json TUNNEL`. This new feature only works for Tunnels created with cloudflared version 2022.3.0 or more recent. +### Bug Fixes +- `cloudflared service install` now starts the underlying agent service on Linux operating system (similarly to the +behaviour in Windows and MacOS). + ## 2022.3.3 ### Bug Fixes - `cloudflared service install` now starts the underlying agent service on Windows operating system (similarly to the -behaviour in Linux and MacOS). +behaviour in MacOS). ## 2022.3.1 ### Bug Fixes diff --git a/cmd/cloudflared/linux_service.go b/cmd/cloudflared/linux_service.go index 4f0b53eb..cc379cdb 100644 --- a/cmd/cloudflared/linux_service.go +++ b/cmd/cloudflared/linux_service.go @@ -43,11 +43,12 @@ const ( serviceConfigFile = "config.yml" serviceCredentialFile = "cert.pem" serviceConfigPath = serviceConfigDir + "/" + serviceConfigFile + cloudflaredService = "cloudflared.service" ) var systemdTemplates = []ServiceTemplate{ { - Path: "/etc/systemd/system/cloudflared.service", + Path: fmt.Sprintf("/etc/systemd/system/%s", cloudflaredService), Content: `[Unit] Description=cloudflared After=network.target @@ -268,16 +269,19 @@ func installSystemd(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) erro return err } } - if err := runCommand("systemctl", "enable", "cloudflared.service"); err != nil { - log.Err(err).Msg("systemctl enable cloudflared.service error") + if err := runCommand("systemctl", "enable", cloudflaredService); err != nil { + log.Err(err).Msgf("systemctl enable %s error", cloudflaredService) return err } if err := runCommand("systemctl", "start", "cloudflared-update.timer"); err != nil { log.Err(err).Msg("systemctl start cloudflared-update.timer error") return err } - log.Info().Msg("running systemctl daemon-reload") - return runCommand("systemctl", "daemon-reload") + if err := runCommand("systemctl", "daemon-reload"); err != nil { + log.Err(err).Msg("systemctl daemon-reload error") + return err + } + return runCommand("systemctl", "start", cloudflaredService) } func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error { @@ -300,7 +304,7 @@ func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error { continue } } - return nil + return runCommand("service", "cloudflared", "start") } func uninstallLinuxService(c *cli.Context) error { @@ -323,8 +327,12 @@ func uninstallLinuxService(c *cli.Context) error { } func uninstallSystemd(log *zerolog.Logger) error { - if err := runCommand("systemctl", "disable", "cloudflared.service"); err != nil { - log.Err(err).Msg("systemctl disable cloudflared.service error") + if err := runCommand("systemctl", "disable", cloudflaredService); err != nil { + log.Err(err).Msgf("systemctl disable %s error", cloudflaredService) + return err + } + if err := runCommand("systemctl", "stop", cloudflaredService); err != nil { + log.Err(err).Msgf("systemctl stop %s error", cloudflaredService) return err } if err := runCommand("systemctl", "stop", "cloudflared-update.timer"); err != nil { @@ -337,10 +345,18 @@ func uninstallSystemd(log *zerolog.Logger) error { return err } } + if err := runCommand("systemctl", "daemon-reload"); err != nil { + log.Err(err).Msg("systemctl daemon-reload error") + return err + } return nil } func uninstallSysv(log *zerolog.Logger) error { + if err := runCommand("service", "cloudflared", "stop"); err != nil { + log.Err(err).Msg("service cloudflared stop error") + return err + } if err := sysvTemplate.Remove(); err != nil { log.Err(err).Msg("error removing service template") return err diff --git a/cmd/cloudflared/service_template.go b/cmd/cloudflared/service_template.go index ad7901d1..3040766f 100644 --- a/cmd/cloudflared/service_template.go +++ b/cmd/cloudflared/service_template.go @@ -95,10 +95,10 @@ func runCommand(command string, args ...string) error { return fmt.Errorf("error starting %s: %v", command, err) } - _, _ = ioutil.ReadAll(stderr) + output, _ := ioutil.ReadAll(stderr) err = cmd.Wait() if err != nil { - return fmt.Errorf("%s returned with error: %v", command, err) + return fmt.Errorf("%s %v returned with error code %v due to: %v", command, args, err, string(output)) } return nil } diff --git a/component-tests/test_service.py b/component-tests/test_service.py index 2e02db6b..f8ace641 100644 --- a/component-tests/test_service.py +++ b/component-tests/test_service.py @@ -141,12 +141,10 @@ class TestServiceMode: def sysv_service_scenario(self, config, tmp_path, extra_assertions=None, use_token=False): with self.run_service(tmp_path, config, root=True, use_token=use_token): - self.sysv_cmd("start") self.sysv_cmd("status") wait_tunnel_ready(tunnel_url=config.get_url()) if extra_assertions is not None: extra_assertions() - self.sysv_cmd("stop") # Service install copies config file to /etc/cloudflared/config.yml subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])