Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ jobs:
run: go get .
- name: Build
run: go build -v ./...
- name: Lint
run: go vet ./...
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/nextmn/docker-setup
go 1.25.5

require (
github.com/nextmn/logrus-formatter v0.1.0
github.com/nextmn/logrus-formatter v0.2.0
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli/v3 v3.6.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/nextmn/logrus-formatter v0.1.0 h1:SUVMFsckd18j/TMveaFed+mu6v4p7U9D9PuTNrI4w28=
github.com/nextmn/logrus-formatter v0.1.0/go.mod h1:+G9FGh86JyAJBDenHuMOtz2d+Dwkg5fYAUDG5rJj76M=
github.com/nextmn/logrus-formatter v0.2.0 h1:uIohVfiR8+CiLBn/gbxExSutq57uZpBq7VBi3+vThKc=
github.com/nextmn/logrus-formatter v0.2.0/go.mod h1:+G9FGh86JyAJBDenHuMOtz2d+Dwkg5fYAUDG5rJj76M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
Expand Down
29 changes: 15 additions & 14 deletions internal/app/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package app

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -39,19 +40,19 @@ func (conf Conf) Oneshot() bool {
}

// Run exit hooks
func (conf Conf) RunExitHooks() {
conf.RunExitHook("pre")
conf.RunExitHook("nat4")
conf.RunExitHook("iproute")
conf.RunExitHook("post")
func (conf Conf) RunExitHooks(ctx context.Context) {
conf.RunExitHook(ctx, "pre")
conf.RunExitHook(ctx, "nat4")
conf.RunExitHook(ctx, "iproute")
conf.RunExitHook(ctx, "post")
}

// Run init hooks
func (conf Conf) RunInitHooks() {
conf.RunInitHook("pre")
conf.RunInitHook("iproute")
conf.RunInitHook("nat4")
conf.RunInitHook("post")
func (conf Conf) RunInitHooks(ctx context.Context) {
conf.RunInitHook(ctx, "pre")
conf.RunInitHook(ctx, "iproute")
conf.RunInitHook(ctx, "nat4")
conf.RunInitHook(ctx, "post")
}

// Add a new hook to the configuration
Expand All @@ -70,9 +71,9 @@ func (conf Conf) AddHooks() {
}

// Run an init hook
func (conf Conf) RunInitHook(name string) {
func (conf Conf) RunInitHook(ctx context.Context, name string) {
if conf.hooksList[name] != nil {
if err := conf.hooksList[name].RunInit(); err != nil {
if err := conf.hooksList[name].RunInit(ctx); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"hook-name": name,
}).Error("Error while running init hook")
Expand All @@ -81,9 +82,9 @@ func (conf Conf) RunInitHook(name string) {
}

// Run an exit hook
func (conf Conf) RunExitHook(name string) {
func (conf Conf) RunExitHook(ctx context.Context, name string) {
if conf.hooksList[name] != nil {
if err := conf.hooksList[name].RunExit(); err != nil {
if err := conf.hooksList[name].RunExit(ctx); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"hook-name": name,
}).Error("Error while running exit hook")
Expand Down
18 changes: 11 additions & 7 deletions internal/app/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@

package app

import (
"context"
)

// init & exit hooks iface
type Hook interface {
String() []string
RunInit() error
RunExit() error
RunInit(context.Context) error
RunExit(context.Context) error
}

// init or exit hook iface
type HookSingle interface {
String() []string
Run() error
Run(context.Context) error
}

// init & exit hooks
Expand All @@ -33,11 +37,11 @@ func (hooks HookMulti) String() []string {
}

// Runs init hook
func (hooks HookMulti) RunInit() error {
return hooks.init.Run()
func (hooks HookMulti) RunInit(ctx context.Context) error {
return hooks.init.Run(ctx)
}

// Runs exit hook
func (hooks HookMulti) RunExit() error {
return hooks.exit.Run()
func (hooks HookMulti) RunExit(ctx context.Context) error {
return hooks.exit.Run(ctx)
}
10 changes: 6 additions & 4 deletions internal/app/iproutehook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
package app

import (
"context"
"fmt"
"os"
"os/exec"
"strings"
)

// Runs iptables
func runIPRoute(args ...string) error {
func runIPRoute(ctx context.Context, args ...string) error {
r := []string{"route"}
r = append(r, args...)
cmd := exec.Command("ip", r...)
cmd := exec.CommandContext(ctx, "ip", r...)
cmd.Env = []string{}
return cmd.Run()
}

Expand Down Expand Up @@ -57,13 +59,13 @@ func NewIPRouteHook(name string, env string) IPRouteHook {
}

// Run the hook if it is set
func (hook IPRouteHook) Run() error {
func (hook IPRouteHook) Run(ctx context.Context) error {
if !hook.isset {
return nil
}
for _, r := range hook.routes {
r = append(r, "proto", "static")
if err := runIPRoute(r...); err != nil {
if err := runIPRoute(ctx, r...); err != nil {
return err
}
}
Expand Down
18 changes: 10 additions & 8 deletions internal/app/nat4hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
package app

import (
"context"
"fmt"
"os"
"os/exec"
"strings"
)

// Runs iptables
func runIP4Tables(args ...string) error {
cmd := exec.Command("iptables", args...)
func runIP4Tables(ctx context.Context, args ...string) error {
cmd := exec.CommandContext(ctx, "iptables", args...)
cmd.Env = []string{}
return cmd.Run()
}

Expand Down Expand Up @@ -47,39 +49,39 @@ func NewNat4Hooks() Nat4Hook {
}

// Runs IPv4 NAT init hook
func (hook Nat4Hook) RunInit() error {
func (hook Nat4Hook) RunInit(ctx context.Context) error {
if !hook.isset {
return nil
}
if err := runIP4Tables("-I", "FORWARD", "-j", "ACCEPT"); err != nil {
if err := runIP4Tables(ctx, "-I", "FORWARD", "-j", "ACCEPT"); err != nil {
return err
}
for _, iface := range hook.ifaces {
if err := runIP4Tables("-t", "nat", "-A", "POSTROUTING", "-o", iface, "-j", "MASQUERADE"); err != nil {
if err := runIP4Tables(ctx, "-t", "nat", "-A", "POSTROUTING", "-o", iface, "-j", "MASQUERADE"); err != nil {
return err
}
}
return nil
}

// Runs IPv4 NAT exit hook
func (hook Nat4Hook) RunExit() error {
func (hook Nat4Hook) RunExit(ctx context.Context) error {
if !hook.isset {
return nil
}
errcount := 0
var lasterr error
for _, iface := range hook.ifaces {
// if there is an error, we continue: will return at the end
if err := runIP4Tables("-t", "nat", "-D", "POSTROUTING", "-o", iface, "-j", "MASQUERADE"); err != nil {
if err := runIP4Tables(ctx, "-t", "nat", "-D", "POSTROUTING", "-o", iface, "-j", "MASQUERADE"); err != nil {
errcount++
lasterr = err
}
}
if errcount == 1 {
return lasterr
} else if errcount > 1 {
return fmt.Errorf("%i iptable errors", errcount)
return fmt.Errorf("%d iptable errors", errcount)
}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions internal/app/userhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package app

import (
"bufio"
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -61,11 +62,12 @@ func NewUserHook(name string, env string) UserHook {
}

// Run the hook if it is set
func (hook UserHook) Run() error {
func (hook UserHook) Run(ctx context.Context) error {
if !hook.isset {
return nil
}
cmd := exec.Command(hook.cmd, hook.args...)
cmd := exec.CommandContext(ctx, hook.cmd, hook.args...)
cmd.Env = []string{}
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os/signal"
"runtime/debug"
"syscall"
"time"

"github.com/nextmn/logrus-formatter/logger"

Expand All @@ -37,11 +38,15 @@ func main() {
Version: version,
Action: func(ctx context.Context, cmd *cli.Command) error {
conf := app.NewConf()
conf.RunInitHooks()
conf.RunInitHooks(ctx)
if !conf.Oneshot() {
defer func() {
shCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 1*time.Second)
defer cancel()
conf.RunExitHooks(shCtx)
}()
<-ctx.Done()
}
conf.RunExitHooks()
return nil
},
}
Expand Down
Loading