You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
subgraph-oz/oz/main.go

186 lines
3.6 KiB

package main
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/subgraph/oz/oz-daemon"
"github.com/subgraph/oz/oz-init"
10 years ago
"io"
"os"
"strconv"
)
func main() {
app := cli.NewApp()
app.Name = "oz"
app.Usage = "command line interface to Oz sandboxes"
app.Author = "Subgraph"
app.Email = "info@subgraph.com"
10 years ago
app.Commands = []cli.Command{
{
10 years ago
Name: "profiles",
Usage: "list available application profiles",
Action: handleProfiles,
},
{
10 years ago
Name: "launch",
Usage: "launch an application profile",
Action: handleLaunch,
},
{
10 years ago
Name: "list",
Usage: "list running sandboxes",
Action: handleList,
},
{
10 years ago
Name: "shell",
Usage: "start a shell in a running container",
Action: handleShell,
},
{
10 years ago
Name: "clean",
Action: handleClean,
},
{
Name: "kill",
Action: handleKill,
},
{
10 years ago
Name: "logs",
Action: handleLogs,
10 years ago
Flags: []cli.Flag{
cli.BoolFlag{
Name: "f",
},
},
},
}
app.Run(os.Args)
}
func handleProfiles(c *cli.Context) {
10 years ago
ps, err := daemon.ListProfiles()
if err != nil {
fmt.Printf("Error listing profiles: %v\n", err)
os.Exit(1)
}
10 years ago
for i, p := range ps {
fmt.Printf("%2d) %-30s %s\n", i+1, p.Name, p.Path)
}
}
func handleLaunch(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Println("Argument needed to launch command")
os.Exit(1)
}
err := daemon.Launch(c.Args()[0], os.Environ())
if err != nil {
fmt.Printf("launch command failed: %v\n", err)
}
}
func handleList(c *cli.Context) {
10 years ago
sboxes, err := daemon.ListSandboxes()
if err != nil {
fmt.Printf("Error listing running containers: %v\n", err)
os.Exit(1)
}
if len(sboxes) == 0 {
fmt.Println("No running containers")
return
}
for _, sb := range sboxes {
fmt.Printf("%2d) %s\n", sb.Id, sb.Profile)
}
}
func handleShell(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Println("Sandbox id argument needed")
os.Exit(1)
}
10 years ago
id, err := strconv.Atoi(c.Args()[0])
if err != nil {
fmt.Println("Sandbox id argument must be an integer")
os.Exit(1)
}
10 years ago
sb, err := getSandboxById(id)
if err != nil {
fmt.Printf("Error retrieving sandbox list: %v\n", err)
os.Exit(1)
}
if sb == nil {
fmt.Printf("No sandbox found with id = %d\n", id)
os.Exit(1)
}
term := os.Getenv("TERM")
10 years ago
fd, err := ozinit.RunShell(sb.Address, term)
if err != nil {
fmt.Printf("start shell command failed: %v\n", err)
os.Exit(1)
}
fmt.Println("Entering interactive shell?\n")
10 years ago
st, err := SetRawTerminal(0)
HandleResize(fd)
f := os.NewFile(uintptr(fd), "")
go io.Copy(f, os.Stdin)
io.Copy(os.Stdout, f)
RestoreTerminal(0, st)
fmt.Println("done..")
}
func getSandboxById(id int) (*daemon.SandboxInfo, error) {
10 years ago
sboxes, err := daemon.ListSandboxes()
if err != nil {
return nil, err
}
for _, sb := range sboxes {
if id == sb.Id {
10 years ago
return &sb, nil
}
}
return nil, nil
}
func handleClean(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Println("Need a profile to clean")
os.Exit(1)
}
err := daemon.Clean(c.Args()[0])
if err != nil {
fmt.Println("Clean failed:", err)
}
}
func handleKill(c *cli.Context) {
if len(c.Args()) == 0 {
fmt.Println("Need a sandbox id to kill\n")
os.Exit(1)
}
id, err := strconv.Atoi(c.Args()[0])
if err != nil {
fmt.Printf("Could not parse id value %s\n", c.Args()[0])
os.Exit(1)
}
if err := daemon.KillSandbox(id); err != nil {
fmt.Println("Kill command failed:", err)
}
}
func handleLogs(c *cli.Context) {
follow := c.Bool("f")
10 years ago
ch, err := daemon.Logs(0, follow)
if err != nil {
fmt.Println("Logs failed", err)
os.Exit(1)
}
for ll := range ch {
fmt.Println(ll)
}
}