-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdaemon.go
142 lines (121 loc) · 3.37 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package pwagent
import (
"context"
"os"
"time"
"github.com/docker/docker/client"
"go.uber.org/multierr"
"go.uber.org/zap"
"pathwar.land/pathwar/v2/go/internal/randstring"
"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwapi"
"pathwar.land/pathwar/v2/go/pkg/pwcompose"
)
type Opts struct {
DomainSuffix string
HostIP string
HostPort string
ModeratorPassword string
AuthSalt string
ForceRecreate bool
NginxDockerImage string
Cleanup bool
RunOnce bool
LoopDelay time.Duration
DefaultAgent bool
Name string
NoRun bool
Logger *zap.Logger
}
func Run(ctx context.Context, cli *client.Client, apiClient *pwapi.HTTPClient, opts Opts) error {
started := time.Now()
opts.applyDefaults()
logger := opts.Logger
err := agentRegister(ctx, apiClient, opts)
if err != nil {
return errcode.TODO.Wrap(err)
}
if opts.Cleanup {
before := time.Now()
err := pwcompose.DownAll(ctx, cli, logger)
if err != nil {
return errcode.ErrCleanPathwarInstances.Wrap(err)
}
logger.Info("docker cleaned up", zap.Duration("duration", time.Since(before)))
}
if opts.NoRun {
return nil
}
iteration := 0
for {
if !opts.RunOnce {
logger.Debug("daemon iteration", zap.Int("number", iteration), zap.Duration("uptime", time.Since(started)))
}
err := runOnce(ctx, cli, apiClient, opts)
if err != nil {
logger.Error("daemon iteration", zap.Error(err))
}
if opts.RunOnce {
break
}
opts.ForceRecreate = false // only do it once
opts.Cleanup = false // only do it once
time.Sleep(opts.LoopDelay)
}
return nil
}
func runOnce(ctx context.Context, cli *client.Client, apiClient *pwapi.HTTPClient, opts Opts) error {
instances, err := apiClient.AgentListInstances(ctx, &pwapi.AgentListInstances_Input{AgentName: opts.Name})
opts.Logger.Debug("api response", zap.Any("instances", instances.GetInstances()))
if err != nil {
return errcode.TODO.Wrap(err)
}
if errs := applyDockerConfig(ctx, &instances, cli, opts); err != nil {
for _, err := range multierr.Errors(errs) {
opts.Logger.Error("apply docker config", zap.Error(err))
}
}
if err := applyNginxConfig(ctx, &instances, cli, opts); err != nil {
return errcode.TODO.Wrap(err)
}
if err := updateAPIState(ctx, &instances, cli, apiClient, opts); err != nil {
return errcode.TODO.Wrap(err)
}
return nil
}
func NewOpts() Opts {
return Opts{
Cleanup: false,
RunOnce: false,
NoRun: false,
LoopDelay: 10 * time.Second,
DefaultAgent: true,
Name: getHostname(),
DomainSuffix: "local",
NginxDockerImage: "docker.io/library/nginx:stable-alpine",
HostIP: "0.0.0.0",
HostPort: "8001",
ModeratorPassword: "",
AuthSalt: "",
}
}
func (opts *Opts) applyDefaults() {
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
if opts.AuthSalt == "" {
opts.AuthSalt = randstring.RandString(10)
opts.Logger.Warn("random salt generated", zap.String("salt", opts.AuthSalt))
}
if opts.ModeratorPassword == "" {
opts.ModeratorPassword = randstring.RandString(10)
opts.Logger.Warn("random moderator password generated", zap.String("password", opts.ModeratorPassword))
}
}
func getHostname() string {
hostname, _ := os.Hostname()
if hostname == "" {
return "dev"
}
return hostname
}