11package util
22
33import (
4- "net/http"
5- "os"
6- "time"
7-
84 "github.com/docker/infrakit/cmd/cli/base"
95 "github.com/docker/infrakit/pkg/discovery"
106 "github.com/docker/infrakit/pkg/log"
11- "github.com/docker/infrakit/pkg/rpc/mux"
127 "github.com/spf13/cobra"
138)
149
@@ -18,82 +13,6 @@ func init() {
1813 base .Register (Command )
1914}
2015
21- func fileServerCommand (plugins func () discovery.Plugins ) * cobra.Command {
22- cmd := & cobra.Command {
23- Use : "fileserver <path>" ,
24- Short : "Fileserver over http" ,
25- }
26-
27- listen := cmd .Flags ().StringP ("listen" , "l" , ":8080" , "Listening port" )
28-
29- cmd .RunE = func (c * cobra.Command , args []string ) error {
30-
31- if len (args ) != 1 {
32- c .Usage ()
33- os .Exit (- 1 )
34- }
35-
36- logger .Info ("Starting file server" , "listen" , * listen )
37-
38- rootFS := args [0 ]
39- handler := http .FileServer (http .Dir (rootFS ))
40- return http .ListenAndServe (* listen , handler )
41- }
42-
43- return cmd
44- }
45-
46- func muxCommand (plugins func () discovery.Plugins ) * cobra.Command {
47- cmd := & cobra.Command {
48- Use : "mux" ,
49- Short : "API mux service" ,
50- }
51-
52- // http://www.speedguide.net/port.php?port=24864 - unassigned by IANA
53- listen := cmd .Flags ().StringP ("listen" , "l" , ":24864" , "Listening port" )
54- autoStop := cmd .Flags ().BoolP ("auto-stop" , "a" , false , "True to stop when no plugins are running" )
55- interval := cmd .Flags ().DurationP ("scan" , "s" , 1 * time .Minute , "Scan interval to check for plugins" )
56-
57- cmd .RunE = func (c * cobra.Command , args []string ) error {
58- logger .Info ("Starting mux server" , "listen" , * listen )
59- server , err := mux .NewServer (* listen , plugins )
60- if err != nil {
61- return err
62- }
63- defer server .Stop ()
64-
65- block := make (chan struct {})
66- // If the sockets are gone, then we can safely exit.
67- go func () {
68- checkNow := time .Tick (* interval )
69- for {
70- select {
71- case <- server .Wait ():
72- logger .Info ("server stopped" )
73- close (block )
74- return
75-
76- case <- checkNow :
77- if m , err := plugins ().List (); err == nil {
78- if len (m ) == 0 && * autoStop {
79- logger .Info ("scan found no plugins." )
80- close (block )
81- return
82- }
83- }
84-
85- }
86- }
87- }()
88-
89- <- block
90-
91- return nil
92- }
93-
94- return cmd
95- }
96-
9716// Command is the head of this module
9817func Command (plugins func () discovery.Plugins ) * cobra.Command {
9918
@@ -102,7 +21,7 @@ func Command(plugins func() discovery.Plugins) *cobra.Command {
10221 Short : "Utilties" ,
10322 }
10423
105- util .AddCommand (muxCommand (plugins ), fileServerCommand (plugins ))
24+ util .AddCommand (muxCommand (plugins ), fileServerCommand (plugins ), trackCommand ( plugins ) )
10625
10726 return util
10827}
0 commit comments