Skip to content

Commit

Permalink
Merge pull request #27 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.8.0
  • Loading branch information
andyone authored Jan 9, 2020
2 parents f44218e + 8461200 commit a79676b
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 41 deletions.
32 changes: 32 additions & 0 deletions COOKBOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* [`wait-fs`](#wait-fs)
* [`connect`](#connect)
* [`app`](#app)
* [`signal`](#signal)
* [`env`](#env)
* [`env-set`](#env-set)
* [Users/Groups](#users-groups)
Expand Down Expand Up @@ -1065,6 +1066,37 @@ command "-" "Check environment"

<br/>

##### `signal`

Sends signal to process.

If `pid-file` not defined signal will be sent to current process.

**Syntax:** `signal <sig> <pid-file>`

**Arguments:**

* `sig` - Signal name or code (_String_ or _Number_)
* `pid-file` - Path to PID file (_String_) [Optional]

**Negative form:** No

**Example:**

```yang
command "myapp --daemon" "Check my app"
signal HUP
```

```yang
command "myapp --daemon" "Check my app"
signal 16
```

<br/>

##### `env`

Checks environment variable value.
Expand Down
2 changes: 1 addition & 1 deletion action/auxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/libs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion action/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
115 changes: 97 additions & 18 deletions action/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"io/ioutil"
"net"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"

"pkg.re/essentialkaos/ek.v11/env"
"pkg.re/essentialkaos/ek.v11/fsutil"
"pkg.re/essentialkaos/ek.v11/mathutil"
"pkg.re/essentialkaos/ek.v11/pid"
"pkg.re/essentialkaos/ek.v11/signal"

"github.com/essentialkaos/bibop/recipe"
)

// ////////////////////////////////////////////////////////////////////////////////// //

// ErrCantReadPIDFile is returned if PID can't be read
var ErrCantReadPIDFile = fmt.Errorf("Can't read PID from PID file")

// ////////////////////////////////////////////////////////////////////////////////// //

// ProcessWorks is action processor for "process-works"
func ProcessWorks(action *recipe.Action) error {
pidFile, err := action.GetS(0)
Expand All @@ -32,19 +41,19 @@ func ProcessWorks(action *recipe.Action) error {
return err
}

pid, err := readPID(pidFile)
ppid := pid.Read(pidFile)

if err != nil {
return err
if ppid == -1 {
return ErrCantReadPIDFile
}

isWorks := fsutil.IsExist("/proc/" + pid)
isWorks := fsutil.IsExist("/proc/" + strconv.Itoa(ppid))

switch {
case !action.Negative && !isWorks:
return fmt.Errorf("Process with PID %s from PID file %s doesn't exist", pid, pidFile)
return fmt.Errorf("Process with PID %d from PID file %s doesn't exist", ppid, pidFile)
case action.Negative && isWorks:
return fmt.Errorf("Process with PID %s from PID file %s exists", pid, pidFile)
return fmt.Errorf("Process with PID %d from PID file %s exists", ppid, pidFile)
}

return err
Expand Down Expand Up @@ -77,13 +86,13 @@ func WaitPID(action *recipe.Action) error {
for range time.NewTicker(25 * time.Millisecond).C {
switch {
case !action.Negative && fsutil.IsExist(pidFile):
pid, err := readPID(pidFile)
ppid := pid.Read(pidFile)

if err != nil {
return err
if ppid == -1 {
return ErrCantReadPIDFile
}

if fsutil.IsExist("/proc/" + pid) {
if fsutil.IsExist("/proc/" + strconv.Itoa(ppid)) {
return nil
}
case action.Negative && !fsutil.IsExist(pidFile):
Expand Down Expand Up @@ -196,6 +205,39 @@ func App(action *recipe.Action) error {
return nil
}

// Signal is action processor for "signal"
func Signal(action *recipe.Action, cmd *exec.Cmd) error {
var sig syscall.Signal
var sigVal, pidFile string
var err error

sigVal, err = action.GetS(0)

if err != nil {
return err
}

if action.Has(1) {
pidFile, err = action.GetS(1)

if err != nil {
return err
}
}

sig, err = parseSignal(sigVal)

if err != nil {
return err
}

if pidFile != "" {
return sendSignalToPID(sig, pidFile)
}

return sendSignalToCmd(sig, cmd)
}

// Env is action processor for "env"
func Env(action *recipe.Action) error {
name, err := action.GetS(0)
Expand Down Expand Up @@ -247,13 +289,50 @@ func EnvSet(action *recipe.Action) error {

// ////////////////////////////////////////////////////////////////////////////////// //

// readPID reads PID from PID file
func readPID(file string) (string, error) {
pidFileData, err := ioutil.ReadFile(file)
// isNumber returns true if given value is number
func isNumber(v string) bool {
if len(v) != 0 && strings.Trim(v, "0123456789") == "" {
return true
}

if err != nil {
return "", fmt.Errorf("Can't read PID file %s: %v", file, err)
return false
}

// parseSignal parses signal definition
func parseSignal(v string) (syscall.Signal, error) {
if isNumber(v) {
sigCode, err := strconv.Atoi(v)

if err != nil {
return syscall.Signal(-1), err
}

return signal.GetByCode(sigCode)
}

return signal.GetByName(v)
}

// sendSignalToCmd sends signal to PID from command
func sendSignalToCmd(sig syscall.Signal, cmd *exec.Cmd) error {
if cmd.Process == nil {
return fmt.Errorf("Can't find process PID (process already dead?)")
}

if cmd.ProcessState.Exited() {
return fmt.Errorf("Can't send signal - process already dead")
}

return signal.Send(cmd.ProcessState.Pid(), sig)
}

// sendSignalToPID sends signal to PID from PID file
func sendSignalToPID(sig syscall.Signal, pidFile string) error {
ppid := pid.Read(pidFile)

if ppid == -1 {
return ErrCantReadPIDFile
}

return strings.TrimRight(string(pidFileData), " \n\r"), nil
return signal.Send(ppid, sig)
}
2 changes: 1 addition & 1 deletion action/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package action

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion bibop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package main

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
4 changes: 2 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cli

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -33,7 +33,7 @@ import (
// Application info
const (
APP = "bibop"
VER = "1.7.0"
VER = "1.8.0"
DESC = "Utility for testing command-line tools"
)

Expand Down
4 changes: 3 additions & 1 deletion cli/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package executor

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -417,6 +417,8 @@ func runAction(a *recipe.Action, cmd *exec.Cmd, input io.Writer, outputStore *ou
return action.Backup(a, tmpDir)
case recipe.ACTION_BACKUP_RESTORE:
return action.BackupRestore(a, tmpDir)
case recipe.ACTION_SIGNAL:
return action.Signal(a, cmd)
}

handler, ok := handlers[a.Name]
Expand Down
2 changes: 1 addition & 1 deletion cli/executor/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package executor

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion output/output_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package output

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion parser/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package parser

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package parser

// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2009-2019 ESSENTIAL KAOS //
// Copyright (c) 2009-2020 ESSENTIAL KAOS //
// Essential Kaos Open Source License <https://essentialkaos.com/ekol> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
Loading

0 comments on commit a79676b

Please sign in to comment.