|
| 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 | +} |
0 commit comments