Skip to content

Commit cc9b573

Browse files
committed
Merge remote-tracking branch 'origin/main' into stevenmasley/suspended_users
2 parents d2bc5b8 + 608eb32 commit cc9b573

Some content is hidden

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

47 files changed

+860
-617
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
charset = utf-8
6+
trim_trailing_whitespace = true
7+
insert_final_newline = true
8+
indent_style = tab
9+
10+
[*.{md,json,yaml,tf,tfvars}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[coderd/database/dump.sql]
15+
indent_style = space
16+
indent_size = 4

.github/workflows/coder.yaml

+17-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ jobs:
4444
with:
4545
version: v1.46.0
4646

47+
style-lint-shellcheck:
48+
name: style/lint/shellcheck
49+
timeout-minutes: 5
50+
runs-on: ubuntu-latest
51+
steps:
52+
- uses: actions/checkout@v3
53+
- name: Run ShellCheck
54+
uses: ludeeus/action-shellcheck@1.1.0
55+
with:
56+
ignore: node_modules
57+
4758
style-lint-typescript:
4859
name: "style/lint/typescript"
4960
timeout-minutes: 5
@@ -133,7 +144,12 @@ jobs:
133144
- name: Install node_modules
134145
run: ./scripts/yarn_install.sh
135146

136-
- run: "make --output-sync -j -B fmt"
147+
- name: Install shfmt
148+
run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.5.0
149+
150+
- run: |
151+
export PATH=${PATH}:$(go env GOPATH)/bin
152+
make --output-sync -j -B fmt
137153
138154
test-go:
139155
name: "test/go"

Makefile

+20-3
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,37 @@ fmt/terraform: $(wildcard *.tf)
4141
terraform fmt -recursive
4242
.PHONY: fmt/terraform
4343

44-
fmt: fmt/prettier fmt/terraform
44+
fmt/shfmt: $(shell shfmt -f .)
45+
@echo "--- shfmt"
46+
# Only do diff check in CI, errors on diff.
47+
ifdef CI
48+
shfmt -d $(shell shfmt -f .)
49+
else
50+
shfmt -w $(shell shfmt -f .)
51+
endif
52+
53+
fmt: fmt/prettier fmt/terraform fmt/shfmt
4554
.PHONY: fmt
4655

4756
gen: coderd/database/querier.go peerbroker/proto/peerbroker.pb.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts
4857

4958
install: build
59+
mkdir -p $(INSTALL_DIR)
5060
@echo "--- Copying from bin to $(INSTALL_DIR)"
5161
cp -r ./dist/coder-$(GOOS)_$(GOOS)_$(GOARCH)*/* $(INSTALL_DIR)
5262
@echo "-- CLI available at $(shell ls $(INSTALL_DIR)/coder*)"
5363
.PHONY: install
5464

55-
lint:
65+
lint: lint/shellcheck lint/go
66+
67+
lint/go:
5668
golangci-lint run
57-
.PHONY: lint
69+
.PHONY: lint/go
70+
71+
# Use shfmt to determine the shell files, takes editorconfig into consideration.
72+
lint/shellcheck: $(shell shfmt -f .)
73+
@echo "--- shellcheck"
74+
shellcheck $(shell shfmt -f .)
5875

5976
peerbroker/proto/peerbroker.pb.go: peerbroker/proto/peerbroker.proto
6077
protoc \

README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Follow](https://img.shields.io/twitter/follow/CoderHQ?label=%40CoderHQ&style=soc
1111
Coder creates remote development machines so you can develop your code from anywhere.
1212

1313
> **Note**:
14-
> Coder is in an alpha state. But, any serious bugs are P1 for us so please report them.
14+
> Coder is in an alpha state, but any serious bugs are P1 for us so please report them.
1515
1616
<p align="center">
1717
<img src="./docs/images/hero-image.png">
@@ -150,10 +150,23 @@ coder templates update gcp-linux
150150
- [Workspace lifecycle](./docs/workspaces.md#workspace-lifecycle)
151151
- [Updating workspaces](./docs/workspaces.md#updating-workspaces)
152152

153+
## Comparison
154+
155+
Please file [an issue](https://github.com/coder/coder/issues/new) if any information is out of date. Also refer to: [What Coder is not](./docs/about.md#what-coder-is-not).
156+
157+
| Tool | Type | Delivery Model | Cost | Environments |
158+
| :---------------------------------------------------------- | :------- | :----------------- | :---------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------- |
159+
| [Coder](https://github.com/coder/coder) | Platform | OSS + Self-Managed | Pay your cloud | All [Terraform](https://www.terraform.io/registry/providers) resources, all clouds, multi-architecture: Linux, Mac, Windows, containers, VMs, amd64, arm64 |
160+
| [code-server](https://github.com/cdr/code-server) | Web IDE | OSS + Self-Managed | Pay your cloud | Linux, Mac, Windows, containers, VMs, amd64, arm64 |
161+
| [Coder (Classic)](https://coder.com/docs) | Platform | Self-Managed | Pay your cloud + license fees | Kubernetes Linux Containers |
162+
| [GitHub Codespaces](https://github.com/features/codespaces) | Platform | SaaS | 2x Azure Compute | Linux containers |
163+
164+
---
165+
166+
_As of 5/27/22_
167+
153168
## Contributing
154169

155170
Read the [contributing docs](./docs/CONTRIBUTING.md).
156171

157-
## Contributors
158-
159172
Find our list of contributors [here](./docs/CONTRIBUTORS.md).

cli/server.go

+62-56
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ func server() *cobra.Command {
6868
pprofAddress string
6969
cacheDir string
7070
dev bool
71-
devFirstEmail string
72-
devFirstPassword string
73-
devMemberEmail string
74-
devMemberPassword string
71+
devUserEmail string
72+
devUserPassword string
7573
postgresURL string
7674
// provisionerDaemonCount is a uint8 to ensure a number > 0.
7775
provisionerDaemonCount uint8
@@ -332,30 +330,19 @@ func server() *cobra.Command {
332330
config := createConfig(cmd)
333331

334332
if dev {
335-
if devFirstPassword == "" {
336-
devFirstPassword, err = cryptorand.String(10)
333+
if devUserPassword == "" {
334+
devUserPassword, err = cryptorand.String(10)
337335
if err != nil {
338336
return xerrors.Errorf("generate random admin password for dev: %w", err)
339337
}
340338
}
341-
if devMemberPassword == "" {
342-
devMemberPassword, err = cryptorand.String(10)
343-
if err != nil {
344-
return xerrors.Errorf("generate random member password for dev: %w", err)
345-
}
346-
}
347-
348-
// Create first user with 1 additional user as a member of the same org.
349-
err = createFirstUser(cmd, client, config, devFirstEmail, devFirstPassword, extraUsers{
350-
Username: "member",
351-
Email: devMemberEmail,
352-
Password: devMemberPassword,
353-
})
339+
restorePreviousSession, err := createFirstUser(logger, cmd, client, config, devUserEmail, devUserPassword)
354340
if err != nil {
355341
return xerrors.Errorf("create first user: %w", err)
356342
}
357-
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", devFirstEmail)
358-
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", devFirstPassword)
343+
defer restorePreviousSession()
344+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", devUserEmail)
345+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", devUserPassword)
359346
_, _ = fmt.Fprintln(cmd.ErrOrStderr())
360347

361348
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), cliui.Styles.Wrap.Render(`Started in dev mode. All data is in-memory! `+cliui.Styles.Bold.Render("Do not use in production")+`. Press `+
@@ -488,10 +475,8 @@ func server() *cobra.Command {
488475
// systemd uses the CACHE_DIRECTORY environment variable!
489476
cliflag.StringVarP(root.Flags(), &cacheDir, "cache-dir", "", "CACHE_DIRECTORY", filepath.Join(os.TempDir(), "coder-cache"), "Specifies a directory to cache binaries for provision operations.")
490477
cliflag.BoolVarP(root.Flags(), &dev, "dev", "", "CODER_DEV_MODE", false, "Serve Coder in dev mode for tinkering")
491-
cliflag.StringVarP(root.Flags(), &devFirstEmail, "dev-admin-email", "", "CODER_DEV_ADMIN_EMAIL", "admin@coder.com", "Specifies the admin email to be used in dev mode (--dev)")
492-
cliflag.StringVarP(root.Flags(), &devFirstPassword, "dev-admin-password", "", "CODER_DEV_ADMIN_PASSWORD", "", "Specifies the admin password to be used in dev mode (--dev) instead of a randomly generated one")
493-
cliflag.StringVarP(root.Flags(), &devMemberEmail, "dev-member-email", "", "CODER_DEV_MEMBER_EMAIL", "member@coder.com", "Specifies the member email to be used in dev mode (--dev)")
494-
cliflag.StringVarP(root.Flags(), &devMemberPassword, "dev-member-password", "", "CODER_DEV_MEMBER_PASSWORD", "", "Specifies the member password to be used in dev mode (--dev) instead of a randomly generated one")
478+
cliflag.StringVarP(root.Flags(), &devUserEmail, "dev-admin-email", "", "CODER_DEV_ADMIN_EMAIL", "admin@coder.com", "Specifies the admin email to be used in dev mode (--dev)")
479+
cliflag.StringVarP(root.Flags(), &devUserPassword, "dev-admin-password", "", "CODER_DEV_ADMIN_PASSWORD", "", "Specifies the admin password to be used in dev mode (--dev) instead of a randomly generated one")
495480
cliflag.StringVarP(root.Flags(), &postgresURL, "postgres-url", "", "CODER_PG_CONNECTION_URL", "", "URL of a PostgreSQL database to connect to")
496481
cliflag.Uint8VarP(root.Flags(), &provisionerDaemonCount, "provisioner-daemons", "", "CODER_PROVISIONER_DAEMONS", 3, "The amount of provisioner daemons to create on start.")
497482
cliflag.StringVarP(root.Flags(), &oauth2GithubClientID, "oauth2-github-client-id", "", "CODER_OAUTH2_GITHUB_CLIENT_ID", "",
@@ -534,58 +519,79 @@ func server() *cobra.Command {
534519
return root
535520
}
536521

537-
type extraUsers struct {
538-
Username string
539-
Email string
540-
Password string
541-
}
542-
543-
func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root, email, password string, users ...extraUsers) error {
522+
// createFirstUser creates the first user and sets a valid session.
523+
// Caller must call restorePreviousSession on server exit.
524+
func createFirstUser(logger slog.Logger, cmd *cobra.Command, client *codersdk.Client, cfg config.Root, email, password string) (func(), error) {
544525
if email == "" {
545-
return xerrors.New("email is empty")
526+
return nil, xerrors.New("email is empty")
546527
}
547528
if password == "" {
548-
return xerrors.New("password is empty")
529+
return nil, xerrors.New("password is empty")
549530
}
550-
first, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{
531+
_, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{
551532
Email: email,
552533
Username: "developer",
553534
Password: password,
554535
OrganizationName: "acme-corp",
555536
})
556537
if err != nil {
557-
return xerrors.Errorf("create first user: %w", err)
538+
return nil, xerrors.Errorf("create first user: %w", err)
558539
}
559540
token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{
560541
Email: email,
561542
Password: password,
562543
})
563544
if err != nil {
564-
return xerrors.Errorf("login with first user: %w", err)
545+
return nil, xerrors.Errorf("login with first user: %w", err)
565546
}
566547
client.SessionToken = token.SessionToken
567548

549+
// capture the current session and if exists recover session on server exit
550+
restorePreviousSession := func() {}
551+
oldURL, _ := cfg.URL().Read()
552+
oldSession, _ := cfg.Session().Read()
553+
if oldURL != "" && oldSession != "" {
554+
restorePreviousSession = func() {
555+
currentURL, err := cfg.URL().Read()
556+
if err != nil {
557+
logger.Error(cmd.Context(), "failed to read current session url", slog.Error(err))
558+
return
559+
}
560+
currentSession, err := cfg.Session().Read()
561+
if err != nil {
562+
logger.Error(cmd.Context(), "failed to read current session token", slog.Error(err))
563+
return
564+
}
565+
566+
// if it's changed since we wrote to it don't restore session
567+
if currentURL != client.URL.String() ||
568+
currentSession != token.SessionToken {
569+
return
570+
}
571+
572+
err = cfg.URL().Write(oldURL)
573+
if err != nil {
574+
logger.Error(cmd.Context(), "failed to recover previous session url", slog.Error(err))
575+
return
576+
}
577+
err = cfg.Session().Write(oldSession)
578+
if err != nil {
579+
logger.Error(cmd.Context(), "failed to recover previous session token", slog.Error(err))
580+
return
581+
}
582+
}
583+
}
584+
568585
err = cfg.URL().Write(client.URL.String())
569586
if err != nil {
570-
return xerrors.Errorf("write local url: %w", err)
587+
return nil, xerrors.Errorf("write local url: %w", err)
571588
}
572589
err = cfg.Session().Write(token.SessionToken)
573590
if err != nil {
574-
return xerrors.Errorf("write session token: %w", err)
591+
return nil, xerrors.Errorf("write session token: %w", err)
575592
}
576593

577-
for _, user := range users {
578-
_, err := client.CreateUser(cmd.Context(), codersdk.CreateUserRequest{
579-
Email: user.Email,
580-
Username: user.Username,
581-
Password: user.Password,
582-
OrganizationID: first.OrganizationID,
583-
})
584-
if err != nil {
585-
return xerrors.Errorf("create extra user %q: %w", user.Email, err)
586-
}
587-
}
588-
return nil
594+
return restorePreviousSession, nil
589595
}
590596

591597
// nolint:revive
@@ -642,16 +648,16 @@ func newProvisionerDaemon(ctx context.Context, coderAPI *coderd.API,
642648
func printLogo(cmd *cobra.Command, spooky bool) {
643649
if spooky {
644650
_, _ = fmt.Fprintf(cmd.OutOrStdout(), `
645-
▄████▄ ▒█████ ▓█████▄ ▓█████ ██▀███
651+
▄████▄ ▒█████ ▓█████▄ ▓█████ ██▀███
646652
▒██▀ ▀█ ▒██▒ ██▒▒██▀ ██▌▓█ ▀ ▓██ ▒ ██▒
647653
▒▓█ ▄ ▒██░ ██▒░██ █▌▒███ ▓██ ░▄█ ▒
648-
▒▓▓▄ ▄██▒▒██ ██░░▓█▄ ▌▒▓█ ▄ ▒██▀▀█▄
654+
▒▓▓▄ ▄██▒▒██ ██░░▓█▄ ▌▒▓█ ▄ ▒██▀▀█▄
649655
▒ ▓███▀ ░░ ████▓▒░░▒████▓ ░▒████▒░██▓ ▒██▒
650656
░ ░▒ ▒ ░░ ▒░▒░▒░ ▒▒▓ ▒ ░░ ▒░ ░░ ▒▓ ░▒▓░
651657
░ ▒ ░ ▒ ▒░ ░ ▒ ▒ ░ ░ ░ ░▒ ░ ▒░
652-
░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
653-
░ ░ ░ ░ ░ ░ ░ ░
654-
░ ░
658+
░ ░ ░ ░ ▒ ░ ░ ░ ░ ░░ ░
659+
░ ░ ░ ░ ░ ░ ░ ░
660+
░ ░
655661
656662
`)
657663
return

coderd/audit/generate.sh

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
# Usage:
99
# ./generate.sh <database type> <database type> ...
1010

11-
1211
set -euo pipefail
1312

14-
cd "$(dirname "$0")" && cd "$(git rev-parse --show-toplevel)"
15-
go run ./scripts/auditgen ./coderd/database "$@"
13+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
14+
PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel)
15+
16+
(
17+
cd "$PROJECT_ROOT"
18+
go run ./scripts/auditgen ./coderd/database "$@"
19+
)

0 commit comments

Comments
 (0)