delete.go 6.8 KB

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