Skip to content

Commit

Permalink
chore: init command (#17)
Browse files Browse the repository at this point in the history
* chore: init command

* chore: ci

* chore: ci

* chore different releases

* feat: queue manager

* chore: add lockfile status tests

* chore: tests and fixes

* chore: opeanpi

* chore: legacy commands

* chore: new scroll related commands

* fix: htop example
  • Loading branch information
MarcStdt authored Jul 21, 2024
1 parent 54062fa commit 703050c
Show file tree
Hide file tree
Showing 37 changed files with 1,194 additions and 586 deletions.
18 changes: 8 additions & 10 deletions .docker/druid-install-command.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
#!/bin/bash

#check if first argument is there, set CHANNEL to it, otherwise default to latest
if [ -z "$1" ]; then
CHANNEL="$CHANNEL/download"
if [ -z "$CHANNEL" ]; then
URL_PATH="latest/download"
else
CHANNEL=download/$1
URL_PATH=download/$CHANNEL
fi

wget -O /app/resources/druid https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid
wget -O /app/resources/druid_rcon https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid_rcon
wget -O /app/resources/druid_rcon_web https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid_rcon_web
wget -O /app/resources/entrypoint.sh https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/entrypoint.sh
wget -O /app/resources/druid https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid
wget -O /app/resources/druid_rcon https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid_rcon
wget -O /app/resources/druid_rcon_web https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid_rcon_web
wget -O /app/resources/entrypoint.sh https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/entrypoint.sh
chmod +x /app/resources/druid /app/resources/druid_rcon /app/resources/druid_rcon_web

# Modify the PATH variable to prioritize /app/resources
export PATH=/app/resources:$PATH

bash /app/resources/entrypoint.sh

#https://github.com/highcard-dev/druid-cli/releases/download/0.1.8-prerelease3/druid
bash /app/resources/entrypoint.sh $@
5 changes: 5 additions & 0 deletions .docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ if [ -z "$input" ] || [[ $input =~ ([^/]+)/([^:]+):([^/]+) ]]; then
then
args+=("--port" "${DRUID_PORT}")
fi

if [ ! -z "${DRUID_INIT}" ];
then
args+=("--init")
fi

echo "Running druid with args from env: ${args[@]}"
exec druid "${args[@]}"
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version: "^1.21"
- name: Extract branch name
id: extract_branch
run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//-/g')" >> $GITHUB_ENV
- uses: paulhatch/semantic-version@v5.3.0
id: version
with:
version_format: "${major}.${minor}.${patch}-${{ env.BRANCH_NAME }}${increment}"
- run: make test-integration-docker
name: Run integration tests inside Docker
- run: make test
Expand All @@ -20,3 +27,14 @@ jobs:
name: Build
- run: make build-plugins
name: Build Plugins
- name: Prerelease
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.version }}
prerelease: true
files: |
bin/druid
bin/druid_rcon
bin/druid_rcon_web_rust
.docker/entrypoint.sh
.docker/druid-install-command.sh
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@
"trace": "verbose",
"debugAdapter": "legacy"

}
},
{
"name": "Test Current File",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${file}",
"env": {},
"args": [],
"showLog": true
}
]
}
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ mock:
test:
go test -v ./test

test_clean:
go clean -testcache
go test -v ./test

test-integration:
go test -v ./test/integration

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func init() {
RootCmd.AddCommand(UpdateCommand)
RootCmd.AddCommand(SemverCmd)
RootCmd.AddCommand(VersionCmd)
RootCmd.AddCommand(ScrollCmd)

c, _ := os.Getwd()

Expand Down
9 changes: 4 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"strings"

"github.com/highcard-dev/daemon/internal/core/services"
"github.com/highcard-dev/daemon/internal/core/services/registry"
Expand Down Expand Up @@ -33,6 +32,8 @@ var RunCmd = &cobra.Command{
}
processLauncher := services.NewProcedureLauncher(client, processManager, services.NewPluginManager(), consoleService, logManager, scrollService)

queueManager := services.NewQueueManager(scrollService, processLauncher)

if !scrollService.LockExists() {
scrollService.WriteNewScrollLock()
logger.Log().Info("Lock file created")
Expand All @@ -55,11 +56,9 @@ var RunCmd = &cobra.Command{
return err
}

parts := strings.Split(args[0], ".")

command := strings.TrimPrefix(args[0], parts[0]+".")
command := args[0]

err = processLauncher.RunNew(command, parts[0], false)
err = queueManager.AddItem(command, false)
return err
},
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/scroll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

import (
"github.com/spf13/cobra"
)

// scrollCmd represents the command for scrolling
var ScrollCmd = &cobra.Command{
Use: "scroll",
Short: "Commands related to the scroll file",
Long: `Commands related to the scroll file`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}

func init() {
ScrollCmd.AddCommand(ScrollValidateCmd)
}
34 changes: 34 additions & 0 deletions cmd/scroll_validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"

"github.com/highcard-dev/daemon/internal/core/domain"
"github.com/spf13/cobra"
)

var ScrollValidateCmd = &cobra.Command{
Use: "validate",
Short: "Validates the scroll file",
Long: `This command validates the scroll file to ensure it meets the required criteria.`,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
scrollDir := cwd
if len(args) > 0 {
scrollDir = args[0]
}

scroll, err := domain.NewScroll(scrollDir)

if err != nil {
return fmt.Errorf("failed to load scroll: %w", err)
}

if err := scroll.Validate(); err != nil {
return fmt.Errorf("failed to validate scroll: %w", err)
}

fmt.Println("Scroll validated successfully.")
return nil
},
}
34 changes: 22 additions & 12 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ignoreVersionCheck bool
var port int
var shutdownWait int
var additionalEndpoints []string
var initScroll bool

var ServeCommand = &cobra.Command{
Use: "serve",
Expand Down Expand Up @@ -87,7 +88,9 @@ to interact and monitor the Scroll Application`,

processLauncher := services.NewProcedureLauncher(client, processManager, pluginManager, consoleService, logManager, scrollService)

scrollHandler := handler.NewScrollHandler(scrollService, pluginManager, processLauncher)
queueManager := services.NewQueueManager(scrollService, processLauncher)

scrollHandler := handler.NewScrollHandler(scrollService, pluginManager, processLauncher, queueManager)
processHandler := handler.NewProcessHandler(processManager)
scrollLogHandler := handler.NewScrollLogHandler(scrollService, logManager, processManager)
scrollMetricHandler := handler.NewScrollMetricHandler(scrollService, processMonitor)
Expand All @@ -104,7 +107,7 @@ to interact and monitor the Scroll Application`,

a := s.Initialize()

signals.SetupSignals(processLauncher, processManager, a, shutdownWait)
signals.SetupSignals(queueManager, processManager, a, shutdownWait)

currentScroll, lock, err := scrollService.Bootstrap(ignoreVersionCheck)
if err != nil {
Expand All @@ -124,14 +127,8 @@ to interact and monitor the Scroll Application`,
if err != nil {
return err
}
//run if something is there
err = processLauncher.StartLockfile(lock)

if err != nil {
return err
}
} else {
logger.Log().Info("No lock file found, bootstrapping")
} else if initScroll {
logger.Log().Info("No lock file found, but init command available. Bootstrapping...")
//There is an error here. We need to bootstrap the files before we render out the templates in the bootstrap func above
err := scrollService.CreateLockAndBootstrapFiles()
if err != nil {
Expand All @@ -151,17 +148,28 @@ to interact and monitor the Scroll Application`,
}
//start scroll.init process
//initialize if nothing is there
err = processLauncher.Initalize(lock)
err = queueManager.AddItem(currentScroll.Init, true)
if err != nil {
return err
}

scrollService.WriteNewScrollLock()

logger.Log().Info("Bootstrapping done")
}

err = queueManager.QueueLockFile()
if err != nil {
return err
}

//run if something is there
go queueManager.Work()

//schedule crons
logger.Log().Info("Schedule crons")

cronManager := services.NewCronManager(currentScroll.Cronjobs, processLauncher)
cronManager := services.NewCronManager(currentScroll.Cronjobs, queueManager)
err = cronManager.Init()

if err != nil {
Expand All @@ -188,6 +196,8 @@ func init() {

ServeCommand.Flags().StringVarP(&userId, "user-id", "u", "", "Allowed user id")

ServeCommand.Flags().BoolVarP(&initScroll, "init", "", false, "Initialize the scroll if no lock file is present")

ServeCommand.Flags().BoolVarP(&ignoreVersionCheck, "ignore-version-check", "", false, "Ignore version check")

ServeCommand.Flags().StringArrayVarP(&additionalEndpoints, "additional-endpoints", "", []string{}, "Additional endpoints to serve. Valid values: annotations")
Expand Down
46 changes: 26 additions & 20 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,17 +374,6 @@ const docTemplate = `{
"wait": {}
}
},
"ProcessCommand": {
"type": "object",
"properties": {
"commands": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/CommandInstructionSet"
}
}
}
},
"ProcessMonitorMetrics": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -494,6 +483,18 @@ const docTemplate = `{
"description": "don't make this a semver, it's not allways",
"type": "string"
},
"commands": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/CommandInstructionSet"
}
},
"cronjobs": {
"type": "array",
"items": {
"$ref": "#/definitions/domain.Cronjob"
}
},
"desc": {
"type": "string"
},
Expand All @@ -512,12 +513,6 @@ const docTemplate = `{
}
}
},
"processes": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/ProcessCommand"
}
},
"version": {
"type": "string"
}
Expand Down Expand Up @@ -565,6 +560,20 @@ const docTemplate = `{
"ConsoleTypePlugin"
]
},
"domain.Cronjob": {
"type": "object",
"properties": {
"command": {
"type": "string"
},
"name": {
"type": "string"
},
"schedule": {
"type": "string"
}
}
},
"domain.Process": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -626,9 +635,6 @@ const docTemplate = `{
"command": {
"type": "string"
},
"process": {
"type": "string"
},
"sync": {
"type": "boolean"
}
Expand Down
Loading

0 comments on commit 703050c

Please sign in to comment.