Skip to content

Commit e10c276

Browse files
Tidy apply and delete process
1 parent 3298e6a commit e10c276

File tree

6 files changed

+122
-44
lines changed

6 files changed

+122
-44
lines changed

cmd/apply.go

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"context"
1515

1616
getter "github.com/hashicorp/go-getter"
17+
"github.com/hashicorp/go-hclog"
1718
"github.com/shipyard-run/shipyard/pkg/shipyard"
1819
"github.com/spf13/cobra"
1920
)
@@ -36,63 +37,86 @@ var applyCmd = &cobra.Command{
3637
var err error
3738
dst := args[0]
3839

40+
fmt.Println("Applying configuration from: ", dst)
41+
fmt.Println("")
42+
43+
// create a logger
44+
log := hclog.New(&hclog.LoggerOptions{Level: hclog.Debug, Color: hclog.AutoColor})
45+
3946
if !IsLocalFolder(dst) {
4047
// fetch the remote server from github
4148
dst, err = pullRemoteBlueprint(dst)
4249
if err != nil {
43-
panic(err)
50+
log.Error("Unable to retrieve blueprint", "error", err)
51+
return
4452
}
4553
}
4654

4755
// Load the files
48-
e, err := shipyard.NewWithFolder(dst)
56+
e, err := shipyard.NewWithFolder(dst, log)
4957
if err != nil {
50-
panic(err)
58+
log.Error("Unable to load blueprint", "error", err)
59+
return
5160
}
5261

5362
// if we have a blueprint show the header
5463
if e.Blueprint() != nil {
5564
fmt.Println("Title", e.Blueprint().Title)
5665
fmt.Println("Author", e.Blueprint().Author)
5766
fmt.Println("")
58-
fmt.Println(e.Blueprint().Intro)
59-
fmt.Println("")
6067
}
6168

69+
fmt.Printf("Creating %d resources\n\n", e.ResourceCount())
70+
6271
err = e.Apply()
6372
if err != nil {
64-
panic(err)
73+
log.Error("Unable to apply blueprint", "error", err)
74+
75+
log.Info("Attempting to roll back state")
76+
err := e.Destroy()
77+
if err != nil {
78+
log.Error("Unable to roll back state, you may need to manually remove Docker containers and networks", "error", err)
79+
}
80+
81+
return
6582
}
6683

6784
// copy the blueprints to our state folder
6885
// this is temporary
6986
err = copy.Copy(dst, StateDir())
7087
if err != nil {
71-
panic(err)
88+
log.Error("Unable to copy blueprint to state folder", "error", err)
89+
return
7290
}
7391

74-
// apply any env vars
75-
if e.Blueprint() != nil && len(e.Blueprint().Environment) > 0 {
76-
fmt.Println("")
77-
fmt.Println("Setting environment variables:")
78-
fmt.Println("")
79-
ef, err := NewEnv(fmt.Sprintf("%s/env.var", StateDir()))
80-
if err != nil {
81-
panic(err)
82-
}
83-
defer ef.Close()
92+
fmt.Println("")
93+
fmt.Println(e.Blueprint().Intro)
94+
fmt.Println("")
8495

85-
for _, e := range e.Blueprint().Environment {
86-
fmt.Printf("export %s=%s\n", e.Key, e.Value)
87-
err := ef.Set(e.Key, e.Value)
96+
// apply any env vars
97+
/*
98+
if e.Blueprint() != nil && len(e.Blueprint().Environment) > 0 {
99+
fmt.Println("")
100+
fmt.Println("Setting environment variables:")
101+
fmt.Println("")
102+
ef, err := NewEnv(fmt.Sprintf("%s/env.var", StateDir()))
88103
if err != nil {
89104
panic(err)
90105
}
91-
}
106+
defer ef.Close()
107+
108+
for _, e := range e.Blueprint().Environment {
109+
fmt.Printf("export %s=%s\n", e.Key, e.Value)
110+
err := ef.Set(e.Key, e.Value)
111+
if err != nil {
112+
panic(err)
113+
}
114+
}
92115
93-
fmt.Println("")
94-
fmt.Println("environment variables will be restored to previous values when using the `yard delete` command")
95-
}
116+
fmt.Println("")
117+
fmt.Println("environment variables will be restored to previous values when using the `yard delete` command")
118+
}
119+
*/
96120

97121
// open any browser windows
98122
if e.Blueprint() != nil {

cmd/delete.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66

7+
"github.com/hashicorp/go-hclog"
78
"github.com/shipyard-run/shipyard/pkg/shipyard"
89
"github.com/spf13/cobra"
910
)
@@ -14,38 +15,48 @@ var deleteCmd = &cobra.Command{
1415
Long: `Delete the current stack`,
1516
Example: `yard delete my-stack`,
1617
Run: func(cmd *cobra.Command, args []string) {
18+
19+
log := hclog.New(&hclog.LoggerOptions{Level: hclog.Debug, Color: hclog.AutoColor})
20+
1721
// When destroying a stack all the config
1822
// which is created with apply is copied
1923
// to the state folder
20-
e, err := shipyard.NewWithFolder(StateDir())
24+
e, err := shipyard.NewWithFolder(StateDir(), log)
2125
if err != nil {
22-
panic(err)
26+
log.Error("Unable to load state", "error", err)
27+
return
2328
}
2429

30+
fmt.Printf("Deleting %d resources\n\n", e.ResourceCount())
31+
2532
err = e.Destroy()
2633
if err != nil {
27-
panic(err)
34+
log.Error("Unable to delete stack", "error", err)
35+
return
2836
}
2937

30-
// remove the environment varibles
31-
if e.Blueprint() != nil && len(e.Blueprint().Environment) > 0 {
32-
fmt.Println("restoring environment variables")
33-
ef, err := NewEnv(fmt.Sprintf("%s/env.var", StateDir()))
34-
if err != nil {
35-
panic(err)
36-
}
37-
defer ef.Close()
38+
/*
39+
// remove the environment varibles
40+
if e.Blueprint() != nil && len(e.Blueprint().Environment) > 0 {
41+
fmt.Println("restoring environment variables")
42+
ef, err := NewEnv(fmt.Sprintf("%s/env.var", StateDir()))
43+
if err != nil {
44+
panic(err)
45+
}
46+
defer ef.Close()
3847
39-
err = ef.Clear()
40-
if err != nil {
41-
panic(err)
48+
err = ef.Clear()
49+
if err != nil {
50+
panic(err)
51+
}
4252
}
43-
}
53+
*/
4454

4555
// delete the contents of the state folder
4656
err = os.RemoveAll(StateDir())
4757
if err != nil {
48-
panic(err)
58+
log.Error("Unable to delete state", "error", err)
59+
return
4960
}
5061
},
5162
}

pkg/config/config.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,23 @@ func New() (*Config, error) {
2828

2929
return c, nil
3030
}
31+
32+
// ResourceCount defines the number of resources in a config
33+
func (c *Config) ResourceCount() int {
34+
// start at 1 as we always have a wan
35+
co := 1
36+
37+
if c.Docs != nil {
38+
co++
39+
}
40+
41+
co += len(c.Clusters)
42+
co += len(c.Containers)
43+
co += len(c.Containers)
44+
co += len(c.HelmCharts)
45+
co += len(c.K8sConfig)
46+
co += len(c.Ingresses)
47+
co += len(c.Execs)
48+
49+
return co
50+
}

pkg/config/config_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package config
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestResourceCount(t *testing.T) {
10+
c, _ := New()
11+
c.Docs = &Docs{}
12+
c.Clusters = []*Cluster{&Cluster{}}
13+
c.Containers = []*Container{&Container{}}
14+
c.Networks = []*Network{&Network{}}
15+
c.HelmCharts = []*Helm{&Helm{}}
16+
c.K8sConfig = []*K8sConfig{&K8sConfig{}}
17+
c.Ingresses = []*Ingress{&Ingress{}}
18+
c.Execs = []*Exec{&Exec{}}
19+
20+
assert.Equal(t, 9, c.ResourceCount())
21+
}

pkg/providers/container_util.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ func execCommand(c clients.Docker, container string, command []string) error {
223223
return xerrors.Errorf("unable to determine status of exec process: %w", err)
224224
}
225225

226-
fmt.Println(i)
227-
228226
if !i.Running {
229227
if i.ExitCode == 0 {
230228
return nil

pkg/shipyard/engine.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ func GenerateClients(l hclog.Logger) (*Clients, error) {
4242
}
4343

4444
// NewWithFolder creates a new shipyard engine with a given configuration folder
45-
func NewWithFolder(folder string) (*Engine, error) {
46-
l := hclog.New(&hclog.LoggerOptions{Level: hclog.Debug, Color: hclog.AutoColor})
45+
func NewWithFolder(folder string, l hclog.Logger) (*Engine, error) {
4746
var err error
4847

4948
cc, err := config.New()
@@ -110,6 +109,11 @@ func (e *Engine) Destroy() error {
110109
return nil
111110
}
112111

112+
// ResourceCount defines the number of resources in a plan
113+
func (e *Engine) ResourceCount() int {
114+
return e.config.ResourceCount()
115+
}
116+
113117
// Blueprint returns the blueprint for the current config
114118
func (e *Engine) Blueprint() *config.Blueprint {
115119
return e.config.Blueprint

0 commit comments

Comments
 (0)