Skip to content

Commit

Permalink
build agent too
Browse files Browse the repository at this point in the history
  • Loading branch information
maddalax committed Nov 23, 2024
1 parent 8d33ae5 commit 9cd9139
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 78 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/release-agent.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Release Dockside Agent

on:
workflow_dispatch:

jobs:
build-and-push:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.CR_PAT }}

- name: Get short commit hash
id: vars
run: echo "::set-output name=short_sha::$(echo $GITHUB_SHA | cut -c1-7)"

- name: Build and push Docker image with inline cache
uses: docker/build-push-action@v5
with:
context: .
dockerfile: ./Dockerfile-agent
platforms: linux/amd64
push: true
cache-from: type=registry,ref=ghcr.io/${{ github.repository_owner }}/dockside:cache
cache-to: type=registry,ref=ghcr.io/${{ github.repository_owner }}/dockside:cache,mode=max
tags: |
ghcr.io/${{ github.repository_owner }}/dockside-agent:latest
ghcr.io/${{ github.repository_owner }}/dockside-agent:${{ steps.vars.outputs.short_sha }}
34 changes: 34 additions & 0 deletions Dockerfile-agent
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Stage 1: Build the Go binary
FROM golang:1.23-alpine AS builder

RUN apk update
RUN apk add git
RUN apk add curl

# Set the working directory inside the container
WORKDIR /app

# Copy go.mod and go.sum files
COPY go.mod go.sum ./

# Download and cache the Go modules
RUN go mod download

# Copy the source code into the container
COPY . .

# Build the Go binary for Linux
RUN go build -o ./dist/dockside-agent ./cmd/agent


# Stage 2: Create the smallest possible image
FROM gcr.io/distroless/base-debian11

# Set the working directory inside the container
WORKDIR /app

# Copy the Go binary from the builder stage
COPY --from=builder /app/dist .

# Command to run the binary
CMD ["./dockside-agent"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
> [!CAUTION]
> dockside is currently in heavy development and should not be used at this time.
dockside (currently unnamed) is a simple but powerful platform as as service tool to help self host your own apps, written in pure go, compiled as a single binary.
dockside is a simple but powerful platform as as service tool to help self host your own apps, written in pure go, compiled as a single binary.

Stack:
- golang
Expand Down
20 changes: 0 additions & 20 deletions Taskfile.yml

This file was deleted.

3 changes: 1 addition & 2 deletions app/agent_entry.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package app

import (
"dockside/app/logger"
"encoding/gob"
"github.com/google/uuid"
"github.com/maddalax/htmgo/framework/service"
Expand Down Expand Up @@ -101,8 +100,8 @@ func (a *Agent) Run() {

go a.registry.GetJobRunner().Start()

// keep the agent running
for {
logger.Info("Agent is running")
time.Sleep(time.Second * 5)
}
}
10 changes: 9 additions & 1 deletion app/service_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"github.com/maddalax/htmgo/framework/service"
"os"
"strconv"
)

type ServiceRegistry struct {
Expand Down Expand Up @@ -56,9 +57,16 @@ func (sr *ServiceRegistry) RegisterJobRunner() {
}

func (sr *ServiceRegistry) RegisterKvClient() {
port := 4222
if os.Getenv("NATS_PORT") != "" {
parsed, err := strconv.Atoi(os.Getenv("NATS_PORT"))
if err == nil {
port = parsed
}
}
client, err := NatsConnect(NatsConnectOptions{
Host: os.Getenv("NATS_HOST"),
Port: 4222,
Port: port,
})
if err != nil {
panic(err)
Expand Down
2 changes: 0 additions & 2 deletions build.sh

This file was deleted.

47 changes: 1 addition & 46 deletions cmd/agent/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ package main
import (
"dockside/app"
"dockside/app/logger"
"log"
"strings"

"github.com/kardianos/service"
service2 "github.com/maddalax/htmgo/framework/service"
)

type program struct{}

func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

func (p *program) run() {
func main() {
locator := service2.NewLocator()
registry := app.CreateServiceRegistry(locator)

Expand All @@ -41,37 +30,3 @@ func (p *program) run() {

agent.Run()
}

func (p *program) Stop(s service.Service) error {

// Clean up here.
return nil
}

func main() {
svcConfig := &service.Config{
Name: "dockside-agent",
DisplayName: "Dockside Agent",
Description: "Dockside Agent",
}

program := &program{}
s, err := service.New(program, svcConfig)
if err != nil {
log.Fatal(err)
}

err = s.Install()

if err != nil {
if strings.HasPrefix(err.Error(), "Init already exists") {
// do nothing
} else {
log.Fatal(err)
}
}

if err := s.Run(); err != nil {
log.Fatal(err)
}
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/go-git/go-git/v5 v5.12.0
github.com/gobwas/glob v0.2.3
github.com/google/uuid v1.6.0
github.com/kardianos/service v1.2.2
github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241116145200-825c4dd7ecca
github.com/maddalax/htmgo/framework v1.0.3-0.20241116145258-909d38c7f412
github.com/maddalax/multiproxy v0.0.0-20241122165622-0c1a6ced1956
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 h1:asbCHRVmodnJTuQ3qamDwqVOIjw
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0/go.mod h1:ggCgvZ2r7uOoQjOyu2Y1NhHmEPPzzuhWgcza5M1Ji1I=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX60=
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand All @@ -102,8 +100,6 @@ github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241116145200-825c4dd7ecc
github.com/maddalax/htmgo/extensions/websocket v0.0.0-20241116145200-825c4dd7ecca/go.mod h1:slhIduwW2a7O6N9NnL6DcbeEc21u7gdx1vg8+FIt45s=
github.com/maddalax/htmgo/framework v1.0.3-0.20241116145258-909d38c7f412 h1:yIhz1kTOJGbv3FvJx89WXXI9ReTdJz+5QRjH420xWSI=
github.com/maddalax/htmgo/framework v1.0.3-0.20241116145258-909d38c7f412/go.mod h1:NGGzWVXWksrQJ9kV9SGa/A1F1Bjsgc08cN7ZVb98RqY=
github.com/maddalax/multiproxy v0.0.0-20241118131035-7aa987133a08 h1:eAz9MXVBSxwfDXmKJB5ABzcdgNTD0LI2aEUf0MMLuHA=
github.com/maddalax/multiproxy v0.0.0-20241118131035-7aa987133a08/go.mod h1:Lo9oV6ozJ5ZIbJ3uYzKCxRECnadRIUPehNf1yNd4+dM=
github.com/maddalax/multiproxy v0.0.0-20241122165622-0c1a6ced1956 h1:u5hs1aWBvMnsQpYs0OQDGZ79C2iYjQCEDeN0wQcv4UE=
github.com/maddalax/multiproxy v0.0.0-20241122165622-0c1a6ced1956/go.mod h1:Lo9oV6ozJ5ZIbJ3uYzKCxRECnadRIUPehNf1yNd4+dM=
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
Expand Down Expand Up @@ -222,7 +218,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
25 changes: 25 additions & 0 deletions run-docker-agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

# Pull the latest image
docker pull ghcr.io/maddalax/dockside-agent:latest

# Stop and remove the existing container, if any
docker stop dockside 2>/dev/null || true
docker rm dockside 2>/dev/null || true

# Determine the volume mount path
if [[ "$(uname)" != "Linux" ]]; then
VOLUME_PATH="$HOME/.dockside/data"
else
VOLUME_PATH="/data/dockside"
fi

# Run the container
docker run -d \
--name dockside \
--restart unless-stopped \
-p 80:80 \
-p 8100:3000 \
-p 4222:4222 \
-v "${VOLUME_PATH}:/data/dockside" \
ghcr.io/maddalax/dockside-agent:latest
60 changes: 60 additions & 0 deletions run-docker-dockside-agent-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -e

# Configuration
REMOTE_USER="root" # Replace with remote SSH username
REMOTE_HOST="fedora-server" # Replace with remote host address or IP
REMOTE_PATH="/tmp/dockside.tar" # Temporary path for the tar file on the remote machine
LOCAL_IMAGE_NAME="ghcr.io/maddalax/dockside:latest"
CONTAINER_NAME="dockside"

# Step 1: Build the image locally (if needed)
docker build -t "$LOCAL_IMAGE_NAME" .

# Step 2: Export the image to a tar file
IMAGE_TAR="dockside.tar"
docker save -o "$IMAGE_TAR" "$LOCAL_IMAGE_NAME"

# Step 3: Transfer the tar file to the remote machine
scp "$IMAGE_TAR" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

# Step 4: Load the image on the remote machine and run the container
ssh "$REMOTE_USER@$REMOTE_HOST" << EOF
set -e
# Load the Docker image
docker load < "$REMOTE_PATH"
# Remove the temporary tar file
rm -f "$REMOTE_PATH"
# Pull the latest image to ensure updates
docker pull "$LOCAL_IMAGE_NAME"
# Stop and remove the existing container if it exists
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
# Determine the volume mount path
if [[ "\$(uname)" != "Linux" ]]; then
VOLUME_PATH="\$HOME/.dockside/data"
else
VOLUME_PATH="/data/dockside"
fi
# Run the container
docker run -d \
--name "$CONTAINER_NAME" \
--restart unless-stopped \
-p 80:80 \
-p 8100:3000 \
-p 4222:4222 \
-v "\${VOLUME_PATH}:/data/dockside" \
"$LOCAL_IMAGE_NAME"
EOF

# Step 5: Cleanup local tar file
rm -f "$IMAGE_TAR"

echo "Docker image deployed and running on $REMOTE_HOST."
60 changes: 60 additions & 0 deletions run-docker-dockside-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

set -e

# Configuration
REMOTE_USER="root" # Replace with remote SSH username
REMOTE_HOST="fedora-server" # Replace with remote host address or IP
REMOTE_PATH="/tmp/dockside.tar" # Temporary path for the tar file on the remote machine
LOCAL_IMAGE_NAME="ghcr.io/maddalax/dockside:latest"
CONTAINER_NAME="dockside"

# Step 1: Build the image locally (if needed)
docker build -t "$LOCAL_IMAGE_NAME" .

# Step 2: Export the image to a tar file
IMAGE_TAR="dockside.tar"
docker save -o "$IMAGE_TAR" "$LOCAL_IMAGE_NAME"

# Step 3: Transfer the tar file to the remote machine
scp "$IMAGE_TAR" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

# Step 4: Load the image on the remote machine and run the container
ssh "$REMOTE_USER@$REMOTE_HOST" << EOF
set -e
# Load the Docker image
docker load < "$REMOTE_PATH"
# Remove the temporary tar file
rm -f "$REMOTE_PATH"
# Pull the latest image to ensure updates
docker pull "$LOCAL_IMAGE_NAME"
# Stop and remove the existing container if it exists
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true
# Determine the volume mount path
if [[ "\$(uname)" != "Linux" ]]; then
VOLUME_PATH="\$HOME/.dockside/data"
else
VOLUME_PATH="/data/dockside"
fi
# Run the container
docker run -d \
--name "$CONTAINER_NAME" \
--restart unless-stopped \
-p 80:80 \
-p 8100:3000 \
-p 4222:4222 \
-v "\${VOLUME_PATH}:/data/dockside" \
"$LOCAL_IMAGE_NAME"
EOF

# Step 5: Cleanup local tar file
rm -f "$IMAGE_TAR"

echo "Docker image deployed and running on $REMOTE_HOST."

0 comments on commit 9cd9139

Please sign in to comment.