@@ -6,7 +6,6 @@ package main
66import (
77 "fmt"
88 "os"
9- "path/filepath"
109
1110 "github.com/rs/zerolog"
1211 "github.com/urfave/cli/v2"
@@ -26,12 +25,6 @@ func runApp(app *cli.App, graceShutdownC chan struct{}) {
2625 Name : "install" ,
2726 Usage : "Install Cloudflare Tunnel as a system service" ,
2827 Action : cliutil .ConfiguredAction (installLinuxService ),
29- Flags : []cli.Flag {
30- & cli.BoolFlag {
31- Name : "legacy" ,
32- Usage : "Generate service file for non-named tunnels" ,
33- },
34- },
3528 },
3629 {
3730 Name : "uninstall" ,
@@ -62,7 +55,7 @@ After=network.target
6255[Service]
6356TimeoutStartSec=0
6457Type=notify
65- ExecStart={{ .Path }} --config /etc/cloudflared/config.yml -- no-autoupdate{{ range .ExtraArgs }} {{ . }}{{ end }}
58+ ExecStart={{ .Path }} --no-autoupdate{{ range .ExtraArgs }} {{ . }}{{ end }}
6659Restart=on-failure
6760RestartSec=5s
6861
@@ -112,7 +105,7 @@ var sysvTemplate = ServiceTemplate{
112105# Description: Cloudflare Tunnel agent
113106### END INIT INFO
114107name=$(basename $(readlink -f $0))
115- cmd="{{.Path}} --config /etc/cloudflared/config.yml -- pidfile /var/run/$name.pid --autoupdate-freq 24h0m0s{{ range .ExtraArgs }} {{ . }}{{ end }}"
108+ cmd="{{.Path}} --pidfile /var/run/$name.pid --autoupdate-freq 24h0m0s{{ range .ExtraArgs }} {{ . }}{{ end }}"
116109pid_file="/var/run/$name.pid"
117110stdout_log="/var/log/$name.log"
118111stderr_log="/var/log/$name.err"
@@ -191,27 +184,6 @@ func isSystemd() bool {
191184 return false
192185}
193186
194- func copyUserConfiguration (userConfigDir , userConfigFile , userCredentialFile string , log * zerolog.Logger ) error {
195- srcCredentialPath := filepath .Join (userConfigDir , userCredentialFile )
196- destCredentialPath := filepath .Join (serviceConfigDir , serviceCredentialFile )
197- if srcCredentialPath != destCredentialPath {
198- if err := copyCredential (srcCredentialPath , destCredentialPath ); err != nil {
199- return err
200- }
201- }
202-
203- srcConfigPath := filepath .Join (userConfigDir , userConfigFile )
204- destConfigPath := filepath .Join (serviceConfigDir , serviceConfigFile )
205- if srcConfigPath != destConfigPath {
206- if err := copyConfig (srcConfigPath , destConfigPath ); err != nil {
207- return err
208- }
209- log .Info ().Msgf ("Copied %s to %s" , srcConfigPath , destConfigPath )
210- }
211-
212- return nil
213- }
214-
215187func installLinuxService (c * cli.Context ) error {
216188 log := logger .CreateLoggerFromContext (c , logger .EnableTerminalLog )
217189
@@ -223,53 +195,20 @@ func installLinuxService(c *cli.Context) error {
223195 Path : etPath ,
224196 }
225197
226- if err := ensureConfigDirExists (serviceConfigDir ); err != nil {
227- return err
228- }
229- if c .Bool ("legacy" ) {
230- userConfigDir := filepath .Dir (c .String ("config" ))
231- userConfigFile := filepath .Base (c .String ("config" ))
232- userCredentialFile := config .DefaultCredentialFile
233- if err = copyUserConfiguration (userConfigDir , userConfigFile , userCredentialFile , log ); err != nil {
234- log .Err (err ).Msgf ("Failed to copy user configuration. Before running the service, ensure that %s contains two files, %s and %s" ,
235- serviceConfigDir , serviceCredentialFile , serviceConfigFile )
236- return err
237- }
238- templateArgs .ExtraArgs = []string {
239- "--origincert" , serviceConfigDir + "/" + serviceCredentialFile ,
240- }
198+ var extraArgsFunc func (c * cli.Context , log * zerolog.Logger ) ([]string , error )
199+ if c .NArg () == 0 {
200+ extraArgsFunc = buildArgsForConfig
241201 } else {
242- src , _ , err := config .ReadConfigFile (c , log )
243- if err != nil {
244- return err
245- }
246-
247- // can't use context because this command doesn't define "credentials-file" flag
248- configPresent := func (s string ) bool {
249- val , err := src .String (s )
250- return err == nil && val != ""
251- }
252- if src .TunnelID == "" || ! configPresent (tunnel .CredFileFlag ) {
253- return fmt .Errorf (`Configuration file %s must contain entries for the tunnel to run and its associated credentials:
254- tunnel: TUNNEL-UUID
255- credentials-file: CREDENTIALS-FILE
256- ` , src .Source ())
257- }
258- if src .Source () != serviceConfigPath {
259- if exists , err := config .FileExists (serviceConfigPath ); err != nil || exists {
260- return fmt .Errorf ("Possible conflicting configuration in %[1]s and %[2]s. Either remove %[2]s or run `cloudflared --config %[2]s service install`" , src .Source (), serviceConfigPath )
261- }
262-
263- if err := copyFile (src .Source (), serviceConfigPath ); err != nil {
264- return fmt .Errorf ("failed to copy %s to %s: %w" , src .Source (), serviceConfigPath , err )
265- }
266- }
202+ extraArgsFunc = buildArgsForToken
203+ }
267204
268- templateArgs . ExtraArgs = [] string {
269- "tunnel" , "run" ,
270- }
205+ extraArgs , err := extraArgsFunc ( c , log )
206+ if err != nil {
207+ return err
271208 }
272209
210+ templateArgs .ExtraArgs = extraArgs
211+
273212 switch {
274213 case isSystemd ():
275214 log .Info ().Msgf ("Using Systemd" )
@@ -280,6 +219,42 @@ credentials-file: CREDENTIALS-FILE
280219 }
281220}
282221
222+ func buildArgsForConfig (c * cli.Context , log * zerolog.Logger ) ([]string , error ) {
223+ if err := ensureConfigDirExists (serviceConfigDir ); err != nil {
224+ return nil , err
225+ }
226+
227+ src , _ , err := config .ReadConfigFile (c , log )
228+ if err != nil {
229+ return nil , err
230+ }
231+
232+ // can't use context because this command doesn't define "credentials-file" flag
233+ configPresent := func (s string ) bool {
234+ val , err := src .String (s )
235+ return err == nil && val != ""
236+ }
237+ if src .TunnelID == "" || ! configPresent (tunnel .CredFileFlag ) {
238+ return nil , fmt .Errorf (`Configuration file %s must contain entries for the tunnel to run and its associated credentials:
239+ tunnel: TUNNEL-UUID
240+ credentials-file: CREDENTIALS-FILE
241+ ` , src .Source ())
242+ }
243+ if src .Source () != serviceConfigPath {
244+ if exists , err := config .FileExists (serviceConfigPath ); err != nil || exists {
245+ return nil , fmt .Errorf ("Possible conflicting configuration in %[1]s and %[2]s. Either remove %[2]s or run `cloudflared --config %[2]s service install`" , src .Source (), serviceConfigPath )
246+ }
247+
248+ if err := copyFile (src .Source (), serviceConfigPath ); err != nil {
249+ return nil , fmt .Errorf ("failed to copy %s to %s: %w" , src .Source (), serviceConfigPath , err )
250+ }
251+ }
252+
253+ return []string {
254+ "--config" , "/etc/cloudflared/config.yml" , "tunnel" , "run" ,
255+ }, nil
256+ }
257+
283258func installSystemd (templateArgs * ServiceTemplateArgs , log * zerolog.Logger ) error {
284259 for _ , serviceTemplate := range systemdTemplates {
285260 err := serviceTemplate .Generate (templateArgs )
0 commit comments