Browse code

Merge pull request #36653 from thaJeztah/refactor-windows-service-code

Split daemon service code to _windows file

Akihiro Suda authored on 2018/03/22 21:14:17
Showing 5 changed files
... ...
@@ -104,10 +104,6 @@ func allocateDaemonPort(addr string) error {
104 104
 	return nil
105 105
 }
106 106
 
107
-// notifyShutdown is called after the daemon shuts down but before the process exits.
108
-func notifyShutdown(err error) {
109
-}
110
-
111 107
 func wrapListeners(proto string, ls []net.Listener) []net.Listener {
112 108
 	switch proto {
113 109
 	case "unix":
... ...
@@ -3,7 +3,6 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"os"
6
-	"path/filepath"
7 6
 	"runtime"
8 7
 
9 8
 	"github.com/docker/docker/cli"
... ...
@@ -25,6 +24,10 @@ func newDaemonCommand() *cobra.Command {
25 25
 		SilenceErrors: true,
26 26
 		Args:          cli.NoArgs,
27 27
 		RunE: func(cmd *cobra.Command, args []string) error {
28
+			if opts.version {
29
+				showVersion()
30
+				return nil
31
+			}
28 32
 			opts.flags = cmd.Flags()
29 33
 			return runDaemon(opts)
30 34
 		},
... ...
@@ -41,45 +44,6 @@ func newDaemonCommand() *cobra.Command {
41 41
 	return cmd
42 42
 }
43 43
 
44
-func runDaemon(opts *daemonOptions) error {
45
-	if opts.version {
46
-		showVersion()
47
-		return nil
48
-	}
49
-
50
-	daemonCli := NewDaemonCli()
51
-
52
-	// Windows specific settings as these are not defaulted.
53
-	if runtime.GOOS == "windows" {
54
-		if opts.daemonConfig.Pidfile == "" {
55
-			opts.daemonConfig.Pidfile = filepath.Join(opts.daemonConfig.Root, "docker.pid")
56
-		}
57
-		if opts.configFile == "" {
58
-			opts.configFile = filepath.Join(opts.daemonConfig.Root, `config\daemon.json`)
59
-		}
60
-	}
61
-
62
-	// On Windows, this may be launching as a service or with an option to
63
-	// register the service.
64
-	stop, runAsService, err := initService(daemonCli)
65
-	if err != nil {
66
-		logrus.Fatal(err)
67
-	}
68
-
69
-	if stop {
70
-		return nil
71
-	}
72
-
73
-	// If Windows SCM manages the service - no need for PID files
74
-	if runAsService {
75
-		opts.daemonConfig.Pidfile = ""
76
-	}
77
-
78
-	err = daemonCli.start(opts)
79
-	notifyShutdown(err)
80
-	return err
81
-}
82
-
83 44
 func showVersion() {
84 45
 	fmt.Printf("Docker version %s, build %s\n", dockerversion.Version, dockerversion.GitCommit)
85 46
 }
86 47
new file mode 100644
... ...
@@ -0,0 +1,8 @@
0
+// +build !windows
1
+
2
+package main
3
+
4
+func runDaemon(opts *daemonOptions) error {
5
+	daemonCli := NewDaemonCli()
6
+	return daemonCli.start(opts)
7
+}
... ...
@@ -1,5 +1,38 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"path/filepath"
5
+
4 6
 	_ "github.com/docker/docker/autogen/winresources/dockerd"
7
+	"github.com/sirupsen/logrus"
5 8
 )
9
+
10
+func runDaemon(opts *daemonOptions) error {
11
+	daemonCli := NewDaemonCli()
12
+
13
+	// On Windows, this may be launching as a service or with an option to
14
+	// register the service.
15
+	stop, runAsService, err := initService(daemonCli)
16
+	if err != nil {
17
+		logrus.Fatal(err)
18
+	}
19
+
20
+	if stop {
21
+		return nil
22
+	}
23
+
24
+	// Windows specific settings as these are not defaulted.
25
+	if opts.configFile == "" {
26
+		opts.configFile = filepath.Join(opts.daemonConfig.Root, `config\daemon.json`)
27
+	}
28
+	if runAsService {
29
+		// If Windows SCM manages the service - no need for PID files
30
+		opts.daemonConfig.Pidfile = ""
31
+	} else if opts.daemonConfig.Pidfile == "" {
32
+		opts.daemonConfig.Pidfile = filepath.Join(opts.daemonConfig.Root, "docker.pid")
33
+	}
34
+
35
+	err = daemonCli.start(opts)
36
+	notifyShutdown(err)
37
+	return err
38
+}
... ...
@@ -6,9 +6,5 @@ import (
6 6
 	"github.com/spf13/pflag"
7 7
 )
8 8
 
9
-func initService(daemonCli *DaemonCli) (bool, bool, error) {
10
-	return false, false, nil
11
-}
12
-
13 9
 func installServiceFlags(flags *pflag.FlagSet) {
14 10
 }