Skip to content

Commit

Permalink
Merge pull request #38 from kanmu/support_overrides_file
Browse files Browse the repository at this point in the history
Support overrides file
  • Loading branch information
winebarrel committed May 31, 2023
2 parents e86cf26 + 173e935 commit 4c48396
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ Flags:
JSON/YAML string that overrides ECS container
definition.
--cluster=STRING ECS cluster name ($DMTS_CLUSTER).
--overrides-file=".demitas.jsonnet"
demitas overrides config file name
($DMTS_OVERRIDES_FILE).
Commands:
run --ecspresso-cmd="ecspresso" --conf-dir="~/.demitas" --config=ecspresso.yml,ecspresso.json,ecspresso.jsonnet,... --container-def="ecs-container-def.jsonnet"
Expand Down
65 changes: 57 additions & 8 deletions definition/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DefinitionOpts struct {
TaskOverrides string `short:"t" help:"JSON/YAML string that overrides ECS task definition."`
ContainerOverrides string `short:"c" help:"JSON/YAML string that overrides ECS container definition."`
Cluster string `env:"DMTS_CLUSTER" help:"ECS cluster name."`
OverridesFile string `env:"DMTS_OVERRIDES_FILE" default:".demitas.jsonnet" help:"demitas overrides config file name."`
}

type Definition struct {
Expand All @@ -46,7 +47,13 @@ func (opts *DefinitionOpts) Load(profile string, command string, image string, c
confDir = filepath.Join(confDir, profile)
}

ecspressoConf, err := loadEcsecspressoConf(confDir, opts)
overrides, err := loadOverridesFile(confDir, opts)

if err != nil {
return nil, err
}

ecspressoConf, err := loadEcsecspressoConf(confDir, opts, overrides)

if err != nil {
return nil, err
Expand Down Expand Up @@ -74,19 +81,19 @@ func (opts *DefinitionOpts) Load(profile string, command string, image string, c
taskDefFile = "ecs-task-def.jsonnet"
}

serviceDef, err := loadServiceDef(confDir, serviceDefFile, opts)
serviceDef, err := loadServiceDef(confDir, serviceDefFile, opts, overrides)

if err != nil {
return nil, err
}

containerDef, err := loadContainerDef(confDir, taskDefFile, opts, command, image)
containerDef, err := loadContainerDef(confDir, taskDefFile, opts, overrides, command, image)

if err != nil {
return nil, err
}

taskDef, err := loadTaskDef(confDir, taskDefFile, containerDef, opts, cpu, memory)
taskDef, err := loadTaskDef(confDir, taskDefFile, containerDef, opts, overrides, cpu, memory)

if err != nil {
return nil, err
Expand All @@ -106,7 +113,17 @@ func (opts *DefinitionOpts) Load(profile string, command string, image string, c
}, nil
}

func loadEcsecspressoConf(confDir string, opts *DefinitionOpts) (*EcspressoConfig, error) {
func loadOverridesFile(confDir string, opts *DefinitionOpts) (*Overrides, error) {
overrides, err := newOoverrides(filepath.Join(confDir, opts.OverridesFile))

if err != nil {
return nil, err
}

return overrides, nil
}

func loadEcsecspressoConf(confDir string, opts *DefinitionOpts, overrides *Overrides) (*EcspressoConfig, error) {
var cfgFile string

for _, f := range opts.Config {
Expand All @@ -127,6 +144,14 @@ func loadEcsecspressoConf(confDir string, opts *DefinitionOpts) (*EcspressoConfi
return nil, err
}

if v := overrides.get("ecspresso_config"); v != "" {
err = ecspressoConf.patch(v)

if err != nil {
return nil, err
}
}

if opts.Cluster != "" {
js, err := json.Marshal(map[string]string{
"cluster": opts.Cluster,
Expand All @@ -152,13 +177,21 @@ func loadEcsecspressoConf(confDir string, opts *DefinitionOpts) (*EcspressoConfi
return ecspressoConf, nil
}

func loadServiceDef(confDir string, serviceDefFile string, opts *DefinitionOpts) (*ServiceDefinition, error) {
func loadServiceDef(confDir string, serviceDefFile string, opts *DefinitionOpts, overrides *Overrides) (*ServiceDefinition, error) {
serviceDef, err := newServiceDefinition(filepath.Join(confDir, serviceDefFile))

if err != nil {
return nil, err
}

if v := overrides.get("service_definition"); v != "" {
err = serviceDef.patch(v)

if err != nil {
return nil, err
}
}

err = serviceDef.patch(opts.ServiceOverrides)

if err != nil {
Expand All @@ -168,13 +201,21 @@ func loadServiceDef(confDir string, serviceDefFile string, opts *DefinitionOpts)
return serviceDef, nil
}

func loadTaskDef(confDir string, taskDefFile string, containerDef *ContainerDefinition, opts *DefinitionOpts, cpu uint64, memory uint64) (*TaskDefinition, error) {
func loadTaskDef(confDir string, taskDefFile string, containerDef *ContainerDefinition, opts *DefinitionOpts, overrides *Overrides, cpu uint64, memory uint64) (*TaskDefinition, error) {
taskDef, err := newTaskDefinition(filepath.Join(confDir, taskDefFile))

if err != nil {
return nil, err
}

if v := overrides.get("task_definition"); v != "" {
err = taskDef.patch(v, nil, 0, 0)

if err != nil {
return nil, err
}
}

err = taskDef.patch(opts.TaskOverrides, containerDef, cpu, memory)

if err != nil {
Expand All @@ -184,13 +225,21 @@ func loadTaskDef(confDir string, taskDefFile string, containerDef *ContainerDefi
return taskDef, nil
}

func loadContainerDef(confDir string, taskDefFile string, opts *DefinitionOpts, command string, image string) (*ContainerDefinition, error) {
func loadContainerDef(confDir string, taskDefFile string, opts *DefinitionOpts, overrides *Overrides, command string, image string) (*ContainerDefinition, error) {
containerDef, err := newContainerDefinition(filepath.Join(confDir, opts.ContainerDef), filepath.Join(confDir, taskDefFile))

if err != nil {
return nil, err
}

if v := overrides.get("container_definition"); v != "" {
err = containerDef.patch(v, "", "")

if err != nil {
return nil, err
}
}

err = containerDef.patch(opts.ConfigOverrides, command, image)

if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions definition/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package definition

import (
"fmt"
"os"

"github.com/kanmu/demitas2/utils"
"github.com/valyala/fastjson"
)

type Overrides struct {
Content []byte
}

func newOoverrides(path string) (*Overrides, error) {
_, err := os.Stat(path)

if err != nil {
return &Overrides{}, nil
}

content, err := utils.EvaluateJsonnet(path)

if err != nil {
return nil, fmt.Errorf("failed to parse overrides file: %w: %s", err, path)
}

overrides := &Overrides{
Content: content,
}

return overrides, nil
}

func (overrides *Overrides) get(key string) string {
var p fastjson.Parser
content, _ := p.ParseBytes(overrides.Content)
v := content.Get(key)

if v != nil {
return v.String()
} else {
return ""
}
}
6 changes: 4 additions & 2 deletions definition/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ func (taskDef *TaskDefinition) patch(overrides string, containerDef *ContainerDe
}
}

containerDefinitions := fmt.Sprintf(`{"containerDefinitions":[%s]}`, string(containerDef.Content))
patchedContent, err = jsonpatch.MergePatch(patchedContent, []byte(containerDefinitions))
if containerDef != nil {
containerDefinitions := fmt.Sprintf(`{"containerDefinitions":[%s]}`, string(containerDef.Content))
patchedContent, err = jsonpatch.MergePatch(patchedContent, []byte(containerDefinitions))
}

if err != nil {
return fmt.Errorf("failed to patch containerDefinitions: %w", err)
Expand Down

0 comments on commit 4c48396

Please sign in to comment.