|
| 1 | +package init |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/docker/infrakit/pkg/cli" |
| 10 | + "github.com/docker/infrakit/pkg/discovery" |
| 11 | + "github.com/docker/infrakit/pkg/launch" |
| 12 | + logutil "github.com/docker/infrakit/pkg/log" |
| 13 | + "github.com/docker/infrakit/pkg/plugin" |
| 14 | + group_types "github.com/docker/infrakit/pkg/plugin/group/types" |
| 15 | + flavor_rpc "github.com/docker/infrakit/pkg/rpc/flavor" |
| 16 | + run "github.com/docker/infrakit/pkg/run/manager" |
| 17 | + group_kind "github.com/docker/infrakit/pkg/run/v0/group" |
| 18 | + manager_kind "github.com/docker/infrakit/pkg/run/v0/manager" |
| 19 | + "github.com/docker/infrakit/pkg/spi/group" |
| 20 | + "github.com/docker/infrakit/pkg/spi/instance" |
| 21 | + "github.com/docker/infrakit/pkg/types" |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +var log = logutil.New("module", "util/init") |
| 26 | + |
| 27 | +func startPlugins(plugins func() discovery.Plugins, services *cli.Services, |
| 28 | + configURL string, starts []string) (*run.Manager, error) { |
| 29 | + |
| 30 | + parsedRules := []launch.Rule{} |
| 31 | + |
| 32 | + if configURL != "" { |
| 33 | + buff, err := services.ProcessTemplate(configURL) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + view, err := services.ToJSON([]byte(buff)) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + configs := types.AnyBytes(view) |
| 42 | + err = configs.Decode(&parsedRules) |
| 43 | + if err != nil { |
| 44 | + return nil, err |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pluginManager, err := run.ManagePlugins(parsedRules, plugins, true, 5*time.Second) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + for _, arg := range starts { |
| 54 | + |
| 55 | + p := strings.Split(arg, "=") |
| 56 | + execName := "inproc" // default is to use inprocess goroutine for running plugins |
| 57 | + if len(p) > 1 { |
| 58 | + execName = p[1] |
| 59 | + } |
| 60 | + |
| 61 | + // the format is kind[:{plugin_name}][={os|inproc}] |
| 62 | + pp := strings.Split(p[0], ":") |
| 63 | + kind := pp[0] |
| 64 | + name := plugin.Name(kind) |
| 65 | + |
| 66 | + // This is some special case for the legacy setup (pre v0.6) |
| 67 | + switch kind { |
| 68 | + case manager_kind.Kind: |
| 69 | + name = plugin.Name(manager_kind.LookupName) |
| 70 | + case group_kind.Kind: |
| 71 | + name = plugin.Name(group_kind.LookupName) |
| 72 | + } |
| 73 | + // customized by user as override |
| 74 | + if len(pp) > 1 { |
| 75 | + name = plugin.Name(pp[1]) |
| 76 | + } |
| 77 | + |
| 78 | + log.Info("Launching", "kind", kind, "name", name) |
| 79 | + err = pluginManager.Launch(execName, kind, name, nil) |
| 80 | + if err != nil { |
| 81 | + log.Warn("failed to launch", "exec", execName, "kind", kind, "name", name) |
| 82 | + } |
| 83 | + } |
| 84 | + return pluginManager, nil |
| 85 | +} |
| 86 | + |
| 87 | +// Command returns the cobra command |
| 88 | +func Command(plugins func() discovery.Plugins) *cobra.Command { |
| 89 | + |
| 90 | + services := cli.NewServices(plugins) |
| 91 | + |
| 92 | + cmd := &cobra.Command{ |
| 93 | + Use: "init <groups template URL | - >", |
| 94 | + Short: "Generates the init script", |
| 95 | + } |
| 96 | + |
| 97 | + cmd.Flags().AddFlagSet(services.ProcessTemplateFlags) |
| 98 | + groupID := cmd.Flags().String("group-id", "", "Group ID") |
| 99 | + sequence := cmd.Flags().Uint("sequence", 0, "Sequence in the group") |
| 100 | + |
| 101 | + configURL := cmd.Flags().String("config-url", "", "URL for the startup configs") |
| 102 | + starts := cmd.Flags().StringSlice("start", []string{}, "start spec for plugin just like infrakit plugin start") |
| 103 | + |
| 104 | + debug := cmd.Flags().Bool("debug", false, "True to debug with lots of traces") |
| 105 | + |
| 106 | + cmd.RunE = func(c *cobra.Command, args []string) error { |
| 107 | + |
| 108 | + if len(args) != 1 { |
| 109 | + cmd.Usage() |
| 110 | + os.Exit(1) |
| 111 | + } |
| 112 | + |
| 113 | + if !*debug { |
| 114 | + logutil.Configure(&logutil.Options{ |
| 115 | + Level: 3, |
| 116 | + Stdout: false, |
| 117 | + Format: "term", |
| 118 | + CallFunc: true, |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + pluginManager, err := startPlugins(plugins, services, *configURL, *starts) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + defer func() { |
| 127 | + pluginManager.TerminateAll() |
| 128 | + pluginManager.WaitForAllShutdown() |
| 129 | + pluginManager.Stop() |
| 130 | + }() |
| 131 | + |
| 132 | + pluginManager.WaitStarting() |
| 133 | + |
| 134 | + input, err := services.ReadFromStdinIfElse( |
| 135 | + func() bool { return args[0] == "-" }, |
| 136 | + func() (string, error) { return services.ProcessTemplate(args[0]) }, |
| 137 | + services.ToJSON, |
| 138 | + ) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + // TODO - update the schema -- this matches the current Plugin/Properties schema |
| 144 | + type spec struct { |
| 145 | + Plugin plugin.Name |
| 146 | + Properties struct { |
| 147 | + ID group.ID |
| 148 | + Properties group_types.Spec |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + specs := []spec{} |
| 153 | + err = types.AnyString(input).Decode(&specs) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + var groupSpec *group_types.Spec |
| 159 | + for _, s := range specs { |
| 160 | + if string(s.Properties.ID) == *groupID { |
| 161 | + copy := s.Properties.Properties |
| 162 | + groupSpec = © |
| 163 | + break |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if groupSpec == nil { |
| 168 | + return fmt.Errorf("no such group: %v", *groupID) |
| 169 | + } |
| 170 | + |
| 171 | + // Get the flavor properties and use that to call the prepare of the Flavor to generate the init |
| 172 | + endpoint, err := plugins().Find(groupSpec.Flavor.Plugin) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + flavorPlugin, err := flavor_rpc.NewClient(groupSpec.Flavor.Plugin, endpoint.Address) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + |
| 182 | + cli.MustNotNil(flavorPlugin, "flavor plugin not found", "name", groupSpec.Flavor.Plugin.String()) |
| 183 | + |
| 184 | + instanceSpec := instance.Spec{} |
| 185 | + if lidLen := len(groupSpec.Allocation.LogicalIDs); lidLen > 0 { |
| 186 | + |
| 187 | + if int(*sequence) >= lidLen { |
| 188 | + return fmt.Errorf("out of bound sequence index: %v in %v", *sequence, groupSpec.Allocation.LogicalIDs) |
| 189 | + } |
| 190 | + |
| 191 | + lid := instance.LogicalID(groupSpec.Allocation.LogicalIDs[*sequence]) |
| 192 | + instanceSpec.LogicalID = &lid |
| 193 | + } |
| 194 | + |
| 195 | + instanceSpec, err = flavorPlugin.Prepare(groupSpec.Flavor.Properties, instanceSpec, |
| 196 | + groupSpec.Allocation, |
| 197 | + group_types.Index{Group: group.ID(*groupID), Sequence: *sequence}) |
| 198 | + |
| 199 | + if err != nil { |
| 200 | + return err |
| 201 | + } |
| 202 | + |
| 203 | + // Here the Init may contain template vars since in the evaluation of the manager / worker |
| 204 | + // init templates, we do not propapage the vars set in the command line here. |
| 205 | + // So we need to evaluate the entire Init as a template again. |
| 206 | + // TODO - this is really better addressed via some formal globally available var store/section |
| 207 | + // that is always available to the templates at the schema / document level. |
| 208 | + applied, err := services.ProcessTemplate("str://" + instanceSpec.Init) |
| 209 | + if err != nil { |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + fmt.Print(applied) |
| 214 | + |
| 215 | + return nil |
| 216 | + } |
| 217 | + |
| 218 | + return cmd |
| 219 | +} |
0 commit comments