Skip to content

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
khash committed Oct 28, 2019
2 parents fdcaab5 + 2465594 commit 1d1d088
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 6 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ You can use the metadata as arguments of a step:

Trackman can use Golang template language.

`Metadata` is an attribute on both Step and the entire workflow. You can use `MergedMetadata` instead of `Metadata` to gain access to a merged list of meta data from the step and the workflow. If any value is defined in both places, step will override workflow.

### Work directory

To set the working directory of a step, use `workdir` attribute on a step.
Expand All @@ -125,6 +127,15 @@ All environment variables in commands and their arguments are replaced with `$`

All environment variables available to Trackman when it starts will be passed on to the step commands.

To specify environment variables that are applies only to a single step, use the `env` attribute:

```yaml
- name: dump
env: ["FOO=BAR"]
```

If the assigned environment variable already exists, it will overwrite the OS environment variable for this step.

### Preflight Checks

You can run some checks before the workflow starts. These could be checking for certain binaries or packages to be installed on the machine before the workflow starts.
Expand Down Expand Up @@ -167,6 +178,8 @@ The following attributes can be set for each step:
| preflights | List of pre-flight checks (see above) | None |
| ask_to_proceed | Stops the execution of the workflow and asks the user for a confirmation to continue | `false` |
| show_command | Shows the command and arguments for this step before running it | `false` |
| disabled | Disables the step (doesn't run it). This can be used for debugging or other selective workflow manipulations | `false` |
| env | Environment variables specific to this step | [] |

## Trackman CLI

Expand All @@ -177,8 +190,8 @@ The CLI supports the following global options:
| Option | Description | Default |
|---|---|---|
| config | Config file | $HOME/.trackman.yaml |
| log-level | Log level | info |
| no-update | Don't update trackman CLI automatically | false |
| log-level | Log level | `info` |
| no-update | Don't update trackman CLI automatically | `false` |

### Run

Expand Down
7 changes: 6 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash

version=$(git describe --tags --always)
if [ -z "$2" ]
then
version=$(git describe --tags --always)
else
version=$2
fi

if [ -z "$1" ]
then
Expand Down
6 changes: 6 additions & 0 deletions samples/env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1
steps:
- name: hello
env: ["FOO=BOO"]
workdir: "$HOME/work/go/src/github.com/cloud66-oss/trackman/samples"
command: ruby enver.rb
2 changes: 2 additions & 0 deletions samples/enver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
puts "FOO:" + ENV["FOO"]
puts "HOME:" + ENV["HOME"]
3 changes: 2 additions & 1 deletion samples/sample.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: 1
metadata:
cloud66.com/uuid: ff97e4c6
cloud66.com/test: 123
steps:
- name: list
command: ls -la
Expand Down Expand Up @@ -49,4 +50,4 @@ steps:
metadata:
cloud66.com/uuid: arger-123
workdir: "$HOME/work/go/src/github.com/cloud66-oss/trackman/samples"
command: "ruby arger.rb hello {{ index .Metadata \"cloud66.com/uuid\" }}"
command: "ruby arger.rb hello {{ index .MergedMetadata \"cloud66.com/test\" }}"
3 changes: 2 additions & 1 deletion samples/timeout.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ steps:
depends_on:
- timeout
- name: timeout
ask_to_proceed: true
ask_to_proceed: false
disabled: true
show_command: true
command: sleep 20
timeout: 10s
Expand Down
11 changes: 11 additions & 0 deletions utils/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"context"
"fmt"
"os"
"os/exec"
"syscall"
"time"
Expand All @@ -19,6 +20,7 @@ type Spinner struct {

cmd string
args []string
env []string
timeout time.Duration
workdir string
step Step
Expand Down Expand Up @@ -78,6 +80,7 @@ func newSpinnerForStep(ctx context.Context, step Step) (*Spinner, error) {
cmd: parts[0],
args: parts[1:],
step: step,
env: step.Env,
workdir: step.Workdir,
}, nil
}
Expand Down Expand Up @@ -106,6 +109,7 @@ func newSpinnerForPreflight(ctx context.Context, preflight *Preflight) (*Spinner
args: parts[1:],
step: *preflight.step,
workdir: preflight.Workdir,
env: preflight.step.Env,
timeout: timeout,
}, nil
}
Expand All @@ -128,6 +132,7 @@ func newSpinnerForProbe(ctx context.Context, step Step) (*Spinner, error) {
cmd: parts[0],
args: parts[1:],
step: step,
env: step.Env,
workdir: step.Workdir,
}, nil
}
Expand Down Expand Up @@ -168,6 +173,12 @@ func (s *Spinner) Run(ctx context.Context) error {
cmd := exec.CommandContext(cmdCtx, s.cmd, s.args...)
cmd.Stderr = errChannel
cmd.Stdout = outChannel
envs := os.Environ()
for _, env := range s.env {
envs = append(envs, env)
}

cmd.Env = envs
cmd.Dir = s.workdir

err := cmd.Start()
Expand Down
25 changes: 25 additions & 0 deletions utils/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ type Step struct {
ContinueOnFail bool `yaml:"continue_on_fail" json:"continue_on_fail"`
Timeout *time.Duration `yaml:"timeout" json:"timeout"`
Workdir string `yaml:"workdir" json:"workdir"`
Env []string `yaml:"env" json:"env"`
Probe *Probe `yaml:"probe" json:"probe"`
DependsOn []string `yaml:"depends_on" json:"depends_on"`
Preflights []Preflight `yaml:"preflights" json:"preflights"`
AskToProceed bool `yaml:"ask_to_proceed" json:"ask_to_proceed"`
ShowCommand bool `yaml:"show_command" json:"show_command"`
Disabled bool `yaml:"disabled" json:"disabled"`

options *StepOptions
workflow *Workflow
Expand All @@ -52,6 +54,24 @@ func (s *Step) String() string {
return str + "\n" + strings.Join(deps, ",")
}

// MergedMetadata merges step and workflow metadata
func (s *Step) MergedMetadata() map[string]string {
if s.Metadata == nil {
return s.workflow.Metadata
}

result := make(map[string]string, len(s.workflow.Metadata))
for k, v := range s.workflow.Metadata {
result[k] = v
}

for k, v := range s.Metadata {
result[k] = v
}

return result
}

// shouldRun returns a step that can be run, hasn't started, isn't done and isn't marked to be done
func (s *Step) shouldRun() bool {
// has this run or marked to run?
Expand Down Expand Up @@ -126,6 +146,11 @@ func (s *Step) Run(ctx context.Context) error {

logger, ctx := LoggerContext(ctx)

if s.Disabled {
logger.WithField(FldStep, s.Name).Info("Disabled step. Skipping")
return nil
}

err := s.parseCommand(ctx)
if err != nil {
// a failure here is down to workflow errors so
Expand Down
2 changes: 1 addition & 1 deletion utils/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (w *Workflow) Run(ctx context.Context) (runErrors error, stepErrors error)
w.logger.WithField(FldStep, toRun.Name).Info(toRun.Command)
}

if toRun.AskToProceed && !viper.GetBool("confirm.yes") {
if !toRun.Disabled && toRun.AskToProceed && !viper.GetBool("confirm.yes") {
// we need an interactive permission for this
if !confirm(fmt.Sprintf("Run %s?", toRun.Name), 1) {
w.logger.WithField(FldStep, toRun.Name).Info("Stopping execution")
Expand Down

0 comments on commit 1d1d088

Please sign in to comment.