Compare commits
25 Commits
feat_docs
...
7b4dcdd1d8
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b4dcdd1d8 | |||
| 72d0aa61cd | |||
| e37a84d77d | |||
| 2d6ca2b0bd | |||
| df2b9e7624 | |||
| 46d1270648 | |||
| 5cbf066ccf | |||
| 515e244592 | |||
| fb4322c040 | |||
| f376d8684b | |||
| d0439394cf | |||
| a91632028c | |||
| bccdcf2ca3 | |||
| 18e3a93a38 | |||
| cfb553c975 | |||
| 1466623070 | |||
| c527d40721 | |||
| 781c096abf | |||
| c94fbd4b48 | |||
| 1f00713c1e | |||
| 12cf423550 | |||
| f99726d3b7 | |||
| 13932e572f | |||
| 20120785bd | |||
| 5af4c963ea |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,8 +1,9 @@
|
||||
*.qcow2
|
||||
.envrc
|
||||
result
|
||||
example.json
|
||||
go.sum
|
||||
go.mod
|
||||
gokill
|
||||
./gokill
|
||||
output.md
|
||||
thoughts.md
|
||||
|
||||
122
README.md
Normal file
122
README.md
Normal file
@@ -0,0 +1,122 @@
|
||||
# gokill
|
||||
'gokill' is a tool that completes some actions when a certain event occurs.
|
||||
actions can vary from shuting down the machine to sending mails over erasing data.
|
||||
actions can be triggert by certain conditions like specific outcomes of unix
|
||||
comands or not having internet connection.
|
||||
|
||||
actions and triggers should be easy to extend and handled like plugins. they
|
||||
also should be self documenting.
|
||||
every action and trigger should be testable at anytime as a 'dry-run'.
|
||||
actions can have a 'stage' defined. the lowest stage is started first,
|
||||
and only when all actions on that stage are finished next stage is triggered
|
||||
|
||||
gokill should run as daemon. config should be read from /etc/somename/config.json
|
||||
|
||||
## Config Example
|
||||
``` json
|
||||
[ //list of triggers
|
||||
{
|
||||
"type": "UsbDisconnect",
|
||||
"name": "First Trigger",
|
||||
"options": {
|
||||
"deviceId": "ata-Samsung_SSD_860_EVO_1TB_S4AALKWJDI102",
|
||||
"waitTillConnected": true //only trigger when usb drive was actually attached before
|
||||
}
|
||||
"actions": [ //list of actions that will be executed when triggered
|
||||
{
|
||||
"name": "unixCommand",
|
||||
"options": {
|
||||
"command": "shutdown -h now"
|
||||
},
|
||||
"stage": 2 // defines the order in which actions are triggered.
|
||||
},
|
||||
{
|
||||
"type": "sendMail",
|
||||
"options": {
|
||||
"smtpserver": "domain.org",
|
||||
"port": 667,
|
||||
"recipients": [ "mail1@host.org", "mail2@host.org" ],
|
||||
"message": "kill switch was triggered",
|
||||
"attachments": [ "/path/atachments" ],
|
||||
"pubkeys": "/path/to/keys.pub"
|
||||
},
|
||||
"stage": 1 //this event is triggered first, then the shutdown
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "EthernetDisconnect",
|
||||
"name": "Second Trigger",
|
||||
"options": {
|
||||
"interfaceName": "eth0",
|
||||
}
|
||||
"actions": [
|
||||
{
|
||||
"name": "unixCommand",
|
||||
"options": {
|
||||
"command": "env DISPLAY=:0 sudo su -c i3lock someUser"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
## nix support
|
||||
|
||||
gokill enjoys full nix support. gokill exposes a nix flakes that outputs a gokill package, a nixosModule and more.
|
||||
That means you can super easily incorporate gokill into your existing nixosConfigurations.
|
||||
Here is a small example config:
|
||||
|
||||
``` nix
|
||||
{
|
||||
services.gokill.enable = true;
|
||||
services.gokill.triggers = [
|
||||
{
|
||||
type = "EthernetDisconnect";
|
||||
name = "MainTrigger";
|
||||
options = {
|
||||
interfaceName = "eth1";
|
||||
};
|
||||
actions = [
|
||||
{
|
||||
type = "Command";
|
||||
options = {
|
||||
command = "echo hello world";
|
||||
};
|
||||
stage = 1;
|
||||
}
|
||||
];
|
||||
}
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
This will automatically configure and enable a systemd running gokill as root user in the background
|
||||
|
||||
## actions
|
||||
- [x] shutdown
|
||||
- [ ] wipe ram
|
||||
- [ ] send mail
|
||||
- [ ] delete data
|
||||
- [ ] shred area
|
||||
- [x] random command
|
||||
- [ ] wordpress post
|
||||
- [ ] ipfs command
|
||||
- [ ] [buskill 'triggers'](https://github.com/BusKill/awesome-buskill-triggers)
|
||||
- [x] [lock-screen](https://github.com/BusKill/buskill-linux/tree/master/triggers)
|
||||
- [x] shutdown
|
||||
- [ ] luks header shredder
|
||||
- [ ] veracrypt self-destruct
|
||||
|
||||
## Triggers
|
||||
- [ ] no internet
|
||||
- [x] [pull usb stick](https://github.com/deepakjois/gousbdrivedetector/blob/master/usbdrivedetector_linux.go)
|
||||
- [x] ethernet unplugged
|
||||
- [ ] power adapter disconnected
|
||||
- [ ] unix command
|
||||
- anyOf
|
||||
- trigger wrapper containing many triggers and fires as soon as one of them
|
||||
is triggered
|
||||
- allOf
|
||||
- [ ] ipfs trigger
|
||||
@@ -7,10 +7,12 @@ import (
|
||||
"unknown.com/gokill/internal"
|
||||
)
|
||||
|
||||
type ActionResultChan chan error
|
||||
|
||||
type Action interface {
|
||||
Execute()
|
||||
DryExecute()
|
||||
Create(internal.ActionConfig, chan bool) (Action, error)
|
||||
Create(internal.ActionConfig, ActionResultChan) (Action, error)
|
||||
}
|
||||
|
||||
type DocumentedAction interface {
|
||||
@@ -23,7 +25,7 @@ type Stage struct {
|
||||
}
|
||||
|
||||
type StagedActions struct {
|
||||
ActionChan chan bool
|
||||
ActionChan ActionResultChan
|
||||
StageCount int
|
||||
Stages []Stage
|
||||
}
|
||||
@@ -40,7 +42,11 @@ func (a StagedActions) executeInternal(f func(Action)) {
|
||||
}
|
||||
|
||||
for range stage.Actions {
|
||||
<-a.ActionChan
|
||||
err := <-a.ActionChan
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error occured on Stage %d: %s\n", idx+1, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -64,11 +70,11 @@ func (a StagedActions) Execute() {
|
||||
a.executeInternal(func(a Action) { a.Execute() })
|
||||
}
|
||||
|
||||
func (a StagedActions) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
func (a StagedActions) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
return StagedActions{}, nil
|
||||
}
|
||||
|
||||
func NewSingleAction(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
func NewSingleAction(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
for _, availableAction := range GetAllActions() {
|
||||
if config.Type == availableAction.GetName() {
|
||||
return availableAction.Create(config, c)
|
||||
@@ -83,7 +89,7 @@ func NewAction(config []internal.ActionConfig) (Action, error) {
|
||||
return config[i].Stage < config[j].Stage
|
||||
})
|
||||
|
||||
stagedActions := StagedActions{make(chan bool), 0, []Stage{}}
|
||||
stagedActions := StagedActions{make(ActionResultChan), 0, []Stage{}}
|
||||
|
||||
stageMap := make(map[int][]Action)
|
||||
|
||||
@@ -116,6 +122,7 @@ func GetAllActions() []DocumentedAction {
|
||||
Printer{},
|
||||
TimeOut{},
|
||||
Command{},
|
||||
ShellScript{},
|
||||
Shutdown{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,20 +9,20 @@ import (
|
||||
|
||||
type Printer struct {
|
||||
Message string
|
||||
ActionChan chan bool
|
||||
ActionChan ActionResultChan
|
||||
}
|
||||
|
||||
func (p Printer) Execute() {
|
||||
fmt.Printf("Print action fires. Message: %s\n", p.Message)
|
||||
p.ActionChan <- true
|
||||
p.ActionChan <- nil
|
||||
}
|
||||
|
||||
func (p Printer) DryExecute() {
|
||||
fmt.Printf("Print action fire test. Message: %s\n", p.Message)
|
||||
p.ActionChan <- true
|
||||
p.ActionChan <- nil
|
||||
}
|
||||
|
||||
func (p Printer) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
func (p Printer) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
var result Printer
|
||||
err := json.Unmarshal(config.Options, &result)
|
||||
|
||||
@@ -39,11 +39,30 @@ func (p Printer) GetName() string {
|
||||
}
|
||||
|
||||
func (p Printer) GetDescription() string {
|
||||
return "When triggered prints the configured message to stdout"
|
||||
return `
|
||||
Prints a given message to stdout.
|
||||
This action is mostly used for debugging purposes.
|
||||
`
|
||||
}
|
||||
|
||||
func (p Printer) GetExample() string {
|
||||
return `
|
||||
{
|
||||
type: "Print",
|
||||
"options": {
|
||||
"message": "Hello World!"
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p Printer) GetOptions() []internal.ConfigOption {
|
||||
return []internal.ConfigOption{
|
||||
{"message", "string", "Message that should be printed", "\"\""},
|
||||
{
|
||||
Name: "message",
|
||||
Type: "string",
|
||||
Description: "Message that should be printed",
|
||||
Default: "\"\"",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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,33 +3,43 @@ package actions
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"encoding/json"
|
||||
|
||||
"unknown.com/gokill/internal"
|
||||
)
|
||||
|
||||
type Shutdown struct {
|
||||
ActionChan chan bool
|
||||
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 <- true
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fmt.Println("Shutdown executed...")
|
||||
|
||||
s.ActionChan <- true
|
||||
s.ActionChan <- nil
|
||||
}
|
||||
|
||||
func (s Shutdown) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
return Shutdown{c}, nil
|
||||
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
|
||||
}
|
||||
|
||||
func (p Shutdown) GetName() string {
|
||||
@@ -37,9 +47,27 @@ func (p Shutdown) GetName() string {
|
||||
}
|
||||
|
||||
func (p Shutdown) GetDescription() string {
|
||||
return "When triggered shuts down the machine"
|
||||
return "Shutsdown the machine by perfoming a ```shutdown -h now```"
|
||||
}
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
type TimeOut struct {
|
||||
Duration time.Duration
|
||||
ActionChan chan bool
|
||||
ActionChan ActionResultChan
|
||||
}
|
||||
|
||||
func (t TimeOut) DryExecute() {
|
||||
@@ -20,10 +20,10 @@ func (t TimeOut) DryExecute() {
|
||||
func (t TimeOut) Execute() {
|
||||
fmt.Printf("Waiting %d seconds\n", t.Duration)
|
||||
time.Sleep(time.Duration(t.Duration) * time.Second)
|
||||
t.ActionChan <- true
|
||||
t.ActionChan <- nil
|
||||
}
|
||||
|
||||
func (t TimeOut) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
func (t TimeOut) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
var result TimeOut
|
||||
err := json.Unmarshal(config.Options, &result)
|
||||
|
||||
@@ -40,7 +40,21 @@ func (p TimeOut) GetName() string {
|
||||
}
|
||||
|
||||
func (p TimeOut) GetDescription() string {
|
||||
return "When triggered waits given duration before continuing with next stage"
|
||||
return `
|
||||
Waits given duration in seconds.
|
||||
This can be used to wait a certain amount of time before continuing to the next Stage
|
||||
`
|
||||
}
|
||||
|
||||
func (p TimeOut) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "Timeout",
|
||||
"options": {
|
||||
"duration": 5
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p TimeOut) GetOptions() []internal.ConfigOption {
|
||||
|
||||
@@ -11,12 +11,37 @@ import (
|
||||
|
||||
type Command struct {
|
||||
Command string `json:"command"`
|
||||
ActionChan chan bool
|
||||
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)
|
||||
c.ActionChan <- true
|
||||
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
|
||||
}
|
||||
|
||||
func (c Command) splitCommandString() (string, []string, error) {
|
||||
@@ -38,8 +63,7 @@ func (c Command) Execute() {
|
||||
fmt.Println("Executing command: ", c.Command)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
c.ActionChan <- false
|
||||
c.ActionChan <- err
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,14 +73,15 @@ func (c Command) Execute() {
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
c.ActionChan <- err
|
||||
}
|
||||
|
||||
fmt.Println(string(stdout[:]))
|
||||
|
||||
c.ActionChan <- true
|
||||
c.ActionChan <- nil
|
||||
}
|
||||
|
||||
func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
|
||||
func CreateCommand(config internal.ActionConfig, c ActionResultChan) (Command, error) {
|
||||
result := Command{}
|
||||
|
||||
err := json.Unmarshal(config.Options, &result)
|
||||
@@ -74,7 +99,7 @@ func CreateCommand(config internal.ActionConfig, c chan bool) (Command, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (cc Command) Create(config internal.ActionConfig, c chan bool) (Action, error) {
|
||||
func (cc Command) Create(config internal.ActionConfig, c ActionResultChan) (Action, error) {
|
||||
return CreateCommand(config, c)
|
||||
}
|
||||
|
||||
@@ -83,7 +108,18 @@ func (p Command) GetName() string {
|
||||
}
|
||||
|
||||
func (p Command) GetDescription() string {
|
||||
return "When triggered executes given command"
|
||||
return "Invoces given command using exec."
|
||||
}
|
||||
|
||||
func (p Command) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "Command",
|
||||
"options": {
|
||||
"command": "srm /path/to/file"
|
||||
}
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p Command) GetOptions() []internal.ConfigOption {
|
||||
|
||||
102
cmd/docbuilder/docbuilder.go
Normal file
102
cmd/docbuilder/docbuilder.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"os"
|
||||
"flag"
|
||||
|
||||
"unknown.com/gokill/actions"
|
||||
"unknown.com/gokill/triggers"
|
||||
"unknown.com/gokill/internal"
|
||||
)
|
||||
|
||||
func getMarkdown(documenter internal.Documenter) string {
|
||||
var result string
|
||||
result += fmt.Sprintf("# %v\n%v\n\n", documenter.GetName(), documenter.GetDescription())
|
||||
|
||||
result += fmt.Sprintf("*Example:*\n``` json\n%v\n```\n## Options:\n", documenter.GetExample())
|
||||
|
||||
for _, opt := range documenter.GetOptions() {
|
||||
sanitizedDefault := "\"\""
|
||||
|
||||
if len(opt.Default) > 0 {
|
||||
sanitizedDefault = opt.Default
|
||||
}
|
||||
|
||||
result += fmt.Sprintf("### %v\n%v \n\n*Type:* %v \n\n*Default:* ```%v``` \n",
|
||||
opt.Name, opt.Description, opt.Type, sanitizedDefault)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func writeToFile(path string, documenter internal.Documenter) error {
|
||||
fileName := fmt.Sprintf("%s/%s.md", path, documenter.GetName())
|
||||
|
||||
f, err := os.Create(fileName)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(getMarkdown(documenter))
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeDocumentersToFiles(destination string) {
|
||||
writeFolder := func(typeName string, documenters []internal.Documenter) {
|
||||
path := fmt.Sprintf("%s/%s", destination, typeName)
|
||||
_ = os.Mkdir(path, os.ModePerm)
|
||||
for _, documenter := range documenters {
|
||||
writeToFile(path, documenter)
|
||||
}
|
||||
}
|
||||
|
||||
actions := actions.GetDocumenters()
|
||||
writeFolder("actions", actions)
|
||||
|
||||
triggers := triggers.GetDocumenters()
|
||||
writeFolder("triggers", triggers)
|
||||
}
|
||||
|
||||
func printDocumentersSummary() {
|
||||
result := fmt.Sprintf("- [Triggers](triggers/README.md)\n")
|
||||
for _, trigger := range triggers.GetDocumenters() {
|
||||
result += fmt.Sprintf("\t- [%s](triggers/%s.md)\n", trigger.GetName(), trigger.GetName())
|
||||
}
|
||||
|
||||
result += fmt.Sprintf("- [Actions](actions/README.md)\n")
|
||||
for _, action := range actions.GetDocumenters() {
|
||||
result += fmt.Sprintf("\t- [%s](actions/%s.md)\n", action.GetName(), action.GetName())
|
||||
}
|
||||
|
||||
fmt.Print(result)
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
outputPath := flag.String("output", "", "path where docs/ shoud be created")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
if *outputPath == "" {
|
||||
printDocumentersSummary()
|
||||
return
|
||||
}
|
||||
|
||||
if len(*outputPath) > 1 {
|
||||
*outputPath = strings.TrimSuffix(*outputPath, "/")
|
||||
}
|
||||
|
||||
writeDocumentersToFiles(*outputPath)
|
||||
}
|
||||
1
docs/.gitignore
vendored
Normal file
1
docs/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
book/*
|
||||
4
docs/SUMMARY.md
Normal file
4
docs/SUMMARY.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Summary
|
||||
|
||||
- [gokill](./README.md)
|
||||
@GOKILL_OPTIONS@
|
||||
21
docs/actions/README.md
Normal file
21
docs/actions/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Actions
|
||||
|
||||
Actions are executed when their parent Trigger got triggered.
|
||||
They then perform some certain task depending on the specific action.
|
||||
Those can vary from shutding down the machine, removing a file or running a bash command.
|
||||
**Some Actions may cause permanent damage to the system. This is intended but should be used with caution.**
|
||||
|
||||
Actions can have a ```Stage``` assigned to define in which order they should run.
|
||||
The lowest stage is executed first and only when finished the next stage is executed.
|
||||
Actions on the same Stage run concurrently.
|
||||
|
||||
Actions have the following syntax:
|
||||
``` json
|
||||
{
|
||||
"type": "SomeAction",
|
||||
"options": { //each action defines its own options
|
||||
"firstOption": "someValue",
|
||||
"stage": 2 //this (positive) number defines the order of multiple actions
|
||||
}
|
||||
}
|
||||
```
|
||||
10
docs/book.toml
Normal file
10
docs/book.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[book]
|
||||
authors = []
|
||||
language = "en"
|
||||
multilingual = false
|
||||
src = "."
|
||||
title = "gokill docs"
|
||||
|
||||
[output.html.fold]
|
||||
enable = true
|
||||
level = 0
|
||||
33
docs/default.nix
Normal file
33
docs/default.nix
Normal file
@@ -0,0 +1,33 @@
|
||||
{ pkgs, lib, self, ... }:
|
||||
|
||||
with lib;
|
||||
let
|
||||
docbuilder = self.packages.x86_64-linux.gokill-docbuilder;
|
||||
|
||||
prepareMD = ''
|
||||
# Copy inputs into the build directory
|
||||
cp -r --no-preserve=all $inputs/* ./
|
||||
cp ${../README.md} ./README.md
|
||||
|
||||
${docbuilder}/bin/docbuilder --output ./
|
||||
substituteInPlace ./SUMMARY.md \
|
||||
--replace "@GOKILL_OPTIONS@" "$(${docbuilder}/bin/docbuilder)"
|
||||
|
||||
cat ./SUMMARY.md
|
||||
'';
|
||||
in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "gokill-docs";
|
||||
phases = [ "buildPhase" ];
|
||||
buildInputs = [ pkgs.mdbook ];
|
||||
|
||||
inputs = sourceFilesBySuffices ./. [ ".md" ".toml" ];
|
||||
|
||||
buildPhase = ''
|
||||
dest=$out/share/doc
|
||||
mkdir -p $dest
|
||||
${prepareMD}
|
||||
mdbook build
|
||||
cp -r ./book/* $dest
|
||||
'';
|
||||
}
|
||||
19
docs/triggers/README.md
Normal file
19
docs/triggers/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
# Triggers
|
||||
|
||||
Triggers wait for certain events and execute the actions defined for them.
|
||||
There are different Triggers for different use cases.
|
||||
For example ```UsbDisconnect``` is triggered when a certain Usb Drive is unplugged.
|
||||
If you want your actions to be triggered when an ethernet cable is pulled use ```EthernetDisconnect``` instead.
|
||||
|
||||
Triggers have the following syntax:
|
||||
``` json
|
||||
{
|
||||
"type": "SomeTrigger",
|
||||
"name": "MyFirstTrigger",
|
||||
"options": { //each trigger defines its own options
|
||||
"firstOption": 23,
|
||||
"secondOption": "foo"
|
||||
},
|
||||
"actions": [] //list actions that should be executed here
|
||||
}
|
||||
```
|
||||
6
flake.lock
generated
6
flake.lock
generated
@@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1697915759,
|
||||
"narHash": "sha256-WyMj5jGcecD+KC8gEs+wFth1J1wjisZf8kVZH13f1Zo=",
|
||||
"lastModified": 1698553279,
|
||||
"narHash": "sha256-T/9P8yBSLcqo/v+FTOBK+0rjzjPMctVymZydbvR/Fak=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "51d906d2341c9e866e48c2efcaac0f2d70bfd43e",
|
||||
"rev": "90e85bc7c1a6fc0760a94ace129d3a1c61c3d035",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
||||
133
flake.nix
133
flake.nix
@@ -7,8 +7,17 @@
|
||||
outputs = { self, nixpkgs, ... }:
|
||||
let
|
||||
forAllSystems = nixpkgs.lib.genAttrs [ "x86_64-linux" ];
|
||||
pkgs = nixpkgs.legacyPackages."x86_64-linux";
|
||||
in
|
||||
{
|
||||
devShell."x86_64-linux" = pkgs.mkShell {
|
||||
packages = with pkgs; [
|
||||
go
|
||||
gotools
|
||||
mdbook
|
||||
];
|
||||
};
|
||||
|
||||
packages.x86_64-linux.gokill = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
|
||||
pname = "gokill";
|
||||
version = "1.0";
|
||||
@@ -19,12 +28,26 @@
|
||||
'';
|
||||
};
|
||||
|
||||
packages.x86_64-linux.gokill-docbuilder = nixpkgs.legacyPackages.x86_64-linux.buildGoModule rec {
|
||||
pname = "docbuilder";
|
||||
version = "1.0";
|
||||
vendorHash = null;
|
||||
buildFLags = "-o . $dest/cmd/gokill/docbuilder";
|
||||
src = ./.;
|
||||
|
||||
postInstall = ''
|
||||
'';
|
||||
};
|
||||
|
||||
|
||||
packages.x86_64-linux.docs = pkgs.callPackage (import ./docs/default.nix) { self = self; };
|
||||
|
||||
packages.x86_64-linux.default = self.packages.x86_64-linux.gokill;
|
||||
|
||||
nixosModules.gokill = { config, lib, pkgs, ... }:
|
||||
let
|
||||
cfg = config.services.gokill;
|
||||
configFile = pkgs.writeText "config.json" ''${cfg.extraConfig}'';
|
||||
configFile = pkgs.writeText "config.json" (builtins.toJSON cfg.triggers);
|
||||
gokill-pkg = self.packages.x86_64-linux.gokill;
|
||||
in
|
||||
{
|
||||
@@ -38,6 +61,45 @@
|
||||
'';
|
||||
};
|
||||
|
||||
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 ''
|
||||
@@ -69,53 +131,30 @@
|
||||
self.nixosModules.gokill
|
||||
{
|
||||
services.gokill.enable = true;
|
||||
services.gokill.extraConfig = ''
|
||||
[
|
||||
services.gokill.triggers = [
|
||||
{
|
||||
"type": "Timeout",
|
||||
"name": "custom timeout",
|
||||
"options": {
|
||||
"duration": 30
|
||||
},
|
||||
"actions": [
|
||||
type = "Timeout";
|
||||
name = "custom timeout";
|
||||
options = {
|
||||
duration = 10;
|
||||
};
|
||||
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
|
||||
type = "Timeout";
|
||||
options = {
|
||||
duration = 5;
|
||||
};
|
||||
stage = 1;
|
||||
}
|
||||
]
|
||||
{
|
||||
type = "Shutdown";
|
||||
options = {
|
||||
};
|
||||
stage = 2;
|
||||
}
|
||||
]
|
||||
'';
|
||||
];
|
||||
}
|
||||
];
|
||||
users.users.root.password = "root";
|
||||
virtualisation.vmVariant.virtualisation.graphics = false;
|
||||
}
|
||||
@@ -131,5 +170,11 @@
|
||||
${self.packages."x86_64-linux".testVm}/bin/run-nixos-vm
|
||||
'');
|
||||
};
|
||||
|
||||
apps.x86_64-linux.docs = {
|
||||
type = "app";
|
||||
program = builtins.toString (nixpkgs.legacyPackages."x86_64-linux".writeScript "docs" ''
|
||||
${pkgs.python3}/bin/python3 -m http.server --directory ${self.packages."x86_64-linux".docs}/share/doc'');
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -36,5 +36,6 @@ type ConfigOption struct {
|
||||
type Documenter interface {
|
||||
GetName() string
|
||||
GetDescription() string
|
||||
GetExample() string
|
||||
GetOptions() []ConfigOption
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package triggers
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"unknown.com/gokill/actions"
|
||||
@@ -17,7 +17,7 @@ type EthernetDisconnect struct {
|
||||
}
|
||||
|
||||
func isEthernetConnected(deviceName string) bool {
|
||||
content, err := ioutil.ReadFile(fmt.Sprintf("/sys/class/net/%s/operstate", deviceName))
|
||||
content, err := os.ReadFile(fmt.Sprintf("/sys/class/net/%s/operstate", deviceName))
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -87,6 +87,21 @@ func (p EthernetDisconnect) GetDescription() string {
|
||||
return "Triggers if Ethernetcable is disconnected."
|
||||
}
|
||||
|
||||
func (p EthernetDisconnect) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "EthernetDisconnect",
|
||||
"name": "Example Trigger",
|
||||
"options": {
|
||||
"interfaceName": "eth0",
|
||||
"waitTillConnected": true
|
||||
},
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p EthernetDisconnect) GetOptions() []internal.ConfigOption {
|
||||
return []internal.ConfigOption{
|
||||
{"waitTillConnected", "bool", "Only trigger when device was connected before", "true"},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
package triggers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -45,7 +44,21 @@ func (p TimeOut) GetName() string {
|
||||
}
|
||||
|
||||
func (p TimeOut) GetDescription() string {
|
||||
return "Triggers after given duration."
|
||||
return "Triggers after given duration. Mostly used for debugging."
|
||||
}
|
||||
|
||||
func (p TimeOut) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "Timeout",
|
||||
"name": "Example Trigger",
|
||||
"options": {
|
||||
"duration": 5
|
||||
},
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
func (p TimeOut) GetOptions() []internal.ConfigOption {
|
||||
|
||||
@@ -85,9 +85,28 @@ func (p UsbDisconnect) GetName() string {
|
||||
}
|
||||
|
||||
func (p UsbDisconnect) GetDescription() string {
|
||||
return "Triggers when given usb drive is disconnected"
|
||||
return `
|
||||
Triggers when given usb drive is disconnected.
|
||||
Currently it simply checks that the file /dev/disk/by-id/$deviceId exists.
|
||||
`
|
||||
}
|
||||
|
||||
func (p UsbDisconnect) GetExample() string {
|
||||
return `
|
||||
{
|
||||
"type": "UsbDisconnect",
|
||||
"name": "Example Trigger",
|
||||
"options": {
|
||||
"deviceId": "ata-Samsung_SSD_860_EVO_1TB_S4AALKWJDI102",
|
||||
"waitTillConnected": true
|
||||
},
|
||||
"actions": [
|
||||
]
|
||||
}
|
||||
`
|
||||
}
|
||||
|
||||
|
||||
func (p UsbDisconnect) GetOptions() []internal.ConfigOption {
|
||||
return []internal.ConfigOption{
|
||||
{"waitTillConnected", "bool", "Only trigger when device was connected before", "true"},
|
||||
|
||||
Reference in New Issue
Block a user