Skip to content

Commit

Permalink
Merge pull request #20 from ibuildthecloud/changes
Browse files Browse the repository at this point in the history
Changes
  • Loading branch information
ibuildthecloud authored Oct 5, 2016
2 parents 94c6a3a + 033cce1 commit 2d705dd
Show file tree
Hide file tree
Showing 336 changed files with 5,605 additions and 881 deletions.
304 changes: 237 additions & 67 deletions cmd/catalog.go

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"bytes"
"errors"
"fmt"
"github.com/rancher/go-rancher/v2"
"io"
"os"
"os/exec"
"strings"
"syscall"
"text/template"

"github.com/rancher/go-rancher/v2"

"github.com/docker/docker/pkg/namesgenerator"
"github.com/urfave/cli"
)
Expand All @@ -31,7 +32,7 @@ func GetRawClient(ctx *cli.Context) (*client.RancherClient, error) {
return nil, err
}
return client.NewRancherClient(&client.ClientOpts{
Url: url + "/v1",
Url: url + "/v2-beta",
AccessKey: config.AccessKey,
SecretKey: config.SecretKey,
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func configSetup(ctx *cli.Context) error {
return err
}

if schema, ok := c.Schemas.CheckSchema("schema"); ok {
if schema, ok := c.GetSchemas().CheckSchema("schema"); ok {
// Normalize URL
config.URL = schema.Links["collection"]
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func runDockerWithOutput(hostname string, c *client.RancherClient, args []string
}

state := getHostState(host)
if state != "active" {
if state != "active" && state != "inactive" {
return fmt.Errorf("Can not contact host %s in state %s", hostname, state)
}

Expand Down
102 changes: 23 additions & 79 deletions cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,22 @@ func EnvCommand() cli.Command {
Flags: envLsFlags,
},
cli.Command{
Name: "create",
Usage: "Create an environment",
Description: "\nBy default, an environment with cattle orchestration framework will be created. This command only works for Account API keys.\n\nExample:\n\t$ rancher env create newEnv\n\t$ rancher env create -o kubernetes newK8sEnv\n\t$ rancher env create -o mesos newMesosEnv\n\t$ rancher env create -o swarm newSwarmEnv\n",
ArgsUsage: "[NEWENVNAME...]",
Action: envCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "orchestration,o",
Usage: "Orchestration framework",
},
},
Name: "create",
Usage: "Create an environment",
Description: `
By default, an environment with cattle orchestration framework will be created. This command only works for Account API keys.
Example:
$ rancher env create newEnv
To add an orchestration framework do TODO
$ rancher env create -o kubernetes newK8sEnv
$ rancher env create -o mesos newMesosEnv
$ rancher env create -o swarm newSwarmEnv
`,
ArgsUsage: "[NEWENVNAME...]",
Action: envCreate,
},
cli.Command{
Name: "rm",
Expand All @@ -57,19 +62,6 @@ func EnvCommand() cli.Command {
Action: envRm,
Flags: []cli.Flag{},
},
cli.Command{
Name: "update",
Usage: "Update environment",
Description: "\nChange the orchestration framework of the environment. This command only works for Account API keys.\n\nExample:\n\t$ rancher env update -o kubernetes 1a5\n\t$ rancher env update -o cattle Default\n\t$ rancher env update -o swarm 1a5\n\t$ rancher env update -o mesos 1a5\n",
ArgsUsage: "[ENVID ENVNAME...]",
Action: envUpdate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "orchestration,o",
Usage: "Orchestration framework",
},
},
},
cli.Command{
Name: "deactivate",
Usage: "Deactivate environment(s)",
Expand Down Expand Up @@ -103,27 +95,14 @@ Example:
}

type EnvData struct {
ID string
Environment *client.Project
Orchestration string
ID string
Environment *client.Project
}

func NewEnvData(project client.Project) *EnvData {
orch := "Cattle"

switch {
case project.Swarm:
orch = "Swarm"
case project.Mesos:
orch = "Mesos"
case project.Kubernetes:
orch = "Kubernetes"
}

return &EnvData{
ID: project.Id,
Environment: &project,
Orchestration: orch,
ID: project.Id,
Environment: &project,
}
}

Expand All @@ -138,40 +117,6 @@ func envRm(ctx *cli.Context) error {
})
}

func envUpdate(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
return err
}

if ctx.NArg() < 1 {
return cli.NewExitError("Environment name/id is required as the first argument", 1)
}

orch := ctx.String("orchestration")
if orch == "" {
return nil
}

env, err := LookupEnvironment(c, ctx.Args()[0])
if err != nil {
return err
}

data := map[string]interface{}{}
setFields(ctx, data)

var newEnv client.Project

err = c.Update("project", &env.Resource, data, &newEnv)
if err != nil {
return err
}

fmt.Println(env.Id)
return nil
}

func envCreate(ctx *cli.Context) error {
c, err := GetRawClient(ctx)
if err != nil {
Expand Down Expand Up @@ -221,15 +166,14 @@ func envLs(ctx *cli.Context) error {
writer := NewTableWriter([][]string{
{"ID", "ID"},
{"NAME", "Environment.Name"},
{"ORCHESTRATION", "Orchestration"},
{"ORCHESTRATION", "Environment.Orchestration"},
{"STATE", "Environment.State"},
{"CREATED", "Environment.Created"},
}, ctx)
defer writer.Close()

collection := client.ProjectCollection{}
listOpts := defaultListOpts(ctx)
if err = c.List("project", listOpts, &collection); err != nil {
collection, err := c.Project.List(defaultListOpts(ctx))
if err != nil {
return err
}

Expand Down
31 changes: 15 additions & 16 deletions cmd/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func HostCommand() cli.Command {
}

type HostsData struct {
ID string
Host client.Host
State string
IPAddresses []client.IpAddress
Labels string
ID string
Host client.Host
State string
ContainerCount int
Labels string
}

func getHostState(host *client.Host) string {
Expand Down Expand Up @@ -87,7 +87,9 @@ func hostLs(ctx *cli.Context) error {
return err
}

collection, err := c.Host.List(nil)
opts := defaultListOpts(ctx)
delete(opts.Filters, "state_ne")
collection, err := c.Host.List(opts)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,24 +127,21 @@ func hostLs(ctx *cli.Context) error {
{"ID", "Host.Id"},
{"HOSTNAME", "Host.Hostname"},
{"STATE", "State"},
{"IP", "{{ips .IPAddresses}}"},
{"CONTAINERS", "ContainerCount"},
{"IP", "Host.AgentIpAddress"},
{"LABELS", "Labels"},
{"DETAIL", "Host.TransitioningMessage"},
}, ctx)

defer writer.Close()

for _, item := range collection.Data {
ips := client.IpAddressCollection{}
// ignore errors getting IPs, machines don't have them
c.GetLink(item.Resource, "ipAddresses", &ips)

writer.Write(&HostsData{
ID: item.Id,
Host: item,
State: getHostState(&item),
Labels: getLabels(&item),
IPAddresses: ips.Data,
ID: item.Id,
Host: item,
State: getHostState(&item),
ContainerCount: len(item.InstanceIds),
Labels: getLabels(&item),
})
}

Expand Down
11 changes: 6 additions & 5 deletions cmd/host_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func hostCreate(ctx *cli.Context) error {
return err
}

machineSchema := c.Schemas.Schema("machine")
flags := buildFlags("", machineSchema, c.Schemas)
machineSchema := c.GetSchemas().Schema("machine")
flags := buildFlags("", machineSchema, c.GetSchemas())
drivers := []string{}

for name := range machineSchema.ResourceFields {
Expand All @@ -132,11 +132,12 @@ func hostCreate(ctx *cli.Context) error {
for i := range hostCommand.Subcommands {
if hostCommand.Subcommands[i].Name == "create" {
hostCommand.Subcommands[i].Flags = append(flags, cli.StringFlag{
Name: "driver,d",
Usage: "Driver to use: " + strings.Join(drivers, ", "),
Name: "driver,d",
Usage: "Driver to use: " + strings.Join(drivers, ", "),
EnvVar: "MACHINE_DRIVER",
})
hostCommand.Subcommands[i].Action = func(ctx *cli.Context) error {
return hostCreateRun(ctx, c, machineSchema, c.Schemas)
return hostCreateRun(ctx, c, machineSchema, c.GetSchemas())
}
hostCommand.Subcommands[i].SkipFlagParsing = false
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func servicePs(ctx *cli.Context) error {
{"NAME", "Name"},
{"IMAGE", "LaunchConfig.ImageUuid"},
{"STATE", "CombinedState"},
{"SCALE", "Service.Scale"},
{"SCALE", "{{len .Service.InstanceIds}}/{{.Service.Scale}}"},
{"ENDPOINTS", "{{endpoint .Service.PublicEndpoints}}"},
{"DETAIL", "Service.TransitioningMessage"},
}, ctx)
Expand Down
48 changes: 48 additions & 0 deletions cmd/questions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"bufio"
"fmt"
"os"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/rancher/go-rancher/catalog"
)

func askQuestions(answers map[string]interface{}, templateVersion catalog.TemplateVersion) (map[string]interface{}, error) {
result := map[string]interface{}{}
for _, q := range templateVersion.Questions {
question := catalog.Question{}
err := mapstructure.Decode(q, &question)
if err != nil {
return nil, err
}

if answer, ok := answers[question.Variable]; ok {
result[question.Variable] = answer
} else {
result[question.Variable] = askQuestion(question)
}
}
return result, nil
}

func askQuestion(question catalog.Question) interface{} {
if len(question.Description) > 0 {
fmt.Println(question.Description)
}
fmt.Printf("%s %s[%s]: ", question.Label, question.Variable, question.Default)

answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return ""
}

answer = strings.TrimSpace(answer)
if answer == "" {
answer = question.Default
}

return answer
}
9 changes: 8 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmd

import (
"github.com/rancher/go-rancher/v2"
"strings"

"github.com/rancher/go-rancher/v2"

"github.com/urfave/cli"
)

Expand Down Expand Up @@ -170,6 +171,11 @@ func RunCommand() cli.Command {
Usage: "Connect a container to a network: host, none, bridge, managed",
Value: "managed",
},
cli.IntFlag{
Name: "scale",
Usage: "Number of containers to run",
Value: 1,
},
},
}
}
Expand Down Expand Up @@ -270,6 +276,7 @@ func serviceRun(ctx *cli.Context) error {
StackId: stack.Id,
LaunchConfig: launchConfig,
StartOnCreate: true,
Scale: int64(ctx.Int("scale")),
}

service, err = c.Service.Create(service)
Expand Down
Loading

0 comments on commit 2d705dd

Please sign in to comment.