@@ -2,7 +2,12 @@ package main
22
33import (
44 "fmt"
5+ "sync"
6+
57 "github.com/docker/infrakit/pkg/discovery"
8+ "github.com/docker/infrakit/pkg/launch"
9+ "github.com/docker/infrakit/pkg/launch/os"
10+ "github.com/docker/infrakit/pkg/template"
611 "github.com/spf13/cobra"
712)
813
@@ -13,7 +18,7 @@ func pluginCommand(plugins func() discovery.Plugins) *cobra.Command {
1318 Short : "Manage plugins" ,
1419 }
1520
16- ls := cobra.Command {
21+ ls := & cobra.Command {
1722 Use : "ls" ,
1823 Short : "List available plugins" ,
1924 }
@@ -34,7 +39,94 @@ func pluginCommand(plugins func() discovery.Plugins) *cobra.Command {
3439 return nil
3540 }
3641
37- cmd .AddCommand (& ls )
42+ start := & cobra.Command {
43+ Use : "start" ,
44+ Short : "Start named plugins. Args are a list of plugin names" ,
45+ }
46+
47+ configURL := start .Flags ().String ("config-url" , "" , "URL for the startup configs" )
48+ osExec := start .Flags ().Bool ("os" , false , "True to use os plugin binaries" )
49+ doWait := start .Flags ().BoolP ("wait" , "w" , false , "True to wait in the foreground; Ctrl-C to exit" )
50+
51+ start .RunE = func (c * cobra.Command , args []string ) error {
52+
53+ configTemplate , err := template .NewTemplate (* configURL , template.Options {
54+ SocketDir : discovery .Dir (),
55+ })
56+ if err != nil {
57+ return err
58+ }
59+
60+ view , err := configTemplate .Render (nil )
61+ if err != nil {
62+ return err
63+ }
64+
65+ configs := launch .Config ([]byte (view ))
66+
67+ parsedRules := []launch.Rule {}
68+ err = configs .Unmarshal (& parsedRules )
69+ if err != nil {
70+ return err
71+ }
72+
73+ monitors := []* launch.Monitor {}
74+
75+ if * osExec {
76+ exec , err := os .NewLauncher ()
77+ if err != nil {
78+ return err
79+ }
80+ monitors = append (monitors , launch .NewMonitor (exec , parsedRules ))
81+ }
82+
83+ input := []chan <- launch.StartPlugin {}
84+ for _ , m := range monitors {
85+ ch , err := m .Start ()
86+ if err != nil {
87+ return err
88+ }
89+ input = append (input , ch )
90+ }
91+
92+ var wait sync.WaitGroup
93+
94+ if * doWait {
95+ wait .Add (1 )
96+ }
97+
98+ // now start all the plugins
99+ for _ , plugin := range args {
100+ fmt .Println ("Starting up" , plugin )
101+
102+ wait .Add (1 )
103+
104+ for _ , ch := range input {
105+
106+ name := plugin
107+ ch <- launch.StartPlugin {
108+ Plugin : name ,
109+ Started : func (config * launch.Config ) {
110+ fmt .Println (name , "started." )
111+ wait .Done ()
112+ },
113+ Error : func (config * launch.Config , err error ) {
114+ fmt .Println ("Error starting" , name , "err=" , err )
115+ wait .Done ()
116+ },
117+ }
118+ }
119+ }
120+
121+ wait .Wait () // wait for everyone to complete
122+
123+ for _ , monitor := range monitors {
124+ monitor .Stop ()
125+ }
126+ return nil
127+ }
128+
129+ cmd .AddCommand (ls , start )
38130
39131 return cmd
40132}
0 commit comments