Exec

Receiver

This service implements an execution worker. You can run commands on os environments.

docker pull registry.goingrid.io/services/exec:v0.1.0

Table of contents

Service properties

This service implements the ingrid protocol message. Following properties are being used:

Property Usage
Class -
Operation -
Control -
Data Accepts only properties if it has been specified in the Template .

Service capabilities

Run command

To execute the following command on the System:

echo "hallo" myinfo whatever there
POST https://rest.app.goingrid.io/cmd/exec HTTP/1.1
content-type: application/json
Authorization: Basic admin nutz

{
    "info": ["myinfo","banana"],
    "operation": ["whatever","there"] 
}

exec:
  image: registry.goingrid.io/services/exec:v0.1.0
  deploy:
    restart_policy:
      condition: on-failure
    placement:
      constraints:
        - node.role == manager
  environment:
    NAME: "exec"
    SERVERHOST: "hive"
    SERVERAUTH: "file:///run/secrets/ing-hive-key"
    INPUTCHANNEL: "cmd_exec"
    TEMPLATE: "{{ setCmd \"echo\" }}{{ addArg \"\\\"hallo\\\"\"}}{{ addArg ( index .Data.info 0 ) }} {{ addArg .Data.operation }}",
  depends_on:
    - hive
  secrets:
    - ing-hive-key
  networks:
    - ing-entry
    - ing-middle
apiVersion: apps/v1
kind: Deployment
metadata:
  name: exec-service-cmd-exec
spec:
  selector:
    matchLabels:
      app: exec-service-cmd-exec
  template:
    metadata:
      labels:
        app: exec-service-cmd-exec
    spec:
      containers:
      - name: exec-service-cmd-exec
        env:
        - name: name
          value: exec-service-cmd-exec
        - name: SERVERHOST
          value: core-ingrid-hive # CHANGE IF REQUIRED
        - name: LOGLEVEL
          value: trace
        - name: LOGFORMAT
          value: TEXT/DEFAULT
        - name: LOGSENSITIVE
          value: "false"
        - name: INPUTCHANNEL
          value: cmd_exec
        - name: TEMPLATE
          value: |- 
            {{ setCmd "echo" }}
            {{ addArg "hallo"}}
            {{ addArg ( index .Data.info 0 ) }} 
            {{ addArg .Data.operation }}
        - name: SERVERAUTH
          valueFrom:
            secretKeyRef:
              name: core-ingrid-serverkey # CHANGE IF REQUIRED
              key: serverkey # CHANGE IF REQUIRED
        image: registry-dev.goingrid.io/services/exec:latest
        imagePullPolicy: Always
        volumeMounts:
        - mountPath: /data
          name: data
        resources:
          limits:
            cpu: 200m
            memory: 32Mi
          requests:
            cpu: 50m
            memory: 16Mi
      serviceAccountName: core-ingrid # CHANGE IF REQUIRED
      serviceAccount: core-ingrid # CHANGE IF REUQIRED
      restartPolicy: Always
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: exec-claim

Service configuration

Parameter Mandatory Description
Template true Template to call to generate the execute command and args

Additionally the log filesystem service includes all properties of the service configuration and the input configuration.

Configuration Template

The configuration parameter Template is a list a go Template that will be set the Command and appened Args to the command.

It can be defined via

  • File file:///your/location/file.tmpl
  • Hive hive://yourtemplate.tmpl
  • String Your Template {{.Data.propname }}

Primary Functions set the Command to execute

setCmd TheCommand

Comand Attributes

addArg AnArg|AnArgArray

Additional Templatefunctions The Default golang template functions are available

see https://golang.org/pkg/text/template/#pkg-examples

Also the following functions are implemented.

"plus": func(a, b int) int 
"minus": func(a, b int) int
"replace": func(input, from, to string) string 
"replaceLine": func(input, search, newline string) string 
"replaceLineAndIdent": func(input, search, newline string) string
"noescape": func(str string) template.HTML 
"marshal": func(v interface{}) string 
"marshalIndent": func(v interface{}) string 
"after": func(value string, a string) string 
"tolower": func(instr string) string 
"toupper": func(instr string) string 
"string": func(v interface{}) string 
"split": func(instr string, by string) []string
"env": func(envname string) string
"set": func(key string, value interface{}) string
"get": func(key string) interface{} 
"niltoblank": func(err error) (msg string) 
"setCmd": func(_cmd string) bool
"addArg": func(arg interface{}) bool

Examples

echo "hallo" myinfo whatever there
{{ setCmd "echo" }}
{{ addArg "\"hallo\""}}
{{ addArg ( index .Data.info 0 ) }}
{{ addArg .Data.operation }}
POST https://rest.app.goingrid.io/cmd/exec HTTP/1.1
content-type: application/json
Authorization: Basic admin nutz

{
    "info": ["myinfo","banana"],
    "operation": ["whatever","there"] 
}
cp file1.txt file2.txt
{{ setCmd "cp" }}
{{ addArg ( index .Data.file 0 ) }}
{{ addArg ( index .Data.file 1 ) }}
POST https://rest.app.goingrid.io/cmd/exec HTTP/1.1
content-type: application/json
Authorization: Basic admin nutz

{
    "file": ["file1.txt","file2.txt"]
}
mv file1.txt file2.txt
{{ setCmd "mv" }}
{{ addArg .Data.file }}
POST https://rest.app.goingrid.io/cmd/exec HTTP/1.1
content-type: application/json
Authorization: Basic admin nutz

{
    "file": ["file1.txt","file2.txt"]
}

Common pitfalls

Not using the shell for more complex commands

If you want to use wildcard/asterisk in your command, you need to invoke a shell.

Following example does not work:

{{ setCmd "rm" }}
{{ addArg "example*" }}

Using wildcard, you have to invoke a shell: Following example does not work:

{{ setCmd "/bin/sh" }}
{{ addArg "-c" }}
{{ addArg "rm example*" }}