Skip to content

Commit f2e976e

Browse files
authored
Merge branch 'coder:main' into bugfix/docker-compose-version
2 parents 5fc2cf1 + e149534 commit f2e976e

File tree

296 files changed

+7725
-2156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

296 files changed

+7725
-2156
lines changed

.github/workflows/coder.yaml

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ jobs:
197197

198198
- uses: hashicorp/setup-terraform@v2
199199
with:
200-
terraform_version: 1.1.2
200+
terraform_version: 1.1.9
201201
terraform_wrapper: false
202202

203203
- name: Test with Mock Database
@@ -264,33 +264,11 @@ jobs:
264264

265265
- uses: hashicorp/setup-terraform@v2
266266
with:
267-
terraform_version: 1.1.2
267+
terraform_version: 1.1.9
268268
terraform_wrapper: false
269269

270-
- name: Start PostgreSQL Database
271-
env:
272-
POSTGRES_PASSWORD: postgres
273-
POSTGRES_USER: postgres
274-
POSTGRES_DB: postgres
275-
PGDATA: /tmp
276-
run: |
277-
docker run \
278-
-e POSTGRES_PASSWORD=postgres \
279-
-e POSTGRES_USER=postgres \
280-
-e POSTGRES_DB=postgres \
281-
-e PGDATA=/tmp \
282-
-p 5432:5432 \
283-
-d postgres:11 \
284-
-c shared_buffers=1GB \
285-
-c max_connections=1000
286-
while ! pg_isready -h 127.0.0.1
287-
do
288-
echo "$(date) - waiting for database to start"
289-
sleep 0.5
290-
done
291-
292270
- name: Test with PostgreSQL Database
293-
run: "make test-postgres"
271+
run: make test-postgres
294272

295273
- name: Upload DataDog Trace
296274
if: always() && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork
@@ -494,7 +472,7 @@ jobs:
494472

495473
- uses: hashicorp/setup-terraform@v2
496474
with:
497-
terraform_version: 1.1.2
475+
terraform_version: 1.1.9
498476
terraform_wrapper: false
499477

500478
- uses: actions/setup-node@v3

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ node_modules
1313
vendor
1414
.eslintcache
1515
yarn-error.log
16+
gotests.coverage
1617
.idea
1718
.DS_Store
1819

@@ -39,3 +40,4 @@ site/out/
3940

4041
.vscode/*.log
4142
**/*.swp
43+
.coderv2/*

Makefile

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum
2020

2121
mkdir -p ./dist
2222
rm -rf ./dist/coder-slim_*
23+
rm -f ./site/out/bin/coder*
2324
./scripts/build_go_slim.sh \
25+
--compress 6 \
2426
--version "$(VERSION)" \
2527
--output ./dist/ \
2628
linux:amd64,armv7,arm64 \
@@ -31,6 +33,7 @@ bin: $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum
3133
build: site/out/index.html $(shell find . -not -path './vendor/*' -type f -name '*.go') go.mod go.sum $(shell find ./examples/templates)
3234
rm -rf ./dist
3335
mkdir -p ./dist
36+
rm -f ./site/out/bin/coder*
3437

3538
# build slim artifacts and copy them to the site output directory
3639
./scripts/build_go_slim.sh \
@@ -57,11 +60,12 @@ coderd/database/dump.sql: $(wildcard coderd/database/migrations/*.sql)
5760
go run coderd/database/dump/main.go
5861

5962
# Generates Go code for querying the database.
60-
coderd/database/querier.go: coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql)
63+
coderd/database/querier.go: coderd/database/sqlc.yaml coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql)
6164
coderd/database/generate.sh
6265

66+
# This target is deprecated, as GNU make has issues passing signals to subprocesses.
6367
dev:
64-
./scripts/develop.sh
68+
@echo Please run ./scripts/develop.sh manually.
6569
.PHONY: dev
6670

6771
fmt/prettier:
@@ -167,14 +171,15 @@ test: test-clean
167171
gotestsum -- -v -short ./...
168172
.PHONY: test
169173

170-
test-postgres: test-clean
171-
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
172-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
173-
-coverpkg=./...,github.com/coder/coder/codersdk \
174-
-count=1 -race -failfast
174+
test-postgres: test-clean test-postgres-docker
175+
DB=ci DB_FROM=$(shell go run scripts/migrate-ci/main.go) gotestsum --junitfile="gotests.xml" --packages="./..." -- \
176+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
177+
-coverpkg=./...,github.com/coder/coder/codersdk \
178+
-count=2 -race -failfast
175179
.PHONY: test-postgres
176180

177181
test-postgres-docker:
182+
docker rm -f test-postgres-docker || true
178183
docker run \
179184
--env POSTGRES_PASSWORD=postgres \
180185
--env POSTGRES_USER=postgres \
@@ -185,12 +190,17 @@ test-postgres-docker:
185190
--name test-postgres-docker \
186191
--restart no \
187192
--detach \
188-
postgres:11 \
193+
postgres:13 \
189194
-c shared_buffers=1GB \
190195
-c max_connections=1000 \
191196
-c fsync=off \
192197
-c synchronous_commit=off \
193198
-c full_page_writes=off
199+
while ! pg_isready -h 127.0.0.1
200+
do
201+
echo "$(date) - waiting for database to start"
202+
sleep 0.5
203+
done
194204
.PHONY: test-postgres-docker
195205

196206
test-clean:

agent/agent.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ import (
2727
"go.uber.org/atomic"
2828
gossh "golang.org/x/crypto/ssh"
2929
"golang.org/x/xerrors"
30+
"inet.af/netaddr"
31+
"tailscale.com/types/key"
3032

3133
"cdr.dev/slog"
3234
"github.com/coder/coder/agent/usershell"
3335
"github.com/coder/coder/peer"
36+
"github.com/coder/coder/peer/peerwg"
3437
"github.com/coder/coder/peerbroker"
3538
"github.com/coder/coder/pty"
3639
"github.com/coder/retry"
@@ -43,20 +46,31 @@ const (
4346
)
4447

4548
type Options struct {
49+
EnableWireguard bool
50+
UploadWireguardKeys UploadWireguardKeys
51+
ListenWireguardPeers ListenWireguardPeers
4652
ReconnectingPTYTimeout time.Duration
4753
EnvironmentVariables map[string]string
4854
Logger slog.Logger
4955
}
5056

5157
type Metadata struct {
52-
OwnerEmail string `json:"owner_email"`
53-
OwnerUsername string `json:"owner_username"`
54-
EnvironmentVariables map[string]string `json:"environment_variables"`
55-
StartupScript string `json:"startup_script"`
56-
Directory string `json:"directory"`
58+
WireguardAddresses []netaddr.IPPrefix `json:"addresses"`
59+
OwnerEmail string `json:"owner_email"`
60+
OwnerUsername string `json:"owner_username"`
61+
EnvironmentVariables map[string]string `json:"environment_variables"`
62+
StartupScript string `json:"startup_script"`
63+
Directory string `json:"directory"`
64+
}
65+
66+
type WireguardPublicKeys struct {
67+
Public key.NodePublic `json:"public"`
68+
Disco key.DiscoPublic `json:"disco"`
5769
}
5870

5971
type Dialer func(ctx context.Context, logger slog.Logger) (Metadata, *peerbroker.Listener, error)
72+
type UploadWireguardKeys func(ctx context.Context, keys WireguardPublicKeys) error
73+
type ListenWireguardPeers func(ctx context.Context, logger slog.Logger) (<-chan peerwg.Handshake, func(), error)
6074

6175
func New(dialer Dialer, options *Options) io.Closer {
6276
if options == nil {
@@ -73,6 +87,9 @@ func New(dialer Dialer, options *Options) io.Closer {
7387
closeCancel: cancelFunc,
7488
closed: make(chan struct{}),
7589
envVars: options.EnvironmentVariables,
90+
enableWireguard: options.EnableWireguard,
91+
postKeys: options.UploadWireguardKeys,
92+
listenWireguardPeers: options.ListenWireguardPeers,
7693
}
7794
server.init(ctx)
7895
return server
@@ -95,6 +112,11 @@ type agent struct {
95112
metadata atomic.Value
96113
startupScript atomic.Bool
97114
sshServer *ssh.Server
115+
116+
enableWireguard bool
117+
network *peerwg.Network
118+
postKeys UploadWireguardKeys
119+
listenWireguardPeers ListenWireguardPeers
98120
}
99121

100122
func (a *agent) run(ctx context.Context) {
@@ -138,6 +160,13 @@ func (a *agent) run(ctx context.Context) {
138160
}()
139161
}
140162

163+
if a.enableWireguard {
164+
err = a.startWireguard(ctx, metadata.WireguardAddresses)
165+
if err != nil {
166+
a.logger.Error(ctx, "start wireguard", slog.Error(err))
167+
}
168+
}
169+
141170
for {
142171
conn, err := peerListener.Accept()
143172
if err != nil {
@@ -366,17 +395,17 @@ func (a *agent) createCommand(ctx context.Context, rawCommand string, env []stri
366395

367396
// Load environment variables passed via the agent.
368397
// These should override all variables we manually specify.
369-
for key, value := range metadata.EnvironmentVariables {
398+
for envKey, value := range metadata.EnvironmentVariables {
370399
// Expanding environment variables allows for customization
371400
// of the $PATH, among other variables. Customers can prepand
372401
// or append to the $PATH, so allowing expand is required!
373-
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, os.ExpandEnv(value)))
402+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", envKey, os.ExpandEnv(value)))
374403
}
375404

376405
// Agent-level environment variables should take over all!
377406
// This is used for setting agent-specific variables like "CODER_AGENT_TOKEN".
378-
for key, value := range a.envVars {
379-
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", key, value))
407+
for envKey, value := range a.envVars {
408+
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", envKey, value))
380409
}
381410

382411
return cmd, nil
@@ -438,6 +467,7 @@ func (a *agent) handleSSHSession(session ssh.Session) error {
438467
}
439468
go func() {
440469
_, _ = io.Copy(stdinPipe, session)
470+
_ = stdinPipe.Close()
441471
}()
442472
err = cmd.Start()
443473
if err != nil {

agent/agent_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"testing"
1717
"time"
1818

19+
scp "github.com/bramvdbogaerde/go-scp"
1920
"github.com/google/uuid"
2021
"github.com/pion/udp"
2122
"github.com/pion/webrtc/v3"
@@ -149,6 +150,20 @@ func TestAgent(t *testing.T) {
149150
require.NoError(t, err)
150151
})
151152

153+
t.Run("SCP", func(t *testing.T) {
154+
t.Parallel()
155+
sshClient, err := setupAgent(t, agent.Metadata{}, 0).SSHClient()
156+
require.NoError(t, err)
157+
scpClient, err := scp.NewClientBySSH(sshClient)
158+
require.NoError(t, err)
159+
tempFile := filepath.Join(t.TempDir(), "scp")
160+
content := "hello world"
161+
err = scpClient.CopyFile(context.Background(), strings.NewReader(content), tempFile, "0755")
162+
require.NoError(t, err)
163+
_, err = os.Stat(tempFile)
164+
require.NoError(t, err)
165+
})
166+
152167
t.Run("EnvironmentVariables", func(t *testing.T) {
153168
t.Parallel()
154169
key := "EXAMPLE"

agent/reaper/reaper.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package reaper
2+
3+
import "github.com/hashicorp/go-reap"
4+
5+
type Option func(o *options)
6+
7+
// WithExecArgs specifies the exec arguments for the fork exec call.
8+
// By default the same arguments as the parent are used as dictated by
9+
// os.Args. Since ForkReap calls a fork-exec it is the responsibility of
10+
// the caller to avoid fork-bombing oneself.
11+
func WithExecArgs(args ...string) Option {
12+
return func(o *options) {
13+
o.ExecArgs = args
14+
}
15+
}
16+
17+
// WithPIDCallback sets the channel that reaped child process PIDs are pushed
18+
// onto.
19+
func WithPIDCallback(ch reap.PidCh) Option {
20+
return func(o *options) {
21+
o.PIDs = ch
22+
}
23+
}
24+
25+
type options struct {
26+
ExecArgs []string
27+
PIDs reap.PidCh
28+
}

agent/reaper/reaper_stub.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
package reaper
44

5-
import "github.com/hashicorp/go-reap"
6-
7-
// IsChild returns true if we're the forked process.
8-
func IsChild() bool {
9-
return false
10-
}
11-
125
// IsInitProcess returns true if the current process's PID is 1.
136
func IsInitProcess() bool {
147
return false
158
}
169

17-
func ForkReap(_ reap.PidCh) error {
10+
func ForkReap(opt ...Option) error {
1811
return nil
1912
}

agent/reaper/reaper_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,16 @@ func TestReap(t *testing.T) {
2424
t.Skip("Detected CI, skipping reaper tests")
2525
}
2626

27-
// Because we're forkexecing these tests will try to run twice...
28-
if reaper.IsChild() {
29-
t.Skip("I'm a child!")
30-
}
31-
3227
// OK checks that's the reaper is successfully reaping
3328
// exited processes and passing the PIDs through the shared
3429
// channel.
3530
t.Run("OK", func(t *testing.T) {
3631
pids := make(reap.PidCh, 1)
37-
err := reaper.ForkReap(pids)
32+
err := reaper.ForkReap(
33+
reaper.WithPIDCallback(pids),
34+
// Provide some argument that immediately exits.
35+
reaper.WithExecArgs("/bin/sh", "-c", "exit 0"),
36+
)
3837
require.NoError(t, err)
3938

4039
cmd := exec.Command("tail", "-f", "/dev/null")

0 commit comments

Comments
 (0)