From 5646a342c28ce0719da2094a367dbda0e5828f75 Mon Sep 17 00:00:00 2001 From: allan Date: Wed, 19 Oct 2022 13:41:26 +0200 Subject: [PATCH 01/22] added go mod stuff --- go.mod | 15 +++++++++++++++ go.sum | 17 +++++++++++++++++ main.go | 8 ++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..79838b3 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module github.com/Allan-Nava/nexus-cli + +go 1.19 + +require ( + github.com/BurntSushi/toml v1.2.0 + github.com/mlabouardy/nexus-cli v0.0.0-20180823085010-e9ab90ee31be + github.com/urfave/cli v1.22.10 +) + +require ( + github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect + github.com/russross/blackfriday/v2 v2.0.1 // indirect + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4ca35a2 --- /dev/null +++ b/go.sum @@ -0,0 +1,17 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= +github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/mlabouardy/nexus-cli v0.0.0-20180823085010-e9ab90ee31be h1:5JIRQAv1vxCnmi/YThMfd7Wpr8Sz3gju9wUUZmh23fA= +github.com/mlabouardy/nexus-cli v0.0.0-20180823085010-e9ab90ee31be/go.mod h1:pmsbmSTdgWwFXXRGWoSScdYQRTEOiymG2DTVN1/FWMQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/urfave/cli v1.22.10 h1:p8Fspmz3iTctJstry1PYS3HVdllxnEzTEsgIgtxTrCk= +github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 2115b07..efd62d4 100644 --- a/main.go +++ b/main.go @@ -21,12 +21,16 @@ func main() { app := cli.NewApp() app.Name = "Nexus CLI" app.Usage = "Manage Docker Private Registry on Nexus" - app.Version = "1.0.0-beta" + app.Version = "1.0.0" app.Authors = []cli.Author{ - cli.Author{ + { Name: "Mohamed Labouardy", Email: "mohamed@labouardy.com", }, + { + Name: "Allan Nava", + Email: "allan.nava@hiway.media", + }, } app.Commands = []cli.Command{ { From bab8c1f610445d31681f1ca09caaa2793f81b650 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Wed, 19 Oct 2022 14:25:20 +0200 Subject: [PATCH 02/22] upgrade version --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index efd62d4..6042e20 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { app := cli.NewApp() app.Name = "Nexus CLI" app.Usage = "Manage Docker Private Registry on Nexus" - app.Version = "1.0.0" + app.Version = "1.0.01" app.Authors = []cli.Author{ { Name: "Mohamed Labouardy", From 83c9f0e48d2fd417ac0fcd090d5b34cb1e4c702a Mon Sep 17 00:00:00 2001 From: allan Date: Wed, 19 Oct 2022 16:09:37 +0200 Subject: [PATCH 03/22] added gjson stuff --- go.mod | 3 +++ go.sum | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/go.mod b/go.mod index 79838b3..af66c21 100644 --- a/go.mod +++ b/go.mod @@ -12,4 +12,7 @@ require ( github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect github.com/russross/blackfriday/v2 v2.0.1 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect + github.com/tidwall/gjson v1.14.3 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.0 // indirect ) diff --git a/go.sum b/go.sum index 4ca35a2..8ad0d1b 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,12 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw= +github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/urfave/cli v1.22.10 h1:p8Fspmz3iTctJstry1PYS3HVdllxnEzTEsgIgtxTrCk= github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 0a9845a6bf7073371b371e77af3cb8bc4290305b Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Wed, 19 Oct 2022 17:12:47 +0200 Subject: [PATCH 04/22] updated list tag with created image --- main.go | 44 ++++++++++++++++++++++------------ registry/registry.go | 56 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 6042e20..abf14fe 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,9 @@ import ( "fmt" "html/template" "os" + "sort" - "github.com/mlabouardy/nexus-cli/registry" + "github.com/Allan-Nava/nexus-cli/registry" "github.com/urfave/cli" ) @@ -183,20 +184,24 @@ func listTagsByImage(c *cli.Context) error { if imgName == "" { cli.ShowSubcommandHelp(c) } + var imageManifests []registry.ImageManifestV1 tags, err := r.ListTagsByImage(imgName) - - compareStringNumber := func(str1, str2 string) bool { - return extractNumberFromString(str1) < extractNumberFromString(str2) + for _, tag := range tags { + manifest, _ := r.ImageManifestV1(imgName, tag) + imageManifests = append(imageManifests, manifest) } - Compare(compareStringNumber).Sort(tags) + // + sort.Slice(imageManifests, func(i, j int) bool { + return imageManifests[i].Date.After(imageManifests[j].Date) + }) if err != nil { return cli.NewExitError(err.Error(), 1) } - for _, tag := range tags { - fmt.Println(tag) + for _, image := range imageManifests { + fmt.Println(image.Tag, " created: ", image.Created) } - fmt.Printf("There are %d images for %s\n", len(tags), imgName) + fmt.Printf("There are %d images for %s\n", len(imageManifests), imgName) return nil } @@ -241,20 +246,29 @@ func deleteImage(c *cli.Context) error { cli.ShowSubcommandHelp(c) } else { tags, err := r.ListTagsByImage(imgName) - compareStringNumber := func(str1, str2 string) bool { + /*compareStringNumber := func(str1, str2 string) bool { return extractNumberFromString(str1) < extractNumberFromString(str2) } - Compare(compareStringNumber).Sort(tags) + Compare(compareStringNumber).Sort(tags)*/ + var imageManifests []registry.ImageManifestV1 + for _, tag := range tags { + manifest, _ := r.ImageManifestV1(imgName, tag) + imageManifests = append(imageManifests, manifest) + } + // + sort.Slice(imageManifests, func(i, j int) bool { + return imageManifests[i].Date.After(imageManifests[j].Date) + }) if err != nil { return cli.NewExitError(err.Error(), 1) } - if len(tags) >= keep { - for _, tag := range tags[:len(tags)-keep] { - fmt.Printf("%s:%s image will be deleted ...\n", imgName, tag) - r.DeleteImageByTag(imgName, tag) + if len(imageManifests) >= keep { + for _, tag := range imageManifests[:len(imageManifests)-keep] { + fmt.Printf("%s:%s image will be deleted ...\n", imgName, tag.Tag) + r.DeleteImageByTag(imgName, tag.Tag) } } else { - fmt.Printf("Only %d images are available\n", len(tags)) + fmt.Printf("Only %d images are available\n", len(imageManifests)) } } } else { diff --git a/registry/registry.go b/registry/registry.go index 8f22558..0065dae 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -4,11 +4,17 @@ import ( "encoding/json" "errors" "fmt" - "github.com/BurntSushi/toml" + "io" + "log" "net/http" "os" + "time" + + "github.com/BurntSushi/toml" + "github.com/tidwall/gjson" ) +const ACCEPT_HEADER_V1 = "application/vnd.docker.distribution.manifest.v1+json" const ACCEPT_HEADER = "application/vnd.docker.distribution.manifest.v2+json" const CREDENTIALS_FILE = ".credentials" @@ -40,6 +46,15 @@ type LayerInfo struct { Digest string `json:"digest"` } +type ImageManifestV1 struct { + SchemaVersion int64 `json:"schemaVersion"` + Name string `json:"name"` + Tag string `json:"tag"` + Architecture string `json:"architecture"` + Created string + Date time.Time +} + func NewRegistry() (Registry, error) { r := Registry{} if _, err := os.Stat(CREDENTIALS_FILE); os.IsNotExist(err) { @@ -136,6 +151,45 @@ func (r Registry) ImageManifest(image string, tag string) (ImageManifest, error) } +func (r Registry) ImageManifestV1(image string, tag string) (ImageManifestV1, error) { + var imageManifest ImageManifestV1 + client := &http.Client{} + + url := fmt.Sprintf("%s/repository/%s/v2/%s/manifests/%s", r.Host, r.Repository, image, tag) + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return imageManifest, err + } + req.SetBasicAuth(r.Username, r.Password) + req.Header.Add("Accept", ACCEPT_HEADER_V1) + + resp, err := client.Do(req) + if err != nil { + return imageManifest, err + } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return imageManifest, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode)) + } + b, err := io.ReadAll(resp.Body) + // b, err := ioutil.ReadAll(resp.Body) Go.1.15 and earlier + if err != nil { + log.Fatalln(err) + } + //json.NewDecoder(resp.Body).Decode(&imageManifest) + compatibilityString := gjson.GetBytes(b, `history.0.v1Compatibility`) + created := gjson.Get(compatibilityString.String(), `created`) + imageManifest.Created = created.String() + imageManifest.Date = created.Time() + imageManifest.Tag = gjson.GetBytes(b, `tag`).String() + imageManifest.Name = gjson.GetBytes(b, `name`).String() + imageManifest.Architecture = gjson.GetBytes(b, `architecture`).String() + imageManifest.SchemaVersion = gjson.GetBytes(b, `schemaVersion`).Int() + // + return imageManifest, nil +} + func (r Registry) DeleteImageByTag(image string, tag string) error { sha, err := r.getImageSHA(image, tag) if err != nil { From 4063975d35dedaf54d9d3a94bfdd994db58f28fe Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Wed, 19 Oct 2022 17:33:03 +0200 Subject: [PATCH 05/22] fix deleteImage --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index abf14fe..2d9f9fd 100644 --- a/main.go +++ b/main.go @@ -257,14 +257,14 @@ func deleteImage(c *cli.Context) error { } // sort.Slice(imageManifests, func(i, j int) bool { - return imageManifests[i].Date.After(imageManifests[j].Date) + return imageManifests[i].Date.Before(imageManifests[j].Date) }) if err != nil { return cli.NewExitError(err.Error(), 1) } if len(imageManifests) >= keep { for _, tag := range imageManifests[:len(imageManifests)-keep] { - fmt.Printf("%s:%s image will be deleted ...\n", imgName, tag.Tag) + fmt.Printf("%s:%s image will be deleted date: %s...\n", imgName, tag.Tag, tag.Created) r.DeleteImageByTag(imgName, tag.Tag) } } else { From 7cd5464441b495124960e07e83c2f09618e7ffb4 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:15:58 +0100 Subject: [PATCH 06/22] fix release --- .github/dependabot.yml | 0 .github/workflows/release.yml | 38 +++++++++++++++++++++++++++++++++++ CHANGELOG.md | 5 +++++ 3 files changed, 43 insertions(+) create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/release.yml create mode 100644 CHANGELOG.md diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..e69de29 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..08afe8f --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,38 @@ +name: Release +# +on: + push: + branches: + - master + tags: + - 'v*.*.*' +# +jobs: + release: + runs-on: ubuntu-latest + # + steps: + - uses: actions/checkout@v3 + # + - name: Setup Go environment + uses: actions/setup-go@v3 + with: + go-version: '1.17' + - name: Install dependencies + run: go mod tidy + - name: Read changelog + id: changelog + run: | + awk '/^## / {s++} s == 1 {print}' CHANGELOG.md > release_notes.md + echo "::set-output name=path::release_notes.md" + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + body_path: ${{ steps.changelog.outputs.path }} + draft: true +# \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..bcf218d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 1.0.0 + +Nexus CLI \ No newline at end of file From f5154f67aa3fe6d00023b08f208779132883a909 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:16:34 +0100 Subject: [PATCH 07/22] fix release --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 08afe8f..ba0b7aa 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Go environment uses: actions/setup-go@v3 with: - go-version: '1.17' + go-version: '1.19' - name: Install dependencies run: go mod tidy - name: Read changelog From d02cceb328f48b9fb513e8920727d9d3255795bc Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:26:31 +0100 Subject: [PATCH 08/22] added go build . and plugin metadata --- .github/workflows/release.yml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ba0b7aa..6ef98f7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -20,6 +20,19 @@ jobs: go-version: '1.19' - name: Install dependencies run: go mod tidy + + - name: Build go + run: go build . + + - name: Get plugin metadata + id: metadata + run: | + sudo apt-get install jq + export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') + export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_ID}.md5 + echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" + echo ::set-output name=github-tag::${GITHUB_REF#refs/*/} + # - name: Read changelog id: changelog run: | @@ -35,4 +48,26 @@ jobs: release_name: Release ${{ github.ref }} body_path: ${{ steps.changelog.outputs.path }} draft: true + # + - name: Add plugin to release + id: upload-plugin-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./${{ steps.metadata.outputs.archive }} + asset_name: ${{ steps.metadata.outputs.archive }} + asset_content_type: application/zip + # + - name: Add checksum to release + id: upload-checksum-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./${{ steps.metadata.outputs.archive-checksum }} + asset_name: ${{ steps.metadata.outputs.archive-checksum }} + asset_content_type: text/plain # \ No newline at end of file From 99b2a920dae67bbc9e4c45e5fec2bde867aa056b Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:30:19 +0100 Subject: [PATCH 09/22] Trying to fix plugin metadata --- .github/workflows/release.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6ef98f7..f9a4f45 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,7 +29,10 @@ jobs: run: | sudo apt-get install jq export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') + export NEXUS_PLUGIN_ARTIFACT=${NEXUS_ID}.zip export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_ID}.md5 + echo "::set-output name=plugin-id::${GRAFANA_PLUGIN_ID}" + echo "::set-output name=archive::${NEXUS_PLUGIN_ARTIFACT}" echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" echo ::set-output name=github-tag::${GITHUB_REF#refs/*/} # From d83af6ce8a2dd29f5d379d73301785cb85b394bd Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:33:10 +0100 Subject: [PATCH 10/22] fix package plugin --- .github/workflows/release.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f9a4f45..d6988dc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -41,6 +41,15 @@ jobs: run: | awk '/^## / {s++} s == 1 {print}' CHANGELOG.md > release_notes.md echo "::set-output name=path::release_notes.md" + + - name: Package plugin + id: package-plugin + run: | + mv dist ${{ steps.metadata.outputs.plugin-id }} + zip ${{ steps.metadata.outputs.archive }} ${{ steps.metadata.outputs.plugin-id }} -r + md5sum ${{ steps.metadata.outputs.archive }} > ${{ steps.metadata.outputs.archive-checksum }} + echo "::set-output name=checksum::$(cat ./${{ steps.metadata.outputs.archive-checksum }} | cut -d' ' -f1)" + - name: Create release id: create_release uses: actions/create-release@v1 From 032dc66b160838b9e39e80fdc4c50d7101d14455 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:34:36 +0100 Subject: [PATCH 11/22] nexus cli --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d6988dc..e2c90ec 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -31,7 +31,7 @@ jobs: export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') export NEXUS_PLUGIN_ARTIFACT=${NEXUS_ID}.zip export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_ID}.md5 - echo "::set-output name=plugin-id::${GRAFANA_PLUGIN_ID}" + echo "::set-output name=plugin-id::${NEXUS_ID}" echo "::set-output name=archive::${NEXUS_PLUGIN_ARTIFACT}" echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" echo ::set-output name=github-tag::${GITHUB_REF#refs/*/} @@ -45,7 +45,7 @@ jobs: - name: Package plugin id: package-plugin run: | - mv dist ${{ steps.metadata.outputs.plugin-id }} + mv nexus-cli ${{ steps.metadata.outputs.plugin-id }} zip ${{ steps.metadata.outputs.archive }} ${{ steps.metadata.outputs.plugin-id }} -r md5sum ${{ steps.metadata.outputs.archive }} > ${{ steps.metadata.outputs.archive-checksum }} echo "::set-output name=checksum::$(cat ./${{ steps.metadata.outputs.archive-checksum }} | cut -d' ' -f1)" From 57dd2aca0c6a6b6fbfa955a1468b52300cd015eb Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:36:56 +0100 Subject: [PATCH 12/22] fix workflow --- .github/workflows/release.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e2c90ec..b49d428 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -82,4 +82,10 @@ jobs: asset_path: ./${{ steps.metadata.outputs.archive-checksum }} asset_name: ${{ steps.metadata.outputs.archive-checksum }} asset_content_type: text/plain + # + - name: Publish to Release + run: | + echo A draft release has been created for your plugin. Please review and publish it. + echo + echo '{ "id": "${{ steps.metadata.outputs.plugin-id }}", "type": "${{ steps.metadata.outputs.plugin-type }}", "url": "https://github.com/${{ github.repository }}", "versions": [ { "version": "${{ steps.metadata.outputs.plugin-version }}", "commit": "${{ github.sha }}", "url": "https://github.com/${{ github.repository }}", "download": { "any": { "url": "https://github.com/${{ github.repository }}/releases/download/v${{ steps.metadata.outputs.plugin-version }}/${{ steps.metadata.outputs.archive }}", "md5": "${{ steps.package-plugin.outputs.checksum }}" } } } ] }' | jq # \ No newline at end of file From 7a523d32e302d2d6f06c281fb72079a672291645 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:37:34 +0100 Subject: [PATCH 13/22] fix --- .github/workflows/release.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b49d428..1af2a53 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -84,8 +84,8 @@ jobs: asset_content_type: text/plain # - name: Publish to Release - run: | - echo A draft release has been created for your plugin. Please review and publish it. - echo - echo '{ "id": "${{ steps.metadata.outputs.plugin-id }}", "type": "${{ steps.metadata.outputs.plugin-type }}", "url": "https://github.com/${{ github.repository }}", "versions": [ { "version": "${{ steps.metadata.outputs.plugin-version }}", "commit": "${{ github.sha }}", "url": "https://github.com/${{ github.repository }}", "download": { "any": { "url": "https://github.com/${{ github.repository }}/releases/download/v${{ steps.metadata.outputs.plugin-version }}/${{ steps.metadata.outputs.archive }}", "md5": "${{ steps.package-plugin.outputs.checksum }}" } } } ] }' | jq + run: | + echo A draft release has been created for your plugin. Please review and publish it. + echo + echo '{ "id": "${{ steps.metadata.outputs.plugin-id }}", "type": "${{ steps.metadata.outputs.plugin-type }}", "url": "https://github.com/${{ github.repository }}", "versions": [ { "version": "${{ steps.metadata.outputs.plugin-version }}", "commit": "${{ github.sha }}", "url": "https://github.com/${{ github.repository }}", "download": { "any": { "url": "https://github.com/${{ github.repository }}/releases/download/v${{ steps.metadata.outputs.plugin-version }}/${{ steps.metadata.outputs.archive }}", "md5": "${{ steps.package-plugin.outputs.checksum }}" } } } ] }' | jq # \ No newline at end of file From 473ef4c1ed11a810617f15038d83441f18323d6f Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:41:47 +0100 Subject: [PATCH 14/22] update plugin metadata --- .github/workflows/release.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1af2a53..6d76254 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,6 +32,7 @@ jobs: export NEXUS_PLUGIN_ARTIFACT=${NEXUS_ID}.zip export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_ID}.md5 echo "::set-output name=plugin-id::${NEXUS_ID}" + echo "::set-output name=plugin-version::${NEXUS_ID}" echo "::set-output name=archive::${NEXUS_PLUGIN_ARTIFACT}" echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" echo ::set-output name=github-tag::${GITHUB_REF#refs/*/} From 43c9af392e3f0097e646b11818c15b150a648d3e Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:43:03 +0100 Subject: [PATCH 15/22] fix release --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6d76254..b23be5d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -2,8 +2,8 @@ name: Release # on: push: - branches: - - master + #branches: + # - master tags: - 'v*.*.*' # From 0cde2503a5c1074bc95b78ae2b96958eadfc6730 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 16:57:50 +0100 Subject: [PATCH 16/22] fix nexus cli --- .github/workflows/release.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b23be5d..65d9ef2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -28,11 +28,12 @@ jobs: id: metadata run: | sudo apt-get install jq - export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') - export NEXUS_PLUGIN_ARTIFACT=${NEXUS_ID}.zip - export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_ID}.md5 + export NEXUS_ID="nexus-cli" + export NEXUS_VERSION=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') + export NEXUS_PLUGIN_ARTIFACT=${NEXUS_VERSION} + export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_VERSION}.md5 echo "::set-output name=plugin-id::${NEXUS_ID}" - echo "::set-output name=plugin-version::${NEXUS_ID}" + echo "::set-output name=plugin-version::${NEXUS_VERSION}" echo "::set-output name=archive::${NEXUS_PLUGIN_ARTIFACT}" echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" echo ::set-output name=github-tag::${GITHUB_REF#refs/*/} From 099b27f0cd67f828d7f42672e9be80b82bf3af00 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 17:01:43 +0100 Subject: [PATCH 17/22] fix --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 65d9ef2..340940b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -28,7 +28,7 @@ jobs: id: metadata run: | sudo apt-get install jq - export NEXUS_ID="nexus-cli" + export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') export NEXUS_VERSION=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') export NEXUS_PLUGIN_ARTIFACT=${NEXUS_VERSION} export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_VERSION}.md5 From 889ff8dd661aca58f269bb7225ef2446a146b8ea Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 17:05:22 +0100 Subject: [PATCH 18/22] fix release build --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 340940b..8492647 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -30,7 +30,7 @@ jobs: sudo apt-get install jq export NEXUS_ID=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') export NEXUS_VERSION=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') - export NEXUS_PLUGIN_ARTIFACT=${NEXUS_VERSION} + export NEXUS_PLUGIN_ARTIFACT=nexus-cli-${NEXUS_VERSION}.zip export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_VERSION}.md5 echo "::set-output name=plugin-id::${NEXUS_ID}" echo "::set-output name=plugin-version::${NEXUS_VERSION}" From 508236cfaa654141c694b9bbd5878aca324d2ff8 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 17:08:00 +0100 Subject: [PATCH 19/22] fix release --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8492647..c2542d4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,7 +32,7 @@ jobs: export NEXUS_VERSION=$(md5sum nexus-cli | sort | md5sum | cut -f1 -d ' ') export NEXUS_PLUGIN_ARTIFACT=nexus-cli-${NEXUS_VERSION}.zip export NEXUS_ARTIFACTS_CHECKSUM=${NEXUS_VERSION}.md5 - echo "::set-output name=plugin-id::${NEXUS_ID}" + echo "::set-output name=plugin-id::nexus-cli" echo "::set-output name=plugin-version::${NEXUS_VERSION}" echo "::set-output name=archive::${NEXUS_PLUGIN_ARTIFACT}" echo "::set-output name=archive-checksum::${NEXUS_ARTIFACTS_CHECKSUM}" From 160436bfc70de7235694cd2bf46a0ad2d910a477 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 17:09:27 +0100 Subject: [PATCH 20/22] fix archive --- .github/workflows/release.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c2542d4..3bf7380 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -46,8 +46,7 @@ jobs: - name: Package plugin id: package-plugin - run: | - mv nexus-cli ${{ steps.metadata.outputs.plugin-id }} + run: |fix zip ${{ steps.metadata.outputs.archive }} ${{ steps.metadata.outputs.plugin-id }} -r md5sum ${{ steps.metadata.outputs.archive }} > ${{ steps.metadata.outputs.archive-checksum }} echo "::set-output name=checksum::$(cat ./${{ steps.metadata.outputs.archive-checksum }} | cut -d' ' -f1)" From a6192d35c09cbac2d7934e886f9104f0e2911f3a Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Tue, 15 Nov 2022 17:23:56 +0100 Subject: [PATCH 21/22] fix release --- .github/workflows/release.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3bf7380..ab46cff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -46,8 +46,8 @@ jobs: - name: Package plugin id: package-plugin - run: |fix - zip ${{ steps.metadata.outputs.archive }} ${{ steps.metadata.outputs.plugin-id }} -r + run: | + zip ${{ steps.metadata.outputs.archive }} nexus-cli -r md5sum ${{ steps.metadata.outputs.archive }} > ${{ steps.metadata.outputs.archive-checksum }} echo "::set-output name=checksum::$(cat ./${{ steps.metadata.outputs.archive-checksum }} | cut -d' ' -f1)" From 2cc0ec19c14a4c7314369ec17bf912883b50d7c7 Mon Sep 17 00:00:00 2001 From: Allan Nava Date: Wed, 25 Dec 2024 09:23:36 +0100 Subject: [PATCH 22/22] change registry.go --- registry/registry.go | 69 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/registry/registry.go b/registry/registry.go index 0065dae..287a71c 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -14,6 +14,9 @@ import ( "github.com/tidwall/gjson" ) + +var Start = time.Now() +var elapsed = time.Since(Start) const ACCEPT_HEADER_V1 = "application/vnd.docker.distribution.manifest.v1+json" const ACCEPT_HEADER = "application/vnd.docker.distribution.manifest.v2+json" const CREDENTIALS_FILE = ".credentials" @@ -152,11 +155,22 @@ func (r Registry) ImageManifest(image string, tag string) (ImageManifest, error) } func (r Registry) ImageManifestV1(image string, tag string) (ImageManifestV1, error) { + var tr = &http.Transport{ + MaxIdleConnsPerHost: 90, + } var imageManifest ImageManifestV1 - client := &http.Client{} - + var client = &http.Client{ + Transport: tr, + } +//elapsed = time.Since(Start) +// log.Printf("tag is %s", tag) +// log.Printf("begin get tag %s", elapsed) +// Start = time.Now() url := fmt.Sprintf("%s/repository/%s/v2/%s/manifests/%s", r.Host, r.Repository, image, tag) req, err := http.NewRequest("GET", url, nil) + req.Header = http.Header{ + "Accept": {"application/vnd.docker.distribution.manifest.v2+json"}, +} if err != nil { return imageManifest, err } @@ -168,7 +182,6 @@ func (r Registry) ImageManifestV1(image string, tag string) (ImageManifestV1, er return imageManifest, err } defer resp.Body.Close() - if resp.StatusCode != 200 { return imageManifest, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode)) } @@ -177,15 +190,51 @@ func (r Registry) ImageManifestV1(image string, tag string) (ImageManifestV1, er if err != nil { log.Fatalln(err) } + resp = nil //json.NewDecoder(resp.Body).Decode(&imageManifest) - compatibilityString := gjson.GetBytes(b, `history.0.v1Compatibility`) - created := gjson.Get(compatibilityString.String(), `created`) + compatibilityString := gjson.GetBytes(b, `config`) + b = nil + digest := gjson.Get(compatibilityString.String(), `digest`) + +// log.Printf("digest is %s", digest) + + url = fmt.Sprintf("%s/repository/%s/v2/%s/blobs/%s", r.Host, r.Repository, image, digest) + req, err = http.NewRequest("GET", url, nil) + req.Header = http.Header{ + "Accept": {"application/vnd.docker.distribution.manifest.v2+json"}, +} + if err != nil { + return imageManifest, err + } + req.SetBasicAuth(r.Username, r.Password) + req.Header.Add("Accept", ACCEPT_HEADER_V1) + + resp, err = client.Do(req) + if err != nil { + return imageManifest, err + } + defer resp.Body.Close() + if resp.StatusCode != 200 { + return imageManifest, errors.New(fmt.Sprintf("HTTP Code: %d", resp.StatusCode)) + } + b, err = io.ReadAll(resp.Body) + // b, err := ioutil.ReadAll(resp.Body) Go.1.15 and earlier + if err != nil { + log.Fatalln(err) + } + resp = nil + //json.NewDecoder(resp.Body).Decode(&imageManifest) + created := gjson.Get(string(b),"created") + b = nil +if !created.Exists() { + return imageManifest, err +} + +// log.Printf("created is %s", created.String()) imageManifest.Created = created.String() imageManifest.Date = created.Time() - imageManifest.Tag = gjson.GetBytes(b, `tag`).String() - imageManifest.Name = gjson.GetBytes(b, `name`).String() - imageManifest.Architecture = gjson.GetBytes(b, `architecture`).String() - imageManifest.SchemaVersion = gjson.GetBytes(b, `schemaVersion`).Int() + imageManifest.Tag = tag + imageManifest.Name = image // return imageManifest, nil } @@ -242,4 +291,4 @@ func (r Registry) getImageSHA(image string, tag string) (string, error) { } return resp.Header.Get("docker-content-digest"), nil -} +} \ No newline at end of file