Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 1 | package image_repositories |
| 2 | |
| 3 | import ( |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 4 | "context" |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 5 | _ "embed" |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 6 | "encoding/json" |
| 7 | "fmt" |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 8 | "io" |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 9 | "net/http" |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 10 | "text/template" |
| 11 | |
| 12 | "github.com/go-git/go-billy/v5" |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 13 | "github.com/google/go-github/v47/github" |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | //go:embed template/Dockerfile |
| 17 | var dockerfileTemplate string |
| 18 | |
| 19 | type Dockerfile struct { |
Mohammed Naser | 5a4eb80 | 2022-09-14 15:19:29 -0400 | [diff] [blame] | 20 | Project string |
| 21 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 22 | BindepImage string |
| 23 | BindepImageTag string |
| 24 | BuilderImage string |
| 25 | BuilderImageTag string |
| 26 | RuntimeImage string |
| 27 | RuntimeImageTag string |
| 28 | |
| 29 | template *template.Template |
| 30 | } |
| 31 | |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 32 | func NewDockerfile(ctx context.Context, ir *ImageRepository) (*Dockerfile, error) { |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 33 | tmpl, err := template.New("Dockerfile").Parse(dockerfileTemplate) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
Mohammed Naser | 96c23ef | 2022-09-08 19:19:23 -0400 | [diff] [blame] | 38 | builderImageTag, err := getImageTag(ctx, ir.githubClient, "docker-openstack-builder", "openstack-builder-focal") |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 43 | runtimeImageTag, err := getImageTag(ctx, ir.githubClient, "docker-openstack-runtime", "openstack-runtime-focal") |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 48 | return &Dockerfile{ |
Mohammed Naser | 5a4eb80 | 2022-09-14 15:19:29 -0400 | [diff] [blame] | 49 | Project: ir.Project, |
| 50 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 51 | BindepImage: "quay.io/vexxhost/bindep-loci", |
| 52 | BindepImageTag: "latest", |
| 53 | BuilderImage: "quay.io/vexxhost/openstack-builder-focal", |
Mohammed Naser | 96c23ef | 2022-09-08 19:19:23 -0400 | [diff] [blame] | 54 | BuilderImageTag: builderImageTag, |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 55 | RuntimeImage: "quay.io/vexxhost/openstack-runtime-focal", |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 56 | RuntimeImageTag: runtimeImageTag, |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 57 | |
| 58 | template: tmpl, |
| 59 | }, nil |
| 60 | } |
| 61 | |
Mohammed Naser | aaba50a | 2022-09-06 16:15:36 -0400 | [diff] [blame] | 62 | type quayTagList struct { |
| 63 | Tags []quayTag `json:"tags"` |
| 64 | Page int `json:"page"` |
| 65 | HasAdditional bool `json:"has_additional"` |
| 66 | } |
| 67 | |
| 68 | type quayTag struct { |
| 69 | Name string `json:"name"` |
| 70 | Reversion bool `json:"reversion"` |
| 71 | StartTimestamp int32 `json:"start_ts"` |
| 72 | ManifestDigest string `json:"manifest_digest"` |
| 73 | IsManifestList bool `json:"is_manifest_list"` |
| 74 | Size int `json:"size"` |
| 75 | LastModified string `json:"last_modified"` |
| 76 | } |
| 77 | |
| 78 | func getImageTag(ctx context.Context, client *github.Client, repository string, image string) (string, error) { |
| 79 | // Grab the latest SHA from the main branch |
| 80 | commit, _, err := client.Repositories.GetCommitSHA1(ctx, "vexxhost", repository, "main", "") |
| 81 | if err != nil { |
| 82 | return "", err |
| 83 | } |
| 84 | |
| 85 | // Check if the image exists in Quay.io |
| 86 | url := fmt.Sprintf("https://quay.io/api/v1/repository/vexxhost/%s/tag/?specificTag=%s", image, commit) |
| 87 | resp, err := http.Get(url) |
| 88 | if err != nil { |
| 89 | return "", err |
| 90 | } |
| 91 | |
| 92 | // Decode the response |
| 93 | var quayResponse quayTagList |
| 94 | decoder := json.NewDecoder(resp.Body) |
| 95 | decoder.DisallowUnknownFields() |
| 96 | err = decoder.Decode(&quayResponse) |
| 97 | if err != nil { |
| 98 | return "", err |
| 99 | } |
| 100 | |
| 101 | // Check if the tag exists |
| 102 | if len(quayResponse.Tags) == 0 { |
| 103 | return "", fmt.Errorf("tag %s does not exist in quay.io/vexxhost/%s", commit, image) |
| 104 | } |
| 105 | |
| 106 | return commit, nil |
| 107 | } |
| 108 | |
Mohammed Naser | dabb1dc | 2022-09-06 14:45:59 -0400 | [diff] [blame] | 109 | func (d *Dockerfile) Write(wr io.Writer) error { |
| 110 | return d.template.Execute(wr, d) |
| 111 | } |
| 112 | |
| 113 | func (d *Dockerfile) WriteFile(fs billy.Filesystem) error { |
| 114 | f, err := fs.Create("Dockerfile") |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | defer f.Close() |
| 119 | |
| 120 | return d.Write(f) |
| 121 | } |