blob: 1bc9aae411b2e7ac6163abf434d72d97054691e1 [file] [log] [blame]
Mohammed Naser09d8ea82022-09-03 19:16:18 -04001package main
2
3import (
4 "context"
5 "fmt"
6 "strconv"
7 "strings"
8
9 "github.com/google/go-github/v47/github"
10 "github.com/gophercloud/gophercloud"
11 "github.com/gophercloud/gophercloud/openstack"
12 "github.com/gophercloud/gophercloud/openstack/orchestration/v1/stacks"
13 "github.com/gophercloud/gophercloud/pagination"
14 "github.com/gophercloud/utils/openstack/clientconfig"
15)
16
17func main() {
18 opts, err := clientconfig.AuthOptions(nil)
19 if err != nil {
20 panic(err)
21 }
22
23 provider, err := openstack.AuthenticatedClient(*opts)
24 if err != nil {
25 panic(err)
26 }
27
28 client, err := openstack.NewOrchestrationV1(provider, gophercloud.EndpointOpts{})
29 if err != nil {
30 panic(err)
31 }
32
33 stacks.List(client, stacks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
34 allStacks, err := stacks.ExtractStacks(page)
35 if err != nil {
36 return false, err
37 }
38
39 for _, stack := range allStacks {
40 if stack.Status == "DELETE_IN_PROGRESS" {
41 fmt.Println("skip delete in progress stack: " + stack.Name)
42 continue
43 }
44
45 if !strings.HasPrefix(stack.Name, "atmosphere-") {
46 panic("stack name does not start with atmosphere: " + stack.Name)
47 }
48
49 s := strings.Split(stack.Name, "-")
50 if len(s) != 3 {
51 panic("stack name does not have 3 parts: " + stack.Name)
52 }
53
54 runId, err := strconv.ParseInt(s[1], 10, 64)
55 if err != nil {
56 panic(err)
57 }
58 runAttempt, err := strconv.ParseInt(s[2], 10, 0)
59 if err != nil {
60 panic(err)
61 }
62
63 githubClient := github.NewClient(nil)
64 run, _, err := githubClient.Actions.GetWorkflowRunAttempt(context.TODO(), "vexxhost", "atmosphere", runId, int(runAttempt), nil)
65 if err != nil {
66 panic(err)
67 }
68
69 if run.GetStatus() == "completed" {
70 fmt.Println("Deleting stack: " + stack.Name)
71
72 _ = stacks.Delete(client, stack.Name, stack.ID)
73 if err != nil {
74 panic(err)
75 }
76 }
77 }
78
79 return true, nil
80 })
81}