Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into bootstrapping-and-resources
Browse files Browse the repository at this point in the history
Signed-off-by: Noah Treuhaft <noah.treuhaft@docker.com>
  • Loading branch information
nwt committed Jan 23, 2017
2 parents 290be1b + 93f6892 commit 5a52ab3
Show file tree
Hide file tree
Showing 41 changed files with 704 additions and 182 deletions.
7 changes: 5 additions & 2 deletions cmd/cli/flavor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ func flavorPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
return err
}

flavorPlugin = flavor_plugin.NewClient(plugin.Name(*name), endpoint.Address)

p, err := flavor_plugin.NewClient(plugin.Name(*name), endpoint.Address)
if err != nil {
return err
}
flavorPlugin = p
return nil
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/cli/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ func groupPluginCommand(plugins func() discovery.Plugins) *cobra.Command {
return err
}

groupPlugin = group_plugin.NewClient(endpoint.Address)

p, err := group_plugin.NewClient(endpoint.Address)
if err != nil {
return err
}
groupPlugin = p
return nil
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/cli/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ func instancePluginCommand(plugins func() discovery.Plugins) *cobra.Command {
return err
}

instancePlugin = instance_plugin.NewClient(plugin.Name(*name), endpoint.Address)

p, err := instance_plugin.NewClient(plugin.Name(*name), endpoint.Address)
if err != nil {
return err
}
instancePlugin = p
return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
cmd.AddCommand(cli.VersionCommand(), cli.InfoCommand(f))

cmd.AddCommand(templateCommand(f))
cmd.AddCommand(managerCommand(f))
cmd.AddCommand(pluginCommand(f), instancePluginCommand(f), groupPluginCommand(f), flavorPluginCommand(f))

err := cmd.Execute()
Expand Down
181 changes: 181 additions & 0 deletions cmd/cli/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package main

import (
"fmt"
"os"

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/manager"
"github.com/docker/infrakit/pkg/plugin"
"github.com/docker/infrakit/pkg/rpc/client"
group_plugin "github.com/docker/infrakit/pkg/rpc/group"
manager_rpc "github.com/docker/infrakit/pkg/rpc/manager"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/template"
"github.com/docker/infrakit/pkg/types"
"github.com/spf13/cobra"
)

func managerCommand(plugins func() discovery.Plugins) *cobra.Command {

var groupPlugin group.Plugin
var groupPluginName string

cmd := &cobra.Command{
Use: "manager",
Short: "Access the manager",
}
cmd.PersistentPreRunE = func(c *cobra.Command, args []string) error {

// Scan for a manager
pm, err := plugins().List()
if err != nil {
return err
}

for name, endpoint := range pm {

rpcClient, err := client.New(endpoint.Address, manager.InterfaceSpec)
if err == nil {

m := manager_rpc.Adapt(rpcClient)

isLeader, err := m.IsLeader()
if err != nil {
return err
}

log.Infoln("Found manager", name, "is leader = ", isLeader)
if isLeader {

groupPlugin = group_plugin.Adapt(rpcClient)
groupPluginName = name

log.Infoln("Found manager as", name, "at", endpoint.Address)

break
}
}
}
return nil
}

commit := cobra.Command{
Use: "commit <template_URL>",
Short: "commit a multi-group configuration, as specified by the URL",
}
pretend := commit.Flags().Bool("pretend", false, "Don't actually commit, only explain the commit")
commit.RunE = func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)

if len(args) != 1 {
cmd.Usage()
os.Exit(1)
}

templateURL := args[0]

log.Infof("Using %v for reading template\n", templateURL)
engine, err := template.NewTemplate(templateURL, template.Options{
SocketDir: discovery.Dir(),
})
if err != nil {
return err
}
view, err := engine.Render(nil)
if err != nil {
return err
}

log.Debugln(view)

// Treat this as an Any and then convert
any := types.AnyString(view)

groups := []plugin.Spec{}
err = any.Decode(&groups)
if err != nil {
log.Warningln("Error parsing the template for plugin specs.")
return err
}

// Check the list of plugins
for _, gp := range groups {
endpoint, err := plugins().Find(gp.Plugin)
if err != nil {
return err
}

// unmarshal the group spec
spec := group.Spec{}
if gp.Properties != nil {
err = gp.Properties.Decode(&spec)
if err != nil {
return err
}
}

// TODO(chungers) -- we need to enforce and confirm the type of this.
// Right now we assume the RPC endpoint is indeed a group.
target, err := group_plugin.NewClient(endpoint.Address)
if err != nil {
return err
}

plan, err := target.CommitGroup(spec, *pretend)
if err != nil {
return err
}

fmt.Println("Group", spec.ID, "with plugin", gp.Plugin, "plan:", plan)
}

return nil
}

inspect := cobra.Command{
Use: "inspect",
Short: "inspect returns the plugin configurations known by the manager",
}
inspect.RunE = func(cmd *cobra.Command, args []string) error {
assertNotNil("no plugin", groupPlugin)

if len(args) != 0 {
cmd.Usage()
os.Exit(1)
}

specs, err := groupPlugin.InspectGroups()
if err != nil {
return err
}

// the format is pluing.Spec
out := []plugin.Spec{}
for _, spec := range specs {

any, err := types.AnyValue(spec)
if err != nil {
return err
}

out = append(out, plugin.Spec{
Plugin: plugin.Name(groupPluginName),
Properties: any,
})
}

view, err := types.AnyValue(out)
if err != nil {
return err
}
fmt.Println(view.String())

return nil
}

cmd.AddCommand(&commit, &inspect)

return cmd
}
74 changes: 73 additions & 1 deletion cmd/cli/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package main

import (
"fmt"
"io/ioutil"
sys_os "os"
"strconv"
"sync"
"syscall"

log "github.com/Sirupsen/logrus"
"github.com/docker/infrakit/pkg/discovery"
"github.com/docker/infrakit/pkg/launch"
"github.com/docker/infrakit/pkg/launch/os"
Expand Down Expand Up @@ -128,7 +133,74 @@ func pluginCommand(plugins func() discovery.Plugins) *cobra.Command {
return nil
}

cmd.AddCommand(ls, start)
stop := &cobra.Command{
Use: "stop",
Short: "Stop named plugins. Args are a list of plugin names. This assumes plugins are local processes and not containers managed by another daemon, like Docker or runc.",
}

all := stop.Flags().Bool("all", false, "True to stop all running plugins")
stop.RunE = func(c *cobra.Command, args []string) error {

allPlugins, err := plugins().List()
if err != nil {
return err
}

targets := args

if *all {
names := []string{}
for n := range allPlugins {
names = append(names, n)
}
targets = names
}

for _, n := range targets {

p, has := allPlugins[n]
if !has {
continue
}

if p.Protocol != "unix" {
log.Warningf("Plugin is not a local process", n)
continue
}

// TODO(chungers) -- here we
pidFile := p.Address + ".pid"

buff, err := ioutil.ReadFile(pidFile)
if err != nil {
log.Warningf("Cannot read PID file for %s: %s", n, pidFile)
continue
}

pid, err := strconv.Atoi(string(buff))
if err != nil {
log.Warningf("Cannot determine PID for %s from file: %s", n, pidFile)
continue
}

process, err := sys_os.FindProcess(pid)
if err != nil {
log.Warningf("Error finding process of plugin %s", n)
continue
}

log.Infoln("Stopping", n, "at PID=", pid)
if err := process.Signal(syscall.SIGTERM); err == nil {
process.Wait()
log.Infoln("Process for", n, "exited")
}

}

return nil
}

cmd.AddCommand(ls, start, stop)

return cmd
}
4 changes: 2 additions & 2 deletions cmd/group/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ func main() {
if err != nil {
return nil, err
}
return instance_client.NewClient(n, endpoint.Address), nil
return instance_client.NewClient(n, endpoint.Address)
}

flavorPluginLookup := func(n plugin.Name) (flavor.Plugin, error) {
endpoint, err := plugins.Find(n)
if err != nil {
return nil, err
}
return flavor_client.NewClient(n, endpoint.Address), nil
return flavor_client.NewClient(n, endpoint.Address)
}

cli.RunPlugin(*name, group_server.PluginServer(
Expand Down
3 changes: 2 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/infrakit/pkg/leader"
"github.com/docker/infrakit/pkg/manager"
group_rpc "github.com/docker/infrakit/pkg/rpc/group"
manager_rpc "github.com/docker/infrakit/pkg/rpc/manager"
"github.com/docker/infrakit/pkg/store"
"github.com/docker/infrakit/pkg/util/docker"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -77,7 +78,7 @@ func runMain(cfg config) error {
return err
}

cli.RunPlugin(cfg.id, group_rpc.PluginServer(mgr))
cli.RunPlugin(cfg.id, group_rpc.PluginServer(mgr), manager_rpc.PluginServer(mgr))

mgr.Stop()
log.Infoln("Manager stopped")
Expand Down
2 changes: 1 addition & 1 deletion examples/flavor/combo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
if err != nil {
return nil, err
}
return flavor_rpc.NewClient(n, endpoint.Address), nil
return flavor_rpc.NewClient(n, endpoint.Address)
}

cli.SetLogLevel(*logLevel)
Expand Down
Loading

0 comments on commit 5a52ab3

Please sign in to comment.