diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a5aca5a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +.git +.github +.env +.env.* +!.env.example +dist/ +*_test.go +deploy-everything +grpc-server +README.md +Makefile +Dockerfile +.dockerignore diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c95394b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,83 @@ +name: CI + +on: + pull_request: + branches: [master] + workflow_dispatch: + +concurrency: + group: ci-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + verify: + name: Lint, test, build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: gofmt + run: | + out="$(gofmt -l .)" + if [ -n "$out" ]; then + echo "::error::gofmt required on:"; echo "$out"; exit 1 + fi + + - name: go vet + run: go vet ./... + + - name: go build (both binaries) + run: make build + + # Integration tests self-skip when EASYPANEL_ENDPOINT / EASYPANEL_API_KEY are absent. + - name: go test + run: go test ./... -count=1 + + docker: + name: Docker build (no push) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-buildx-action@v3 + + - name: Build server image + uses: docker/build-push-action@v6 + with: + context: . + target: server + push: false + load: true + tags: deploy-everything:pr-${{ github.event.pull_request.number || 'manual' }} + build-args: | + VERSION=pr-${{ github.event.pull_request.number || 'manual' }} + COMMIT=${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Build CLI image + uses: docker/build-push-action@v6 + with: + context: . + target: cli + push: false + load: true + tags: deploy-everything-cli:pr-${{ github.event.pull_request.number || 'manual' }} + build-args: | + VERSION=pr-${{ github.event.pull_request.number || 'manual' }} + COMMIT=${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Smoke test images + run: | + docker run --rm deploy-everything-cli:pr-${{ github.event.pull_request.number || 'manual' }} version + docker run --rm deploy-everything:pr-${{ github.event.pull_request.number || 'manual' }} version diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..eab60b3 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,259 @@ +name: Build & Release + +# Tagging policy +# push to master -> images tagged :dev and :dev- (no GitHub Release) +# push tag vX.Y.Z -> images tagged :X.Y.Z :X.Y :X :latest +# + cross-compiled binaries attached to a GitHub Release +# pre-release tags (vX.Y.Z-rc1) get :X.Y.Z-rc1 only, never :latest +on: + push: + branches: [master] + tags: ['v*'] + workflow_dispatch: + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +env: + REGISTRY: ghcr.io + SERVER_IMAGE: ghcr.io/${{ github.repository }} + CLI_IMAGE: ghcr.io/${{ github.repository }}-cli + +jobs: + # ------------------------------------------------------------------ + # Gate: nothing gets published unless vet/build/test pass. + # ------------------------------------------------------------------ + verify: + name: Verify + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - run: go vet ./... + - run: go build ./... + # Integration tests self-skip without EASYPANEL_ENDPOINT / EASYPANEL_API_KEY. + - run: go test ./... -count=1 + + # ------------------------------------------------------------------ + # Resolve version metadata once, reuse everywhere. + # ------------------------------------------------------------------ + meta: + name: Resolve version + runs-on: ubuntu-latest + needs: verify + outputs: + version: ${{ steps.v.outputs.version }} + is_release: ${{ steps.v.outputs.is_release }} + commit: ${{ steps.v.outputs.commit }} + build_date: ${{ steps.v.outputs.build_date }} + steps: + - uses: actions/checkout@v4 + + - id: v + run: | + if [[ "${GITHUB_REF}" == refs/tags/v* ]]; then + echo "version=${GITHUB_REF#refs/tags/}" >> "$GITHUB_OUTPUT" + echo "is_release=true" >> "$GITHUB_OUTPUT" + else + echo "version=dev" >> "$GITHUB_OUTPUT" + echo "is_release=false" >> "$GITHUB_OUTPUT" + fi + echo "commit=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT" + echo "build_date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT" + + # ------------------------------------------------------------------ + # Multi-arch images -> GHCR. One matrix leg per image target. + # ------------------------------------------------------------------ + images: + name: Image (${{ matrix.target }}) + runs-on: ubuntu-latest + needs: meta + permissions: + contents: read + packages: write + attestations: write + id-token: write + strategy: + fail-fast: false + matrix: + include: + - target: server + image: ghcr.io/${{ github.repository }} + - target: cli + image: ghcr.io/${{ github.repository }}-cli + steps: + - uses: actions/checkout@v4 + + - uses: docker/setup-qemu-action@v3 + - uses: docker/setup-buildx-action@v3 + + - name: Login to GHCR + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Docker tags & labels + id: dm + uses: docker/metadata-action@v5 + with: + images: ${{ matrix.image }} + flavor: latest=false + tags: | + type=raw,value=dev,enable=${{ github.ref == 'refs/heads/master' }} + type=sha,prefix=dev-,format=short,enable=${{ github.ref == 'refs/heads/master' }} + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=semver,pattern={{major}},enable=${{ !contains(github.ref, '-') }} + type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/v') && !contains(github.ref, '-') }} + labels: | + org.opencontainers.image.version=${{ needs.meta.outputs.version }} + + - name: Build & push + id: push + uses: docker/build-push-action@v6 + with: + context: . + target: ${{ matrix.target }} + platforms: linux/amd64,linux/arm64 + push: true + provenance: mode=max + sbom: true + tags: ${{ steps.dm.outputs.tags }} + labels: ${{ steps.dm.outputs.labels }} + build-args: | + VERSION=${{ needs.meta.outputs.version }} + COMMIT=${{ needs.meta.outputs.commit }} + BUILD_DATE=${{ needs.meta.outputs.build_date }} + cache-from: type=gha,scope=${{ matrix.target }} + cache-to: type=gha,mode=max,scope=${{ matrix.target }} + + - name: Attest provenance + uses: actions/attest-build-provenance@v2 + with: + subject-name: ${{ matrix.image }} + subject-digest: ${{ steps.push.outputs.digest }} + push-to-registry: true + + - name: Summary + run: | + { + echo "### ${{ matrix.image }}" + echo '```' + echo "${{ steps.dm.outputs.tags }}" + echo '```' + echo "digest: \`${{ steps.push.outputs.digest }}\`" + } >> "$GITHUB_STEP_SUMMARY" + + # ------------------------------------------------------------------ + # Release binaries (tags only). + # ------------------------------------------------------------------ + binaries: + name: Binary ${{ matrix.goos }}/${{ matrix.goarch }} + runs-on: ubuntu-latest + needs: meta + if: needs.meta.outputs.is_release == 'true' + permissions: + contents: read + strategy: + fail-fast: false + matrix: + include: + - { goos: linux, goarch: amd64 } + - { goos: linux, goarch: arm64 } + - { goos: darwin, goarch: amd64 } + - { goos: darwin, goarch: arm64 } + - { goos: windows, goarch: amd64 } + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-go@v5 + with: + go-version-file: go.mod + cache: true + + - name: Build + env: + CGO_ENABLED: '0' + GOOS: ${{ matrix.goos }} + GOARCH: ${{ matrix.goarch }} + VERSION: ${{ needs.meta.outputs.version }} + COMMIT: ${{ needs.meta.outputs.commit }} + BUILD_DATE: ${{ needs.meta.outputs.build_date }} + run: | + set -euo pipefail + MOD=github.com/igun997/deploy-everything + LDFLAGS="-s -w \ + -X ${MOD}/internal/version.Version=${VERSION} \ + -X ${MOD}/internal/version.Commit=${COMMIT} \ + -X ${MOD}/internal/version.Date=${BUILD_DATE}" + EXT=""; [ "${GOOS}" = "windows" ] && EXT=".exe" + STAGE="stage/deploy-everything_${VERSION}_${GOOS}_${GOARCH}" + mkdir -p "$STAGE" dist + go build -trimpath -ldflags "$LDFLAGS" -o "$STAGE/deploy-everything${EXT}" . + go build -trimpath -ldflags "$LDFLAGS" -o "$STAGE/grpc-server${EXT}" ./cmd/grpc-server + cp README.md .env.example "$STAGE/" + if [ "${GOOS}" = "windows" ]; then + (cd stage && zip -qr "../dist/$(basename "$STAGE").zip" "$(basename "$STAGE")") + else + tar -czf "dist/$(basename "$STAGE").tar.gz" -C stage "$(basename "$STAGE")" + fi + + - uses: actions/upload-artifact@v4 + with: + name: dist-${{ matrix.goos }}-${{ matrix.goarch }} + path: dist/* + if-no-files-found: error + retention-days: 7 + + release: + name: GitHub Release + runs-on: ubuntu-latest + needs: [meta, images, binaries] + if: needs.meta.outputs.is_release == 'true' + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/download-artifact@v4 + with: + pattern: dist-* + merge-multiple: true + path: dist + + - name: Checksums + run: | + cd dist + sha256sum * > checksums.txt + cat checksums.txt + + - name: Publish release + uses: softprops/action-gh-release@v2 + with: + tag_name: ${{ needs.meta.outputs.version }} + name: ${{ needs.meta.outputs.version }} + generate_release_notes: true + prerelease: ${{ contains(needs.meta.outputs.version, '-') }} + files: dist/* + body: | + ## Container images + + ```bash + docker pull ${{ env.SERVER_IMAGE }}:${{ needs.meta.outputs.version }} + docker pull ${{ env.CLI_IMAGE }}:${{ needs.meta.outputs.version }} + ``` + + Platforms: `linux/amd64`, `linux/arm64`. + Commit: `${{ needs.meta.outputs.commit }}` diff --git a/.gitignore b/.gitignore index 76d212f..3fd0416 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ .env -deploy-everything -grpc-server + +# Build output (anchored: must not match cmd/grpc-server/ source dir) +/deploy-everything +/grpc-server +/dist/ +/stage/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4c6433a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,92 @@ +# syntax=docker/dockerfile:1.7 +# +# Multi-stage, multi-arch build for deploy-everything. +# +# Targets: +# server (default) -> gRPC PaaS server, listens on $GRPC_PORT (50051) +# cli -> deploy-everything CLI (entrypoint = binary) +# +# Build examples: +# docker build -t deploy-everything:dev . +# docker build --target cli -t deploy-everything-cli:dev . +# docker build --build-arg VERSION=v1.2.3 --build-arg COMMIT=$(git rev-parse --short HEAD) . + +ARG GO_VERSION=1.25 + +# ---------- deps ---------- +FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS deps +WORKDIR /src +RUN apk add --no-cache git ca-certificates +COPY go.mod go.sum ./ +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod download + +# ---------- build ---------- +FROM deps AS build +ARG TARGETOS +ARG TARGETARCH +ARG VERSION=dev +ARG COMMIT=none +ARG BUILD_DATE=unknown + +COPY . . + +# CGO off => static binaries that run on distroless/scratch. +ENV CGO_ENABLED=0 +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ + go build -trimpath \ + -ldflags "-s -w \ + -X github.com/igun997/deploy-everything/internal/version.Version=${VERSION} \ + -X github.com/igun997/deploy-everything/internal/version.Commit=${COMMIT} \ + -X github.com/igun997/deploy-everything/internal/version.Date=${BUILD_DATE}" \ + -o /out/grpc-server ./cmd/grpc-server + +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + GOOS=${TARGETOS} GOARCH=${TARGETARCH} \ + go build -trimpath \ + -ldflags "-s -w \ + -X github.com/igun997/deploy-everything/internal/version.Version=${VERSION} \ + -X github.com/igun997/deploy-everything/internal/version.Commit=${COMMIT} \ + -X github.com/igun997/deploy-everything/internal/version.Date=${BUILD_DATE}" \ + -o /out/deploy-everything . + +# ---------- server (default) ---------- +FROM gcr.io/distroless/static-debian12:nonroot AS server +ARG VERSION=dev +ARG COMMIT=none +ARG BUILD_DATE=unknown +LABEL org.opencontainers.image.title="deploy-everything gRPC server" \ + org.opencontainers.image.description="gRPC PaaS adapter for Easypanel" \ + org.opencontainers.image.source="https://github.com/igun997/deploy-everything" \ + org.opencontainers.image.licenses="MIT" \ + org.opencontainers.image.version="${VERSION}" \ + org.opencontainers.image.revision="${COMMIT}" \ + org.opencontainers.image.created="${BUILD_DATE}" + +COPY --from=build /out/grpc-server /usr/local/bin/grpc-server +COPY --from=build /out/deploy-everything /usr/local/bin/deploy-everything + +ENV GRPC_PORT=50051 +EXPOSE 50051 +USER nonroot:nonroot +ENTRYPOINT ["/usr/local/bin/grpc-server"] + +# ---------- cli ---------- +FROM gcr.io/distroless/static-debian12:nonroot AS cli +ARG VERSION=dev +ARG COMMIT=none +ARG BUILD_DATE=unknown +LABEL org.opencontainers.image.title="deploy-everything CLI" \ + org.opencontainers.image.description="CLI for Easypanel tRPC control" \ + org.opencontainers.image.source="https://github.com/igun997/deploy-everything" \ + org.opencontainers.image.licenses="MIT" \ + org.opencontainers.image.version="${VERSION}" \ + org.opencontainers.image.revision="${COMMIT}" \ + org.opencontainers.image.created="${BUILD_DATE}" + +COPY --from=build /out/deploy-everything /usr/local/bin/deploy-everything +USER nonroot:nonroot +ENTRYPOINT ["/usr/local/bin/deploy-everything"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0cffdbc --- /dev/null +++ b/Makefile @@ -0,0 +1,82 @@ +SHELL := /bin/bash + +MODULE := github.com/igun997/deploy-everything +VERSION ?= $(shell git describe --tags --exact-match 2>/dev/null || echo dev) +COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo none) +BUILD_DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ) + +LDFLAGS := -s -w \ + -X $(MODULE)/internal/version.Version=$(VERSION) \ + -X $(MODULE)/internal/version.Commit=$(COMMIT) \ + -X $(MODULE)/internal/version.Date=$(BUILD_DATE) + +DIST := dist +IMAGE ?= ghcr.io/igun997/deploy-everything +CLI_IMAGE ?= ghcr.io/igun997/deploy-everything-cli +PLATFORMS ?= linux/amd64,linux/arm64 + +.PHONY: help +help: ## Show targets + @grep -hE '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) | awk 'BEGIN{FS=":.*?## "};{printf " \033[36m%-18s\033[0m %s\n",$$1,$$2}' + +.PHONY: tidy +tidy: ## go mod tidy + go mod tidy + +.PHONY: vet +vet: ## go vet + go vet ./... + +.PHONY: fmt-check +fmt-check: ## Fail if gofmt would change files + @out="$$(gofmt -l .)"; if [ -n "$$out" ]; then echo "gofmt needed:"; echo "$$out"; exit 1; fi + +.PHONY: test +test: ## Run tests (integration tests skip without EASYPANEL_* env) + go test ./... -count=1 + +.PHONY: build +build: ## Build both binaries for host platform into dist/ + mkdir -p $(DIST) + go build -trimpath -ldflags '$(LDFLAGS)' -o $(DIST)/deploy-everything . + go build -trimpath -ldflags '$(LDFLAGS)' -o $(DIST)/grpc-server ./cmd/grpc-server + +.PHONY: build-cross +build-cross: ## Cross-compile release binaries for all platforms + @mkdir -p $(DIST) + @set -e; for p in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do \ + os=$${p%/*}; arch=$${p#*/}; ext=""; [ "$$os" = "windows" ] && ext=".exe"; \ + echo "-> $$os/$$arch"; \ + CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags '$(LDFLAGS)' \ + -o $(DIST)/deploy-everything_$${os}_$${arch}$$ext .; \ + CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags '$(LDFLAGS)' \ + -o $(DIST)/grpc-server_$${os}_$${arch}$$ext ./cmd/grpc-server; \ + done + +.PHONY: docker +docker: ## Build server image locally (tag: dev) + docker build --target server \ + --build-arg VERSION=$(VERSION) \ + --build-arg COMMIT=$(COMMIT) \ + --build-arg BUILD_DATE=$(BUILD_DATE) \ + -t $(IMAGE):dev . + +.PHONY: docker-cli +docker-cli: ## Build CLI image locally (tag: dev) + docker build --target cli \ + --build-arg VERSION=$(VERSION) \ + --build-arg COMMIT=$(COMMIT) \ + --build-arg BUILD_DATE=$(BUILD_DATE) \ + -t $(CLI_IMAGE):dev . + +.PHONY: docker-buildx +docker-buildx: ## Multi-arch server image (needs buildx; add PUSH=1 to push) + docker buildx build --target server --platform $(PLATFORMS) \ + --build-arg VERSION=$(VERSION) \ + --build-arg COMMIT=$(COMMIT) \ + --build-arg BUILD_DATE=$(BUILD_DATE) \ + -t $(IMAGE):$(VERSION) $(if $(PUSH),--push,--load) . + +.PHONY: clean +clean: ## Remove build output + rm -rf $(DIST) diff --git a/README.md b/README.md index 9014de7..d3928a8 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,7 @@ grpcurl -plaintext -H "authorization: your-token" -d '{ │ ├── license.go # CLI: license info │ ├── wordpress.go # CLI: WordPress shortcuts │ ├── whoami.go # CLI: auth check +│ ├── version.go # CLI: version command (ldflags metadata) │ └── grpc-server/ │ └── main.go # gRPC server entrypoint ├── internal/ @@ -267,12 +268,20 @@ grpcurl -plaintext -H "authorization: your-token" -d '{ │ │ └── interceptor.go # gRPC auth interceptor (unary + stream) │ ├── easypanel/ │ │ └── client.go # Reusable Easypanel HTTP client -│ └── server/ -│ └── paas.go # PaaS gRPC service implementation +│ ├── server/ +│ │ └── paas.go # PaaS gRPC service implementation +│ └── version/ +│ └── version.go # Build metadata injected via -ldflags ├── proto/ │ ├── paas.proto # Protobuf service definition │ ├── paas.pb.go # Generated message types │ └── paas_grpc.pb.go # Generated gRPC stubs +├── .github/workflows/ +│ ├── ci.yml # PR checks: fmt, vet, test, build, image build +│ └── release.yml # master -> :dev images, tags -> release + binaries +├── Dockerfile # Multi-stage, multi-arch (targets: server, cli) +├── Makefile # build / test / cross / docker targets +├── .dockerignore ├── grpc_e2e_test.go # E2E tests (live panel) ├── .env.example # Environment template ├── .gitignore @@ -322,6 +331,84 @@ protoc --go_out=. --go_opt=paths=source_relative \ proto/paas.proto ``` +--- + +## Build & Docker + +### Local builds (Makefile) + +```bash +make help # list targets +make build # both binaries -> dist/ (version metadata injected) +make build-cross # linux/darwin/windows, amd64+arm64 -> dist/ +make test # go test ./... (integration tests skip without EASYPANEL_* env) +make vet fmt-check # static checks, same as CI +make docker # local server image -> ghcr.io/igun997/deploy-everything:dev +make docker-cli # local CLI image -> ghcr.io/igun997/deploy-everything-cli:dev +make docker-buildx PUSH=1 # multi-arch (linux/amd64+arm64) build and push +``` + +Version metadata is injected at link time into `internal/version`: + +```bash +./dist/deploy-everything version +# v1.2.3 (commit a1b2c3d, built 2026-01-01T00:00:00Z, linux/amd64, go1.25.0) +./dist/grpc-server version +``` + +### Images + +One `Dockerfile`, two final stages, both distroless + static (`CGO_ENABLED=0`), running as `nonroot`: + +| Stage | Image | Entrypoint | +|-------|-------|------------| +| `server` (default) | `ghcr.io/igun997/deploy-everything` | `grpc-server` (port 50051; CLI also included) | +| `cli` | `ghcr.io/igun997/deploy-everything-cli` | `deploy-everything` | + +```bash +# Run the gRPC server +docker run --rm -p 50051:50051 --env-file .env \ + ghcr.io/igun997/deploy-everything:dev + +# Run a CLI command +docker run --rm --env-file .env \ + ghcr.io/igun997/deploy-everything-cli:dev projects list +``` + +--- + +## CI/CD + +| Workflow | Trigger | Does | +|----------|---------|------| +| `.github/workflows/ci.yml` | pull request → `master` | `gofmt` check, `go vet`, build both binaries, `go test`, build both images (no push) + `version` smoke test | +| `.github/workflows/release.yml` | push to `master`, tag `v*` | verify → multi-arch image build/push to GHCR (+ SBOM, provenance attestation); on tags also cross-compiled archives attached to a GitHub Release | + +### Image tagging policy + +| Git event | Image tags | +|-----------|-----------| +| push/merge to `master` | `dev`, `dev-` | +| tag `v0.0.1` (any `0.x.y`) | `0.0.1`, `0.0`, `latest` | +| tag `v1.2.3` | `1.2.3`, `1.2`, `1`, `latest` | +| pre-release tag `v1.2.3-rc1` | `1.2.3-rc1` only (no `latest`, no `1.2`/`1`) | + +So `:dev` always points at the current `master`, `:latest` always points at the newest stable release. + +`0.x.y` tags intentionally get no major-only tag — `docker/metadata-action` skips `{{major}}` for `0.x` because semver treats it as initial development with no compatibility guarantee. + +### Cutting a release + +```bash +git tag -a v1.2.3 -m "v1.2.3" +git push origin v1.2.3 +``` + +That publishes `ghcr.io/igun997/deploy-everything:1.2.3` (+ `1.2`, `1`, `latest`), the matching `-cli` image, and a GitHub Release with `tar.gz`/`zip` archives plus `checksums.txt` for linux/darwin (amd64+arm64) and windows/amd64. + +No secrets needed — GHCR push uses the built-in `GITHUB_TOKEN`. Packages inherit repo visibility on first publish; make them public in the repo's package settings if desired. + ## License MIT + diff --git a/cmd/grpc-server/main.go b/cmd/grpc-server/main.go new file mode 100644 index 0000000..1175c8b --- /dev/null +++ b/cmd/grpc-server/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "fmt" + "log" + "net" + "os" + "strings" + + "github.com/igun997/deploy-everything/internal/auth" + "github.com/igun997/deploy-everything/internal/easypanel" + "github.com/igun997/deploy-everything/internal/server" + "github.com/igun997/deploy-everything/internal/version" + pb "github.com/igun997/deploy-everything/proto" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/reflection" +) + +func main() { + _ = godotenv.Load() + + if len(os.Args) > 1 && (os.Args[1] == "version" || os.Args[1] == "--version" || os.Args[1] == "-v") { + fmt.Println(version.String()) + return + } + + // Config from env + endpoint := mustEnv("EASYPANEL_ENDPOINT") + token := mustEnv("EASYPANEL_API_KEY") + grpcPort := getEnv("GRPC_PORT", "50051") + grpcTokens := mustEnv("GRPC_AUTH_TOKENS") // comma-separated tokens + defaultDomain := mustEnv("DEFAULT_DOMAIN") // e.g. cv911b.easypanel.host + customDomain := getEnv("CUSTOM_DOMAIN", "") // e.g. srv-opc.ciptadusa.com + + // Easypanel client + ep := easypanel.NewClient(endpoint, token) + + // Auth tokens + tokens := strings.Split(grpcTokens, ",") + for i := range tokens { + tokens[i] = strings.TrimSpace(tokens[i]) + } + + // gRPC server with auth interceptor + grpcServer := grpc.NewServer( + grpc.UnaryInterceptor(auth.Interceptor(tokens)), + grpc.StreamInterceptor(auth.StreamInterceptor(tokens)), + ) + + // Register PaaS service + paasServer := server.NewPaaSServer(ep, defaultDomain, customDomain) + pb.RegisterPaaSServer(grpcServer, paasServer) + + // Enable reflection for debugging (grpcurl, etc) + reflection.Register(grpcServer) + + // Listen + lis, err := net.Listen("tcp", ":"+grpcPort) + if err != nil { + log.Fatalf("listen: %v", err) + } + + fmt.Printf("grpc-server %s\n", version.String()) + fmt.Printf("gRPC PaaS server listening on :%s\n", grpcPort) + if err := grpcServer.Serve(lis); err != nil { + log.Fatalf("serve: %v", err) + } +} + +func mustEnv(key string) string { + v := os.Getenv(key) + if v == "" { + log.Fatalf("%s not set", key) + } + return v +} + +func getEnv(key, fallback string) string { + if v := os.Getenv(key); v != "" { + return v + } + return fallback +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..f80520a --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,22 @@ +package cmd + +import ( + "fmt" + + "github.com/igun997/deploy-everything/internal/version" + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(versionCmd) + rootCmd.Version = version.String() +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print build version, commit and date", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Println(version.String()) + return nil + }, +} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..e3f7c72 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,27 @@ +// Package version exposes build metadata injected at link time. +// +// Values are set with -ldflags, e.g.: +// +// go build -ldflags "-X github.com/igun997/deploy-everything/internal/version.Version=v1.2.3" . +package version + +import ( + "fmt" + "runtime" +) + +// Injected via -ldflags "-X .=". +var ( + // Version is the semver tag (release builds) or "dev" for untagged builds. + Version = "dev" + // Commit is the git SHA the binary was built from. + Commit = "none" + // Date is the RFC3339 build timestamp. + Date = "unknown" +) + +// String returns a single-line human readable build identifier. +func String() string { + return fmt.Sprintf("%s (commit %s, built %s, %s/%s, %s)", + Version, Commit, Date, runtime.GOOS, runtime.GOARCH, runtime.Version()) +}