mirror of
https://github.com/dokku/dokku.git
synced 2026-08-29 10:08:53 +02:00
feat: port scheduler-docker-local :report subcommand to golang
The bash :report implementation for the scheduler-docker-local plugin is replaced with a compiled golang binary. The plugin already shipped golang code, so the report subcommand is added alongside its existing triggers binary, and the init-process and parallel-schedule-count helpers remain in bash for the deploy pipeline.
This commit is contained in:
3
plugins/scheduler-docker-local/.gitignore
vendored
3
plugins/scheduler-docker-local/.gitignore
vendored
@@ -1,5 +1,8 @@
|
||||
/triggers/*
|
||||
/triggers
|
||||
/cron-*
|
||||
/report
|
||||
/report-subcommand
|
||||
/scheduler-cron-write
|
||||
/scheduler-storage-exec
|
||||
/subcommands/report
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
TRIGGERS = triggers/scheduler-cron-write triggers/scheduler-storage-exec
|
||||
BUILD = triggers
|
||||
TRIGGERS = triggers/report triggers/scheduler-cron-write triggers/scheduler-storage-exec
|
||||
BUILD = report-subcommand triggers
|
||||
PLUGIN_NAME = scheduler-docker-local
|
||||
|
||||
clean-report-subcommand:
|
||||
rm -rf report-subcommand
|
||||
|
||||
report-subcommand: clean-report-subcommand src/subcommands/subcommands.go
|
||||
GOARCH=$(GOARCH) go build -mod=readonly -ldflags="-s -w" $(GO_ARGS) -o report-subcommand src/subcommands/subcommands.go
|
||||
ln -sf ../report-subcommand subcommands/report
|
||||
|
||||
include ../../common.mk
|
||||
|
||||
92
plugins/scheduler-docker-local/internal-functions
Executable file → Normal file
92
plugins/scheduler-docker-local/internal-functions
Executable file → Normal file
@@ -5,98 +5,6 @@ source "$PLUGIN_AVAILABLE_PATH/config/functions"
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
cmd-scheduler-docker-local-report() {
|
||||
declare desc="displays a scheduler-docker-local report for one or more apps"
|
||||
declare cmd="scheduler-docker-local:report"
|
||||
[[ "$1" == "$cmd" ]] && shift 1
|
||||
fn-report-parse-args "$@"
|
||||
set -- "${REPORT_ARGS[@]}"
|
||||
declare APP="${1:-}" INFO_FLAG="${2:-}"
|
||||
|
||||
if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
|
||||
INFO_FLAG="$APP"
|
||||
APP=""
|
||||
fi
|
||||
|
||||
if [[ "$REPORT_IS_GLOBAL" == "true" ]]; then
|
||||
APP="--global"
|
||||
fi
|
||||
|
||||
if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
|
||||
INFO_FLAG="true"
|
||||
fi
|
||||
|
||||
if [[ "$APP" == "--global" ]]; then
|
||||
cmd-scheduler-docker-local-report-single "$APP" "$INFO_FLAG" "$REPORT_FORMAT"
|
||||
elif [[ -z "$APP" ]]; then
|
||||
for app in $(dokku_apps); do
|
||||
cmd-scheduler-docker-local-report-single "$app" "$INFO_FLAG" "$REPORT_FORMAT" | tee || true
|
||||
done
|
||||
else
|
||||
cmd-scheduler-docker-local-report-single "$APP" "$INFO_FLAG" "$REPORT_FORMAT"
|
||||
fi
|
||||
}
|
||||
|
||||
cmd-scheduler-docker-local-report-single() {
|
||||
declare APP="$1" INFO_FLAG="$2" FORMAT="${3:-stdout}"
|
||||
if [[ "$INFO_FLAG" == "true" ]]; then
|
||||
INFO_FLAG=""
|
||||
fi
|
||||
local flag_map=()
|
||||
if [[ "$APP" == "--global" ]]; then
|
||||
flag_map=(
|
||||
"--scheduler-docker-local-computed-init-process: $(fn-scheduler-docker-local-computed-init-process "$APP")"
|
||||
"--scheduler-docker-local-computed-parallel-schedule-count: $(fn-scheduler-docker-local-computed-parallel-schedule-count "$APP")"
|
||||
"--scheduler-docker-local-global-init-process: $(fn-scheduler-docker-local-global-init-process "$APP")"
|
||||
"--scheduler-docker-local-global-parallel-schedule-count: $(fn-scheduler-docker-local-global-parallel-schedule-count "$APP")"
|
||||
)
|
||||
else
|
||||
verify_app_name "$APP"
|
||||
flag_map=(
|
||||
"--scheduler-docker-local-computed-init-process: $(fn-scheduler-docker-local-computed-init-process "$APP")"
|
||||
"--scheduler-docker-local-computed-parallel-schedule-count: $(fn-scheduler-docker-local-computed-parallel-schedule-count "$APP")"
|
||||
"--scheduler-docker-local-global-init-process: $(fn-scheduler-docker-local-global-init-process "$APP")"
|
||||
"--scheduler-docker-local-global-parallel-schedule-count: $(fn-scheduler-docker-local-global-parallel-schedule-count "$APP")"
|
||||
"--scheduler-docker-local-init-process: $(fn-scheduler-docker-local-init-process "$APP")"
|
||||
"--scheduler-docker-local-parallel-schedule-count: $(fn-scheduler-docker-local-parallel-schedule-count "$APP")"
|
||||
)
|
||||
fi
|
||||
|
||||
fn-report-validate-format "$FORMAT" "$INFO_FLAG"
|
||||
|
||||
if [[ "$FORMAT" == "json" ]]; then
|
||||
fn-report-emit-json flag_map "scheduler-docker-local"
|
||||
return
|
||||
fi
|
||||
|
||||
if [[ -z "$INFO_FLAG" ]]; then
|
||||
if [[ "$APP" == "--global" ]]; then
|
||||
dokku_log_info2_quiet "global scheduler-docker-local information"
|
||||
else
|
||||
dokku_log_info2_quiet "${APP} scheduler-docker-local information"
|
||||
fi
|
||||
for flag in "${flag_map[@]}"; do
|
||||
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
|
||||
dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
|
||||
done
|
||||
else
|
||||
local match=false
|
||||
for flag in "${flag_map[@]}"; do
|
||||
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
|
||||
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
|
||||
value=${flag#*: }
|
||||
size="${#value}"
|
||||
if [[ "$size" -ne 0 ]]; then
|
||||
echo "$value" && match=true
|
||||
else
|
||||
match=true
|
||||
fi
|
||||
fi
|
||||
done
|
||||
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
|
||||
fi
|
||||
}
|
||||
|
||||
fn-scheduler-docker-local-init-process() {
|
||||
declare APP="$1"
|
||||
fn-plugin-property-get-default "scheduler-docker-local" "$APP" "init-process" ""
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
source "$PLUGIN_AVAILABLE_PATH/scheduler-docker-local/internal-functions"
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
cmd-scheduler-docker-local-report-single "$@"
|
||||
91
plugins/scheduler-docker-local/report.go
Normal file
91
plugins/scheduler-docker-local/report.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package schedulerdockerlocal
|
||||
|
||||
import (
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
// ReportSingleApp is an internal function that displays the scheduler-docker-local report for one or more apps
|
||||
func ReportSingleApp(appName string, format string, infoFlag string) error {
|
||||
if appName != "--global" {
|
||||
if err := common.VerifyAppName(appName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var flags map[string]common.ReportFunc
|
||||
if appName == "--global" {
|
||||
flags = map[string]common.ReportFunc{
|
||||
"--scheduler-docker-local-computed-init-process": reportComputedInitProcess,
|
||||
"--scheduler-docker-local-computed-parallel-schedule-count": reportComputedParallelScheduleCount,
|
||||
"--scheduler-docker-local-global-init-process": reportGlobalInitProcess,
|
||||
"--scheduler-docker-local-global-parallel-schedule-count": reportGlobalParallelScheduleCount,
|
||||
}
|
||||
} else {
|
||||
flags = map[string]common.ReportFunc{
|
||||
"--scheduler-docker-local-computed-init-process": reportComputedInitProcess,
|
||||
"--scheduler-docker-local-computed-parallel-schedule-count": reportComputedParallelScheduleCount,
|
||||
"--scheduler-docker-local-global-init-process": reportGlobalInitProcess,
|
||||
"--scheduler-docker-local-global-parallel-schedule-count": reportGlobalParallelScheduleCount,
|
||||
"--scheduler-docker-local-init-process": reportInitProcess,
|
||||
"--scheduler-docker-local-parallel-schedule-count": reportParallelScheduleCount,
|
||||
}
|
||||
}
|
||||
|
||||
flagKeys := []string{}
|
||||
for flagKey := range flags {
|
||||
flagKeys = append(flagKeys, flagKey)
|
||||
}
|
||||
|
||||
infoFlags := common.CollectReport(appName, infoFlag, flags)
|
||||
return common.ReportSingleApp(common.ReportSingleAppInput{
|
||||
ReportType: "scheduler-docker-local",
|
||||
AppName: appName,
|
||||
InfoFlag: infoFlag,
|
||||
InfoFlags: infoFlags,
|
||||
InfoFlagKeys: flagKeys,
|
||||
Format: format,
|
||||
TrimPrefix: true,
|
||||
UppercaseFirstCharacter: true,
|
||||
EmitLegacyPrefix: false,
|
||||
})
|
||||
}
|
||||
|
||||
func reportInitProcess(appName string) string {
|
||||
return common.PropertyGet("scheduler-docker-local", appName, "init-process")
|
||||
}
|
||||
|
||||
func reportGlobalInitProcess(appName string) string {
|
||||
return common.PropertyGet("scheduler-docker-local", "--global", "init-process")
|
||||
}
|
||||
|
||||
func reportComputedInitProcess(appName string) string {
|
||||
value := reportInitProcess(appName)
|
||||
if value == "" {
|
||||
value = reportGlobalInitProcess(appName)
|
||||
}
|
||||
if value == "" {
|
||||
value = "true"
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func reportParallelScheduleCount(appName string) string {
|
||||
return common.PropertyGet("scheduler-docker-local", appName, "parallel-schedule-count")
|
||||
}
|
||||
|
||||
func reportGlobalParallelScheduleCount(appName string) string {
|
||||
return common.PropertyGet("scheduler-docker-local", "--global", "parallel-schedule-count")
|
||||
}
|
||||
|
||||
func reportComputedParallelScheduleCount(appName string) string {
|
||||
value := reportParallelScheduleCount(appName)
|
||||
if value == "" {
|
||||
value = reportGlobalParallelScheduleCount(appName)
|
||||
}
|
||||
if value == "" {
|
||||
value = "1"
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
schedulerdockerlocal "github.com/dokku/dokku/plugins/scheduler-docker-local"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
|
||||
// main entrypoint to all subcommands
|
||||
func main() {
|
||||
parts := strings.Split(os.Args[0], "/")
|
||||
subcommand := parts[len(parts)-1]
|
||||
|
||||
var err error
|
||||
switch subcommand {
|
||||
case "report":
|
||||
args := flag.NewFlagSet("scheduler-docker-local:report", flag.ExitOnError)
|
||||
format := args.String("format", "stdout", "format: [ stdout | json ]")
|
||||
reportArgs, flagErr := common.ParseReportArgs("scheduler-docker-local", os.Args[2:])
|
||||
if flagErr == nil {
|
||||
args.Parse(reportArgs.OSArgs)
|
||||
appName := args.Arg(0)
|
||||
if reportArgs.IsGlobal {
|
||||
appName = "--global"
|
||||
}
|
||||
err = schedulerdockerlocal.CommandReport(appName, *format, reportArgs.InfoFlag)
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
common.LogFailWithError(err)
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,9 @@ func main() {
|
||||
AsUser: *asUser,
|
||||
Command: cmd,
|
||||
})
|
||||
case "report":
|
||||
appName := flag.Arg(0)
|
||||
err = schedulerdockerlocal.ReportSingleApp(appName, "", "")
|
||||
default:
|
||||
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
|
||||
}
|
||||
|
||||
29
plugins/scheduler-docker-local/subcommands.go
Normal file
29
plugins/scheduler-docker-local/subcommands.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package schedulerdockerlocal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/dokku/dokku/plugins/common"
|
||||
)
|
||||
|
||||
// CommandReport displays a scheduler-docker-local report for one or more apps
|
||||
func CommandReport(appName string, format string, infoFlag string) error {
|
||||
if len(appName) == 0 {
|
||||
apps, err := common.DokkuApps()
|
||||
if err != nil {
|
||||
if errors.Is(err, common.NoAppsExist) {
|
||||
common.LogWarn(err.Error())
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
for _, appName := range apps {
|
||||
if err := ReportSingleApp(appName, format, infoFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return ReportSingleApp(appName, format, infoFlag)
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
source "$PLUGIN_AVAILABLE_PATH/scheduler-docker-local/internal-functions"
|
||||
set -eo pipefail
|
||||
[[ $DOKKU_TRACE ]] && set -x
|
||||
|
||||
cmd-scheduler-docker-local-report "$@"
|
||||
Reference in New Issue
Block a user