mirror of
https://github.com/rancher/os.git
synced 2025-08-31 06:11:12 +00:00
Add addon to rancherctl
This commit is contained in:
100
cmd/control/addon.go
Normal file
100
cmd/control/addon.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/rancherio/os/config"
|
||||
"github.com/rancherio/os/util"
|
||||
)
|
||||
|
||||
func addonSubCommands() []cli.Command {
|
||||
return []cli.Command{
|
||||
{
|
||||
Name: "enable",
|
||||
Usage: "turn on a module",
|
||||
Action: enable,
|
||||
},
|
||||
{
|
||||
Name: "disable",
|
||||
Usage: "turn off a module",
|
||||
Action: disable,
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Usage: "list modules and state",
|
||||
Action: list,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func disable(c *cli.Context) {
|
||||
changed := false
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, addon := range c.Args() {
|
||||
filtered := make([]string, 0, len(c.Args()))
|
||||
for _, existing := range cfg.EnabledAddons {
|
||||
if existing != addon {
|
||||
filtered = append(filtered, existing)
|
||||
}
|
||||
}
|
||||
|
||||
if len(filtered) != len(c.Args()) {
|
||||
cfg.EnabledAddons = filtered
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
if changed {
|
||||
if err = cfg.Save(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func enable(c *cli.Context) {
|
||||
changed := false
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, addon := range c.Args() {
|
||||
if _, ok := cfg.Addons[addon]; ok && !util.Contains(cfg.EnabledAddons, addon) {
|
||||
cfg.EnabledAddons = append(cfg.EnabledAddons, addon)
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
|
||||
if changed {
|
||||
if err = cfg.Save(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func list(c *cli.Context) {
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
enabled := map[string]bool{}
|
||||
|
||||
for _, addon := range cfg.EnabledAddons {
|
||||
enabled[addon] = true
|
||||
}
|
||||
|
||||
for addon, _ := range cfg.Addons {
|
||||
if _, ok := enabled[addon]; ok {
|
||||
fmt.Printf("%s enabled\n", addon)
|
||||
} else {
|
||||
fmt.Printf("%s disabled\n", addon)
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,6 +15,7 @@ func Main() {
|
||||
app.Version = config.VERSION
|
||||
app.Author = "Rancher Labs, Inc."
|
||||
app.Email = "darren@rancher.com"
|
||||
app.EnableBashCompletion = true
|
||||
|
||||
app.Commands = []cli.Command{
|
||||
{
|
||||
@@ -24,24 +25,17 @@ func Main() {
|
||||
Subcommands: configSubcommands(),
|
||||
},
|
||||
{
|
||||
Name: "module",
|
||||
ShortName: "m",
|
||||
Usage: "module settings",
|
||||
Subcommands: []cli.Command{
|
||||
{
|
||||
Name: "activate",
|
||||
Usage: "turn on a module and possibly reboot",
|
||||
},
|
||||
{
|
||||
Name: "deactivate",
|
||||
Usage: "turn off a module and possibly reboot",
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Usage: "list modules and state",
|
||||
},
|
||||
},
|
||||
Name: "addon",
|
||||
ShortName: "a",
|
||||
Usage: "addon settings",
|
||||
Subcommands: addonSubCommands(),
|
||||
},
|
||||
//{
|
||||
// Name: "reload",
|
||||
// ShortName: "a",
|
||||
// Usage: "reload configuration of a service and restart the container",
|
||||
// Action: reload,
|
||||
//},
|
||||
{
|
||||
Name: "os",
|
||||
Usage: "operating system upgrade/downgrade",
|
||||
|
53
cmd/control/reload.go
Normal file
53
cmd/control/reload.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package control
|
||||
|
||||
import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
"github.com/codegangsta/cli"
|
||||
"github.com/rancherio/os/config"
|
||||
"github.com/rancherio/os/docker"
|
||||
)
|
||||
|
||||
func parseContainers(cfg *config.Config) map[string]*docker.Container {
|
||||
result := map[string]*docker.Container{}
|
||||
|
||||
for _, containerConfig := range cfg.SystemContainers {
|
||||
container := docker.NewContainer(config.DOCKER_SYSTEM_HOST, &containerConfig)
|
||||
if containerConfig.Id != "" {
|
||||
result[containerConfig.Id] = container
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func reload(c *cli.Context) {
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
containers := parseContainers(cfg)
|
||||
toStart := make([]*docker.Container, 0, len(c.Args()))
|
||||
|
||||
for _, id := range c.Args() {
|
||||
if container, ok := containers[id]; ok {
|
||||
toStart = append(toStart, container.Stage())
|
||||
}
|
||||
}
|
||||
|
||||
var firstErr error
|
||||
for _, c := range toStart {
|
||||
err := c.Start().Err
|
||||
if err != nil {
|
||||
log.Errorf("Failed to start %s : %v", c.ContainerCfg.Id, err)
|
||||
if firstErr != nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if firstErr != nil {
|
||||
log.Fatal(firstErr)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user