diff --git a/cmd/deploy_rollback.go b/cmd/deploy_rollback.go new file mode 100644 index 0000000..6f57816 --- /dev/null +++ b/cmd/deploy_rollback.go @@ -0,0 +1,34 @@ +package cmd + +import ( + "fmt" + + "github.com/aryansharma9917/codewise-cli/pkg/deploy" + "github.com/spf13/cobra" +) + +var rollbackEnv string +var rollbackRevision int + +var deployRollbackCmd = &cobra.Command{ + Use: "rollback", + Short: "Rollback a deployment to a previous Helm revision", + RunE: func(cmd *cobra.Command, args []string) error { + + if rollbackEnv == "" { + return fmt.Errorf("please provide --env") + } + + if rollbackRevision <= 0 { + return fmt.Errorf("please provide a valid --revision") + } + + return deploy.Rollback(rollbackEnv, rollbackRevision) + }, +} + +func init() { + deployCmd.AddCommand(deployRollbackCmd) + deployRollbackCmd.Flags().StringVar(&rollbackEnv, "env", "", "Environment name") + deployRollbackCmd.Flags().IntVar(&rollbackRevision, "revision", 0, "Helm revision to rollback to") +} diff --git a/cmd/deploy_status.go b/cmd/deploy_status.go new file mode 100644 index 0000000..0edb68a --- /dev/null +++ b/cmd/deploy_status.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + + "github.com/aryansharma9917/codewise-cli/pkg/deploy" + "github.com/spf13/cobra" +) + +var statusEnv string + +var deployStatusCmd = &cobra.Command{ + Use: "status", + Short: "Show deployment status for an environment", + RunE: func(cmd *cobra.Command, args []string) error { + + if statusEnv == "" { + return fmt.Errorf("please provide --env") + } + + return deploy.Status(statusEnv) + }, +} + +func init() { + deployCmd.AddCommand(deployStatusCmd) + deployStatusCmd.Flags().StringVar(&statusEnv, "env", "", "Environment name") +} diff --git a/helm/chart/templates/deployment.yaml b/helm/chart/templates/deployment.yaml index 03b19c0..20e1eb5 100644 --- a/helm/chart/templates/deployment.yaml +++ b/helm/chart/templates/deployment.yaml @@ -6,7 +6,7 @@ metadata: app: {{ .Release.Name }} spec: - replicas: 1 + replicas: 2 selector: matchLabels: diff --git a/pkg/deploy/rollback.go b/pkg/deploy/rollback.go new file mode 100644 index 0000000..11419c9 --- /dev/null +++ b/pkg/deploy/rollback.go @@ -0,0 +1,49 @@ +package deploy + +import ( + "fmt" + "os/exec" +) + +func Rollback(envName string, revision int) error { + + environment, err := LoadEnvironment(envName) + if err != nil { + return err + } + + ns := environment.K8s.Namespace + ctx := environment.K8s.Context + release := environment.Helm.Release + + fmt.Println("Starting rollback...") + fmt.Println("Environment:", envName) + fmt.Println("Release:", release) + fmt.Println("Revision:", revision) + fmt.Println() + + args := []string{ + "rollback", + release, + fmt.Sprintf("%d", revision), + "-n", + ns, + } + + if ctx != "" { + args = append(args, "--kube-context", ctx) + } + + cmd := exec.Command("helm", args...) + cmd.Stdout = nil + cmd.Stderr = nil + + if err := cmd.Run(); err != nil { + return fmt.Errorf("rollback failed") + } + + fmt.Println("Rollback executed successfully.") + fmt.Println("Verifying rollout...") + + return MonitorRollout(environment) +} diff --git a/pkg/deploy/status.go b/pkg/deploy/status.go new file mode 100644 index 0000000..3246ffb --- /dev/null +++ b/pkg/deploy/status.go @@ -0,0 +1,71 @@ +package deploy + +import ( + "fmt" + "os/exec" + "strings" +) + +func Status(envName string) error { + + environment, err := LoadEnvironment(envName) + if err != nil { + return err + } + + ns := environment.K8s.Namespace + ctx := environment.K8s.Context + release := environment.Helm.Release + + fmt.Println("Deployment Status") + fmt.Println("-----------------") + fmt.Println("Environment:", envName) + fmt.Println("Namespace:", ns) + fmt.Println("Release:", release) + fmt.Println() + + // Helm Status + args := []string{ + "status", + release, + "-n", + ns, + } + + if ctx != "" { + args = append(args, "--kube-context", ctx) + } + + cmd := exec.Command("helm", args...) + out, err := cmd.CombinedOutput() + if err != nil { + fmt.Println("Helm status not available") + } else { + fmt.Println(string(out)) + } + + // Pods + fmt.Println("Pods:") + podArgs := []string{ + "get", + "pods", + "-n", + ns, + "-o", + "wide", + } + + if ctx != "" { + podArgs = append(podArgs, "--context", ctx) + } + + podCmd := exec.Command("kubectl", podArgs...) + podsOut, err := podCmd.CombinedOutput() + if err != nil { + fmt.Println("Unable to fetch pods") + } else { + fmt.Println(strings.TrimSpace(string(podsOut))) + } + + return nil +}