Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 0 additions & 17 deletions .devcontainer/Dockerfile

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/local-prov.yaml

This file was deleted.

55 changes: 11 additions & 44 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cmd

import (
"fmt"
"os"
"xo/src/bundle"
"xo/src/massdriver"
"xo/src/telemetry"

"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
Expand All @@ -22,12 +20,6 @@ var bundleCmd = &cobra.Command{
Long: ``,
}

var bundlePullv0Cmd = &cobra.Command{
Use: "pullv0",
Short: "Pulls a bundle from Massdriver",
RunE: runBundlePullv0,
}

var bundlePullCmd = &cobra.Command{
Use: "pull",
Short: "Pulls a bundle from Massdriver",
Expand All @@ -37,51 +29,26 @@ var bundlePullCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(bundleCmd)

bundleCmd.AddCommand(bundlePullv0Cmd)

bundleCmd.AddCommand(bundlePullCmd)
bundlePullCmd.Flags().StringP("tag", "t", "latest", "Bundle tag (defaults to 'latest')")
bundlePullCmd.Flags().StringP("version", "v", "", "Bundle version")
bundlePullCmd.Flags().StringP("name", "n", "", "Bundle name")
viper.BindPFlag("bundle.tag", bundlePullCmd.Flags().Lookup("tag"))
viper.BindPFlag("bundle.version", bundlePullCmd.Flags().Lookup("version"))
viper.BindPFlag("bundle.name", bundlePullCmd.Flags().Lookup("name"))
}

func runBundlePullv0(cmd *cobra.Command, args []string) error {
ctx, span := otel.Tracer("xo").Start(telemetry.GetContextWithTraceParentFromEnv(), "runBundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()

client, initErr := massdriver.InitializeMassdriverClient()
if initErr != nil {
return telemetry.LogError(span, initErr, "an error occurred while initializing massdriver client")
}

outFile, openErr := os.OpenFile("bundle.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
if openErr != nil {
return telemetry.LogError(span, openErr, "unable to open bundle.tar.gz file")
}
defer outFile.Close()

log.Info().Msg("pulling bundle...")
pullErr := bundle.PullV0(ctx, client, outFile)
if pullErr != nil {
return telemetry.LogError(span, pullErr, "an error occurred while pulling bundle")
}
log.Info().Msg("bundle pulled")

return nil
}

func runBundlePull(cmd *cobra.Command, args []string) error {
ctx, span := otel.Tracer("xo").Start(telemetry.GetContextWithTraceParentFromEnv(), "runBundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()

bundleName := viper.GetString("bundle.name")
if bundleName == "" {
return fmt.Errorf("required flag bundleName must be set via flag or environment variable")
return telemetry.LogError(span, fmt.Errorf("required flag bundleName must be set via flag or environment variable"), "an error occurred while pulling bundle")
}
bundleVersion := viper.GetString("bundle.version")
if bundleVersion == "" {
return telemetry.LogError(span, fmt.Errorf("required flag bundleVersion must be set via flag or environment variable"), "an error occurred while pulling bundle")
}
tag := viper.GetString("bundle.tag")

mdClient, clientErr := client.New()
if clientErr != nil {
Expand All @@ -90,17 +57,17 @@ func runBundlePull(cmd *cobra.Command, args []string) error {

repo, repoErr := sdkbundle.GetBundleRepository(mdClient, bundleName)
if repoErr != nil {
return repoErr
return telemetry.LogError(span, repoErr, "an error occurred while getting bundle repository")
}

fileStore, fileErr := file.New("bundle")
if fileErr != nil {
return fileErr
return telemetry.LogError(span, fileErr, "an error occurred while creating file store")
}
defer fileStore.Close()

log.Info().Msg("pulling bundle...")
desc, pullErr := bundle.PullV1(ctx, repo, fileStore, tag)
log.Info().Msgf("pulling bundle %s:%s...", bundleName, bundleVersion)
desc, pullErr := bundle.Pull(ctx, repo, fileStore, bundleVersion)
if pullErr != nil {
return telemetry.LogError(span, pullErr, "an error occurred while pulling bundle")
}
Expand Down
10 changes: 9 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,13 @@ func initLogging() {
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
log.Logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stdout,
FormatTimestamp: func(i interface{}) string {
return ""
},
FormatLevel: func(i interface{}) string {
return ""
},
})
}
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/honeycombio/honeycomb-opentelemetry-go v0.11.0
github.com/honeycombio/otel-config-go v1.17.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/massdriver-cloud/mass v0.0.0-20240819225958-4ef28e971211
github.com/massdriver-cloud/massdriver-sdk-go v0.0.8
github.com/massdriver-cloud/terraform-config-inspect v0.0.0-20240906041648-e5461c213cea
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMD
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/massdriver-cloud/mass v0.0.0-20240819225958-4ef28e971211 h1:AipPe343bMha9B5x3xlMUCiKZkyyIRD6D4FXBvbbTbg=
github.com/massdriver-cloud/mass v0.0.0-20240819225958-4ef28e971211/go.mod h1:VJMa3gsc/wmGE61ZicpBQv1xv8JjIPzHpJK2gZYEgqI=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.8 h1:YMrUjVmJrl8dy5jpV3r/cm2MVSPCi9vpescKsHNrcxc=
github.com/massdriver-cloud/massdriver-sdk-go v0.0.8/go.mod h1:6bXMPQmfeKHD3Tm/cwnrkqiZaDEgRUUq0+uZckKRsbM=
github.com/massdriver-cloud/terraform-config-inspect v0.0.0-20240906041648-e5461c213cea h1:+fS2DYjasqMDMzVeKJTiBLbqYJjv93MTf1yZV2nxjf8=
Expand Down
37 changes: 1 addition & 36 deletions src/bundle/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,18 @@ package bundle

import (
"context"
"encoding/base64"
"fmt"
"io"
"xo/src/api"
"xo/src/massdriver"
"xo/src/telemetry"

v1 "github.com/opencontainers/image-spec/specs-go/v1"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"

oras "oras.land/oras-go/v2"
)

func PullV0(ctx context.Context, client *massdriver.MassdriverClient, outFile io.Writer) error {
func Pull(ctx context.Context, repo oras.Target, target oras.Target, tag string) (v1.Descriptor, error) {
_, span := otel.Tracer("xo").Start(ctx, "BundlePull")
telemetry.SetSpanAttributes(span)
defer span.End()

bundleBytes, getErr := api.GetBundleSourceCode(client.GQLCLient, client.Specification.BundleID, client.Specification.OrganizationUUID)
if getErr != nil {
span.RecordError(getErr)
span.SetStatus(codes.Error, getErr.Error())
return fmt.Errorf("an error occurred while getting bundle source code: %w", getErr)
}

decodedBundleBytes, decodeErr := base64.StdEncoding.DecodeString(string(bundleBytes))
if decodeErr != nil {
span.RecordError(decodeErr)
span.SetStatus(codes.Error, decodeErr.Error())
return fmt.Errorf("an error occurred while decoding bundle source code: %w", decodeErr)
}

_, writeErr := outFile.Write(decodedBundleBytes)
if writeErr != nil {
span.RecordError(writeErr)
span.SetStatus(codes.Error, writeErr.Error())
return fmt.Errorf("an error occurred while writing bundle source code: %w", writeErr)
}

return nil
}

func PullV1(ctx context.Context, repo oras.Target, target oras.Target, tag string) (v1.Descriptor, error) {
_, span := otel.Tracer("xo").Start(ctx, "BundlePullV1")
telemetry.SetSpanAttributes(span)
defer span.End()

return oras.Copy(ctx, repo, tag, target, tag, oras.DefaultCopyOptions)
}
56 changes: 3 additions & 53 deletions src/bundle/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,17 @@ package bundle_test
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"testing"
"xo/src/bundle"
"xo/src/massdriver"

"github.com/massdriver-cloud/mass/pkg/gqlmock"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
oras "oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/content/memory"
)

func TestPullV0(t *testing.T) {
type testData struct {
name string
data []byte
}
tests := []testData{
{
name: "basic",
data: []byte(`data`),
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gqlClient := gqlmock.NewClientWithSingleJSONResponse(map[string]interface{}{
"data": map[string]interface{}{
"bundleSourceCode": map[string]interface{}{
"source": base64.StdEncoding.EncodeToString(tc.data),
},
},
})

client := massdriver.MassdriverClient{
GQLCLient: gqlClient,
Specification: &massdriver.Specification{
BundleID: "bundleuuid1",
OrganizationUUID: "orguuid1",
},
}

buf := new(bytes.Buffer)

pullErr := bundle.PullV0(context.Background(), &client, buf)
if pullErr != nil {
t.Errorf("pull failed: %v", pullErr)
}

got := buf.String()
want := string(tc.data)
if got != want {
t.Errorf("got %v, want %v", got, want)
}
})
}

}

func TestPullV1(t *testing.T) {
func TestPull(t *testing.T) {
ctx := context.Background()

// Pre-populate source store with fake files
Expand Down Expand Up @@ -130,9 +80,9 @@ func TestPullV1(t *testing.T) {

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
desc, pullErr := bundle.PullV1(ctx, tc.sourceRepo, tc.target, tc.tag)
desc, pullErr := bundle.Pull(ctx, tc.sourceRepo, tc.target, tc.tag)
if (pullErr != nil) != tc.wantErr {
t.Fatalf("PullV1WithRepo() error = %v, wantErr %v", pullErr, tc.wantErr)
t.Fatalf("Pull error = %v, wantErr %v", pullErr, tc.wantErr)
}
if tc.wantErr {
return
Expand Down
20 changes: 7 additions & 13 deletions src/massdriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ type MassdriverClient struct {
}

type Specification struct {
Action string `envconfig:"ACTION"`
BundleID string `envconfig:"BUNDLE_ID" required:"true"`
BundleName string `envconfig:"BUNDLE_NAME"`
BundleType string `envconfig:"BUNDLE_TYPE"`
DeploymentID string `envconfig:"DEPLOYMENT_ID" required:"true"`
ManifestID string `envconfig:"MANIFEST_ID"`
OrganizationUUID string `envconfig:"ORGANIZATION_UUID" required:"true"`
OrganizationID string `envconfig:"ORGANIZATION_ID" required:"true"`
PackageID string `envconfig:"PACKAGE_ID" required:"true"`
PackageName string `envconfig:"PACKAGE_NAME" required:"true"`
TargetMode string `envconfig:"TARGET_MODE"`
Token string `envconfig:"TOKEN" required:"true"`
URL string `envconfig:"URL"`
Action string `envconfig:"ACTION"`
BundleName string `envconfig:"BUNDLE_NAME"`
DeploymentID string `envconfig:"DEPLOYMENT_ID" required:"true"`
OrganizationID string `envconfig:"ORGANIZATION_ID" required:"true"`
PackageName string `envconfig:"PACKAGE_NAME" required:"true"`
Token string `envconfig:"TOKEN" required:"true"`
URL string `envconfig:"URL"`
}

func InitializeMassdriverClient() (*MassdriverClient, error) {
Expand Down