Compare commits
15 Commits
e37a84d77d
...
feat_docs
| Author | SHA1 | Date | |
|---|---|---|---|
| c0111f2720 | |||
| bcaacc1634 | |||
| abbd1561f2 | |||
| b9b7c0bf3b | |||
| 8898565ff8 | |||
| 06534e0bbd | |||
| d4a660383e | |||
| e96bbb5f49 | |||
| ad9060c8f6 | |||
| 58946000e1 | |||
| b08446bbff | |||
| 6c750a947f | |||
| b2e20c5d75 | |||
| 737b90d597 | |||
| e9969abf1c |
@@ -45,7 +45,7 @@ func (a StagedActions) executeInternal(f func(Action)) {
|
||||
err := <-a.ActionChan
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error occured on Stage %d: %s\n", idx+1, err)
|
||||
fmt.Printf("Error occured on Stage %d: %s", idx+1, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -122,7 +122,6 @@ func GetAllActions() []DocumentedAction {
|
||||
Printer{},
|
||||
TimeOut{},
|
||||
Command{},
|
||||
ShellScript{},
|
||||
Shutdown{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (p Printer) GetExample() string {
|
||||
return `
|
||||
{
|
||||
type: "Print",
|
||||
"options": {
|
||||
"options: {
|
||||
"message": "Hello World!"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
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,24 +3,23 @@ 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", s.Timeout).Run(); err != nil {
|
||||
if err := exec.Command("shutdown", "-h", "now").Run(); err != nil {
|
||||
fmt.Println("Failed to initiate shutdown:", err)
|
||||
}
|
||||
|
||||
@@ -30,16 +29,7 @@ func (s Shutdown) Execute() {
|
||||
}
|
||||
|
||||
func (s Shutdown) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
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
|
||||
return Shutdown{c}, nil
|
||||
}
|
||||
|
||||
func (p Shutdown) GetName() string {
|
||||
@@ -54,20 +44,10 @@ 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{
|
||||
{
|
||||
Name: "time",
|
||||
Type: "string",
|
||||
Description: "TIME parameter passed to shutdown as follows ```shutdown -h TIME```",
|
||||
Default: "now",
|
||||
},
|
||||
}
|
||||
return []internal.ConfigOption{}
|
||||
}
|
||||
|
||||
@@ -14,33 +14,8 @@ 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\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
|
||||
}
|
||||
|
||||
fmt.Printf("Test Executing Command:\n%s ", c.Command)
|
||||
c.ActionChan <- nil
|
||||
}
|
||||
|
||||
@@ -73,7 +48,6 @@ func (c Command) Execute() {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
c.ActionChan <- err
|
||||
}
|
||||
|
||||
fmt.Println(string(stdout[:]))
|
||||
|
||||
@@ -15,7 +15,9 @@ Actions have the following syntax:
|
||||
"type": "SomeAction",
|
||||
"options": { //each action defines its own options
|
||||
"firstOption": "someValue",
|
||||
"stage": 2 //this (positive) number defines the order of multiple actions
|
||||
"Stage": 2 //this (positive) number defines the order of multiple actions
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To get a list of all actions and their options from the commandline run ``` gokill -d ```
|
||||
|
||||
@@ -17,3 +17,5 @@ Triggers have the following syntax:
|
||||
"actions": [] //list actions that should be executed here
|
||||
}
|
||||
```
|
||||
|
||||
To get a list of all triggers and their options from the commandline run ```gokill -d```
|
||||
|
||||
112
flake.nix
112
flake.nix
@@ -47,7 +47,7 @@
|
||||
nixosModules.gokill = { config, lib, pkgs, ... }:
|
||||
let
|
||||
cfg = config.services.gokill;
|
||||
configFile = pkgs.writeText "config.json" (builtins.toJSON cfg.triggers);
|
||||
configFile = pkgs.writeText "config.json" ''${cfg.extraConfig}'';
|
||||
gokill-pkg = self.packages.x86_64-linux.gokill;
|
||||
in
|
||||
{
|
||||
@@ -61,45 +61,6 @@
|
||||
'';
|
||||
};
|
||||
|
||||
triggers = lib.mkOption {
|
||||
description = "list of triggers";
|
||||
default = [];
|
||||
type = with lib.types; lib.types.listOf ( submodule {
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
};
|
||||
|
||||
name = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
};
|
||||
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
};
|
||||
|
||||
actions = lib.mkOption {
|
||||
description = "list of actions";
|
||||
type = with lib.types; lib.types.listOf ( submodule {
|
||||
options = {
|
||||
type = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
};
|
||||
|
||||
options = lib.mkOption {
|
||||
type = lib.types.attrs;
|
||||
};
|
||||
|
||||
stage = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
extraConfig = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
description = lib.mdDoc ''
|
||||
@@ -131,30 +92,53 @@
|
||||
self.nixosModules.gokill
|
||||
{
|
||||
services.gokill.enable = true;
|
||||
services.gokill.triggers = [
|
||||
{
|
||||
type = "Timeout";
|
||||
name = "custom timeout";
|
||||
options = {
|
||||
duration = 10;
|
||||
};
|
||||
actions = [
|
||||
{
|
||||
type = "Timeout";
|
||||
options = {
|
||||
duration = 5;
|
||||
};
|
||||
stage = 1;
|
||||
}
|
||||
{
|
||||
type = "Shutdown";
|
||||
options = {
|
||||
};
|
||||
stage = 2;
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
services.gokill.extraConfig = ''
|
||||
[
|
||||
{
|
||||
"type": "Timeout",
|
||||
"name": "custom timeout",
|
||||
"options": {
|
||||
"duration": 30
|
||||
},
|
||||
"actions": [
|
||||
{
|
||||
"type": "Print",
|
||||
"options": {
|
||||
"message": "Stage 1 triggered. Waiting 25 seconds"
|
||||
},
|
||||
"stage": 1
|
||||
},
|
||||
{
|
||||
"type": "Timeout",
|
||||
"options": {
|
||||
"duration": 20
|
||||
},
|
||||
"stage": 1
|
||||
},
|
||||
{
|
||||
"type": "Timeout",
|
||||
"options": {
|
||||
"duration": 5
|
||||
},
|
||||
"stage": 2
|
||||
},
|
||||
{
|
||||
"type": "Print",
|
||||
"options": {
|
||||
"message": "Shutdown in 5 seconds..."
|
||||
},
|
||||
"stage": 2
|
||||
},
|
||||
{
|
||||
"type": "Shutdown",
|
||||
"options": {
|
||||
},
|
||||
"stage": 3
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
'';
|
||||
users.users.root.password = "root";
|
||||
virtualisation.vmVariant.virtualisation.graphics = false;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package triggers
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
|
||||
"unknown.com/gokill/actions"
|
||||
@@ -17,7 +17,7 @@ type EthernetDisconnect struct {
|
||||
}
|
||||
|
||||
func isEthernetConnected(deviceName string) bool {
|
||||
content, err := os.ReadFile(fmt.Sprintf("/sys/class/net/%s/operstate", deviceName))
|
||||
content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/operstate", deviceName))
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -95,7 +95,7 @@ func (p EthernetDisconnect) GetExample() string {
|
||||
"options": {
|
||||
"interfaceName": "eth0",
|
||||
"waitTillConnected": true
|
||||
},
|
||||
}
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ func (p TimeOut) GetExample() string {
|
||||
"name": "Example Trigger",
|
||||
"options": {
|
||||
"duration": 5
|
||||
},
|
||||
}
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ func (p UsbDisconnect) GetExample() string {
|
||||
"options": {
|
||||
"deviceId": "ata-Samsung_SSD_860_EVO_1TB_S4AALKWJDI102",
|
||||
"waitTillConnected": true
|
||||
},
|
||||
}
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user