TUN-5954: Start cloudflared service in Linux too similarly to other OSs

This commit is contained in:
Nuno Diegues 2022-03-25 10:51:15 +00:00
parent 62e1330e45
commit 092e76eb55
4 changed files with 31 additions and 13 deletions

View File

@ -4,10 +4,14 @@
achievable with: `cloudflared tunnel token --cred-file /path/to/file.json TUNNEL`. This new feature only works for 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. 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 ## 2022.3.3
### Bug Fixes ### Bug Fixes
- `cloudflared service install` now starts the underlying agent service on Windows operating system (similarly to the - `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 ## 2022.3.1
### Bug Fixes ### Bug Fixes

View File

@ -43,11 +43,12 @@ const (
serviceConfigFile = "config.yml" serviceConfigFile = "config.yml"
serviceCredentialFile = "cert.pem" serviceCredentialFile = "cert.pem"
serviceConfigPath = serviceConfigDir + "/" + serviceConfigFile serviceConfigPath = serviceConfigDir + "/" + serviceConfigFile
cloudflaredService = "cloudflared.service"
) )
var systemdTemplates = []ServiceTemplate{ var systemdTemplates = []ServiceTemplate{
{ {
Path: "/etc/systemd/system/cloudflared.service", Path: fmt.Sprintf("/etc/systemd/system/%s", cloudflaredService),
Content: `[Unit] Content: `[Unit]
Description=cloudflared Description=cloudflared
After=network.target After=network.target
@ -268,16 +269,19 @@ func installSystemd(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) erro
return err return err
} }
} }
if err := runCommand("systemctl", "enable", "cloudflared.service"); err != nil { if err := runCommand("systemctl", "enable", cloudflaredService); err != nil {
log.Err(err).Msg("systemctl enable cloudflared.service error") log.Err(err).Msgf("systemctl enable %s error", cloudflaredService)
return err return err
} }
if err := runCommand("systemctl", "start", "cloudflared-update.timer"); err != nil { if err := runCommand("systemctl", "start", "cloudflared-update.timer"); err != nil {
log.Err(err).Msg("systemctl start cloudflared-update.timer error") log.Err(err).Msg("systemctl start cloudflared-update.timer error")
return err return err
} }
log.Info().Msg("running systemctl daemon-reload") if err := runCommand("systemctl", "daemon-reload"); err != nil {
return runCommand("systemctl", "daemon-reload") log.Err(err).Msg("systemctl daemon-reload error")
return err
}
return runCommand("systemctl", "start", cloudflaredService)
} }
func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error { func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error {
@ -300,7 +304,7 @@ func installSysv(templateArgs *ServiceTemplateArgs, log *zerolog.Logger) error {
continue continue
} }
} }
return nil return runCommand("service", "cloudflared", "start")
} }
func uninstallLinuxService(c *cli.Context) error { func uninstallLinuxService(c *cli.Context) error {
@ -323,8 +327,12 @@ func uninstallLinuxService(c *cli.Context) error {
} }
func uninstallSystemd(log *zerolog.Logger) error { func uninstallSystemd(log *zerolog.Logger) error {
if err := runCommand("systemctl", "disable", "cloudflared.service"); err != nil { if err := runCommand("systemctl", "disable", cloudflaredService); err != nil {
log.Err(err).Msg("systemctl disable cloudflared.service error") 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 return err
} }
if err := runCommand("systemctl", "stop", "cloudflared-update.timer"); err != nil { if err := runCommand("systemctl", "stop", "cloudflared-update.timer"); err != nil {
@ -337,10 +345,18 @@ func uninstallSystemd(log *zerolog.Logger) error {
return err return err
} }
} }
if err := runCommand("systemctl", "daemon-reload"); err != nil {
log.Err(err).Msg("systemctl daemon-reload error")
return err
}
return nil return nil
} }
func uninstallSysv(log *zerolog.Logger) error { 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 { if err := sysvTemplate.Remove(); err != nil {
log.Err(err).Msg("error removing service template") log.Err(err).Msg("error removing service template")
return err return err

View File

@ -95,10 +95,10 @@ func runCommand(command string, args ...string) error {
return fmt.Errorf("error starting %s: %v", command, err) return fmt.Errorf("error starting %s: %v", command, err)
} }
_, _ = ioutil.ReadAll(stderr) output, _ := ioutil.ReadAll(stderr)
err = cmd.Wait() err = cmd.Wait()
if err != nil { 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 return nil
} }

View File

@ -141,12 +141,10 @@ class TestServiceMode:
def sysv_service_scenario(self, config, tmp_path, extra_assertions=None, use_token=False): 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): with self.run_service(tmp_path, config, root=True, use_token=use_token):
self.sysv_cmd("start")
self.sysv_cmd("status") self.sysv_cmd("status")
wait_tunnel_ready(tunnel_url=config.get_url()) wait_tunnel_ready(tunnel_url=config.get_url())
if extra_assertions is not None: if extra_assertions is not None:
extra_assertions() extra_assertions()
self.sysv_cmd("stop")
# Service install copies config file to /etc/cloudflared/config.yml # Service install copies config file to /etc/cloudflared/config.yml
subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"]) subprocess.run(["sudo", "rm", "/etc/cloudflared/config.yml"])