Browse Source

schema and CLI updates

Ivan Galakhov 4 năm trước cách đây
mục cha
commit
c9d47310e0

+ 28 - 3
cli/cmd/api/api.go

@@ -6,7 +6,6 @@ import (
 	"encoding/base64"
 	"encoding/json"
 	"fmt"
-	"github.com/porter-dev/porter/cli/cmd/deploy"
 	"io/ioutil"
 	"net/http"
 	"path/filepath"
@@ -31,6 +30,29 @@ type HTTPError struct {
 	Errors []string `json:"errors"`
 }
 
+type EventStatus int64
+
+const (
+	EventStatusSuccess    EventStatus = 1
+	EventStatusInProgress             = 2
+	EventStatusFailed                 = 3
+)
+
+// Event represents an event that happens during
+type Event struct {
+	ID     string      `json:"event_id"` // events with the same id wil be treated the same, and the highest index one is retained
+	Name   string      `json:"name"`
+	Index  int64       `json:"index"` // priority of the event, used for sorting
+	Status EventStatus `json:"status"`
+	Info   string      `json:"info"` // extra information (can be error or success)
+}
+
+// StreamEventForm is used to send event data to the api
+type StreamEventForm struct {
+	Event `json:"event"`
+	Token string `json:"token"`
+}
+
 // NewClient constructs a new client based on a set of options
 func NewClient(baseURL string, cookieFileName string) *Client {
 	home := homedir.HomeDir()
@@ -94,6 +116,9 @@ func (c *Client) sendRequest(req *http.Request, v interface{}, useCookie bool) (
 			return &errRes, nil
 		}
 
+		fmt.Println("error request")
+		fmt.Printf("%+v\n", req)
+
 		return nil, fmt.Errorf("unknown error, status code: %d", res.StatusCode)
 	}
 
@@ -125,8 +150,8 @@ func (c *Client) saveCookie(cookie *http.Cookie) error {
 }
 
 // StreamEvent sends an event from deployment to the api
-func (c *Client) StreamEvent(event deploy.Event, token string, projID uint, name string) error {
-	form := deploy.StreamEventForm{
+func (c *Client) StreamEvent(event Event, token string, projID uint, name string) error {
+	form := StreamEventForm{
 		Event: event,
 		Token: token,
 	}

+ 12 - 9
cli/cmd/deploy.go

@@ -403,18 +403,21 @@ func updateBuildWithAgent(updateAgent *deploy.DeployAgent, client *api.Client) e
 
 	// minor thought: this ends up happening four times when upgrade is ran, when it should really only happen once
 	// maybe some way to only do this once?
-	release, err := client.GetReleaseWebhook(context.Background(), config.Project, config.Cluster, name, namespace)
+	fmt.Println(app)
+	fmt.Println(namespace)
+
+	release, err := client.GetReleaseWebhook(context.Background(), config.Project, config.Cluster, app, namespace)
 
 	if err != nil {
 		return err
 	}
 
 	if stream {
-		updateAgent.StreamEvent(deploy.Event{
+		updateAgent.StreamEvent(api.Event{
 			ID:     "build",
 			Name:   "Build",
 			Index:  100,
-			Status: deploy.EventStatusInProgress,
+			Status: api.EventStatusInProgress,
 			Info:   "",
 		}, release.WebhookToken)
 	}
@@ -424,11 +427,11 @@ func updateBuildWithAgent(updateAgent *deploy.DeployAgent, client *api.Client) e
 	if err != nil {
 		if stream {
 			// another concern: is it safe to ignore the error here?
-			updateAgent.StreamEvent(deploy.Event{
+			updateAgent.StreamEvent(api.Event{
 				ID:     "build",
 				Name:   "Build",
 				Index:  110,
-				Status: deploy.EventStatusInProgress,
+				Status: api.EventStatusInProgress,
 				Info:   err.Error(),
 			}, release.WebhookToken)
 		}
@@ -440,11 +443,11 @@ func updateBuildWithAgent(updateAgent *deploy.DeployAgent, client *api.Client) e
 
 	if err != nil {
 		if stream {
-			updateAgent.StreamEvent(deploy.Event{
+			updateAgent.StreamEvent(api.Event{
 				ID:     "build",
 				Name:   "Build",
 				Index:  120,
-				Status: deploy.EventStatusInProgress,
+				Status: api.EventStatusInProgress,
 				Info:   err.Error(),
 			}, release.WebhookToken)
 		}
@@ -452,11 +455,11 @@ func updateBuildWithAgent(updateAgent *deploy.DeployAgent, client *api.Client) e
 	}
 
 	if stream {
-		updateAgent.StreamEvent(deploy.Event{
+		updateAgent.StreamEvent(api.Event{
 			ID:     "build",
 			Name:   "Build",
 			Index:  130,
-			Status: deploy.EventStatusSuccess,
+			Status: api.EventStatusSuccess,
 			Info:   "",
 		}, release.WebhookToken)
 	}

+ 3 - 25
cli/cmd/deploy/deploy.go

@@ -51,29 +51,6 @@ type DeployOpts struct {
 	Local bool
 }
 
-type EventStatus int64
-
-const (
-	EventStatusSuccess    EventStatus = 1
-	EventStatusInProgress             = 2
-	EventStatusFailed                 = 3
-)
-
-// Event represents an event that happens during
-type Event struct {
-	ID     string      `json:"event_id"` // events with the same id wil be treated the same, and the highest index one is retained
-	Name   string      `json:"name"`
-	Index  int64       `json:"index"` // priority of the event, used for sorting
-	Status EventStatus `json:"status"`
-	Info   string      `json:"info"` // extra information (can be error or success)
-}
-
-// StreamEventForm is used to send event data to the api
-type StreamEventForm struct {
-	Event `json:"event"`
-	Token string `json:"token"`
-}
-
 // NewDeployAgent creates a new DeployAgent given a Porter API client, application
 // name, and DeployOpts.
 func NewDeployAgent(client *api.Client, app string, opts *DeployOpts) (*DeployAgent, error) {
@@ -462,8 +439,9 @@ func (d *DeployAgent) downloadRepoToDir(downloadURL string) (string, error) {
 	return res, nil
 }
 
-func (d *DeployAgent) StreamEvent(event Event, token string) error {
-	return d.client.StreamEvent(event, token, d.opts.ProjectID, d.release.Name)
+func (d *DeployAgent) StreamEvent(event api.Event, token string) error {
+	//return d.client.StreamEvent(event, token, d.opts.ProjectID, d.release.Name)
+	return nil
 }
 
 type NestedMapFieldNotFoundError struct {

+ 1 - 0
cli/cmd/docker/agent.go

@@ -253,6 +253,7 @@ func (a *Agent) getPushOptions(image string) (types.ImagePushOptions, error) {
 }
 
 func GetServerURLFromTag(image string) (string, error) {
+	image = strings.Replace(image, "index.docker.io", "docker.io", -1)
 	named, err := reference.ParseNamed(image)
 
 	if err != nil {

+ 4 - 0
internal/repository/event.go

@@ -1,4 +1,8 @@
 package repository
 
+import "github.com/porter-dev/porter/internal/models"
+
 type EventRepository interface {
+	CreateEventContainer(am *models.EventContainer) (*models.EventContainer, error)
+	CreateSubEvent(am *models.SubEvent) (*models.SubEvent, error)
 }

+ 16 - 0
internal/repository/gorm/event.go

@@ -1,10 +1,12 @@
 package gorm
 
 import (
+	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/repository"
 	"gorm.io/gorm"
 )
 
+// EventRepository holds both EventContainer and SubEvent models
 type EventRepository struct {
 	db *gorm.DB
 }
@@ -14,3 +16,17 @@ type EventRepository struct {
 func NewEventRepository(db *gorm.DB) repository.EventRepository {
 	return &EventRepository{db}
 }
+
+func (repo EventRepository) CreateEventContainer(am *models.EventContainer) (*models.EventContainer, error) {
+	if err := repo.db.Create(am).Error; err != nil {
+		return nil, err
+	}
+	return am, nil
+}
+
+func (repo EventRepository) CreateSubEvent(am *models.SubEvent) (*models.SubEvent, error) {
+	if err := repo.db.Create(am).Error; err != nil {
+		return nil, err
+	}
+	return am, nil
+}

+ 4 - 0
server/api/release_handler.go

@@ -1654,6 +1654,10 @@ func (app *App) HandleUpdateReleaseSteps(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
+	if release.EventContainer == 0 {
+		// create new event container
+	}
+
 	fmt.Printf("%#v", release.EventContainer)
 }