Skip to content

Commit

Permalink
quick fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NorseGaud committed Apr 3, 2024
1 parent ae49356 commit 85b0ce0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/t1-with-tag-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 't1-with-tag-1'
on:
workflow_dispatch:

jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22+brew-git",
"run-id:${{ github.run_id }}",
"unique-id:1"
]
steps:
- uses: actions/checkout@v3
- run: |
ls -laht
sw_vers
hostname
echo "123"
21 changes: 21 additions & 0 deletions .github/workflows/t1-with-tag-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: 't1-with-tag-2'
on:
workflow_dispatch:

jobs:
testJob:
runs-on: [
"self-hosted",
"anka",
"anka-template:d792c6f6-198c-470f-9526-9c998efe7ab4",
"anka-template-tag:vanilla+port-forward-22",
"run-id:${{ github.run_id }}",
"unique-id:1"
]
steps:
- uses: actions/checkout@v3
- run: |
ls -laht
sw_vers
hostname
echo "123"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'apple'
name: 't1-without-tag'
on:
workflow_dispatch:

Expand All @@ -17,5 +17,4 @@ jobs:
ls -laht
sw_vers
hostname
echo "123"
sleep 10
echo "123"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: 'banana'
name: 't2-dual-without-tag'
on:
workflow_dispatch:

Expand Down
45 changes: 19 additions & 26 deletions internal/anka/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ type AnkaJson struct {
Message string
}

type AnkaListTemplate struct {
Name string
UUID string
Tag string
Status string
}

type Cli struct {
Plugin string
License struct {
Expand Down Expand Up @@ -301,12 +294,12 @@ func HostHasVmCapacity(ctx context.Context) bool {
return true
}

func (cli *Cli) EnsureVMTemplateExists(ctx context.Context, template string, tag string) error {
func (cli *Cli) EnsureVMTemplateExists(ctx context.Context, targetTemplate string, targetTag string) error {
logger := logging.GetLoggerFromContext(ctx)
ankaCLI := GetAnkaCLIFromContext(ctx)
globals := config.GetGlobalsFromContext(ctx)
pullTemplate := false
list, err := ankaCLI.ExecuteParseJson(ctx, "anka", "-j", "list", template)
list, err := ankaCLI.ExecuteParseJson(ctx, "anka", "-j", "list", targetTemplate)
if err != nil {
list, innerErr := ankaCLI.ExecuteParseJson(ctx, "anka", "-j", "list")
if innerErr != nil {
Expand All @@ -317,15 +310,14 @@ func (cli *Cli) EnsureVMTemplateExists(ctx context.Context, template string, tag
}
logger.DebugContext(ctx, "list output", "json", list)
if list.Status == "ERROR" {
if list.Message == fmt.Sprintf("%s: not found", template) {
if list.Message == fmt.Sprintf("%s: not found", targetTemplate) {
pullTemplate = true
} else {
logger.ErrorContext(ctx, "error executing anka list", "err", list.Message)
return err
}
}
if list.Status == "OK" {
var ankaListTemplate AnkaListTemplate
// ensure tag is proper; skip if tag is hard coded and we already have it locally
if bodySlice, ok := list.Body.([]interface{}); ok {
body, ok := bodySlice[0].(map[string]interface{})
Expand All @@ -334,30 +326,31 @@ func (cli *Cli) EnsureVMTemplateExists(ctx context.Context, template string, tag
logger.ErrorContext(ctx, "unable to parse bodySlice[0] to map[string]interface{}")
return fmt.Errorf("unable to parse bodySlice[0] to map[string]interface{}")
}
ankaListTemplate.Name = body["name"].(string)
ankaListTemplate.UUID = body["uuid"].(string)
ankaListTemplate.Tag = body["version"].(string)
ankaListTemplate.Status = body["status"].(string)
} else {
logger.ErrorContext(ctx, "unable to parse list.Body to []interface{}")
return fmt.Errorf("unable to parse list.Body to []interface{}")
}
if tag != "(using latest)" {
if ankaListTemplate.Tag != tag {
pullTemplate = true
fmt.Printf("1\n")
if status, ok := body["status"].(string); ok {
if status == "failed" {
return fmt.Errorf("vm template is not running and instead %s", status)
}
}
if version, ok := body["version"].(string); ok {
if targetTag != "(using latest)" {
if version != targetTag {
pullTemplate = true
}
} else {
// always pull to ensure latest, if (using latest)
pullTemplate = true
}
}
} else {
// always pull to ensure latest
pullTemplate = true
return fmt.Errorf("unable to parse list.Body to []interface{}")
}
}
if pullTemplate {
if !globals.PullLock.TryLock() {
return fmt.Errorf("a pull is already running on this host")
}
defer globals.PullLock.Unlock()
err := cli.AnkaRegistryPull(ctx, template, tag)
err := cli.AnkaRegistryPull(ctx, targetTemplate, targetTag)
if err != nil {
logger.ErrorContext(ctx, "error executing anka registry pull", "err", err)
return err
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

var version string // Declare version variable to hold the version set by go build

var runOnce string

func main() {
logger := logging.New()
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -108,6 +110,9 @@ func main() {
if ctx.Err() != nil {
return
}
if runOnce == "true" { // only run once; used for testing
return
}
time.Sleep(time.Duration(service.SleepInterval) * time.Second)
}
}
Expand Down

0 comments on commit 85b0ce0

Please sign in to comment.