a964f74dff8849e3f392b173a1a1a4fb0d8593f3

Author
Toby Padilla <toby@charm.sh>
Committer
Toby Padilla <toby@charm.sh>
Date

Message

Move server into separate module

Diff

This diff is truncated to protect this page.

  1diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml
  2deleted file mode 100644
  3index 4543ee2284951e92c1cc23a1ef000375b5a4c735..0000000000000000000000000000000000000000
  4--- a/.github/workflows/cd.yml
  5+++ /dev/null
  6@@ -1,182 +0,0 @@
  7-name: CD
  8-
  9-on:
 10-  push:
 11-    branches:
 12-      - main
 13-  pull_request:
 14-
 15-
 16-jobs:
 17-  cd:
 18-    strategy:
 19-      matrix:
 20-        go-version: [~1.17]
 21-    runs-on: ubuntu-latest
 22-    env:
 23-      GO111MODULE: "on"
 24-      CONTAINER_REPO: "ghcr.io/${{ github.repository }}"
 25-      ENVIRONMENT: development
 26-      AWS_DEFAULT_REGION: us-east-1
 27-      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
 28-      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
 29-      SOFT_SERVE_REPO_KEYS: "${{ secrets.SOFT_SERVE_REPO_KEYS }}"
 30-
 31-    steps:
 32-    - name: Install Go
 33-      uses: actions/setup-go@v2
 34-      with:
 35-        go-version: ${{ matrix.go-version }}
 36-
 37-    - name: Checkout code
 38-      uses: actions/checkout@v2
 39-      with:
 40-        fetch-depth: 0
 41-
 42-    # Remove this later
 43-    - name: Clone internal repositories
 44-      run: |
 45-        git clone -b release https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/charm-internal ../charm
 46-        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/bubbletea-internal ../bubbletea
 47-        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/wish ../wish
 48-
 49-    - name: Login to GitHub Container Registry
 50-      uses: docker/login-action@v1
 51-      if: github.event_name == 'push'
 52-      with:
 53-        registry: ghcr.io
 54-        username: ${{ github.repository_owner }}
 55-        password: ${{ secrets.GITHUB_TOKEN }}
 56-
 57-    - name: Build Docker images using GoReleaser
 58-      uses: goreleaser/goreleaser-action@master
 59-      if: github.event_name == 'push'
 60-      with:
 61-        version: latest
 62-        # https://github.com/goreleaser/goreleaser/discussions/1534
 63-        args: -f .goreleaser.yml --snapshot
 64-
 65-    # Must add GH Actions write access
 66-    # https://docs.github.com/en/packages/managing-github-packages-using-github-actions-workflows/publishing-and-installing-a-package-with-github-actions
 67-    - name: Push Docker images
 68-      if: github.event_name == 'push'
 69-      run: |
 70-        docker push $CONTAINER_REPO:snapshot
 71-        docker push $CONTAINER_REPO:$GITHUB_SHA-snapshot
 72-
 73-    - name: Setup Terraform
 74-      uses: hashicorp/setup-terraform@v1
 75-      with:
 76-        # terraform_version: 0.13.0
 77-        cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
 78-
 79-    - name: Terraform Variables
 80-      id: tfvars
 81-      run: |
 82-        cat <<EOF >live.auto.tfvars
 83-        environment        = "$ENVIRONMENT"
 84-        aws_region         = "$AWS_DEFAULT_REGION"
 85-        app_image          = "$CONTAINER_REPO:$GITHUB_SHA-snapshot"
 86-        authorization_keys = <<EOT
 87-        $SOFT_SERVE_REPO_KEYS
 88-        EOT
 89-        EOF
 90-        TF_VARS="-var-file=live.auto.tfvars"
 91-        echo "::set-output name=vars::$TF_VARS"
 92-
 93-    - name: Terraform Format
 94-      id: fmt
 95-      run: terraform fmt -check
 96-
 97-    - name: Terraform Init
 98-      id: init
 99-      run: terraform init
100-
101-    - name: Terraform Validate
102-      id: validate
103-      run: terraform validate -no-color
104-
105-    - name: Terraform Plan
106diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
107deleted file mode 100644
108index 3b17b83d7ab869ff6c63a500ebfbe8c93c217aa9..0000000000000000000000000000000000000000
109--- a/.github/workflows/ci.yml
110+++ /dev/null
111@@ -1,34 +0,0 @@
112-name: CI
113-
114-on:
115-  push:
116-    branches: [ main ]
117-  pull_request:
118-
119-jobs:
120-
121-  build:
122-    strategy:
123-      matrix:
124-        go-version: [~1.17]
125-    runs-on: ubuntu-latest
126-    steps:
127-    - uses: actions/checkout@v2
128-
129-    # Remove this later
130-    - name: Clone internal repositories
131-      run: |
132-        git clone -b release https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/charm-internal ../charm
133-        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/bubbletea-internal ../bubbletea
134-        git clone -b master https://${{ secrets.ACCESS_TOKEN }}@github.com/charmbracelet/wish ../wish
135-
136-    - name: Set up Go
137-      uses: actions/setup-go@v2
138-      with:
139-        go-version: ${{ matrix.go-version }}
140-
141-    - name: Build
142-      run: go build -v ./...
143-
144-    - name: Test
145-      run: go test -v ./...
146diff --git a/.github/workflows/soft-serve.yml b/.github/workflows/soft-serve.yml
147deleted file mode 100644
148index 311bb63f478aaeb2b7b0d93f3da03d7da35a34a2..0000000000000000000000000000000000000000
149--- a/.github/workflows/soft-serve.yml
150+++ /dev/null
151@@ -1,22 +0,0 @@
152-name: Soft-Serve
153-
154-on:
155-  push:
156-    branches:
157-      - main
158-
159-jobs:
160-  softserve:
161-    runs-on: ubuntu-latest
162-    steps:
163-      - name: Checkout code
164-        uses: actions/checkout@v2
165-        with:
166-          fetch-depth: 0
167-
168-      - name: Push to Soft-Serve
169-        uses: charmbracelet/soft-serve-action@v1
170-        with:
171-          server: "beta.charm.sh"
172-          ssh-key: "${{ secrets.CHARM_SOFT_SERVE_KEY }}"
173-          name: "soft-serve"
174diff --git a/.gitignore b/.gitignore
175index afadd7767fc59b5be7cf7e842c28ebc8535e4d76..343b19865854d6c96f2dff68a316a7498c90922b 100644
176--- a/.gitignore
177+++ b/.gitignore
178@@ -1,7 +1,3 @@
179-soft-serve
180-.ssh
181-.repos
182-dist
183-.terraform*
184-*.tfstate*
185-*auto.tfvars
186+cmd/soft/soft
187+cmd/soft/.ssh
188+cmd/soft/.repos
189diff --git a/Dockerfile b/Dockerfile
190deleted file mode 100644
191index 447d7c4cdb52e0b47f0a7dcdc6dfb050900f1fed..0000000000000000000000000000000000000000
192--- a/Dockerfile
193+++ /dev/null
194@@ -1,23 +0,0 @@
195-FROM alpine:latest
196-
197-RUN apk update && apk add --update nfs-utils git && rm -rf /var/cache/apk/*
198-
199-COPY soft-serve /usr/local/bin/soft-serve
200-
201-# Create directories
202-WORKDIR /soft-serve
203-# Expose data volume
204-VOLUME /soft-serve
205-
206-# Environment variables
207-ENV SOFT_SERVE_KEY_PATH "/soft-serve/ssh/soft_serve_server_ed25519"
208-ENV SOFT_SERVE_REPO_KEYS ""
209-ENV SOFT_SERVE_REPO_KEYS_PATH "/soft-serve/ssh/soft_serve_git_authorized_keys"
210-ENV SOFT_SERVE_REPO_PATH "/soft-serve/repos"
211-
212-# Expose ports
213-# SSH
214-EXPOSE 23231/tcp
215-
216-# Set the default command
217-ENTRYPOINT [ "/usr/local/bin/soft-serve" ]
218diff --git a/cmd/soft/main.go b/cmd/soft/main.go
219new file mode 100644
220index 0000000000000000000000000000000000000000..49fbd734be7a36b347ff4eb380423f1112292920
221--- /dev/null
222+++ b/cmd/soft/main.go
223@@ -0,0 +1,37 @@
224+package main
225+
226+import (
227+	"log"
228+
229+	"github.com/charmbracelet/soft"
230+
231+	"github.com/meowgorithm/babyenv"
232+)
233+
234+type serverConfig struct {
235+	Host     string `env:"SOFT_SERVE_HOST" default:""`
236+	Port     int    `env:"SOFT_SERVE_PORT" default:"23231"`
237+	KeyPath  string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
238+	RepoPath string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
239+	AuthKey  string `env:"SOFT_SERVE_AUTH_KEY" default:""`
240+}
241+
242+func main() {
243+	var cfg serverConfig
244+	err := babyenv.Parse(&cfg)
245+	if err != nil {
246+		log.Fatalln(err)
247+	}
248+	s := soft.NewServer(
249+		cfg.Host,
250+		cfg.Port,
251+		cfg.KeyPath,
252+		cfg.RepoPath,
253+		cfg.AuthKey,
254+	)
255+	log.Printf("Starting SSH server on %s:%d\n", cfg.Host, cfg.Port)
256+	err = s.ListenAndServe()
257+	if err != nil {
258+		log.Fatalln(err)
259+	}
260+}
261diff --git a/config/config.go b/config/config.go
262index f36c943312f7b119283fa332df9fe8dd53aabe8d..8eafe28106c904b2036b2696eb470c97b19c7ea6 100644
263--- a/config/config.go
264+++ b/config/config.go
265@@ -6,8 +6,8 @@ import (
266 	"fmt"
267 	"os"
268 	"path/filepath"
269-	"soft-serve/git"
270 
271+	"github.com/charmbracelet/soft/git"
272 	gg "github.com/go-git/go-git/v5"
273 	"github.com/go-git/go-git/v5/plumbing/object"
274 )
275diff --git a/go.mod b/go.mod
276index c7408bd7120adf343dfb9e4084c5edbbe8d21f2e..d1ad65ecf9fb3e5b05acb5dbddcad2472d29f268 100644
277--- a/go.mod
278+++ b/go.mod
279@@ -1,4 +1,4 @@
280-module soft-serve
281+module github.com/charmbracelet/soft
282 
283 go 1.17
284 
285diff --git a/main.go b/main.go
286deleted file mode 100644
287index 9ee1f8d3bd2d5b26d2b3ca36e2dd643878a9821d..0000000000000000000000000000000000000000
288--- a/main.go
289+++ /dev/null
290@@ -1,59 +0,0 @@
291-package main
292-
293-import (
294-	"fmt"
295-	"log"
296-	"soft-serve/config"
297-	"soft-serve/git"
298-	"soft-serve/tui"
299-
300-	"github.com/charmbracelet/wish"
301-	bm "github.com/charmbracelet/wish/bubbletea"
302-	gm "github.com/charmbracelet/wish/git"
303-	lm "github.com/charmbracelet/wish/logging"
304-	"github.com/gliderlabs/ssh"
305-
306-	"github.com/meowgorithm/babyenv"
307-)
308-
309-type serverConfig struct {
310-	Port     int    `env:"SOFT_SERVE_PORT" default:"23231"`
311-	Host     string `env:"SOFT_SERVE_HOST" default:""`
312-	InitKey  string `env:"SOFT_SERVE_REPO_KEY" default:""`
313-	KeyPath  string `env:"SOFT_SERVE_KEY_PATH" default:".ssh/soft_serve_server_ed25519"`
314-	RepoPath string `env:"SOFT_SERVE_REPO_PATH" default:".repos"`
315-}
316-
317-func main() {
318-	var scfg serverConfig
319-	var cfg *config.Config
320-	var err error
321-	err = babyenv.Parse(&scfg)
322-	if err != nil {
323-		log.Fatalln(err)
324-	}
325-	rs := git.NewRepoSource(scfg.RepoPath)
326-	cfg, err = config.NewConfig(scfg.Host, scfg.Port, scfg.InitKey, rs)
327-	if err != nil {
328-		log.Fatalln(err)
329-	}
330-	s, err := wish.NewServer(
331-		ssh.PublicKeyAuth(cfg.PublicKeyHandler),
332-		ssh.PasswordAuth(cfg.PasswordHandler),
333-		wish.WithAddress(fmt.Sprintf("%s:%d", scfg.Host, scfg.Port)),
334-		wish.WithHostKeyPath(scfg.KeyPath),
335-		wish.WithMiddlewares(
336-			bm.Middleware(tui.SessionHandler(cfg)),
337-			gm.Middleware(scfg.RepoPath, cfg),
338-			lm.Middleware(),
339-		),
340-	)
341-	if err != nil {
342-		log.Fatalln(err)
343-	}
344-	log.Printf("Starting SSH server on %s:%d\n", scfg.Host, scfg.Port)
345-	err = s.ListenAndServe()
346-	if err != nil {
347-		log.Fatalln(err)
348-	}
349-}
350diff --git a/main.tf b/main.tf
351deleted file mode 100644
352index 192b4852a679e16b21b11a4b0d6f7ad8c87be255..0000000000000000000000000000000000000000
353--- a/main.tf
354+++ /dev/null
355@@ -1,45 +0,0 @@
356-terraform {
357-  backend "s3" {
358-    bucket = "charm-terraform-backend"
359-    key    = "soft-serve-development"
360-    region = "us-east-1"
361-  }
362-}
363-
364-variable "environment" {
365-  default = "development"
366-}
367-
368-variable "aws_region" {
369-  default = "us-east-1"
370-}
371-
372-variable "app_image" {
373-  default = "ghcr.io/charmbracelet/soft-serve-internal:snapshot"
374-}
375-
376-variable "force_new_deployment" {
377-  default = false
378-}
379-
380-variable "authorization_keys" {
381-  default = ""
382-}
383-
384-module "soft_serve" {
385-  # source = "../terraform-aws-soft-serve"
386-  source  = "app.terraform.io/charm/soft-serve/aws"
387-  version = "0.3.2"
388-
389-  environment                  = var.environment
390-  aws_region                   = var.aws_region
391-  ecs_task_execution_role_name = "softServeEcsTaskExecutionRole-${var.environment}"
392-  app_image                    = var.app_image
393-  app_count                    = 2
394-  app_ssh_port                 = 23231
395-  fargate_cpu                  = "1024"
396-  fargate_memory               = "2048"
397-  force_new_deployment         = var.force_new_deployment
398-  app_use_default_ssh_port     = true
399-  authorization_keys           = var.authorization_keys
400-}
401diff --git a/server.go b/server.go
402new file mode 100644
403index 0000000000000000000000000000000000000000..fc5ae2a24f058545ada0a409d69033521070e24b
404--- /dev/null
405+++ b/server.go
406@@ -0,0 +1,44 @@
407+package soft
408+
409+import (
410+	"fmt"
411+	"log"
412+
413+	"github.com/charmbracelet/soft/config"
414+	"github.com/charmbracelet/soft/git"
415+	"github.com/charmbracelet/soft/tui"
416+
417+	"github.com/charmbracelet/wish"
418+	bm "github.com/charmbracelet/wish/bubbletea"
419+	gm "github.com/charmbracelet/wish/git"
420+	lm "github.com/charmbracelet/wish/logging"
421+	"github.com/gliderlabs/ssh"
422+)
423+
424+// NewServer returns a new *ssh.Server configured to serve Soft Serve. The SSH
425+// server key-pair will be created if none exists. An initial admin SSH public
426+// key can be provided with authKey. If authKey is provided, access will be
427+// restricted to that key. If authKey is not provided, the server will be
428+// publicly writable until configured otherwise by cloning the `config` repo.
429+func NewServer(host string, port int, serverKeyPath string, repoPath string, authKey string) *ssh.Server {
430+	rs := git.NewRepoSource(repoPath)
431+	cfg, err := config.NewConfig(host, port, authKey, rs)
432+	if err != nil {
433+		log.Fatalln(err)
434+	}
435+	s, err := wish.NewServer(
436+		ssh.PublicKeyAuth(cfg.PublicKeyHandler),
437+		ssh.PasswordAuth(cfg.PasswordHandler),
438+		wish.WithAddress(fmt.Sprintf("%s:%d", host, port)),
439+		wish.WithHostKeyPath(serverKeyPath),
440+		wish.WithMiddlewares(
441+			bm.Middleware(tui.SessionHandler(cfg)),
442+			gm.Middleware(repoPath, cfg),
443+			lm.Middleware(),
444+		),
445+	)
446+	if err != nil {
447+		log.Fatalln(err)
448+	}
449+	return s
450+}
451diff --git a/tui/bubble.go b/tui/bubble.go
452index 400a249ee7c813f3b3b9119c978e5586442e2ecf..ef899922505aba30939cbc53a855cdf1dfe97a04 100644
453--- a/tui/bubble.go
454+++ b/tui/bubble.go
455@@ -2,15 +2,15 @@ package tui
456 
457 import (
458 	"fmt"
459-	"soft-serve/config"
460-	"soft-serve/git"
461-	"soft-serve/tui/bubbles/repo"
462-	"soft-serve/tui/bubbles/selection"
463-	"soft-serve/tui/style"
464 	"strings"
465 
466 	tea "github.com/charmbracelet/bubbletea"
467 	"github.com/charmbracelet/lipgloss"
468+	"github.com/charmbracelet/soft/config"
469+	"github.com/charmbracelet/soft/git"
470+	"github.com/charmbracelet/soft/tui/bubbles/repo"
471+	"github.com/charmbracelet/soft/tui/bubbles/selection"
472+	"github.com/charmbracelet/soft/tui/style"
473 )
474 
475 type sessionState int
476diff --git a/tui/bubbles/commits/bubble.go b/tui/bubbles/commits/bubble.go
477index 593bceb81f111459bbf5a57a0bd45babdd723a06..662304a117acbd52503139274e01848e65938921 100644
478--- a/tui/bubbles/commits/bubble.go
479+++ b/tui/bubbles/commits/bubble.go
480@@ -1,7 +1,7 @@
481 package commits
482 
483 import (
484-	"soft-serve/git"
485+	"soft-serve/server/git"
486 	"strings"
487 
488 	"github.com/charmbracelet/bubbles/viewport"
489diff --git a/tui/bubbles/repo/bubble.go b/tui/bubbles/repo/bubble.go
490index bf2ac0c591c724b58b391fbb32975980163ce3b1..e36effc6602c53a09f51d50edd64b417d3527a83 100644
491--- a/tui/bubbles/repo/bubble.go
492+++ b/tui/bubbles/repo/bubble.go
493@@ -4,8 +4,6 @@ import (
494 	"bytes"
495 	"fmt"
496 	"log"
497-	"soft-serve/git"
498-	"soft-serve/tui/style"
499 	"strconv"
500 	"text/template"
501 	"time"
502@@ -14,6 +12,8 @@ import (
503 	tea "github.com/charmbracelet/bubbletea"
504 	"github.com/charmbracelet/glamour"
505 	"github.com/charmbracelet/lipgloss"
506+	"github.com/charmbracelet/soft/git"
507+	"github.com/charmbracelet/soft/tui/style"
508 	"github.com/muesli/reflow/truncate"
509 	"github.com/muesli/reflow/wrap"
510 )
511diff --git a/tui/bubbles/selection/bubble.go b/tui/bubbles/selection/bubble.go
512index bd985657a2ec33c6e67cc42b222e721b055fee06..954222e1b53d8c067cb87923ab93be5bfeae91c6 100644
513--- a/tui/bubbles/selection/bubble.go
514+++ b/tui/bubbles/selection/bubble.go
515@@ -1,11 +1,11 @@
516 package selection
517 
518 import (
519-	"soft-serve/tui/style"
520 	"strings"
521 
522 	tea "github.com/charmbracelet/bubbletea"
523 	"github.com/charmbracelet/lipgloss"
524+	"github.com/charmbracelet/soft/tui/style"
525 	"github.com/muesli/reflow/truncate"
526 )
527 
528diff --git a/tui/commands.go b/tui/commands.go
529index 7f777cc2e40db1418837fd27abfeb1fd4be2a8ee..3d8ebdbc0ae86c30e3c1e692b88ee32e8bd4e1c8 100644
530--- a/tui/commands.go
531+++ b/tui/commands.go
532@@ -3,13 +3,13 @@ package tui
533 import (
534 	"fmt"
535 	"log"
536-	"soft-serve/config"
537-	"soft-serve/tui/bubbles/repo"
538-	"soft-serve/tui/bubbles/selection"
539 	"time"
540 
541 	tea "github.com/charmbracelet/bubbletea"
542 	"github.com/charmbracelet/lipgloss"
543+	"github.com/charmbracelet/soft/config"
544+	"github.com/charmbracelet/soft/tui/bubbles/repo"
545+	"github.com/charmbracelet/soft/tui/bubbles/selection"
546 	"github.com/muesli/termenv"
547 )
548 
549diff --git a/tui/defaults.go b/tui/defaults.go
550deleted file mode 100644
551index 2643296c08599b7f7d832dd7c1cdb65365cb69c3..0000000000000000000000000000000000000000
552--- a/tui/defaults.go
553+++ /dev/null
554@@ -1,94 +0,0 @@
555-package tui
556-
557-import (
558-	"os"
559-	"path/filepath"
560-	"soft-serve/git"
561-
562-	gg "github.com/go-git/go-git/v5"
563-	"github.com/go-git/go-git/v5/plumbing/object"
564-)
565-
566diff --git a/tui/session.go b/tui/session.go
567index 51eedc361eb876aef7534ae193d0b049cf73767a..c91e5a31dc0152e125b24061d6ae007d30561575 100644
568--- a/tui/session.go
569+++ b/tui/session.go
570@@ -2,9 +2,9 @@ package tui
571 
572 import (
573 	"fmt"
574-	"soft-serve/config"
575 
576 	tea "github.com/charmbracelet/bubbletea"
577+	"github.com/charmbracelet/soft/config"
578 	"github.com/gliderlabs/ssh"
579 )
580