Skip to content

Commit

Permalink
impl upload files
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Feb 17, 2024
1 parent 03a7535 commit aef291e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 78 deletions.
33 changes: 0 additions & 33 deletions core/server/api_container/server/startosis_engine/plan.yml

This file was deleted.

10 changes: 6 additions & 4 deletions core/server/api_container/server/startosis_engine/plan_yaml.go
Expand Up @@ -25,14 +25,16 @@ type Service struct {
Entrypoint []string `yaml:"entrypoint,omitempty"` // done
EnvVars []*EnvironmentVariable `yaml:"envVars,omitempty"` // done
Ports []*Port `yaml:"ports,omitempty"` // done
Files []*FileMount `yaml:"files,omitempty"`
Files []*FileMount `yaml:"files,omitempty"` // done

// TODO: support remaining fields in the ServiceConfig
}

// FilesArtifact represents a collection of files.
type FilesArtifact struct {
Uuid string `yaml:"uuid,omitempty"`
Name string `yaml:"name,omitempty"`
Files map[string]string `yaml:"files,omitempty"`
Uuid string `yaml:"uuid,omitempty"`
Name string `yaml:"name,omitempty"`
Files []string `yaml:"files,omitempty"`
}

// EnvironmentVariable represents an environment variable.
Expand Down
Expand Up @@ -8,7 +8,9 @@ import (
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/remove_service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/render_templates"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/tasks"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/upload_files"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/builtin_argument"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_types/service_config"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_packages"
Expand Down Expand Up @@ -130,6 +132,8 @@ func (pyg *PlanYamlGeneratorImpl) GenerateYaml() ([]byte, error) {
pyg.updatePlanYamlFromRunPython(scheduledInstruction)

Check failure on line 132 in core/server/api_container/server/startosis_engine/plan_yaml_generator.go

View workflow job for this annotation

GitHub Actions / golang-lint

Error return value of `pyg.updatePlanYamlFromRunPython` is not checked (errcheck)
case render_templates.RenderTemplatesBuiltinName:
err = pyg.updatePlanYamlFromRenderTemplates(scheduledInstruction)
case upload_files.UploadFilesBuiltinName:
err = pyg.updatePlanYamlFromUploadFiles(scheduledInstruction)
default:
return nil, nil
}
Expand Down Expand Up @@ -256,35 +260,59 @@ func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromRunPython(addServiceInstruct
// TODO: update the plan yaml based on an add_service
}

func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromUploadFiles(addServiceInstruction *instructions_plan.ScheduledInstruction) error {
panic("remove service not implemented yet")
func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromUploadFiles(uploadFilesInstruction *instructions_plan.ScheduledInstruction) error {
var filesArtifact *FilesArtifact

// get the name of returned files artifact
filesArtifactName, castErr := kurtosis_types.SafeCastToString(uploadFilesInstruction.GetReturnedValue(), "files artifact name")
if castErr != nil {
return castErr
}
filesArtifact = &FilesArtifact{

Check failure on line 271 in core/server/api_container/server/startosis_engine/plan_yaml_generator.go

View workflow job for this annotation

GitHub Actions / golang-lint

Files is missing in FilesArtifact (exhaustruct)
Uuid: string(uploadFilesInstruction.GetUuid()), // give the FilesArtifact the uuid of the originating instruction
Name: filesArtifactName,
}

// get files of returned files artifact off render templates config
arguments := uploadFilesInstruction.GetInstruction().GetArguments()
src, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, upload_files.SrcArgName)
if err != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unable to extract value for '%s' argument", upload_files.SrcArgName)
}
filesArtifact.Files = []string{src.GoString()}

// add the files artifact to the yaml and index
pyg.planYaml.FilesArtifacts = append(pyg.planYaml.FilesArtifacts, filesArtifact)
pyg.filesArtifactIndex[filesArtifactName] = filesArtifact
return nil
// TODO: update the plan yaml based on an add_service
}

func (pyg *PlanYamlGeneratorImpl) updatePlanYamlFromRenderTemplates(renderTemplatesInstruction *instructions_plan.ScheduledInstruction) error {
arguments := renderTemplatesInstruction.GetInstruction().GetArguments()
var filesArtifact *FilesArtifact

// get the name of returned files artifact
artifactName, err := builtin_argument.ExtractArgumentValue[starlark.String](arguments, render_templates.ArtifactNameArgName)
if err != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unable to parse '%s'", render_templates.ArtifactNameArgName)
filesArtifactName, castErr := kurtosis_types.SafeCastToString(renderTemplatesInstruction.GetReturnedValue(), "files artifact name")
if castErr != nil {
return castErr
}
filesArtifactName := artifactName.GoString()
filesArtifact = &FilesArtifact{

Check failure on line 298 in core/server/api_container/server/startosis_engine/plan_yaml_generator.go

View workflow job for this annotation

GitHub Actions / golang-lint

Files is missing in FilesArtifact (exhaustruct)
Uuid: string(renderTemplatesInstruction.GetUuid()), // give the FilesArtifact the uuid of the originating instruction
Name: filesArtifactName,
}

// get files of returned files artifact off render templates config
arguments := renderTemplatesInstruction.GetInstruction().GetArguments()
renderTemplateConfig, err := builtin_argument.ExtractArgumentValue[*starlark.Dict](arguments, render_templates.TemplateAndDataByDestinationRelFilepathArg)
if err != nil {
return startosis_errors.WrapWithInterpretationError(err, "Unable to parse '%s'", render_templates.TemplateAndDataByDestinationRelFilepathArg)
}
files := map[string]string{}
for _, filepath := range renderTemplateConfig.AttrNames() {
files[filepath] = "" // TODO: are files just the file names/the paths at those files? is it possible to get any other information about them
files := []string{}
for _, filepath := range renderTemplateConfig.Keys() {
filepathStr, castErr := kurtosis_types.SafeCastToString(filepath, "filepath")
if castErr != nil {
return castErr
}
files = append(files, filepathStr)
}
filesArtifact.Files = files

Expand Down
Expand Up @@ -60,27 +60,28 @@ func (suite *PlanYamlGeneratorTestSuite) SetupTest() {
suite.serviceNetwork.EXPECT().GetApiContainerInfo().Return(apiContainerInfo)
}

func TestRunPlanYamlGeneratorTestSuite(t *testing.T) {
suite.Run(t, new(PlanYamlGeneratorTestSuite))
}
//func TestRunPlanYamlGeneratorTestSuite(t *testing.T) {
// suite.Run(t, new(PlanYamlGeneratorTestSuite))
//}

func (suite *PlanYamlGeneratorTestSuite) TearDownTest() {
suite.packageContentProvider.RemoveAll()
}

func (suite *PlanYamlGeneratorTestSuite) TestCurrentlyBeingWorkedOn() {
barModulePath := "github.com/foo/bar/hi.txt"
seedModules := map[string]string{
barModulePath: "a=\"World!\"",
}
require.Nil(suite.T(), suite.packageContentProvider.BulkAddFileContent(seedModules))

packageId := "github.com/kurtosis-tech/plan-yaml-prac"
mainFunctionName := ""
relativePathToMainFile := "main.star"

serializedScript := `def run(plan, args):
hi_files_artifact = plan.render_templates(
config={
"hi.txt":struct(
template="hello world!",
data={}
)
},
hi_files_artifact = plan.upload_files(
src="github.com/foo/bar/hi.txt",
name="hi-file"
)
Expand Down Expand Up @@ -121,28 +122,8 @@ func (suite *PlanYamlGeneratorTestSuite) TestCurrentlyBeingWorkedOn() {
yamlBytes, err := pyg.GenerateYaml()
require.NoError(suite.T(), err)

expectedYamlString := `packageId: github.com/kurtosis-tech/postgres-package
services:
- name: tedi
image: ubuntu:latest
envVars:
- key: PASSWORD
value: tedi
ports:
- name: dashboard
number: 1234
transportProtocol: TCP
applicationProtocol: http
files:
- name: hi_files_artifact
files_artifacts:
- name: hi_files_artifact
files:
- "/root"
`
err = os.WriteFile("./plan.yml", yamlBytes, 0644)
require.NoError(suite.T(), err)
require.Equal(suite.T(), expectedYamlString, string(yamlBytes))
}

func (suite *PlanYamlGeneratorTestSuite) TestPlanYamlGeneratorVerySimpleScript() {
Expand Down

0 comments on commit aef291e

Please sign in to comment.