From 1cc12b0dd5f7cec2500823dae6f5d85896506ca9 Mon Sep 17 00:00:00 2001 From: "A.Unger" Date: Thu, 4 Jun 2020 13:43:03 +0200 Subject: [PATCH] add kill, list and run commands --- pkg/command/kill.go | 55 ++++++++++++++++++++++++++++++++++++++++++ pkg/command/list.go | 55 ++++++++++++++++++++++++++++++++++++++++++ pkg/command/run.go | 57 ++++++++++++++++++++++++++++++++++++++++++++ pkg/config/config.go | 3 +++ 4 files changed, 170 insertions(+) create mode 100644 pkg/command/kill.go create mode 100644 pkg/command/list.go create mode 100644 pkg/command/run.go diff --git a/pkg/command/kill.go b/pkg/command/kill.go new file mode 100644 index 000000000..19e56e5dd --- /dev/null +++ b/pkg/command/kill.go @@ -0,0 +1,55 @@ +package command + +import ( + "fmt" + "log" + "net" + "net/rpc" + "os" + + "github.com/micro/cli/v2" + "github.com/owncloud/ocis/pkg/config" + "github.com/owncloud/ocis/pkg/register" +) + +// KillCommand is the entrypoint for the accounts command. +func KillCommand(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "kill", + Usage: "Kill an extension by name", + Category: "Runtime", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "hostname", + Value: "localhost", + EnvVars: []string{"OCIS_RUNTIME_HOSTNAME"}, + Destination: &cfg.Runtime.Hostname, + }, + &cli.StringFlag{ + Name: "port", + Value: "10666", + EnvVars: []string{"OCIS_RUNTIME_PORT"}, + Destination: &cfg.Runtime.Port, + }, + }, + Action: func(c *cli.Context) error { + client, err := rpc.DialHTTP("tcp", net.JoinHostPort(cfg.Runtime.Hostname, cfg.Runtime.Port)) + if err != nil { + log.Fatal("dialing:", err) + } + + var arg1 int + + if err := client.Call("Service.Kill", os.Args[2], &arg1); err != nil { + log.Fatal(err) + } + fmt.Printf("process %v terminated", os.Args[2]) + + return nil + }, + } +} + +func init() { + register.AddCommand(KillCommand) +} diff --git a/pkg/command/list.go b/pkg/command/list.go new file mode 100644 index 000000000..b61925266 --- /dev/null +++ b/pkg/command/list.go @@ -0,0 +1,55 @@ +package command + +import ( + "fmt" + "log" + "net" + "net/rpc" + + "github.com/micro/cli/v2" + "github.com/owncloud/ocis/pkg/config" + "github.com/owncloud/ocis/pkg/register" +) + +// ListCommand is the entrypoint for the accounts command. +func ListCommand(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "list", + Usage: "Lists running ocis extensions", + Category: "Runtime", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "hostname", + Value: "localhost", + EnvVars: []string{"OCIS_RUNTIME_HOSTNAME"}, + Destination: &cfg.Runtime.Hostname, + }, + &cli.StringFlag{ + Name: "port", + Value: "10666", + EnvVars: []string{"OCIS_RUNTIME_PORT"}, + Destination: &cfg.Runtime.Port, + }, + }, + Action: func(c *cli.Context) error { + client, err := rpc.DialHTTP("tcp", net.JoinHostPort(cfg.Runtime.Hostname, cfg.Runtime.Port)) + if err != nil { + log.Fatal("dialing:", err) + } + + var arg1 string + + if err := client.Call("Service.List", struct{}{}, &arg1); err != nil { + log.Fatal(err) + } + + fmt.Println(arg1) + + return nil + }, + } +} + +func init() { + register.AddCommand(ListCommand) +} diff --git a/pkg/command/run.go b/pkg/command/run.go new file mode 100644 index 000000000..2c2b3d779 --- /dev/null +++ b/pkg/command/run.go @@ -0,0 +1,57 @@ +package command + +import ( + "fmt" + "log" + "net" + "net/rpc" + "os" + + "github.com/micro/cli/v2" + "github.com/owncloud/ocis/pkg/config" + "github.com/owncloud/ocis/pkg/register" + "github.com/refs/pman/pkg/process" +) + +// RunCommand is the entrypoint for the accounts command. +func RunCommand(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "run", + Usage: "Runs an extension", + Category: "Runtime", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "hostname", + Value: "localhost", + EnvVars: []string{"OCIS_RUNTIME_HOSTNAME"}, + Destination: &cfg.Runtime.Hostname, + }, + &cli.StringFlag{ + Name: "port", + Value: "10666", + EnvVars: []string{"OCIS_RUNTIME_PORT"}, + Destination: &cfg.Runtime.Port, + }, + }, + Action: func(c *cli.Context) error { + client, err := rpc.DialHTTP("tcp", net.JoinHostPort(cfg.Runtime.Hostname, cfg.Runtime.Port)) + if err != nil { + log.Fatal("dialing:", err) + } + + proc := process.NewProcEntry(os.Args[2], []string{os.Args[2]}...) + var res int + + if err := client.Call("Service.Start", proc, &res); err != nil { + log.Fatal(err) + } + + fmt.Println(res) + return nil + }, + } +} + +func init() { + register.AddCommand(RunCommand) +} diff --git a/pkg/config/config.go b/pkg/config/config.go index 63e80226d..41bba5e06 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -14,6 +14,7 @@ import ( settings "github.com/owncloud/ocis-settings/pkg/config" thumbnails "github.com/owncloud/ocis-thumbnails/pkg/config" webdav "github.com/owncloud/ocis-webdav/pkg/config" + pman "github.com/refs/pman/pkg/config" ) // Log defines the available logging configuration. @@ -73,6 +74,7 @@ type Config struct { Thumbnails *thumbnails.Config WebDAV *webdav.Config Settings *settings.Config + Runtime *pman.Config } // New initializes a new configuration with or without defaults. @@ -91,5 +93,6 @@ func New() *Config { Proxy: proxy.New(), Thumbnails: thumbnails.New(), Settings: settings.New(), + Runtime: pman.NewConfig(), } }