Compare commits
3 Commits
515e244592
...
df2b9e7624
| Author | SHA1 | Date | |
|---|---|---|---|
| df2b9e7624 | |||
| 46d1270648 | |||
| 5cbf066ccf |
@@ -122,6 +122,7 @@ func GetAllActions() []DocumentedAction {
|
||||
Printer{},
|
||||
TimeOut{},
|
||||
Command{},
|
||||
ShellScript{},
|
||||
Shutdown{},
|
||||
}
|
||||
}
|
||||
|
||||
119
actions/shell_script.go
Normal file
119
actions/shell_script.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package actions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"os"
|
||||
|
||||
"unknown.com/gokill/internal"
|
||||
)
|
||||
|
||||
type ShellScript struct {
|
||||
Path string `json:"path"`
|
||||
ActionChan ActionResultChan
|
||||
}
|
||||
|
||||
func isExecutableFile(path string) bool {
|
||||
fi, err := os.Lstat(path)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Test executing Shellscript Failed.")
|
||||
return false
|
||||
}
|
||||
|
||||
mode := fi.Mode()
|
||||
|
||||
//TODO: should check if current user can execute
|
||||
if mode&01111 == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c ShellScript) DryExecute() {
|
||||
fmt.Printf("Test Executing ShellScript:\n%s\n", c.Path)
|
||||
|
||||
_, err := os.Open(c.Path)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Test executing Shellscript Failed.")
|
||||
c.ActionChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
if !isExecutableFile(c.Path) {
|
||||
fmt.Println("Test executing Shellscript Failed.")
|
||||
c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path)
|
||||
return
|
||||
}
|
||||
|
||||
c.ActionChan <- nil
|
||||
}
|
||||
|
||||
func (c ShellScript) Execute() {
|
||||
if !isExecutableFile(c.Path) {
|
||||
fmt.Println("Test executing Shellscript Failed.")
|
||||
c.ActionChan <- fmt.Errorf("File is not executable: %s", c.Path)
|
||||
return
|
||||
}
|
||||
|
||||
cmd := exec.Command("/bin/sh", c.Path)
|
||||
|
||||
stdout, err := cmd.Output()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
c.ActionChan <- err
|
||||
}
|
||||
|
||||
fmt.Println(string(stdout[:]))
|
||||
c.ActionChan <- nil
|
||||
}
|
||||
|
||||
func CreateShellScript(config internal.ActionConfig, c ActionResultChan) (ShellScript, error) {
|
||||
result := ShellScript{}
|
||||
|
||||
err := json.Unmarshal(config.Options, &result)
|
||||
|
||||
if err != nil {
|
||||
return ShellScript{}, err
|
||||
}
|
||||
|
||||
if result.Path == "" {
|
||||
return ShellScript{}, internal.OptionMissingError{"path"}
|
||||
}
|
||||
|
||||
result.ActionChan = c
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (cc ShellScript) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
return CreateShellScript(config, c)
|
||||
}
|
||||
|
||||
func (p ShellScript) GetName() string {
|
||||
return "ShellScript"
|
||||
}
|
||||
|
||||
func (p ShellScript) GetDescription() string {
|
||||
return "Executes the given shell script."
|
||||
}
|
||||
|
||||
func (p ShellScript) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "ShellScript",
|
||||
"options": {
|
||||
"path": "/path/to/file.sh"
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p ShellScript) GetOptions() []internal.ConfigOption {
|
||||
return []internal.ConfigOption{
|
||||
{"path", "string", "path to script to execute", ""},
|
||||
}
|
||||
}
|
||||
@@ -3,23 +3,24 @@ package actions
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"encoding/json"
|
||||
|
||||
"unknown.com/gokill/internal"
|
||||
)
|
||||
|
||||
type Shutdown struct {
|
||||
Timeout string `json:"time"`
|
||||
ActionChan ActionResultChan
|
||||
}
|
||||
|
||||
func (s Shutdown) DryExecute() {
|
||||
fmt.Printf("shutdown -h %s\n", s.Timeout)
|
||||
fmt.Println("Test Shutdown executed...")
|
||||
|
||||
s.ActionChan <- nil
|
||||
|
||||
}
|
||||
|
||||
func (s Shutdown) Execute() {
|
||||
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
|
||||
if err := exec.Command("shutdown", "-h", s.Timeout).Run(); err != nil {
|
||||
fmt.Println("Failed to initiate shutdown:", err)
|
||||
}
|
||||
|
||||
@@ -29,7 +30,16 @@ func (s Shutdown) Execute() {
|
||||
}
|
||||
|
||||
func (s Shutdown) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
return Shutdown{c}, nil
|
||||
var result Shutdown
|
||||
err := json.Unmarshal(config.Options, &result)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Parsing Shutdown options failed.")
|
||||
return Shutdown{}, err
|
||||
}
|
||||
|
||||
result.ActionChan = c
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p Shutdown) GetName() string {
|
||||
@@ -44,10 +54,20 @@ func (p Shutdown) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "Shutdown",
|
||||
"options": {
|
||||
"time": "+5" //wait 5 minutes before shutdown
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p Shutdown) GetOptions() []internal.ConfigOption {
|
||||
return []internal.ConfigOption{}
|
||||
return []internal.ConfigOption{
|
||||
{
|
||||
Name: "time",
|
||||
Type: "string",
|
||||
Description: "TIME parameter passed to shutdown as follows ```shutdown -h TIME```",
|
||||
Default: "now",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,33 @@ type Command struct {
|
||||
ActionChan ActionResultChan
|
||||
}
|
||||
|
||||
func isCommandAvailable(name string) bool {
|
||||
cmd := exec.Command("/bin/sh", "-c", "command -v "+name)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (c Command) DryExecute() {
|
||||
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
||||
fmt.Printf("Test Executing Command:\n%s\n", c.Command)
|
||||
command, _, err := c.splitCommandString()
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error during argument parsing of command '%s'\n", c.Command)
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
isAvailable := isCommandAvailable(command)
|
||||
|
||||
if !isAvailable {
|
||||
fmt.Printf("Command %s not found\n", command)
|
||||
c.ActionChan <- fmt.Errorf("Command %s not found!", command)
|
||||
return
|
||||
}
|
||||
|
||||
c.ActionChan <- nil
|
||||
}
|
||||
|
||||
@@ -48,6 +73,7 @@ func (c Command) Execute() {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
c.ActionChan <- err
|
||||
}
|
||||
|
||||
fmt.Println(string(stdout[:]))
|
||||
|
||||
Reference in New Issue
Block a user