|
| 1 | +package x |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/docker/infrakit/pkg/discovery" |
| 9 | + "github.com/docker/infrakit/pkg/plugin" |
| 10 | + instance_rpc "github.com/docker/infrakit/pkg/rpc/instance" |
| 11 | + "github.com/docker/infrakit/pkg/spi/instance" |
| 12 | + "github.com/docker/infrakit/pkg/x/maxlife" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func maxlifeCommand(plugins func() discovery.Plugins) *cobra.Command { |
| 17 | + |
| 18 | + cmd := &cobra.Command{ |
| 19 | + Use: "maxlife <instance plugin name>...", |
| 20 | + Short: "Sets max life on the given instances", |
| 21 | + } |
| 22 | + |
| 23 | + //name := cmd.Flags().String("name", "", "Name to use as name of this plugin") |
| 24 | + poll := cmd.Flags().DurationP("poll", "i", 10*time.Second, "Polling interval") |
| 25 | + maxlifeDuration := cmd.Flags().DurationP("maxlife", "m", 10*time.Minute, "Max lifetime of the resource") |
| 26 | + flagTags := cmd.Flags().StringSliceP("tag", "t", []string{}, "Tags to filter instance by") |
| 27 | + |
| 28 | + cmd.RunE = func(c *cobra.Command, args []string) error { |
| 29 | + |
| 30 | + if len(args) == 0 { |
| 31 | + cmd.Usage() |
| 32 | + os.Exit(-1) |
| 33 | + } |
| 34 | + |
| 35 | + tags := toTags(*flagTags) |
| 36 | + |
| 37 | + // Now we have a list of instance plugins to maxlife |
| 38 | + plugins, err := getInstancePlugins(plugins, args) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + // For each we start a goroutine to poll and kill instances |
| 44 | + controllers := []*maxlife.Controller{} |
| 45 | + |
| 46 | + for name, plugin := range plugins { |
| 47 | + |
| 48 | + controller := maxlife.NewController(name, plugin, *poll, *maxlifeDuration, tags) |
| 49 | + controller.Start() |
| 50 | + |
| 51 | + controllers = append(controllers, controller) |
| 52 | + } |
| 53 | + |
| 54 | + // TODO - publish events when we start taking down instances. |
| 55 | + done := make(chan struct{}) |
| 56 | + |
| 57 | + <-done |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + return cmd |
| 62 | +} |
| 63 | + |
| 64 | +func ensureMaxlife(name string, plugin instance.Plugin, stop chan struct{}, poll, maxlife time.Duration, |
| 65 | + tags map[string]string, initialCount int) { |
| 66 | +} |
| 67 | +func getInstancePlugins(plugins func() discovery.Plugins, names []string) (map[string]instance.Plugin, error) { |
| 68 | + targets := map[string]instance.Plugin{} |
| 69 | + for _, target := range names { |
| 70 | + endpoint, err := plugins().Find(plugin.Name(target)) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + if p, err := instance_rpc.NewClient(plugin.Name(target), endpoint.Address); err == nil { |
| 75 | + targets[target] = p |
| 76 | + } else { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + } |
| 80 | + return targets, nil |
| 81 | +} |
| 82 | + |
| 83 | +func toTags(slice []string) map[string]string { |
| 84 | + tags := map[string]string{} |
| 85 | + |
| 86 | + for _, tag := range slice { |
| 87 | + kv := strings.SplitN(tag, "=", 2) |
| 88 | + if len(kv) != 2 { |
| 89 | + log.Warn("bad format tag", "input", tag) |
| 90 | + continue |
| 91 | + } |
| 92 | + key := strings.TrimSpace(kv[0]) |
| 93 | + val := strings.TrimSpace(kv[1]) |
| 94 | + if key != "" && val != "" { |
| 95 | + tags[key] = val |
| 96 | + } |
| 97 | + } |
| 98 | + return tags |
| 99 | +} |
0 commit comments