Skip to content
This repository was archived by the owner on Sep 18, 2021. It is now read-only.

Commit 93a1eef

Browse files
committed
feat(cli): add destroy command
1 parent d299d4c commit 93a1eef

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

commands/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var DeployApplication = &cobra.Command{
3131
)
3232
if err == nil {
3333
defer os.Remove(inventoryFile)
34-
err = routines.ExecAnsiblePlaybook("application-deploy", inventoryFile)
34+
err = routines.ExecAnsiblePlaybook("application-deploy", inventoryFile, nil)
3535
}
3636

3737
taskResult := routines.TaskResult{

commands/destroy.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sync"
7+
8+
"github.com/spf13/cobra"
9+
10+
"github.com/getstackhead/stackhead/cli/ansible"
11+
"github.com/getstackhead/stackhead/cli/routines"
12+
)
13+
14+
// DestroyApplication is a command object for Cobra that provides the destroy command
15+
var DestroyApplication = &cobra.Command{
16+
Use: "destroy [project name] [ipv4 address]",
17+
Example: "destroy my_project_name 192.168.178.14",
18+
Short: "Destroy a deployed project on a target server",
19+
Long: `destroy will tear down the given project that has been deployed onto the server`,
20+
Args: cobra.ExactArgs(2),
21+
Run: func(cmd *cobra.Command, args []string) {
22+
routines.RunTask(
23+
routines.Text(fmt.Sprintf("Destroying project \"%s\" on server with IP \"%s\"", args[0], args[1])),
24+
routines.Execute(func(wg *sync.WaitGroup, result chan routines.TaskResult) {
25+
defer wg.Done()
26+
27+
// Generate Inventory file
28+
inventoryFile, err := ansible.CreateInventoryFile(
29+
ansible.IPAddress(args[1]),
30+
)
31+
if err == nil {
32+
defer os.Remove(inventoryFile)
33+
options := make(map[string]string)
34+
options["project_name"] = args[0]
35+
err = routines.ExecAnsiblePlaybook("application-destroy", inventoryFile, options)
36+
}
37+
38+
taskResult := routines.TaskResult{
39+
Error: err != nil,
40+
}
41+
if err != nil {
42+
taskResult.Message = err.Error()
43+
}
44+
45+
result <- taskResult
46+
}),
47+
)
48+
},
49+
}

commands/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var SetupServer = &cobra.Command{
2828
inventoryFile, err := ansible.CreateInventoryFile(ansible.IPAddress(args[0]))
2929
if err == nil {
3030
defer os.Remove(inventoryFile)
31-
err = routines.ExecAnsiblePlaybook("server-provision", inventoryFile)
31+
err = routines.ExecAnsiblePlaybook("server-provision", inventoryFile, nil)
3232
}
3333

3434
taskResult := routines.TaskResult{

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func init() {
4646
rootCmd.AddCommand(commands.Init())
4747
rootCmd.AddCommand(commands.SetupServer)
4848
rootCmd.AddCommand(commands.DeployApplication)
49+
rootCmd.AddCommand(commands.DestroyApplication)
4950
}
5051

5152
// initConfig reads in config file and ENV variables if set.

routines/exec.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,20 @@ func ExecAnsibleGalaxy(args ...string) error {
4343
}
4444

4545
// ExecAnsiblePlaybook is shortcut for executing a playbook within the StackHead collection via ansible-playbook binary
46-
func ExecAnsiblePlaybook(playbookName string, inventoryPath string) error {
46+
func ExecAnsiblePlaybook(playbookName string, inventoryPath string, options map[string]string) error {
4747
stackHeadLocation, err := ansible.GetStackHeadCollectionLocation()
4848
if err != nil {
4949
return err
5050
}
51-
return Exec("ansible-playbook", stackHeadLocation+"/playbooks/"+playbookName+".yml", "-i", inventoryPath)
51+
52+
args := []string{stackHeadLocation + "/playbooks/" + playbookName + ".yml", "-i", inventoryPath}
53+
if len(options) > 0 {
54+
var extraVars []string
55+
for i, arg := range options {
56+
extraVars = append(extraVars, i+"="+arg)
57+
}
58+
args = append(args, "--extra-vars", strings.Join(extraVars, ","))
59+
}
60+
61+
return Exec("ansible-playbook", args...)
5262
}

0 commit comments

Comments
 (0)