Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
add launch command
Browse files Browse the repository at this point in the history
  • Loading branch information
jgillich committed Apr 23, 2017
1 parent 372193c commit f36b3c6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 25 deletions.
10 changes: 0 additions & 10 deletions command/config.go
Expand Up @@ -7,8 +7,6 @@ import (

"gopkg.in/urfave/cli.v2"

"os/user"

yaml "gopkg.in/yaml.v2"
)

Expand All @@ -29,14 +27,6 @@ func ParseConfig(c *cli.Context) *Config {
}

func (c *Config) save(path string) error {
if path[:2] == "~/" {
u, err := user.Current()
if err != nil {
return err
}
path = filepath.Join(u.HomeDir, path[2:])
}

d, err := yaml.Marshal(c)
if err != nil {
return err
Expand Down
21 changes: 21 additions & 0 deletions command/delete.go
@@ -0,0 +1,21 @@
package command

import (
"fmt"

"gitlab.com/faststack/machinestack/client"
"gopkg.in/urfave/cli.v2"
)

// Delete deletes a machine
func Delete(c *cli.Context) error {
name := c.Args().Get(0)
client := client.New(c.String("machinestack"), c.String("token"))
if err := client.MachineDelete(name); err != nil {
return err
}

fmt.Printf("Machine '%v' was deleted.", name)

return nil
}
43 changes: 43 additions & 0 deletions command/launch.go
@@ -0,0 +1,43 @@
package command

import (
"os"

"gitlab.com/faststack/machinestack/client"
"gitlab.com/faststack/machinestack/model"
"gopkg.in/urfave/cli.v2"
)

// Launch launches a new machine
func Launch(c *cli.Context) error {
machine := model.Machine{
Image: c.Args().Get(0),
Name: c.String("name"),
Driver: "lxd",
}

client := client.New(c.String("machinestack"), c.String("token"))

if err := client.MachineCreate(&machine); err != nil {
return err
}

sessionID, err := client.SessionCreate(machine.Name)
if err != nil {
return err
}

if !c.Bool("noattach") {
if err := client.SessionIO(sessionID, os.Stdin, os.Stdout); err != nil {
return err
}
}

if c.Bool("rm") {
if err := client.MachineDelete(machine.Name); err != nil {
return err
}
}

return nil
}
7 changes: 0 additions & 7 deletions command/list.go
Expand Up @@ -7,15 +7,8 @@ import (

"github.com/olekukonko/tablewriter"
"gitlab.com/faststack/machinestack/client"
"gitlab.com/faststack/machinestack/model"
)

// ListResponse is the data format of a list call
// TODO move to machinestack
type ListResponse struct {
Data []model.Machine
}

// List lists all machines
func List(c *cli.Context) error {
client := client.New(c.String("machinestack"), c.String("token"))
Expand Down
2 changes: 2 additions & 0 deletions command/login.go
Expand Up @@ -40,5 +40,7 @@ func Login(c *cli.Context) error {
return err
}

fmt.Printf("Logged in as '%v'.", username)

return nil
}
4 changes: 2 additions & 2 deletions command/exec.go → command/shell.go
Expand Up @@ -7,8 +7,8 @@ import (
"gopkg.in/urfave/cli.v2"
)

// Exec executes a command and attaches to its tty
func Exec(c *cli.Context) error {
// Shell executes a command and attaches to its tty
func Shell(c *cli.Context) error {
name := c.Args().Get(0)

client := client.New(c.String("machinestack"), c.String("token"))
Expand Down
36 changes: 30 additions & 6 deletions fastastack.go
Expand Up @@ -43,19 +43,43 @@ func main() {
},
Commands: []*cli.Command{
{
Name: "login",
Usage: "Login with your username and password",
Action: command.Login,
Name: "launch",
Usage: "Launch a new machine",
Action: command.Launch,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Usage: "Set the name of the machine",
},
&cli.BoolFlag{
Name: "rm",
Usage: "Delete machine at the end of the session",
},
&cli.BoolFlag{
Name: "noattach",
Usage: "Do not attach to the machine",
},
},
},
{
Name: "list",
Usage: "List your machines",
Action: command.List,
},
{
Name: "exec",
Usage: "Execute a command",
Action: command.Exec,
Name: "login",
Usage: "Login with your username and password",
Action: command.Login,
},
{
Name: "shell",
Usage: "Attach to a terminal session",
Action: command.Shell,
},
{
Name: "delete",
Usage: "Delete machine",
Action: command.Delete,
},
},
}
Expand Down

0 comments on commit f36b3c6

Please sign in to comment.