Skip to content

Commit

Permalink
add auto start networks
Browse files Browse the repository at this point in the history
  • Loading branch information
reddec committed Apr 7, 2020
1 parent 0b1c9c2 commit 82509c6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
build
dist
dist
test-data
35 changes: 34 additions & 1 deletion cmd/tinc-web-boot/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package main

import (
"context"
"github.com/alecthomas/kong"
"log"
"os"
"os/signal"
"time"
"tinc-web-boot/cmd/tinc-web-boot/internal"
"tinc-web-boot/network"
"tinc-web-boot/tincd"
)

type Main struct {
APIPort int `name:"api-port" env:"API_PORT" help:"API port" default:"18655"`
TincBin string `name:"tinc-bin" env:"TINC_BIN" help:"Custom tinc binary location" default:"tincd"`
Host string `name:"host" env:"HOST" help:"Binding host" default:"127.0.0.1"`
Dir string `name:"dir" env:"DIR" help:"Directory for config" default:"networks"`
}

func main() {
Expand All @@ -30,5 +37,31 @@ func (m *Main) Run() error {
return err
}
log.Println("Detected Tinc binary:", binary)
return nil

ctx, closer := context.WithCancel(context.Background())
go func() {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Kill, os.Interrupt)
for range c {
closer()
break
}
}()
defer closer()

stor := &network.Storage{Root: m.Dir}
err = stor.Init()
if err != nil {
return err
}

pool, err := tincd.New(ctx, stor, m.APIPort, binary)
if err != nil {
return err
}
defer pool.Stop()

time.Sleep(10 * time.Second)

return ctx.Err()
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.13

require (
github.com/alecthomas/kong v0.2.4
github.com/gin-gonic/gin v1.6.2 // indirect
github.com/gin-gonic/gin v1.6.2
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee
)
1 change: 1 addition & 0 deletions network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Config struct {
Name string
Port uint16
Interface string
AutoStart bool
ConnectTo []string
}

Expand Down
7 changes: 7 additions & 0 deletions network/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func (cfg *Config) MarshalText() (text []byte, err error) {
"Name": {cfg.Name},
"Port": {fmt.Sprint(cfg.Port)},
"Interface": {cfg.Interface},
"AutoStart": {cfg.AutoStart},
}
for _, con := range cfg.ConnectTo {
params["ConnectTo"] = append(params["ConnectTo"], con)
Expand All @@ -26,6 +27,7 @@ func (cfg *Config) UnmarshalText(text []byte) error {
cfg.Interface = params.First("Interface", "")
cfg.Name = params.First("Name", "")
cfg.Port = params.FirstUint16("Port")
cfg.AutoStart = params.FirstBool("AutoStart")
return nil
}

Expand Down Expand Up @@ -108,6 +110,11 @@ func (mm *multiMap) FirstUint16(name string) uint16 {
return uint16(x)
}

func (mm *multiMap) FirstBool(name string) bool {
t := strings.ToLower(mm.First(name, "false"))
return t == "true" || t == "yes" || t == "on"
}

func parseContent(content string) (params multiMap, tail string) {
var offset = 0
params = make(map[string][]string)
Expand Down
28 changes: 26 additions & 2 deletions tincd/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@ import (
"tinc-web-boot/network"
)

func New(ctx context.Context, storage *network.Storage, apiPort int, tincBin string) *poolImpl {
return &poolImpl{
func New(ctx context.Context, storage *network.Storage, apiPort int, tincBin string) (*poolImpl, error) {
pool := &poolImpl{
ctx: ctx,
apiPort: apiPort,
tincBin: tincBin,
storage: storage,
}

list, err := storage.List()
if err != nil {
return nil, err
}

var toStart []*netImpl

for _, ntw := range list {
impl, _ := pool.ensure(ntw)
cfg, err := ntw.Read()
if err != nil {
return nil, fmt.Errorf("read config of network %s: %w", ntw.Name(), err)
}
if cfg.AutoStart {
toStart = append(toStart, impl)
}
}

for _, impl := range toStart {
impl.Start()
}

return pool, nil
}

type poolImpl struct {
Expand Down

0 comments on commit 82509c6

Please sign in to comment.