Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwillem committed Apr 21, 2024
1 parent 2a41f3b commit 20d233b
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 105 deletions.
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Whip your servers into line. Chief Whip is a _fast_ and _simple_ Ansible replace

# Features

| Works | Planned | NOT planned |
| Finished | Planned | NOT planned\* |
| ---------- | ------------------ | ------------- |
| ssh auth | external inventory | non-linux |
| ssh agent | facts | sudo / become |
Expand Down Expand Up @@ -47,20 +47,18 @@ Ansible has grown too complex, as illustrated by this Hacker News comment:
# Other reading

- [Top Ansible tasks](https://mike42.me/blog/2019-01-the-top-100-ansible-modules)
- [I'm done with Red Hat](https://www.jeffgeerling.com/blog/2023/im-done-red-hat-enterprise-linux)
- [Is Ansible turing complete?](https://stackoverflow.com/questions/40127586/is-ansible-turing-complete)
- [gossh: declarative config management using Go](https://github.com/krilor/gossh)
- [Ansible's YAML file is essentially code](https://news.ycombinator.com/item?id=16238005)
- [Top Ansible tasks](https://mike42.me/blog/2019-01-the-top-100-ansible-modules)
- [Configuration complexity clock](http://mikehadlow.blogspot.com/2012/05/configuration-complexity-clock.html?m=1)
- [Original Ansible site: simple and efficient](https://web.archive.org/web/20130314042108/http://www.ansibleworks.com/)
- [Michael DeHaan announces Jetporch](https://laserllama.substack.com/p/a-new-it-automation-project-moving)
- [Michael DeHaan discontinnues Jetporch](https://web.archive.org/web/20231230013721/https://jetporch.substack.com/p/discontinuing-jet)
- Some recent config mgt alternatives:
- [JetPorch docs](https://github.com/jetporch/jetporch_docs/blob/main/SUMMARY.md)
- [Ploy](https://github.com/davesavic/ploy)
- [Bruce](https://github.com/brucedom/bruce)
- [mgmt](https://github.com/purpleidea/mgmt/) since 2016, got a complex DSL
- [gossh: declarative config management using Go](https://github.com/krilor/gossh)
- [JetPorch](https://github.com/jetporch/jetporch_docs/blob/main/SUMMARY.md) ([launched](https://laserllama.substack.com/p/a-new-it-automation-project-moving) and [discontinued](https://web.archive.org/web/20231230013721/https://jetporch.substack.com/p/discontinuing-jet))
- [Ploy](https://github.com/davesavic/ploy) Jan 2024, not Ansible compatible
- [Bruce](https://github.com/brucedom/bruce) since Apr 2023, not Ansible compatible
- [mgmt](https://github.com/purpleidea/mgmt/) since 2016, full featured, high complexity, not Ansible compatible

# FAQ

Expand Down
24 changes: 23 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,30 @@
- [x] support vault
- [ ] add taskrunner syntax validation so we can lint the tasks before actual run
- [ ] struct based cli arg parsing? such as go-arg or go-flags or kong
- [ ] module auto doc generation

# Todo for MVP / internal use

- [ ] tests for vault
- [x] tui progress shows DONE when there is an error
- [ ] rename runner to module
- [ ] dont pass whole task to runner
- [ ] add: creates as backwards compat
- [ ] bug: tree does not work?
- [ ] args vs \_args

```
# - name: inital request of wildcard certificate
# command: "certbot certonly -n --agree-tos [email protected] --dns-cloudflare --dns-cloudflare-credentials /etc/certbot/sans.ec.creds -d '*.sans.ec'"
# args:
# creates: /etc/letsencrypt/live/sans.ec/cert.pem
# tags: certbot
```

- [ ] get_url
- [ ] apt: state latest?
- [x] apt: pkg should be "name" ?
- [ ] tags
- [x] bug: \_args get assigned to every task
- [x] bug: tree, prefixmap props don't trickle down in map (eg handler for /etc)
- [x] bug: task (pre-) runner should be able to modify their vars, args
Expand All @@ -61,7 +82,8 @@

nice:

- support for ansible vault https://github.com/sosedoff/ansible-vault-go/blob/master/vault.go
x support for ansible vault https://github.com/sosedoff/ansible-vault-go/blob/master/vault.go

- fix tests
- tree: move assets from run param to arg param
- tree sync: use tar or std serialization
Expand Down
8 changes: 5 additions & 3 deletions cmd/deputy/deputy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
)

func main() {
job := getJobFromStdin()
runJob(getJobFromStdin())
}

func runJob(job *model.Job) {
// send task results back to whip
encoder := gob.NewEncoder(os.Stdout)

Expand All @@ -28,7 +30,7 @@ func main() {
}

if tr.Status != 0 {
break
return
}

if tr.Changed {
Expand Down Expand Up @@ -60,7 +62,7 @@ func main() {
panic(err)
}
if tr.Status != 0 {
break
return
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/deputy/deputy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@ import (
"github.com/gwillem/whip/internal/playbook"
"github.com/gwillem/whip/internal/runners"
"github.com/gwillem/whip/internal/testutil"
"github.com/k0kubun/pp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_DeputyIntegration(t *testing.T) {
pb, err := playbook.Load(testutil.FixturePath("playbook/simple.yml"))
assert.NoError(t, err)
pp.Println(pb)
require.NoError(t, err)

for _, play := range *pb {
for _, task := range play.Tasks {
res := runners.Run(&task, nil)

pp.Print(res)
assert.Equal(t, 0, res.Status)
require.Equal(t, 0, res.Status)
}
}
}
5 changes: 3 additions & 2 deletions cmd/whip/reporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,16 @@ func reportResults(results <-chan model.TaskResult, stats map[model.TargetName]m
handler.Quit()

if len(failed) > 0 {
fmt.Println()
log.Task("Failed tasks")
for _, f := range failed {
for _, line := range strings.Split(strings.TrimSpace(f.Output), "\n") {
fmt.Println(" " + red(line))
log.Progress(fmt.Sprintf("%s %s", f.Host, red(line)))
}
}
}

if verbosity > 0 {
log.Task("Summary")
for k, stats := range stats {
log.Ok(fmt.Sprint(k, " ", stats))
}
Expand Down
13 changes: 6 additions & 7 deletions devbook.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
- hosts:
- [email protected]
# - ubuntu@192.168.64.17
# - [email protected]
- root@192.168.64.17
vars:
install:
path: /data
Expand All @@ -12,17 +12,16 @@
- tiny

tasks:
- lineinfile:
path: /tmp/postfix/main.cf
line: "inet_interfaces = 127.0.0.1"
- apt:
pkg:
name:
- moreutils
- xz-utils
- git
- atop state=absent

- lineinfile:
path: /tmp/postfix/main.cf
line: "inet_interfaces = 127.0.0.1"

# - name: install ssh keys
# authorized_key:
# user: ubuntu
Expand Down
12 changes: 1 addition & 11 deletions fixture/job.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,5 @@
}
]
}
],
"assets": {
"name": "/Users/willem/git/whip/fixture/assets/sample",
"files": [
{
"path": "/etc/nginx/nginx.conf",
"data": "aHR0cCB7CiAgICBzZXJ2ZXIgewogICAgICAgIGxpc3RlbiA4MDsKICAgICAgICByb290IC92YXIvd3d3L2h0bWw7CiAgICB9Cn0=",
"mode": 420
}
]
}
]
}
7 changes: 1 addition & 6 deletions fixture/playbook/duplicate_runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@

tasks:
- name: install ssh keys
authorized_key:
user: ubuntu
key: "{{item}}"
command: main runner
shell: this is a duplicate runner
loop:
- abc
- xyz
2 changes: 1 addition & 1 deletion fixture/playbook/task-loop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

tasks:
- name: install ssh keys
authorized_key:
command:
user: ubuntu
key: "{{item}}"
loop:
Expand Down
File renamed without changes.
5 changes: 0 additions & 5 deletions fixture/vault/sample-pki.age

This file was deleted.

5 changes: 5 additions & 0 deletions fixture/vault/sample.age
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
age-encryption.org/v1
-> X25519 wSDpoxlZgP/AY32nfL3P01xPcmcg8RbyNyIwdgAUBlA
diGQqI0YCfRPYTOKgFZEcCrLPhww5JkR6s2h91d8VNY
--- QWioTvm5ePzU/5MPjbnPZtvetYWbaepZDJa9YKlQrho
��^��GwZ���X�����Q>q�F�v�NX��$�V
6 changes: 6 additions & 0 deletions fixture/vault/sample.ansible
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$ANSIBLE_VAULT;1.1;AES256
61633337373135626538333230336665353763636130336235343764383032393332353036653564
3937613336396262326130623666393961656332653130620a386430666533626337343732613937
34396461316337616166343438613334343434613432326666633761666534396365326162626562
3239376538356136610a303465313131616564626565316665346630636662343330393932303430
3862
Binary file removed fixture/vault/secret
Binary file not shown.
Binary file removed fixture/vault/secret2
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/nikolalohinski/gonja v1.5.0
github.com/pkg/sftp v1.13.5
github.com/sosedoff/ansible-vault-go v0.2.0
github.com/spf13/afero v1.9.5
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sosedoff/ansible-vault-go v0.2.0 h1:XqkBdqbXgTuFQ++NdrZvSdUTNozeb6S3V5x7FVs17vg=
github.com/sosedoff/ansible-vault-go v0.2.0/go.mod h1:wMU54HNJfY0n0KIgbpA9m15NBfaUDlJrAsaZp0FwzkI=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
Expand Down
3 changes: 2 additions & 1 deletion internal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (tr TaskResult) String() string {
runner = tr.Task.Runner
}

return fmt.Sprintf("TaskResult %s from %s (%.2f sec) -- %s:%d", runner, tr.Host, tr.Duration.Seconds())
return fmt.Sprintf("TaskResult %s from %s (%.2f sec)",
runner, tr.Host, tr.Duration.Seconds())
}

func (ta TaskArgs) String(s string) string {
Expand Down
12 changes: 6 additions & 6 deletions internal/playbook/playbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/gwillem/whip/internal/model"
tu "github.com/gwillem/whip/internal/testutil"
"github.com/k0kubun/pp"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadPlaybookSimple1(t *testing.T) {
Expand Down Expand Up @@ -57,7 +57,7 @@ func TestExpandTaskLoops(t *testing.T) {
},
Tasks: []model.Task{
{
Runner: "authorized_key",
Runner: "command",
Name: "install ssh keys",
Args: model.TaskArgs{
"key": "{{item}}",
Expand All @@ -70,7 +70,7 @@ func TestExpandTaskLoops(t *testing.T) {
Notify: []string{},
},
{
Runner: "authorized_key",
Runner: "command",
Name: "install ssh keys",
Args: model.TaskArgs{
"key": "{{item}}",
Expand All @@ -89,13 +89,13 @@ func TestExpandTaskLoops(t *testing.T) {
}

func TestFilesWithMeta(t *testing.T) {
pb, err := Load(tu.FixturePath("playbook/tree.yml"))
assert.NoError(t, err)
pp.Println(pb)
_, err := Load(tu.FixturePath("playbook/tree.yml"))
require.NoError(t, err)
}

func TestDuplicateRunner(t *testing.T) {
_, err := Load(tu.FixturePath("playbook/duplicate_runner.yml"))
require.Error(t, err)
var e *mapstructure.Error
assert.ErrorAs(t, err, &e)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/runners/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func getState(s string) string {
func buildWanted(args model.TaskArgs) (aptPkgState, error) {
pkglist := aptPkgState{}
defaultState := getState(args.String("state"))
for _, p := range args.StringSlice("pkg") {
for _, p := range args.StringSlice("name") {
state := defaultState
args := parser.ParseArgString(p)
if args["state"] != "" {
Expand Down
3 changes: 2 additions & 1 deletion internal/runners/apt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func Test_buildAptCmd(t *testing.T) {
"name": []string{"foo", "bar"},
"state": "latest",
}
want := []string{"/usr/bin/apt-get", "-y", "purge", "foo+", "bar+"}
// want := aptPkgState(aptPkgState{"install": map[string]bool{"bar": true, "foo": true}})
want := aptPkgState{"install": map[string]bool{"bar": true, "foo": true}}
got, err := buildWanted(args)
assert.NoError(t, err)
assert.Equal(t, want, got)
Expand Down
3 changes: 1 addition & 2 deletions internal/runners/runners_run_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runners

import (
"fmt"
"testing"

"github.com/gwillem/whip/internal/model"
Expand All @@ -11,7 +10,7 @@ import (
func init() {
registerRunner("dummy", runner{
prerun: func(t *model.Task) model.TaskResult {
fmt.Println("inside dummy runner")
// fmt.Println("inside dummy runner")
return model.TaskResult{Status: Skipped}
},
})
Expand Down
Loading

0 comments on commit 20d233b

Please sign in to comment.