Skip to content

Commit

Permalink
add crane
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu committed Aug 25, 2023
1 parent b805e79 commit 2b81b70
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 292 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: auto generator
run: |
bash download.sh
export SYNC_DIR=skopeo
export SYNC_DIR=docs
/tmp/repos
- uses: peter-evans/create-pull-request@v5
with:
Expand Down
68 changes: 0 additions & 68 deletions dockerhub/count.go

This file was deleted.

72 changes: 72 additions & 0 deletions dockerhub/crane.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2023 [email protected].
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package dockerhub

import (
"context"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"golang.org/x/mod/semver"
"strings"
)

func skipPlatform(tag string) bool {
if strings.HasSuffix(tag, "arm64") || strings.HasSuffix(tag, "amd64") {
return false
}
return true
}

func list(ctx context.Context, src string, filter func(tag string) bool) ([]string, error) {
options := make([]crane.Option, 0)
o := crane.GetOptions(options...)

repo, err := name.NewRepository(src, o.Name...)
if err != nil {
return nil, fmt.Errorf("parsing repo %q: %w", src, err)
}

puller, err := remote.NewPuller(o.Remote...)
if err != nil {
return nil, err
}

lister, err := puller.Lister(ctx, repo)
if err != nil {
return nil, fmt.Errorf("reading tags for %s: %w", repo, err)
}

versions := make([]string, 0)

for lister.HasNext() {
tags, err := lister.Next(ctx)
if err != nil {
return nil, err
}
for _, tag := range tags.Tags {
newTag := tag
if filter != nil && !filter(tag) {
continue
}
versions = append(versions, newTag)
}
}
semver.Sort(versions)
return versions, nil
}
11 changes: 5 additions & 6 deletions dockerhub/generator_test.go → dockerhub/crane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ limitations under the License.
package dockerhub

import (
"context"
"testing"
)

func Test_getCIRun(t *testing.T) {
data, err := getCIRun("../.cirun.yml")
if err != nil {
t.Fatal(err)
}
t.Log(data)
func Test_list(t *testing.T) {
tags, err := list(context.TODO(), "labring/helm", skipPlatform)
t.Logf("%+v", tags)
t.Logf("%+v", err)
}
60 changes: 21 additions & 39 deletions dockerhub/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ limitations under the License.
package dockerhub

import (
"context"
"fmt"
"github.com/cuisongliu/logger"
"golang.org/x/sync/errgroup"
"os"
)

Expand All @@ -32,50 +29,35 @@ func Do() {
return
}
logger.Info("using syncDir %s", syncDir)
workflowDir := ".github/workflows"
err := autoRemoveGenerator(syncDir)
if err != nil {
logger.Fatal("autoRemoveGenerator sync config error %s", err.Error())
return
}
err = autoRemoveGenerator(workflowDir)
if err != nil {
logger.Fatal("autoRemoveGenerator workflow config error %s", err.Error())
return
}
got, err := fetchDockerHubAllRepo()
_, err = fetchDockerHubAllRepo()
if err != nil {
logger.Fatal("fetchDockerHubAllRepo error %s", err.Error())
return
}
data, err := getCIRun(".cirun.yml")
if err != nil {
logger.Fatal("getCIRun error %s", err.Error())
return
}
logger.Info("get docker hub all repo success")

g, _ := errgroup.WithContext(context.Background())

for k, v := range got {
// Capture the range variables.
k, v := k, v
g.Go(func() error {
if len(v.Repos) == 0 {
return nil
}
if err = generatorSyncFile(syncDir, k, v); err != nil {
return fmt.Errorf("generatorSyncFile %s error: %w", k, err)
}
if err = generatorWorkflowFile(workflowDir, syncDir, k, data); err != nil {
return fmt.Errorf("generatorWorkflowFile %s error: %w", k, err)
}
return nil
})
}

// Wait for all goroutines to finish and return the first error.
if err = g.Wait(); err != nil {
logger.Fatal(err.Error())
}
//g, _ := errgroup.WithContext(context.Background())

//for k, v := range got {
// // Capture the range variables.
// k, v := k, v
// g.Go(func() error {
// if len(v.Repos) == 0 {
// return nil
// }
// if err = generatorSyncFile(syncDir, k, v); err != nil {
// return fmt.Errorf("generatorSyncFile %s error: %w", k, err)
// }
// return nil
// })
//}
//
//// Wait for all goroutines to finish and return the first error.
//if err = g.Wait(); err != nil {
// logger.Fatal(err.Error())
//}
}
112 changes: 1 addition & 111 deletions dockerhub/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package dockerhub
import (
"fmt"
"github.com/cuisongliu/logger"
"gopkg.in/yaml.v2"
"html/template"
"os"
"path"
Expand All @@ -42,41 +41,6 @@ const tmpl = `docker.io:
tls-verify: false
`

const workflowTmpl = `name: {{ .SYNC_FILE_NAME }}
on:
push:
branches: [ main ]
paths:
- "{{ .SYNC_FILE }}"
- ".github/workflows/{{ .SYNC_FILE_NAME }}"
schedule:
- cron: '0 16 * * *'
workflow_dispatch:
env:
USERNAME: {{ .USER_KEY }}
PASSWORD: {{ .PASSWORD_KEY }}
jobs:
image-sync:
runs-on: {{.RUN_ON}}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: check podman
run: |
sudo podman version
- name: sync images
run: |
sudo podman run -it --rm -v ${PWD}:/workspace -w /workspace quay.io/skopeo/stable:latest \
sync --src yaml --dest docker {{ .SYNC_FILE }} {{ .REGISTRY_KEY }}/{{ .REPOSITORY_KEY }} \
--dest-username $USERNAME --dest-password "$PASSWORD" \
--keep-going --retry-times 2 --all
`

const prefix = "auto-sync"

func autoRemoveGenerator(dir string) error {
Expand All @@ -98,7 +62,7 @@ func autoRemoveGenerator(dir string) error {
return nil
}

func generatorSyncFile(dir, key string, repos RepoInfoList) error {
func generatorSyncFile(dir, key string, repos RepoInfo) error {
f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s.yaml", prefix, key)))
if err != nil {
return err
Expand All @@ -112,77 +76,3 @@ func generatorSyncFile(dir, key string, repos RepoInfoList) error {
logger.Info("generator sync config %s-%s.yaml success", prefix, key)
return nil
}

func getCIRun(file string) ([]string, error) {
type Runner struct {
Name string `yaml:"name"`
Cloud string `yaml:"cloud"`
MachineImage string `yaml:"machine_image"`
InstanceType []string `yaml:"instance_type"`
Labels []string `yaml:"labels"`
Region []string `yaml:"region"`
}

type Config struct {
Runners []Runner `yaml:"runners"`
}

data, err := os.ReadFile(file) //replace with your config file
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}

var config Config

err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
if len(config.Runners) == 0 {
return nil, fmt.Errorf("not found runners")
}
if len(config.Runners[0].Labels) == 0 {
return nil, fmt.Errorf("not found labels")
}
for _, label := range config.Runners[0].Labels {
if !strings.HasPrefix(label, "cirun") {
return nil, fmt.Errorf("not found cirun labels, must has prefix %s", "cirun")
}
}
return config.Runners[0].Labels, nil
}

func generatorWorkflowFile(dir, syncDir, key string, labels []string) error {
syncFileName := fmt.Sprintf("%s-%s.yaml", prefix, key)
syncFile := path.Join(syncDir, syncFileName)
f, err := os.Create(path.Join(dir, fmt.Sprintf("%s-%s.yaml", prefix, key)))
if err != nil {
return err
}
defer f.Close()
t := template.Must(template.New("repos").Parse(workflowTmpl))

runOn := "ubuntu-22.04"
if len(labels) > 0 {
runOn = fmt.Sprintf("%s--${{ github.run_id }}", labels[0])
}

err = t.Execute(f, map[string]string{
"PREFIX": prefix,
"SYNC_FILE": syncFile,
"SYNC_FILE_NAME": syncFileName,
"RUN_ON": runOn,
"USER_KEY": "${{ vars.A_REGISTRY_USERNAME }}",
"PASSWORD_KEY": "${{ secrets.A_REGISTRY_TOKEN }}",
"REGISTRY_KEY": "${{ vars.A_REGISTRY_NAME }}",
"REPOSITORY_KEY": "${{ vars.A_REGISTRY_REPOSITORY }}",
})
if err != nil {
return err
}
logger.Info("generator workflow config %s-%s.yaml success", prefix, key)
return nil
}
Loading

0 comments on commit 2b81b70

Please sign in to comment.