-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathboot.go
More file actions
155 lines (142 loc) · 4.72 KB
/
boot.go
File metadata and controls
155 lines (142 loc) · 4.72 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
// Package main provides the entry point for the Drycc builder service.
package main
import (
"context"
"fmt"
"log"
"os"
"runtime"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
"github.com/distribution/distribution/v3/registry/storage/driver/factory"
_ "github.com/distribution/distribution/v3/registry/storage/driver/s3-aws"
"github.com/drycc/builder/pkg"
"github.com/drycc/builder/pkg/cleaner"
"github.com/drycc/builder/pkg/conf"
"github.com/drycc/builder/pkg/controller/token"
"github.com/drycc/builder/pkg/gitreceive"
"github.com/drycc/builder/pkg/healthsrv"
"github.com/drycc/builder/pkg/k8s"
"github.com/drycc/builder/pkg/sshd"
"github.com/drycc/builder/pkg/sys"
pkglog "github.com/drycc/pkg/log"
"github.com/kelseyhightower/envconfig"
"github.com/urfave/cli/v3"
)
const (
serverConfAppName = "drycc-builder-server"
gitReceiveConfAppName = "drycc-builder-git-receive"
gitHomeDir = "/workspace"
)
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
if os.Getenv("DRYCC_DEBUG") == "true" {
pkglog.DefaultLogger.SetDebug(true)
log.Printf("Running in debug mode")
}
app := &cli.Command{}
app.Commands = []*cli.Command{
{
Name: "server",
Aliases: []string{"srv"},
Usage: "Run the git server",
Action: func(_ context.Context, _ *cli.Command) error {
cnf := new(sshd.Config)
if err := envconfig.Process(serverConfAppName, cnf); err != nil {
return fmt.Errorf("getting config for %s [%s]", serverConfAppName, err)
}
fs := sys.RealFS()
env := sys.RealEnv()
pushLock := sshd.NewInMemoryRepositoryLock(cnf.GitLockTimeout())
circ := sshd.NewCircuit()
storageParams, err := conf.GetStorageParams(env)
if err != nil {
return fmt.Errorf("error getting storage parameters (%s)", err)
}
var storageDriver storagedriver.StorageDriver
storageDriver, err = factory.Create(context.Background(), "s3", storageParams)
if err != nil {
return fmt.Errorf("error creating storage driver (%s)", err)
}
kubeClient, err := k8s.NewInCluster()
if err != nil {
return fmt.Errorf("error getting kubernetes client [%s]", err)
}
log.Printf("Starting health check server on port %d", cnf.HealthSrvPort)
healthSrvCh := make(chan error)
go func() {
if err := healthsrv.Start(cnf, kubeClient.CoreV1().Namespaces(), storageDriver, circ); err != nil {
healthSrvCh <- err
}
}()
log.Printf("Starting deleted app cleaner")
cleanerErrCh := make(chan error)
go func() {
if err := cleaner.Run(gitHomeDir, kubeClient.CoreV1().Namespaces(), fs, cnf.CleanerPollSleepDuration(), storageDriver); err != nil {
cleanerErrCh <- err
}
}()
log.Printf("Starting SSH server on %s:%d", cnf.SSHHostIP, cnf.SSHHostPort)
sshCh := make(chan int)
go func() {
sshCh <- pkg.RunBuilder(cnf, gitHomeDir, circ, pushLock)
}()
select {
case err := <-healthSrvCh:
return fmt.Errorf("error running health server (%s)", err)
case i := <-sshCh:
return fmt.Errorf("unexpected SSH server stop with code %d", i)
case err := <-cleanerErrCh:
return fmt.Errorf("error running the deleted app cleaner (%s)", err)
}
},
},
{
Name: "git-receive",
Aliases: []string{"gr"},
Usage: "Run the git-receive hook",
Action: func(_ context.Context, _ *cli.Command) error {
cnf := new(gitreceive.Config)
if err := envconfig.Process(gitReceiveConfAppName, cnf); err != nil {
return fmt.Errorf("error getting config for %s [%s]", gitReceiveConfAppName, err)
}
cnf.CheckDurations()
env := sys.RealEnv()
storageParams, err := conf.GetStorageParams(env)
if err != nil {
return fmt.Errorf("error getting storage parameters (%s)", err)
}
var storageDriver storagedriver.StorageDriver
storageDriver, err = factory.Create(context.Background(), "s3", storageParams)
if err != nil {
return fmt.Errorf("error creating storage driver (%s)", err)
}
if err := gitreceive.Run(cnf, env, storageDriver); err != nil {
return fmt.Errorf("error running git receive hook [%s]", err)
}
return nil
},
},
{
Name: "refresh-token",
Usage: "Refresh the OAuth m2m access token in Valkey (CronJob entry point)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "force",
Usage: "Refresh regardless of current token lifetime",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
if err := token.Refresh(ctx, c.Bool("force")); err != nil {
return fmt.Errorf("token refresh failed: %w", err)
}
log.Printf("Token refresh completed successfully")
return nil
},
},
}
if err := app.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}