delete.go 6.8 KB

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