Skip to content
Open
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
14 changes: 0 additions & 14 deletions pkg/ecso/api/environmentapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,6 @@ func (api *environmentAPI) EnvironmentUp(p *ecso.Project, env *ecso.Environment,
return err
}

if err := api.uploadEnvironmentResources(bucket, env, version, w); err != nil {
return err
}

result, err := api.deployEnvironmentStack(bucket, p, env, version, dryRun, w)
if err != nil {
return err
Expand Down Expand Up @@ -348,13 +344,3 @@ func (api *environmentAPI) deployEnvironmentStack(bucket string, project *ecso.P

return cfn.Deploy(pkg, stackName, dryRun, ui.NewPrefixWriter(w, " "))
}

func (api *environmentAPI) uploadEnvironmentResources(bucket string, env *ecso.Environment, version string, w io.Writer) error {
info := ui.NewInfoWriter(w)

fmt.Fprintf(info, "Uploading resources for the '%s' environment to S3", env.Name)

s3Helper := helpers.NewS3Helper(api.s3API, env.Region)

return s3Helper.UploadDir(env.GetResourceDir(), bucket, env.GetResourceBucketPrefix(), ui.NewPrefixWriter(w, " "))
}
64 changes: 51 additions & 13 deletions pkg/ecso/cli/environment_down.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package cli

import (
"fmt"
"io"

"github.com/bernos/ecso/pkg/ecso"
"github.com/bernos/ecso/pkg/ecso/commands"
"github.com/bernos/ecso/pkg/ecso/config"
"github.com/bernos/ecso/pkg/ecso/dispatcher"
"github.com/bernos/ecso/pkg/ecso/ui"
"gopkg.in/urfave/cli.v1"
)

var EnvironmentDownFlags = struct {
Force cli.BoolFlag
}{
Force: cli.BoolFlag{
Name: "force",
Usage: "Required. Confirms the environment will be terminated",
},
}

func NewEnvironmentDownCliCommand(project *ecso.Project, dispatcher dispatcher.Dispatcher) cli.Command {
flags := struct {
Force cli.BoolFlag
}{
Force: cli.BoolFlag{
Name: "force",
Usage: "Required. Confirms the environment will be terminated",
},
}

fn := func(ctx *cli.Context, cfg *config.Config) (ecso.Command, error) {
return makeEnvironmentCommand(ctx, project, func(env *ecso.Environment) ecso.Command {
return commands.NewEnvironmentDownCommand(env.Name, cfg.EnvironmentAPI(env.Region)).
WithForce(ctx.Bool(flags.Force.Name))
})
return &environmentDownCommandWrapper{ctx, cfg}, nil
}

return cli.Command{
Expand All @@ -32,7 +34,43 @@ func NewEnvironmentDownCliCommand(project *ecso.Project, dispatcher dispatcher.D
ArgsUsage: "ENVIRONMENT",
Action: MakeAction(dispatcher, fn),
Flags: []cli.Flag{
flags.Force,
EnvironmentDownFlags.Force,
},
}
}

type environmentDownCommandWrapper struct {
cliCtx *cli.Context
cfg *config.Config
}

func (wrapper *environmentDownCommandWrapper) Execute(ctx *ecso.CommandContext, r io.Reader, w io.Writer) error {
var (
env = ctx.Project.Environments[wrapper.cliCtx.Args().First()]
blue = ui.NewBannerWriter(w, ui.BlueBold)
green = ui.NewBannerWriter(w, ui.GreenBold)
)

cmd, err := makeEnvironmentCommand(wrapper.cliCtx, ctx.Project, func(env *ecso.Environment) ecso.Command {
return commands.NewEnvironmentDownCommand(wrapper.cliCtx.Args().First(), wrapper.cfg.EnvironmentAPI(env.Region)).
WithForce(wrapper.cliCtx.Bool(EnvironmentDownFlags.Force.Name))
})

if err != nil {
return err
}

fmt.Fprintf(blue, "Stopping '%s' environment", env.Name)

if err := cmd.Execute(ctx, r, w); err != nil {
return err
}

fmt.Fprintf(green, "Successfully stopped '%s' environment", env.Name)

return nil
}

func (wrapper *environmentDownCommandWrapper) Validate(ctx *ecso.CommandContext) error {
return nil
}
7 changes: 3 additions & 4 deletions pkg/ecso/commands/environment_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ func NewEnvironmentAddCommand(environmentName string, environmentAPI api.Environ
}

func (c *EnvironmentAddCommand) Execute(ctx *ecso.CommandContext, r io.Reader, w io.Writer) error {
if ctx.Project.HasEnvironment(c.environmentName) {
return ecso.NewEnvironmentExistsError(c.environmentName)
}

ctx.Project.AddEnvironment(&ecso.Environment{
Name: c.environmentName,
Region: c.region,
Expand All @@ -104,5 +100,8 @@ func (c *EnvironmentAddCommand) Execute(ctx *ecso.CommandContext, r io.Reader, w
}

func (c *EnvironmentAddCommand) Validate(ctx *ecso.CommandContext) error {
if ctx.Project.HasEnvironment(c.environmentName) {
return ecso.NewEnvironmentExistsError(c.environmentName)
}
return nil
}
14 changes: 1 addition & 13 deletions pkg/ecso/commands/environment_down.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package commands

import (
"fmt"
"io"

"github.com/bernos/ecso/pkg/ecso"
"github.com/bernos/ecso/pkg/ecso/api"
"github.com/bernos/ecso/pkg/ecso/ui"
)

func NewEnvironmentDownCommand(environmentName string, environmentAPI api.EnvironmentAPI) *EnvironmentDownCommand {
Expand All @@ -32,19 +30,9 @@ func (cmd *EnvironmentDownCommand) Execute(ctx *ecso.CommandContext, r io.Reader
var (
project = ctx.Project
env = cmd.Environment(ctx)
blue = ui.NewBannerWriter(w, ui.BlueBold)
green = ui.NewBannerWriter(w, ui.GreenBold)
)

fmt.Fprintf(blue, "Stopping '%s' environment", env.Name)

if err := cmd.environmentAPI.EnvironmentDown(project, env, w); err != nil {
return err
}

fmt.Fprintf(green, "Successfully stopped '%s' environment", env.Name)

return nil
return cmd.environmentAPI.EnvironmentDown(project, env, w)
}

func (cmd *EnvironmentDownCommand) Validate(ctx *ecso.CommandContext) error {
Expand Down
18 changes: 6 additions & 12 deletions pkg/ecso/commands/environment_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,15 @@ func (cmd *EnvironmentUpCommand) ensureEnvironmentFiles(ctx *ecso.CommandContext
}

templateData := struct {
ServiceDiscoveryLambdaVersion string
InstanceDrainerLambdaVersion string
DNSCleanerLambdaVersion string
Environment *ecso.Environment
Environment *ecso.Environment
}{
ServiceDiscoveryLambdaVersion: resources.ServiceDiscoveryLambdaVersion,
InstanceDrainerLambdaVersion: resources.InstanceDrainerLambdaVersion,
DNSCleanerLambdaVersion: resources.DNSCleanerLambdaVersion,
Environment: env,
Environment: env,
}

transform := resources.TemplateTransformation(templateData)

if cmd.force {
w := resources.NewFileSystemResourceWriter(project.Dir())
return w.WriteResources(templateData, resources.EnvironmentFiles...)
return resources.RestoreAssetsWithTransform(ecso.EnvironmentBaseDir, "environment", "environment/", transform)
}

stackExists, err := cmd.environmentAPI.IsEnvironmentUp(env)
Expand All @@ -108,6 +103,5 @@ func (cmd *EnvironmentUpCommand) ensureEnvironmentFiles(ctx *ecso.CommandContext
return fmt.Errorf("This looks like the first time you've run `environment up` for the %s environment from this repository, however there is already a CloudFormation stack up and running. This could mean that someone has already created the %s environment for the %s project. If you really know what you are doing, you can rerun `environment up` with the `--force` flag.", env.Name, env.Name, project.Name)
}

w := resources.NewFileSystemResourceWriter(project.Dir())
return w.WriteResources(templateData, resources.EnvironmentFiles...)
return resources.RestoreAssetsWithTransform(ecso.EnvironmentBaseDir, "environment", "environment/", transform)
}
20 changes: 4 additions & 16 deletions pkg/ecso/commands/service_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,11 @@ func (cmd *ServiceAddCommand) Validate(ctx *ecso.CommandContext) error {
if cmd.route != "" && cmd.port == 0 {
return fmt.Errorf("Port is required")
}

return nil
}

func (cmd *ServiceAddCommand) createResources(project *ecso.Project, service *ecso.Service) error {
cfnWriter := resources.NewFileSystemResourceWriter(service.GetCloudFormationTemplateDir())
resourceWriter := resources.NewFileSystemResourceWriter(service.Dir())

templateData := struct {
Service *ecso.Service
Project *ecso.Project
Expand All @@ -91,21 +89,11 @@ func (cmd *ServiceAddCommand) createResources(project *ecso.Project, service *ec
Project: project,
}

var serviceResources *resources.ServiceResources
transform := resources.TemplateTransformation(templateData)

if len(service.Route) > 0 {
serviceResources = &resources.WebService
} else {
serviceResources = &resources.WorkerService
}

if err := cfnWriter.WriteResource(serviceResources.CloudFormationTemplate, templateData); err != nil {
return err
return resources.RestoreAssetDirWithTransform(service.Dir(), "services/web", transform)
}

if err := resourceWriter.WriteResource(serviceResources.ComposeFile, templateData); err != nil {
return err
}

return nil
return resources.RestoreAssetDirWithTransform(service.Dir(), "services/worker", transform)
}
14 changes: 9 additions & 5 deletions pkg/ecso/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
)

var (
// EnvironmentBaseDir is the path relative to the .ecso dir that contains
// all environment files
EnvironmentBaseDir = filepath.Join(ecsoDotDir, "environment")

// EnvironmentCloudFormationDir is the path relative to the .ecso dir that the
// environment cloudformation templates are stored
EnvironmentCloudFormationDir = filepath.Join(ecsoDotDir, "environment", "cloudformation")
EnvironmentCloudFormationDir = filepath.Join(EnvironmentBaseDir, "cloudformation")

// EnvironmentResourceDir is the path relative to the .ecso dir that the
// environment resource files are stored
Expand All @@ -27,10 +31,10 @@ var (
type Environment struct {
project *Project

Name string
Region string
CloudFormationParameters map[string]string
CloudFormationTags map[string]string
Name string `yaml:"Name"`
Region string `yaml:"Region"`
CloudFormationParameters map[string]string `yaml:"CloudFormationParameters"`
CloudFormationTags map[string]string `yaml:"CloudFormationTags"`
}

func (e *Environment) GetCloudFormationStackName() string {
Expand Down
51 changes: 16 additions & 35 deletions pkg/ecso/project.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package ecso

import (
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/bernos/ecso/pkg/ecso/resources"
"gopkg.in/yaml.v2"
)

const (
ecsoDotDir = ".ecso"
projectFilename = "project.json"
projectFilename = "project.yaml"
)

// LoadCurrentProject loads the current ecso project from the project.json
// LoadCurrentProject loads the current ecso project from the project.yaml
// file located at the dir given by GetCurrentProjectDir()
func LoadCurrentProject() (*Project, error) {
dir, err := GetCurrentProjectDir()
Expand All @@ -39,7 +40,7 @@ func GetCurrentProjectDir() (string, error) {
return os.Getwd()
}

// LoadProject loads a project from the project.json file in the dir
// LoadProject loads a project from the project.yaml file in the dir
// given by dir
func LoadProject(dir string) (*Project, error) {
project := NewProject(dir, "unknown", "unknown")
Expand All @@ -50,7 +51,7 @@ func LoadProject(dir string) (*Project, error) {
return nil, err
}

err = json.Unmarshal(data, project)
err = yaml.Unmarshal(data, project)

return project, err
}
Expand All @@ -72,10 +73,10 @@ func NewProject(dir, name, version string) *Project {
type Project struct {
dir string

Name string
EcsoVersion string
Environments map[string]*Environment
Services map[string]*Service
Name string `yaml:"Name"`
EcsoVersion string `yaml:"EcsoVersion"`
Environments map[string]*Environment `yaml:"Environments"`
Services map[string]*Service `yaml:"Services"`
}

func (p *Project) Dir() string {
Expand All @@ -98,16 +99,12 @@ func (p *Project) ProjectFile() string {
return filepath.Join(p.DotDir(), projectFilename)
}

func (p *Project) UnmarshalJSON(b []byte) error {
func (p *Project) UnmarshalYAML(unmarshal func(interface{}) error) error {
type Alias Project

aux := &struct {
*Alias
}{
Alias: (*Alias)(p),
}
aux := (*Alias)(p)

if err := json.Unmarshal(b, aux); err != nil {
if err := unmarshal(aux); err != nil {
return err
}

Expand All @@ -123,25 +120,9 @@ func (p *Project) UnmarshalJSON(b []byte) error {
}

func (p *Project) Save() error {
w, err := os.Create(p.ProjectFile())
if err != nil {
return err
}

_, err = p.WriteTo(w)

return err
}

func (p *Project) WriteTo(w io.Writer) (int64, error) {
b, err := json.Marshal(p)
if err != nil {
return 0, err
}

n, err := w.Write(b)
transform := resources.TemplateTransformation(p)

return int64(n), err
return resources.RestoreAssetWithTransform(p.DotDir(), projectFilename, "", transform)
}

func (p *Project) AddEnvironment(environment *Environment) {
Expand Down
Loading