forked from thechangelog/nightly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flyio.go
176 lines (147 loc) · 3.99 KB
/
flyio.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
import (
"fmt"
"os"
"strings"
"time"
"dagger.io/dagger"
)
type Flyio struct {
app string
deployWait string
publishedImageRef string
org string
pipeline *Pipeline
region string
registry string
token *dagger.Secret
version string
volume string
volumeSize string
}
func newFlyio(p *Pipeline) *Flyio {
token := os.Getenv("FLY_API_TOKEN")
if token == "" {
panic("FLY_API_TOKEN env var must be set")
}
f := &Flyio{
app: p.app,
deployWait: "180",
org: "changelog",
pipeline: p,
region: "ord",
registry: "registry.fly.io",
token: p.dag.SetSecret("FLY_API_TOKEN", token),
version: p.tools.Flyctl(),
volumeSize: "2",
}
f.volume = strings.ReplaceAll(f.app, "-", "_")
return f
}
func (f *Flyio) Cli() *dagger.Container {
flyctl := f.pipeline.Container().Pipeline("flyctl").
From(fmt.Sprintf("flyio/flyctl:v%s", f.version)).
File("/flyctl")
// we need Alpine so that we can run shell scripts that set secrets in secure way
container := f.pipeline.Container().Pipeline("fly.io").
From(fmt.Sprintf("alpine:%s", f.pipeline.tools.Alpine())).
WithFile("/usr/local/bin/flyctl", flyctl, dagger.ContainerWithFileOpts{Permissions: 755}).
WithExec([]string{"flyctl", "version"}).
WithSecretVariable("FLY_API_TOKEN", f.token).
WithEnvVariable("RUN_AT", time.Now().String()).
WithNewFile("fly.toml", dagger.ContainerWithNewFileOpts{
Contents: f.Config(),
})
_, err := container.File("fly.toml").Export(f.pipeline.ctx, "fly.toml")
if err != nil {
panic(err)
}
return container
}
func (f *Flyio) Config() string {
return fmt.Sprintf(`# https://fly.io/docs/reference/configuration/
app = "%s"
primary_region = "%s"
[env]
# used by supercronic - https://changelog-media.sentry.io/settings/projects/changelog-com/keys/
SENTRY_DSN = "https://[email protected]/5668962"
DB_DIR = "/app/dist"
[mounts]
source = "%s"
destination = "/app/dist"
[http_service]
internal_port = 80
force_https = true
[[http_service.checks]]
method = "GET"
path = "/health"
interval = "5s"
timeout = "4s"`, f.app, f.region, f.volume)
}
func (f *Flyio) App() *Flyio {
cli := f.Cli()
_, err := cli.
WithExec([]string{"flyctl", "status"}).
Sync(f.pipeline.ctx)
if err != nil {
_, err = cli.
WithExec([]string{"flyctl", "apps", "create", f.app, "--org", f.org}).
WithExec([]string{"flyctl", "volume", "create", f.volume, "--yes", "--region", f.region, "--size", f.volumeSize}).
Sync(f.pipeline.ctx)
if err != nil {
panic(err)
}
}
return f
}
func (f *Flyio) ImageRef() string {
gitSHA := os.Getenv("GITHUB_SHA")
if gitSHA == "" {
gitSHA = "dev"
}
return fmt.Sprintf("%s/%s:%s", f.registry, f.app, gitSHA)
}
func (f *Flyio) Publish() *Flyio {
var err error
f.publishedImageRef, err = f.pipeline.workspace.
Pipeline("publish").
WithRegistryAuth(f.registry, "x", f.token).
Publish(f.pipeline.ctx, f.ImageRef())
if err != nil {
panic(err)
}
return f
}
func (f *Flyio) Secrets(secrets map[string]string) *Flyio {
cli := f.Cli().Pipeline("secrets")
var envs []string
for name, secret := range secrets {
cli = cli.WithSecretVariable(name, f.pipeline.dag.SetSecret(name, secret))
envs = append(envs, fmt.Sprintf(`%s="$%s"`, name, name))
}
_, err := cli.WithNewFile("/flyctl-set-secrets-and-keep-hidden.sh", dagger.ContainerWithNewFileOpts{
Contents: fmt.Sprintf(`#!/bin/sh
flyctl secrets set %s --app %s --stage`, strings.Join(envs, " "), f.app),
Permissions: 755,
}).
WithExec([]string{"/flyctl-set-secrets-and-keep-hidden.sh"}).
Sync(f.pipeline.ctx)
if err != nil {
panic(err)
}
return f
}
func (f *Flyio) Deploy() *Flyio {
_, err := f.Cli().Pipeline("deploy").
WithExec([]string{
"flyctl", "deploy", "--now",
"--app", f.app,
"--image", f.publishedImageRef,
"--wait-timeout", f.deployWait,
}).
Sync(f.pipeline.ctx)
if err != nil {
panic(err)
}
return f
}