mirror of
https://github.com/goharbor/harbor.git
synced 2024-08-20 11:02:58 +02:00
sync image state handler
This commit is contained in:
parent
7e9c1d73ba
commit
f7b8546c25
|
@ -16,6 +16,11 @@
|
||||||
package imgout
|
package imgout
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/distribution"
|
"github.com/docker/distribution"
|
||||||
|
@ -121,7 +126,7 @@ type Checker struct {
|
||||||
// check existence of project, if it does not exist, create it,
|
// check existence of project, if it does not exist, create it,
|
||||||
// if it exists, check whether the user has write privilege to it.
|
// if it exists, check whether the user has write privilege to it.
|
||||||
func (c *Checker) Enter() (string, error) {
|
func (c *Checker) Enter() (string, error) {
|
||||||
exist, err := c.projectExist()
|
exist, canWrite, err := c.projectExist()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("an error occurred while checking existence of project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
c.logger.Errorf("an error occurred while checking existence of project %s on %s with user %s : %v", c.project, c.dstURL, c.dstUsr, err)
|
||||||
return "", err
|
return "", err
|
||||||
|
@ -136,14 +141,10 @@ func (c *Checker) Enter() (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Infof("project %s already exists on %s", c.project, c.dstURL)
|
c.logger.Infof("project %s already exists on %s", c.project, c.dstURL)
|
||||||
canWrite, err := c.canWrite()
|
|
||||||
if err != nil {
|
|
||||||
c.logger.Errorf("an error occurred while checking %s 's privileges to project %s on %s : %v", c.dstUsr, c.project, c.dstURL, err)
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !canWrite {
|
if !canWrite {
|
||||||
c.logger.Errorf("the user %s has no write privilege to project %s on %s", c.dstUsr, c.project, c.dstURL)
|
err = fmt.Errorf("the user %s has no write privilege to project %s on %s", c.dstUsr, c.project, c.dstURL)
|
||||||
|
c.logger.Errorf("%v", err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
c.logger.Infof("the user %s has write privilege to project %s on %s", c.dstUsr, c.project, c.dstURL)
|
c.logger.Infof("the user %s has write privilege to project %s on %s", c.dstUsr, c.project, c.dstURL)
|
||||||
|
@ -151,18 +152,96 @@ func (c *Checker) Enter() (string, error) {
|
||||||
return StatePullManifest, nil
|
return StatePullManifest, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) projectExist() (bool, error) {
|
// check the existence of project, if it exists, returning whether the user has write privilege to it
|
||||||
exist := true
|
func (c *Checker) projectExist() (exist, canWrite bool, err error) {
|
||||||
return exist, nil
|
url := strings.TrimRight(c.dstURL, "/") + "/api/projects/?project_name=" + c.project
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req.SetBasicAuth(c.dstUsr, c.dstPwd)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusUnauthorized {
|
||||||
|
exist = true
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusOK {
|
||||||
|
projects := make([]models.Project, 1)
|
||||||
|
if err = json.Unmarshal(data, &projects); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(projects) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
exist = true
|
||||||
|
// TODO handle canWrite when new API is ready
|
||||||
|
canWrite = true
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = fmt.Errorf("an error occurred while checking existen of project %s on %s with user %s: %d %s",
|
||||||
|
c.project, c.dstURL, c.dstUsr, resp.StatusCode, string(data))
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) createProject() error {
|
func (c *Checker) createProject() error {
|
||||||
return nil
|
// TODO handle publicity of project
|
||||||
}
|
project := struct {
|
||||||
|
ProjectName string `json:"project_name"`
|
||||||
|
Public bool `json:"public"`
|
||||||
|
}{
|
||||||
|
ProjectName: c.project,
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Checker) canWrite() (bool, error) {
|
data, err := json.Marshal(project)
|
||||||
canWrite := true
|
if err != nil {
|
||||||
return canWrite, nil
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
url := strings.TrimRight(c.dstURL, "/") + "/api/projects/"
|
||||||
|
req, err := http.NewRequest("POST", url, bytes.NewReader(data))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
req.SetBasicAuth(c.dstUsr, c.dstPwd)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusCreated {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
message, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("an error occurred while reading message from response: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("failed to create project %s on %s with user %s: %d %s",
|
||||||
|
c.project, c.dstURL, c.dstUsr, resp.StatusCode, string(message))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type ManifestPuller struct {
|
type ManifestPuller struct {
|
||||||
|
@ -274,6 +353,7 @@ func (m *ManifestPusher) Enter() (string, error) {
|
||||||
m.logger.Errorf("an error occurred while getting payload of manifest for %s:%s : %v", name, tag, err)
|
m.logger.Errorf("an error occurred while getting payload of manifest for %s:%s : %v", name, tag, err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = m.dstClient.PushManifest(tag, mediaType, data); err != nil {
|
if _, err = m.dstClient.PushManifest(tag, mediaType, data); err != nil {
|
||||||
m.logger.Errorf("an error occurred while pushing manifest of %s:%s to %s : %v", name, tag, m.dstURL, err)
|
m.logger.Errorf("an error occurred while pushing manifest of %s:%s to %s : %v", name, tag, m.dstURL, err)
|
||||||
return "", err
|
return "", err
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user