|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + |
| 7 | + log "github.com/Sirupsen/logrus" |
| 8 | + "github.com/docker/infrakit/pkg/discovery" |
| 9 | + "github.com/docker/infrakit/pkg/manager" |
| 10 | + "github.com/docker/infrakit/pkg/plugin" |
| 11 | + "github.com/docker/infrakit/pkg/rpc/client" |
| 12 | + group_plugin "github.com/docker/infrakit/pkg/rpc/group" |
| 13 | + manager_rpc "github.com/docker/infrakit/pkg/rpc/manager" |
| 14 | + "github.com/docker/infrakit/pkg/spi/group" |
| 15 | + "github.com/docker/infrakit/pkg/template" |
| 16 | + "github.com/docker/infrakit/pkg/types" |
| 17 | + "github.com/spf13/cobra" |
| 18 | +) |
| 19 | + |
| 20 | +func managerCommand(plugins func() discovery.Plugins) *cobra.Command { |
| 21 | + |
| 22 | + var groupPlugin group.Plugin |
| 23 | + var groupPluginName string |
| 24 | + |
| 25 | + cmd := &cobra.Command{ |
| 26 | + Use: "manager", |
| 27 | + Short: "Access the manager", |
| 28 | + } |
| 29 | + cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error { |
| 30 | + |
| 31 | + // Scan for a manager |
| 32 | + pm, err := plugins().List() |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + for name, endpoint := range pm { |
| 38 | + |
| 39 | + rpcClient, err := client.New(endpoint.Address, manager.InterfaceSpec) |
| 40 | + if err == nil { |
| 41 | + |
| 42 | + m := manager_rpc.Adapt(rpcClient) |
| 43 | + |
| 44 | + isLeader, err := m.IsLeader() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + log.Infoln("Found manager", name, "is leader = ", isLeader) |
| 50 | + if isLeader { |
| 51 | + |
| 52 | + groupPlugin = group_plugin.Adapt(rpcClient) |
| 53 | + groupPluginName = name |
| 54 | + |
| 55 | + log.Infoln("Found manager as", name, "at", endpoint.Address) |
| 56 | + |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + return nil |
| 62 | + } |
| 63 | + |
| 64 | + commit := cobra.Command{ |
| 65 | + Use: "commit <template_URL>", |
| 66 | + Short: "commit a multi-group configuration, as specified by the URL", |
| 67 | + } |
| 68 | + pretend := commit.Flags().Bool("pretend", false, "Don't actually commit, only explain the commit") |
| 69 | + commit.RunE = func(cmd *cobra.Command, args []string) error { |
| 70 | + assertNotNil("no plugin", groupPlugin) |
| 71 | + |
| 72 | + if len(args) != 1 { |
| 73 | + cmd.Usage() |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + |
| 77 | + templateURL := args[0] |
| 78 | + |
| 79 | + log.Infof("Using %v for reading template\n", templateURL) |
| 80 | + engine, err := template.NewTemplate(templateURL, template.Options{ |
| 81 | + SocketDir: discovery.Dir(), |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + view, err := engine.Render(nil) |
| 87 | + if err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + log.Debugln(view) |
| 92 | + |
| 93 | + // Treat this as an Any and then convert |
| 94 | + any := types.AnyString(view) |
| 95 | + |
| 96 | + groups := []plugin.Spec{} |
| 97 | + err = any.Decode(&groups) |
| 98 | + if err != nil { |
| 99 | + log.Warningln("Error parsing the template for plugin specs.") |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + // Check the list of plugins |
| 104 | + for _, gp := range groups { |
| 105 | + endpoint, err := plugins().Find(gp.Plugin) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + // unmarshal the group spec |
| 111 | + spec := group.Spec{} |
| 112 | + if gp.Properties != nil { |
| 113 | + err = gp.Properties.Decode(&spec) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // TODO(chungers) -- we need to enforce and confirm the type of this. |
| 120 | + // Right now we assume the RPC endpoint is indeed a group. |
| 121 | + target, err := group_plugin.NewClient(endpoint.Address) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + plan, err := target.CommitGroup(spec, *pretend) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + fmt.Println("Group", spec.ID, "with plugin", gp.Plugin, "plan:", plan) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | + } |
| 136 | + |
| 137 | + inspect := cobra.Command{ |
| 138 | + Use: "inspect", |
| 139 | + Short: "inspect returns the plugin configurations known by the manager", |
| 140 | + } |
| 141 | + inspect.RunE = func(cmd *cobra.Command, args []string) error { |
| 142 | + assertNotNil("no plugin", groupPlugin) |
| 143 | + |
| 144 | + if len(args) != 0 { |
| 145 | + cmd.Usage() |
| 146 | + os.Exit(1) |
| 147 | + } |
| 148 | + |
| 149 | + specs, err := groupPlugin.InspectGroups() |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + // the format is pluing.Spec |
| 155 | + out := []plugin.Spec{} |
| 156 | + for _, spec := range specs { |
| 157 | + |
| 158 | + any, err := types.AnyValue(spec) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + out = append(out, plugin.Spec{ |
| 164 | + Plugin: plugin.Name(groupPluginName), |
| 165 | + Properties: any, |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + view, err := types.AnyValue(out) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + fmt.Println(view.String()) |
| 174 | + |
| 175 | + return nil |
| 176 | + } |
| 177 | + |
| 178 | + cmd.AddCommand(&commit, &inspect) |
| 179 | + |
| 180 | + return cmd |
| 181 | +} |
0 commit comments