Skip to content

Commit

Permalink
Upload and release, to support large files
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Mar 16, 2019
1 parent 7074ef7 commit 7114525
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 13 deletions.
79 changes: 66 additions & 13 deletions pkg/shipclient/release.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package shipclient

import (
"io/ioutil"
"os"
"time"

"github.com/replicatedhq/replicated/pkg/types"
"github.com/replicatedhq/replicated/pkg/util"
)

type GraphQLResponseListReleases struct {
Expand All @@ -22,13 +25,27 @@ type ShipRelease struct {
ReleaseNotes string `json:"releaseNotes"`
}

type GraphQLResponseCreateRelease struct {
Data *ShipReleaseCreateData `json:"data,omitempty"`
Errors []GraphQLError `json:"errors,omitempty"`
type GraphQLResponseFinalizeRelease struct {
Data *ShipFinalizeCreateData `json:"data,omitempty"`
Errors []GraphQLError `json:"errors,omitempty"`
}

type ShipReleaseCreateData struct {
ShipRelease *ShipRelease `json:"createRelease"`
type ShipFinalizeCreateData struct {
ShipRelease *ShipRelease `json:"finalizeUploadedRelease"`
}

type GraphQLResponseUploadRelease struct {
Data ShipReleaseUploadData `json:"data,omitempty"`
Errors []GraphQLError `json:"errors,omitempty"`
}

type ShipReleaseUploadData struct {
ShipPendingReleaseData *ShipPendingReleaseData `json:"uploadRelease"`
}

type ShipPendingReleaseData struct {
UploadURI string `json:"uploadUri"`
UploadID string `json:"id"`
}

type GraphQLResponseLintRelease struct {
Expand Down Expand Up @@ -114,12 +131,44 @@ query allReleases($appId: ID!) {
}

func (c *GraphQLClient) CreateRelease(appID string, yaml string) (*types.ReleaseInfo, error) {
response := GraphQLResponseCreateRelease{}
response := GraphQLResponseUploadRelease{}

request := GraphQLRequest{
Query: `
mutation createRelease($appId: ID!, $spec: String!) {
createRelease(appId: $appId, spec: $spec) {
mutation uploadRelease($appId: ID!) {
uploadRelease(appId: $appId) {
id
uploadUri
}
}`,
Variables: map[string]interface{}{
"appId": appID,
},
}

if err := c.executeRequest(request, &response); err != nil {
return nil, err
}

tmpFile, err := ioutil.TempFile(os.TempDir(), "replicated-")
if err != nil {
return nil, err
}
defer os.Remove(tmpFile.Name())

if _, err := tmpFile.Write([]byte(yaml)); err != nil {
return nil, err
}
tmpFile.Close()

if err := util.UploadFile(tmpFile.Name(), response.Data.ShipPendingReleaseData.UploadURI); err != nil {
return nil, err
}

request = GraphQLRequest{
Query: `
mutation finalizeUploadedRelease($appId: ID! $uploadId: String) {
finalizeUploadedRelease(appId: $appId, uploadId: $uploadId) {
id
sequence
spec
Expand All @@ -128,12 +177,15 @@ mutation createRelease($appId: ID!, $spec: String!) {
}
}`,
Variables: map[string]interface{}{
"appId": appID,
"spec": yaml,
"appId": appID,
"uploadId": response.Data.ShipPendingReleaseData.UploadID,
},
}

if err := c.executeRequest(request, &response); err != nil {
// call finalize release
finalizeResponse := GraphQLResponseFinalizeRelease{}

if err := c.executeRequest(request, &finalizeResponse); err != nil {
return nil, err
}

Expand All @@ -142,16 +194,17 @@ mutation createRelease($appId: ID!, $spec: String!) {
return nil, err
}

createdAt, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700 (MST)", response.Data.ShipRelease.CreatedAt)
createdAt, err := time.Parse("Mon Jan 02 2006 15:04:05 MST-0700 (MST)", finalizeResponse.Data.ShipRelease.CreatedAt)
if err != nil {
return nil, err
}

releaseInfo := types.ReleaseInfo{
AppID: appID,
CreatedAt: createdAt.In(location),
EditedAt: time.Now(),
Editable: false,
Sequence: response.Data.ShipRelease.Sequence,
Sequence: finalizeResponse.Data.ShipRelease.Sequence,
Version: "ba",
}

Expand Down
38 changes: 38 additions & 0 deletions pkg/util/upload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package util

import (
"net/http"
"os"
)

func UploadFile(fullpath string, url string) error {
file, err := os.Open(fullpath)
if err != nil {
return err
}
defer file.Close()

req, err := http.NewRequest("PUT", url, file)
if err != nil {
return err
}

fi, err := os.Stat(fullpath)
if err != nil {
return err
}
req.ContentLength = fi.Size()

req.Header.Set("Content-Type", "application/x-yaml")

res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}

if res.StatusCode != http.StatusOK {
return err
}

return nil
}

0 comments on commit 7114525

Please sign in to comment.