Skip to content

Commit 454869a

Browse files
Add purge command, fix bugs
1 parent ff0f154 commit 454869a

File tree

20 files changed

+204
-60
lines changed

20 files changed

+204
-60
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ sidecar "consul" {
2828
### Helm charts
2929
* Added ability to set the namespace
3030

31+
### Purge Command
32+
* Added `shipyard purge` command to remove all cached images, blueprints, and helm charts
33+
34+
### Bugfixes
35+
* Remote exec now correctly pulls a container
3136

3237
## version 0.0.18
3338

cmd/exec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func newExecCmd(dt clients.ContainerTasks) *cobra.Command {
3333
`,
3434
Args: cobra.MinimumNArgs(1),
3535
DisableFlagParsing: true,
36+
SilenceUsage: true,
3637
RunE: func(cmd *cobra.Command, args []string) error {
3738
parameters, command := parseParameters(args)
3839

cmd/get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func newGetCmd(bp clients.Getter) *cobra.Command {
2323
# Fetch a blueprint from GitHub
2424
yard get github.com/shipyard-run/blueprints//vault-k8s
2525
`,
26-
Args: cobra.ArbitraryArgs,
26+
Args: cobra.ArbitraryArgs,
27+
SilenceUsage: true,
2728
RunE: func(cmd *cobra.Command, args []string) error {
2829
// create the shipyard home
2930
os.MkdirAll(utils.ShipyardHome(), os.FileMode(0755))

cmd/purge.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/docker/docker/api/types"
10+
"github.com/hashicorp/go-hclog"
11+
"github.com/shipyard-run/shipyard/pkg/clients"
12+
"github.com/shipyard-run/shipyard/pkg/utils"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
func newPurgeCmd(dt clients.Docker, il clients.ImageLog, l hclog.Logger) *cobra.Command {
17+
purgeCmd := &cobra.Command{
18+
Use: "purge",
19+
Short: "Purges Docker images, Helm charts, and Blueprints downloaded by Shipyard",
20+
Long: "Purges Docker images, Helm charts, and Blueprints downloaded by Shipyard",
21+
Example: `
22+
shipyard purge
23+
`,
24+
Args: cobra.ArbitraryArgs,
25+
RunE: newPurgeCmdFunc(dt, il, l),
26+
SilenceUsage: true,
27+
}
28+
29+
return purgeCmd
30+
}
31+
32+
func newPurgeCmdFunc(dt clients.Docker, il clients.ImageLog, l hclog.Logger) func(cmd *cobra.Command, args []string) error {
33+
return func(cmd *cobra.Command, args []string) error {
34+
images, _ := il.Read(clients.ImageTypeDocker)
35+
36+
for _, i := range images {
37+
l.Info("Removing image", "image", i)
38+
39+
_, err := dt.ImageRemove(context.Background(), i, types.ImageRemoveOptions{Force: true, PruneChildren: true})
40+
if err != nil {
41+
return fmt.Errorf("Unable to delete image: %s, error: %s", i, err)
42+
}
43+
}
44+
il.Clear()
45+
46+
hcp := filepath.Join(utils.ShipyardHome(), "helm_charts")
47+
l.Info("Removing Helm charts", "path", hcp)
48+
err := os.RemoveAll(hcp)
49+
if err != nil {
50+
return fmt.Errorf("Unable to remove cached Helm charts: %s", err)
51+
}
52+
53+
bcp := filepath.Join(utils.ShipyardHome(), "helm_charts")
54+
l.Info("Removing Blueprints", "path", bcp)
55+
err = os.RemoveAll(bcp)
56+
if err != nil {
57+
return fmt.Errorf("Unable to remove cached Blueprints: %s", err)
58+
}
59+
60+
return nil
61+
}
62+
}

cmd/push.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func newPushCmd(ct clients.ContainerTasks, kc clients.Kubernetes, ht clients.HTT
2121
Example: `yard push nicholasjackson/fake-service:v0.1.3 k8s_cluster.k3s`,
2222
DisableFlagsInUseLine: true,
2323
Args: cobra.MaximumNArgs(3),
24+
SilenceUsage: true,
2425
RunE: func(cmd *cobra.Command, args []string) error {
2526
if len(args) < 2 {
2627
return xerrors.Errorf("Push requires two arguments [image] [cluster]")

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func init() {
4848
rootCmd.AddCommand(newGetCmd(engineClients.Getter))
4949
rootCmd.AddCommand(destroyCmd)
5050
rootCmd.AddCommand(statusCmd)
51+
rootCmd.AddCommand(newPurgeCmd(engineClients.Docker, engineClients.ImageLog, logger))
5152
rootCmd.AddCommand(taintCmd)
5253
rootCmd.AddCommand(newExecCmd(engineClients.ContainerTasks))
5354
rootCmd.AddCommand(versionCmd)

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func newRunCmdFunc(e shipyard.Engine, bp clients.Getter, hc clients.HTTP, bc cli
6262
}
6363

6464
// check the shipyard version
65-
text, ok := utils.CheckVersion(version)
65+
text, ok := bc.CheckVersion(version)
6666
if !ok {
6767
fmt.Println("")
6868
fmt.Println(text)

cmd/run_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func setupRun(t *testing.T) (*cobra.Command, *mocks.Engine, *clientmocks.Getter,
2828
mockBrowser := &clientmocks.System{}
2929
mockBrowser.On("OpenBrowser", mock.Anything).Return(nil)
3030
mockBrowser.On("Preflight").Return(nil)
31+
mockBrowser.On("CheckVersion", mock.Anything).Return("", false)
3132

3233
mockTasks := &clientmocks.MockContainerTasks{}
3334
mockTasks.On("SetForcePull", mock.Anything)

functional_tests/main_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ func FeatureContext(s *godog.Suite) {
7979
cmd.Stdout = os.Stdout
8080
cmd.Stderr = os.Stderr
8181
cmd.Run()
82+
83+
// purge the cache
84+
cmd = exec.Command("yard-dev", []string{"purge"}...)
85+
cmd.Stdout = os.Stdout
86+
cmd.Stderr = os.Stderr
87+
cmd.Run()
8288
})
8389
}
8490

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/mattn/go-isatty v0.0.12 // indirect
2121
github.com/mitchellh/go-homedir v1.1.0
2222
github.com/mitchellh/mapstructure v1.1.2
23+
github.com/opencontainers/runtime-tools v0.0.0-20181011054405-1d69bd0f9c39
2324
github.com/prometheus/common v0.7.0 // indirect
2425
github.com/spf13/cobra v0.0.5
2526
github.com/spf13/viper v1.5.0

0 commit comments

Comments
 (0)