Skip to content

Commit

Permalink
0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Oct 30, 2024
1 parent 510f31a commit 3e78964
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 57 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/t1-failure-no-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:not-a-tag",
]
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/t1-failure-no-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:c092c6f6-198c-470f-9526-9c998efe7ab5",
"anka-template-tag:vanilla+port-forward-22+brew-git",
]
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/t1-failure-tag-1-in-vm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22+brew-git",
]
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/t1-with-tag-1-matrix-nodes-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: 't1-with-tag-1-matrix-nodes-2'
on:
workflow_dispatch:

jobs:
t1-with-tag-1-matrix-nodes-2:
strategy:
matrix:
node: ["1", "2"]
runs-on: [
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22+brew-git",
"unique-id:${{ github.run_id }}-${{ matrix.node }}",
]
steps:
- uses: actions/checkout@v3
- run: |
ls -laht
sw_vers
hostname
echo "123"
env
7 changes: 3 additions & 4 deletions .github/workflows/t1-with-tag-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ on:
workflow_dispatch:

jobs:
testJob:
t1-with-tag-1:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22+brew-git",
]
Expand All @@ -16,4 +14,5 @@ jobs:
ls -laht
sw_vers
hostname
echo "123"
echo "123"
- run: whoami
2 changes: 0 additions & 2 deletions .github/workflows/t1-with-tag-2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22",
]
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/t1-without-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
]
steps:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/t2-dual-without-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
]
steps:
Expand All @@ -18,8 +16,6 @@ jobs:
echo "123"
testJob2:
runs-on: [
"self-hosted",
"anka",
"anka-template:6abae54f-025d-4a27-b5eb-b985d5eddac9",
]
steps:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.8.2
0.9.0
7 changes: 4 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ func GetPluginFromContext(ctx context.Context) Plugin {
}

type Globals struct {
RunOnce string
PullLock *sync.Mutex
PluginsPath string
RunOnce string
PullLock *sync.Mutex
PluginsPath string
DebugEnabled bool
}

func GetGlobalsFromContext(ctx context.Context) Globals {
Expand Down
6 changes: 3 additions & 3 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func NewClient(ctx context.Context, config config.Database) (*Database, error) {
Password: config.Password, // no password set
DB: config.Database, // use default DB,
})
logging.DevDebug(ctx, fmt.Sprintf("created redis client: %v", rdb))
logging.DevContext(ctx, fmt.Sprintf("created redis client: %v", rdb))

logging.DevDebug(ctx, "pinging redis client")
logging.DevContext(ctx, "pinging redis client")
ping := rdb.Ping(ctx)
if ping.Err() != nil && ping.Err().Error() != "" {
return nil, errors.New("error pinging redis client: " + ping.Err().Error())
Expand All @@ -38,7 +38,7 @@ func NewClient(ctx context.Context, config config.Database) (*Database, error) {
return nil, err
}

logging.DevDebug(ctx, fmt.Sprintf("pinged redis client: %s", pong))
logging.DevContext(ctx, fmt.Sprintf("pinged redis client: %s", pong))

if pong != "PONG" {
return nil, fmt.Errorf("unable to connect to Redis, received: %s", pong)
Expand Down
8 changes: 6 additions & 2 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ type ContextHandler struct {
attrs []slog.Attr
}

func IsDebugEnabled() bool {
return strings.ToUpper(os.Getenv("LOG_LEVEL")) == "DEBUG" || strings.ToUpper(os.Getenv("LOG_LEVEL")) == "DEV"
}

func New() *slog.Logger {
logLevel := os.Getenv("LOG_LEVEL")
var options *slog.HandlerOptions
if strings.ToUpper(logLevel) == "DEBUG" || strings.ToUpper(logLevel) == "DEV" {
if IsDebugEnabled() {
handler := &ContextHandler{Handler: NewPrettyHandler(&slog.HandlerOptions{
Level: slog.LevelDebug,
AddSource: true,
Expand Down Expand Up @@ -101,7 +105,7 @@ func Panic(workerCtx context.Context, pluginCtx context.Context, errorMessage st
panic(errorMessage)
}

func DevDebug(pluginCtx context.Context, errorMessage string) {
func DevContext(pluginCtx context.Context, errorMessage string) {
if strings.ToUpper(os.Getenv("LOG_LEVEL")) == "DEV" {
logger := GetLoggerFromContext(pluginCtx)
logger.DebugContext(pluginCtx, errorMessage)
Expand Down
27 changes: 14 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ func main() {
logger.InfoContext(parentCtx, "plugins path", slog.String("pluginsPath", pluginsPath))

parentCtx = context.WithValue(parentCtx, config.ContextKey("globals"), config.Globals{
RunOnce: runOnce,
PullLock: &sync.Mutex{},
PluginsPath: pluginsPath,
RunOnce: runOnce,
PullLock: &sync.Mutex{},
PluginsPath: pluginsPath,
DebugEnabled: logging.IsDebugEnabled(),
})

httpTransport := http.DefaultTransport
Expand Down Expand Up @@ -364,30 +365,30 @@ func worker(parentCtx context.Context, logger *slog.Logger, loadedConfig config.
if plugin.Repo == "" {
logger.InfoContext(pluginCtx, "no repo set for plugin; assuming it's an organization level plugin")
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("isRepoSet"), false)
logging.DevDebug(pluginCtx, "set isRepoSet to false")
logging.DevContext(pluginCtx, "set isRepoSet to false")
} else {
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("isRepoSet"), true)
logging.DevDebug(pluginCtx, "set isRepoSet to true")
logging.DevContext(pluginCtx, "set isRepoSet to true")
}

if plugin.PrivateKey == "" && loadedConfig.GlobalPrivateKey != "" {
logging.DevDebug(pluginCtx, "using global private key")
logging.DevContext(pluginCtx, "using global private key")
plugin.PrivateKey = loadedConfig.GlobalPrivateKey
}

pluginCtx = context.WithValue(pluginCtx, config.ContextKey("plugin"), plugin)
pluginCtx = logging.AppendCtx(pluginCtx, slog.String("pluginName", plugin.Name))

if !strings.Contains(plugin.Plugin, "_receiver") {
logging.DevDebug(pluginCtx, "plugin is not a receiver; loading the anka CLI")
logging.DevContext(pluginCtx, "plugin is not a receiver; loading the anka CLI")
ankaCLI, err := anka.NewCLI(pluginCtx)
if err != nil {
pluginCancel()
logger.ErrorContext(pluginCtx, "unable to create anka cli", "error", err)
return
}
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("ankacli"), ankaCLI)
logging.DevDebug(pluginCtx, "loaded the anka CLI")
logging.DevContext(pluginCtx, "loaded the anka CLI")
}

if databaseURL != "" || plugin.Database.URL != "" {
Expand All @@ -398,7 +399,7 @@ func worker(parentCtx context.Context, logger *slog.Logger, loadedConfig config.
databasePassword = plugin.Database.Password
databaseDatabase = plugin.Database.Database
}
logging.DevDebug(pluginCtx, "connecting to database")
logging.DevContext(pluginCtx, "connecting to database")
databaseClient, err := database.NewClient(pluginCtx, config.Database{
URL: databaseURL,
Port: databasePort,
Expand All @@ -411,21 +412,21 @@ func worker(parentCtx context.Context, logger *slog.Logger, loadedConfig config.
}
logger.InfoContext(pluginCtx, "connected to database", slog.Any("database", databaseClient))
pluginCtx = context.WithValue(pluginCtx, config.ContextKey("database"), databaseClient)
logging.DevDebug(pluginCtx, "connected to database")
logging.DevContext(pluginCtx, "connected to database")
}

logger.InfoContext(pluginCtx, "starting plugin")

for {
select {
case <-pluginCtx.Done():
logging.DevDebug(pluginCtx, "plugin for loop::pluginCtx.Done()")
logging.DevContext(pluginCtx, "plugin for loop::pluginCtx.Done()")
metricsData.SetStatus(pluginCtx, logger, "stopped")
logger.WarnContext(pluginCtx, shutDownMessage)
pluginCancel()
return
default:
logging.DevDebug(pluginCtx, "plugin for loop::default")
// logging.DevContext(pluginCtx, "plugin for loop::default")
run.Plugin(workerCtx, pluginCtx, pluginCancel, logger, firstPluginStarted, metricsData)
if workerCtx.Err() != nil || toRunOnce == "true" {
pluginCancel()
Expand All @@ -436,7 +437,7 @@ func worker(parentCtx context.Context, logger *slog.Logger, loadedConfig config.
select {
case <-time.After(time.Duration(plugin.SleepInterval) * time.Second):
case <-pluginCtx.Done():
logging.DevDebug(pluginCtx, "plugin for loop::default::pluginCtx.Done()")
logging.DevContext(pluginCtx, "plugin for loop::default::pluginCtx.Done()")
break
}
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/handlers/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ plugins:
- If you are attempting to register runners for an entire organization, do NOT set `repo` and make sure your Github App has `Self-hosted runners` > `Read and write` permissions.
- If your Organization level runner is registered and your public repo jobs are not picking it up even though the labels are a perfect match, make sure the Runner groups (likely `Default`) has `Allow public repositories`.

---

Next, in your workflow yml you need to add several labels to `runs-on`. Here is the list and an example:

1. `self-hosted` (required)
1. `anka` (required)
1. `anka-template:{UUID OF TEMPLATE HERE}` (required)
1. `anka-template-tag:{TAG NAME OF TEMPLATE HERE}` (optional; uses latest if not populated)
<!-- 1. `run-id:${{ github.run_id }}` (do not change this) - label that is used to ensure that jobs in the same workspace don't compete for the same runner. -->
<!-- 1. `unique-id:{UNIQUE ID OF JOB HERE}` - a label that is used to ensure multiple jobs in the same run don't compete for the same runner. -->

**WARNING/NOTE:** If you are using the same template/tag/labels in `runs-on` for multiple runs/jobs, you will need to ensure that each job has a unique `unique-id` label or else they will compete for the same runner. To do this, set `"unique-id:${{ github.run_id }}"`. If you are starting multiple jobs in the same run, each job will need a unique `unique-id` label like `"unique-id:${{ github.run_id }}-1"` changing 1 to 2, 3, etc, for each job.

(from [t1-with-tag-1.yml](.github/workflows/t1-with-tag-1.yml))

```
Expand All @@ -56,8 +58,6 @@ on:
jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22+brew-git",
]
Expand Down
7 changes: 5 additions & 2 deletions plugins/handlers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func CheckForCompletedJobs(
// BE VERY CAREFUL when you use return here. You could orphan the job if you're not careful.
checkForCompletedJobsMu.Lock()
// do not use 'continue' in the loop or else the ranOnce won't happen
logger.DebugContext(pluginCtx, "CheckForCompletedJobs "+ctxPlugin.Name+" | runOnce "+fmt.Sprint(runOnce))
// logging.DevContext(pluginCtx, "CheckForCompletedJobs "+ctxPlugin.Name+" | runOnce "+fmt.Sprint(runOnce))
select {
case <-failureChannel:
// logger.ErrorContext(pluginCtx, "CheckForCompletedJobs"+ctxPlugin.Name+" failureChannel")
Expand Down Expand Up @@ -773,7 +773,6 @@ func Run(

// Install runner
globals := config.GetGlobalsFromContext(pluginCtx)
logger.InfoContext(pluginCtx, "installing github runner inside of vm")
installRunnerPath := filepath.Join(globals.PluginsPath, "handlers", "github", "install-runner.bash")
registerRunnerPath := filepath.Join(globals.PluginsPath, "handlers", "github", "register-runner.bash")
startRunnerPath := filepath.Join(globals.PluginsPath, "handlers", "github", "start-runner.bash")
Expand All @@ -789,6 +788,7 @@ func Run(
}
return
}
logger.DebugContext(pluginCtx, "copying install-runner.bash, register-runner.bash, and start-runner.bash to vm")
err = ankaCLI.AnkaCopy(pluginCtx,
installRunnerPath,
registerRunnerPath,
Expand All @@ -812,6 +812,7 @@ func Run(
default:
}

logger.DebugContext(pluginCtx, "installing github runner inside of vm")
installRunnerErr = ankaCLI.AnkaRun(pluginCtx, "./install-runner.bash")
if installRunnerErr != nil {
logger.ErrorContext(pluginCtx, "error executing install-runner.bash", "err", installRunnerErr)
Expand All @@ -829,6 +830,7 @@ func Run(
return
default:
}
logger.DebugContext(pluginCtx, "registering github runner inside of vm")
registerRunnerErr = ankaCLI.AnkaRun(pluginCtx,
"./register-runner.bash",
vm.Name, *runnerRegistration.Token, repositoryURL, strings.Join(workflowJob.Labels, ","), ctxPlugin.RunnerGroup,
Expand All @@ -850,6 +852,7 @@ func Run(
return
default:
}
logger.DebugContext(pluginCtx, "starting github runner inside of vm")
startRunnerErr = ankaCLI.AnkaRun(pluginCtx, "./start-runner.bash")
if startRunnerErr != nil {
logger.ErrorContext(pluginCtx, "error executing start-runner.bash", "err", startRunnerErr)
Expand Down
Loading

0 comments on commit 3e78964

Please sign in to comment.