delete.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "github.com/fatih/color"
  8. api "github.com/porter-dev/porter/api/client"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/spf13/cobra"
  11. )
  12. // deleteCmd represents the "porter delete" base command
  13. var deleteCmd = &cobra.Command{
  14. Use: "delete",
  15. Short: "Deletes a deployment",
  16. Long: fmt.Sprintf(`
  17. %s
  18. Destroys a deployment, which is read based on env variables.
  19. %s
  20. The following are the environment variables that can be used to set certain values while
  21. deleting a configuration:
  22. PORTER_CLUSTER Cluster ID that contains the project
  23. PORTER_PROJECT Project ID that contains the application
  24. `,
  25. color.New(color.FgBlue, color.Bold).Sprintf("Help for \"porter delete\":"),
  26. color.New(color.FgGreen, color.Bold).Sprintf("porter delete"),
  27. ),
  28. Run: func(cmd *cobra.Command, args []string) {
  29. err := checkLoginAndRun(args, deleteDeployment)
  30. if err != nil {
  31. os.Exit(1)
  32. }
  33. },
  34. }
  35. // deleteAppsCmd represents the "porter delete apps" subcommand
  36. var deleteAppsCmd = &cobra.Command{
  37. Use: "apps",
  38. Aliases: []string{"app", "applications", "application"},
  39. Short: "Deletes an existing app",
  40. Args: cobra.ExactArgs(1),
  41. Run: func(cmd *cobra.Command, args []string) {
  42. err := checkLoginAndRun(args, deleteApp)
  43. if err != nil {
  44. os.Exit(1)
  45. }
  46. },
  47. }
  48. // deleteJobsCmd represents the "porter delete jobs" subcommand
  49. var deleteJobsCmd = &cobra.Command{
  50. Use: "jobs",
  51. Aliases: []string{"job"},
  52. Short: "Deletes an existing job",
  53. Args: cobra.ExactArgs(1),
  54. Run: func(cmd *cobra.Command, args []string) {
  55. err := checkLoginAndRun(args, deleteJob)
  56. if err != nil {
  57. os.Exit(1)
  58. }
  59. },
  60. }
  61. // deleteAddonsCmd represents the "porter delete addons" subcommand
  62. var deleteAddonsCmd = &cobra.Command{
  63. Use: "addons",
  64. Aliases: []string{"addon"},
  65. Short: "Deletes an existing addon",
  66. Args: cobra.ExactArgs(1),
  67. Run: func(cmd *cobra.Command, args []string) {
  68. err := checkLoginAndRun(args, deleteAddon)
  69. if err != nil {
  70. os.Exit(1)
  71. }
  72. },
  73. }
  74. // deleteHelmCmd represents the "porter delete helm" subcommand
  75. var deleteHelmCmd = &cobra.Command{
  76. Use: "helm",
  77. Aliases: []string{"helmrepo", "helmrepos"},
  78. Short: "Deletes an existing helm repo",
  79. Args: cobra.ExactArgs(1),
  80. Run: func(cmd *cobra.Command, args []string) {
  81. err := checkLoginAndRun(args, deleteHelm)
  82. if err != nil {
  83. os.Exit(1)
  84. }
  85. },
  86. }
  87. func init() {
  88. deleteCmd.PersistentFlags().StringVar(
  89. &namespace,
  90. "namespace",
  91. "default",
  92. "Namespace of the application",
  93. )
  94. deleteCmd.AddCommand(deleteAppsCmd)
  95. deleteCmd.AddCommand(deleteJobsCmd)
  96. deleteCmd.AddCommand(deleteAddonsCmd)
  97. deleteCmd.AddCommand(deleteHelmCmd)
  98. rootCmd.AddCommand(deleteCmd)
  99. }
  100. func deleteDeployment(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  101. projectID := cliConf.Project
  102. if projectID == 0 {
  103. return fmt.Errorf("project id must be set")
  104. }
  105. clusterID := cliConf.Cluster
  106. if clusterID == 0 {
  107. return fmt.Errorf("cluster id must be set")
  108. }
  109. var deploymentID uint
  110. if deplIDStr := os.Getenv("PORTER_DEPLOYMENT_ID"); deplIDStr != "" {
  111. deplID, err := strconv.ParseUint(deplIDStr, 10, 32)
  112. if err != nil {
  113. return fmt.Errorf("error parsing deployment ID: %s", deplIDStr)
  114. }
  115. deploymentID = uint(deplID)
  116. } else {
  117. return fmt.Errorf("Deployment ID must be defined, set by PORTER_DEPLOYMENT_ID")
  118. }
  119. return client.DeleteDeployment(
  120. context.Background(), projectID, clusterID, deploymentID,
  121. )
  122. }
  123. func deleteApp(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  124. name := args[0]
  125. resp, err := client.GetRelease(
  126. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  127. )
  128. if err != nil {
  129. return err
  130. }
  131. rel := *resp
  132. if rel.Chart.Name() != "web" && rel.Chart.Name() != "worker" {
  133. return fmt.Errorf("no app found with name: %s", name)
  134. }
  135. color.New(color.FgBlue).Printf("Deleting app: %s\n", name)
  136. err = client.DeleteRelease(
  137. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  138. )
  139. if err != nil {
  140. return err
  141. }
  142. return nil
  143. }
  144. func deleteJob(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  145. name := args[0]
  146. resp, err := client.GetRelease(
  147. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  148. )
  149. if err != nil {
  150. return err
  151. }
  152. rel := *resp
  153. if rel.Chart.Name() != "job" {
  154. return fmt.Errorf("no job found with name: %s", name)
  155. }
  156. color.New(color.FgBlue).Printf("Deleting job: %s\n", name)
  157. err = client.DeleteRelease(
  158. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  159. )
  160. if err != nil {
  161. return err
  162. }
  163. return nil
  164. }
  165. func deleteAddon(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  166. name := args[0]
  167. resp, err := client.GetRelease(
  168. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  169. )
  170. if err != nil {
  171. return err
  172. }
  173. rel := *resp
  174. if rel.Chart.Name() == "web" || rel.Chart.Name() == "worker" || rel.Chart.Name() == "job" {
  175. return fmt.Errorf("no addon found with name: %s", name)
  176. }
  177. color.New(color.FgBlue).Printf("Deleting addon: %s\n", name)
  178. err = client.DeleteRelease(
  179. context.Background(), cliConf.Project, cliConf.Cluster, namespace, name,
  180. )
  181. if err != nil {
  182. return err
  183. }
  184. return nil
  185. }
  186. func deleteHelm(_ *types.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  187. name := args[0]
  188. resp, err := client.ListHelmRepos(context.Background(), cliConf.Project)
  189. if err != nil {
  190. return err
  191. }
  192. var repo *types.HelmRepo
  193. for _, r := range resp {
  194. if r.Name == name {
  195. repo = r
  196. break
  197. }
  198. }
  199. if repo == nil {
  200. return fmt.Errorf("no helm repo found with name: %s", name)
  201. }
  202. color.New(color.FgBlue).Printf("Deleting helm repo: %s\n", name)
  203. err = client.DeleteHelmRepo(context.Background(), cliConf.Project, repo.ID)
  204. if err != nil {
  205. return err
  206. }
  207. return nil
  208. }