8000 feat: Add template pull cmd by sreya · Pull Request #2329 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: Add template pull cmd #2329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
pr comments
  • Loading branch information
sreya committed Jun 15, 2022
commit dbca68245e641daee19ee6f407015c455230955b
25 changes: 0 additions & 25 deletions cli/clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ import (
"path/filepath"
"testing"

"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli"
"github.com/coder/coder/cli/config"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
)

// New creates a CLI instance with a configuration pointed to a
Expand Down Expand Up @@ -48,29 +46,6 @@ func CreateTemplateVersionSource(t *testing.T, responses *echo.Responses) string
return directory
}

// GenTemplateVersion returns a unique bundle that can be used to create
// a template version source.
func GenTemplateVersion() *echo.Responses {
return &echo.Responses{
Parse: []*proto.Parse_Response{
{
Type: &proto.Parse_Response_Log{
Log: &proto.Log{
Output: uuid.NewString(),
},
},
},

{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{},
},
},
},
Provision: echo.ProvisionComplete,
}
}

func extractTar(t *testing.T, data []byte, directory string) {
reader := tar.NewReader(bytes.NewBuffer(data))
for {
Expand Down
5 changes: 3 additions & 2 deletions cli/templatepull.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func templatePull() *cobra.Command {
return xerrors.Errorf("template by name: %w", err)
}

// Pull the versions for the template. We'll find the latest
// one and download the source.
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
Expand All @@ -61,10 +63,9 @@ func templatePull() *cobra.Command {
return versions[i].CreatedAt.After(versions[j].CreatedAt)
})

// TemplateVersionsByTemplate returns the versions in order from newest
// to oldest.
latest := versions[0]

// Download the tar archive.
raw, ctype, err := client.Download(ctx, latest.Job.SourceHash)
if err != nil {
return xerrors.Errorf("download template: %w", err)
Expand Down
33 changes: 29 additions & 4 deletions cli/templatepull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"path/filepath"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/coder/coder/pty/ptytest"
)

Expand All @@ -26,10 +28,10 @@ func TestTemplatePull(t *testing.T) {
user := coderdtest.CreateFirstUser(t, client)

// Create an initial template bundle.
source1 := clitest.GenTemplateVersion()
source1 := genTemplateVersionSource()
// Create an updated template bundle. This will be used to ensure
// that templates are correctly returned in order from latest to oldest.
source2 := clitest.GenTemplateVersion()
source2 := genTemplateVersionSource()

expected, err := echo.Tar(source2)
require.NoError(t, err)
Expand Down Expand Up @@ -64,10 +66,10 @@ func TestTemplatePull(t *testing.T) {
user := coderdtest.CreateFirstUser(t, client)

// Create an initial template bundle.
source1 := clitest.GenTemplateVersion()
source1 := genTemplateVersionSource()
// Create an updated template bundle. This will be used to ensure
// that templates are correctly returned in order from latest to oldest.
source2 := clitest.GenTemplateVersion()
source2 := genTemplateVersionSource()

expected, err := echo.Tar(source2)
require.NoError(t, err)
Expand Down Expand Up @@ -116,3 +118,26 @@ func TestTemplatePull(t *testing.T) {
require.True(t, bytes.Equal(actual, expected), "tar files differ")
})
}

// genTemplateVersionSource returns a unique bundle that can be used to create
// a template version source.
func genTemplateVersionSource() *echo.Responses {
return &echo.Responses{
Parse: []*proto.Parse_Response{
{
Type: &proto.Parse_Response_Log{
Log: &proto.Log{
Output: uuid.NewString(),
},
},
},

{
Type: &proto.Parse_Response_Complete{
Complete: &proto.Parse_Complete{},
},
},
},
Provision: echo.ProvisionComplete,
}
}
16 changes: 8 additions & 8 deletions codersdk/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ const (
)

type ProvisionerJob struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Error string `json:"error,omitempty"`
Status ProvisionerJobStatus `json:"status"`
WorkerID *uuid.UUID `json:"worker_id,omitempty"`
SourceHash string `json:"source_hash"`
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
Error string `json:"error,omitempty"`
Status ProvisionerJobStatus `json:"status"`
WorkerID *uuid.UUID `json:"worker_id,omitempty"`
StorageSource string `json:"storage_source"`
}

type ProvisionerJobLog struct {
Expand Down
2 changes: 1 addition & 1 deletion site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export interface ProvisionerJob {
readonly error?: string
readonly status: ProvisionerJobStatus
readonly worker_id?: string
readonly source_hash: string
readonly storage_source: string
}

// From codersdk/provisionerdaemons.go:73:6
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const MockProvisionerJob: TypesGen.ProvisionerJob = {
created_at: "",
id: "test-provisioner-job",
status: "succeeded",
source_hash: "asdf",
storage_source: "asdf",
}

export const MockFailedProvisionerJob: TypesGen.ProvisionerJob = {
Expand Down
0