Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
Check if aci is already in the nexus before push + rewrit of push in …
Browse files Browse the repository at this point in the history
…pure golang
  • Loading branch information
bob authored and n0rad committed Sep 3, 2017
1 parent c0b5ddc commit e1ef36b
Showing 1 changed file with 122 additions and 13 deletions.
135 changes: 122 additions & 13 deletions dgr/aci-push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ import (
"net/http"
"strings"

"net/url"
"os"
"path"

"bytes"
"io"
"mime/multipart"
"path/filepath"

"github.com/blablacar/dgr/dgr/common"
"github.com/n0rad/go-erlog/data"
"github.com/n0rad/go-erlog/errs"
"github.com/n0rad/go-erlog/logs"
"github.com/rkt/rkt/rkt/config"
Expand Down Expand Up @@ -44,21 +54,120 @@ func (aci *Aci) Push() error {
return aci.upload(common.ExtractNameVersionFromManifest(im))
}

func (aci *Aci) nexusRegistryCheck(urlReq url.URL, params map[string]string) (ok bool, err error) {
logs.WithField("url", urlReq.String()).Debug("Checking remote registry")
client := &http.Client{}
queryCheck := urlReq.Query()
for key, val := range params {
queryCheck.Set(key, val)
}
urlReq.RawQuery = queryCheck.Encode()

/* Authenticate */
req, err := http.NewRequest("HEAD", urlReq.String(), nil)
req.SetBasicAuth(Home.Config.Push.Username, Home.Config.Push.Password)
resp, err := client.Do(req)
if err != nil {
logs.WithE(err).Info("Failed to check version")
return false, err
}
ok = false
switch {
case resp.StatusCode >= 200 && resp.StatusCode <= 300:
logs.WithField("statusCode", resp.StatusCode).
WithField("status", resp.Status).
Info("Aci already uploaded.")
case 404 == resp.StatusCode:
logs.WithField("statusCode", resp.StatusCode).
WithField("status", resp.Status).
Info("Ready to upload")
ok = true
case 401 == resp.StatusCode || 403 == resp.StatusCode:
logs.WithField("statusCode", resp.StatusCode).
WithField("status", resp.Status).
Info("Failed to login to the registry")
case resp.StatusCode >= 500:
logs.WithField("statusCode", resp.StatusCode).
WithField("status", resp.Status).
Info("Registry server error")
}
return
}

func (aci *Aci) nexusRegistryPush(urlReq url.URL, aciPath string, params map[string]string) (err error) {
file, err := os.Open(aciPath)
if err != nil {
return err
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for key, val := range params {
_ = writer.WriteField(key, val)
}
part, err := writer.CreateFormFile("file", filepath.Base(aciPath))
if err != nil {
return err
}
_, err = io.Copy(part, file)

err = writer.Close()
if err != nil {
return err
}

client := &http.Client{}

/* Authenticate */
req, err := http.NewRequest("POST", urlReq.String(), body)
req.SetBasicAuth(Home.Config.Push.Username, Home.Config.Push.Password)
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := client.Do(req)
if err != nil {
return errs.WithE(err, "Failed to execute request")
}
defer resp.Body.Close()
logs.WithField("statusCode", resp.StatusCode).
WithField("status", resp.Status).
WithField("aciPath", aciPath).
Debug("Upload logs")
if resp.StatusCode != 201 {
return errs.WithE(err, resp.Status)
}
return
}

func (aci *Aci) upload(name *common.ACFullname) error {
if Home.Config.Push.Type == "maven" && name.DomainName() == "aci.blbl.cr" { // TODO this definitely need to be removed
logs.WithF(aci.fields).Info("Uploading aci")
if err := common.ExecCmd("curl", "-f", "-i", "-L",
"-F", "r=releases",
"-F", "hasPom=false",
"-F", "e=aci",
"-F", "g=com.blablacar.aci.linux.amd64",
"-F", "p=aci",
"-F", "v="+name.Version(),
"-F", "a="+strings.Split(string(name.Name()), "/")[1],
"-F", "file=@"+aci.target+pathImageGzAci,
"-u", Home.Config.Push.Username+":"+Home.Config.Push.Password,
Home.Config.Push.Url+"/service/local/artifact/maven/content"); err != nil {
return errs.WithEF(err, aci.fields, "Failed to push aci")
urlCheck, err := url.Parse(Home.Config.Push.Url)
if err != nil {
return errs.WithEF(err, data.Fields{
"url": Home.Config.Push.Url,
}, "Failed to parse url in dgr config")
}
formArgs := map[string]string{
"r": "releases",
"hasPom": "false",
"e": "aci",
"p": "aci",
"extension": "aci",
"packaging": "aci",
"g": "com.blablacar.aci.linux.amd64",
"v": name.Version(),
"a": strings.Split(string(name.Name()), "/")[1],
}
urlCheck.Path = urlCheck.Path + "/service/local/artifact/maven/content"
isNotInRegistry, err := aci.nexusRegistryCheck(*urlCheck, formArgs)
if err != nil {
return errs.WithEF(err, aci.fields, "Failed to check aci in remote registry")
}
aciPath := path.Join(aci.target, pathImageGzAci)
if isNotInRegistry {
logs.WithF(aci.fields).Info("Uploading aci")
if err = aci.nexusRegistryPush(*urlCheck, aciPath, formArgs); err != nil {
return errs.WithEF(err, aci.fields, "Failed to push aci")
}
}
} else {
systemConf := Home.Config.Rkt.SystemConfig
Expand Down

0 comments on commit e1ef36b

Please sign in to comment.