Skip to content
This repository has been archived by the owner on Jul 16, 2018. It is now read-only.

Commit

Permalink
Merge pull request #505 from jstrachan/changes
Browse files Browse the repository at this point in the history
added a new command to clear content-repository
  • Loading branch information
jstrachan authored Jul 27, 2017
2 parents 7c8b33b + 0e7b1bc commit 42e3d1b
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ gofabric8
/release
*.iml
.idea
.settings
.project
.DS_Store
108 changes: 108 additions & 0 deletions cmds/clean_clean_jenkins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmds

import (
"fmt"

"github.com/fabric8io/gofabric8/client"
"github.com/fabric8io/gofabric8/util"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)

type cleanUpJenkinsParams struct {
confirm bool
}

// NewCmdCleanUpJenkins delete files in the tenants content repository
func NewCmdCleanUpJenkins(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "jenkins",
Short: "Deletes all the jenkins jobs in your tenant Jenkins service",
Long: `Deletes all the jenkins jobs in your tenant Jenkins service`,
Aliases: []string{"content-repository"},

Run: func(cmd *cobra.Command, args []string) {
p := cleanUpJenkinsParams{}
if cmd.Flags().Lookup(yesFlag).Value.String() == "true" {
p.confirm = true
}
err := p.cleanUpJenkins(f)
if err != nil {
util.Fatalf("%s", err)
}
return
},
}
return cmd
}

func (p *cleanUpJenkinsParams) cleanUpJenkins(f *cmdutil.Factory) error {
c, cfg := client.NewClient(f)
ns, _, _ := f.DefaultNamespace()
oc, _ := client.NewOpenShiftClient(cfg)
initSchema()

userNS, err := detectCurrentUserNamespace(ns, c, oc)
if err != nil {
return err
}
jenkinsNS := fmt.Sprintf("%s-jenkins", userNS)

if !p.confirm {
confirm := ""
util.Warn("WARNING this is destructive and will remove ALL of the jenkins jobs\n")
util.Info("for your tenant: ")
util.Successf("%s", userNS)
util.Info(" running in namespace: ")
util.Successf("%s\n", jenkinsNS)
util.Warn("\nContinue [y/N]: ")
fmt.Scanln(&confirm)
if confirm != "y" {
util.Warn("Aborted\n")
return nil
}
}
util.Info("Cleaning jenkins for tenant: ")
util.Successf("%s", userNS)
util.Info(" running in namespace: ")
util.Successf("%s\n", jenkinsNS)

err = ensureDeploymentOrDCHasReplicas(c, oc, jenkinsNS, "jenkins", 1)
if err != nil {
return err
}
pod, err := waitForReadyPodForDeploymentOrDC(c, oc, jenkinsNS, "jenkins")
if err != nil {
return err
}
util.Infof("Found running jenkins pod %s\n", pod)

kubeCLI := "kubectl"
err = runCommand(kubeCLI, "exec", "-it", pod, "-n", jenkinsNS, "--", "bash", "-c", "rm -rf /var/lib/jenkins/jobs/*")
if err != nil {
return err
}
err = runCommand(kubeCLI, "delete", "pod", pod, "-n", jenkinsNS)
if err != nil {
return err
}
if err == nil {
util.Info("Completed!\n")
}
return err
}
108 changes: 108 additions & 0 deletions cmds/clean_content_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cmds

import (
"fmt"

"github.com/fabric8io/gofabric8/client"
"github.com/fabric8io/gofabric8/util"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)

type cleanUpContentRepo struct {
confirm bool
}

// NewCmdCleanUpContentRepository delete files in the tenants content repository
func NewCmdCleanUpContentRepository(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "content-repo",
Short: "Hard delete all fabric8 apps, environments and configurations",
Long: `Hard delete all fabric8 apps, environments and configurations`,
Aliases: []string{"content-repository"},

Run: func(cmd *cobra.Command, args []string) {
p := cleanUpContentRepo{}
if cmd.Flags().Lookup(yesFlag).Value.String() == "true" {
p.confirm = true
}
err := p.cleanContentRepository(f)
if err != nil {
util.Fatalf("%s", err)
}
return
},
}
return cmd
}

func (p *cleanUpContentRepo) cleanContentRepository(f *cmdutil.Factory) error {
c, cfg := client.NewClient(f)
ns, _, _ := f.DefaultNamespace()
oc, _ := client.NewOpenShiftClient(cfg)
initSchema()

userNS, err := detectCurrentUserNamespace(ns, c, oc)
if err != nil {
return err
}
jenkinsNS := fmt.Sprintf("%s-jenkins", userNS)

if !p.confirm {
confirm := ""
util.Warn("WARNING this is destructive and will remove ALL of the releases in your content-repository\n")
util.Info("for your tenant: ")
util.Successf("%s", userNS)
util.Info(" running in namespace: ")
util.Successf("%s\n", jenkinsNS)
util.Warn("\nContinue [y/N]: ")
fmt.Scanln(&confirm)
if confirm != "y" {
util.Warn("Aborted\n")
return nil
}
}
util.Info("Cleaning content-repository for tenant: ")
util.Successf("%s", userNS)
util.Info(" running in namespace: ")
util.Successf("%s\n", jenkinsNS)

err = ensureDeploymentOrDCHasReplicas(c, oc, jenkinsNS, "content-repository", 1)
if err != nil {
return err
}
pod, err := waitForReadyPodForDeploymentOrDC(c, oc, jenkinsNS, "content-repository")
if err != nil {
return err
}
util.Infof("Found running content-repository pod %s\n", pod)

kubeCLI := "kubectl"
err = runCommand(kubeCLI, "exec", "-it", pod, "-n", jenkinsNS, "--", "rm", "-rf", "/var/www/html/content/repositories")
if err != nil {
return err
}
err = runCommand(kubeCLI, "exec", "-it", pod, "-n", jenkinsNS, "--", "du", "-hc", "/var/www/html/content")
if err != nil {
return err
}
if err == nil {
util.Info("Completed!\n")
}
return err
}
32 changes: 32 additions & 0 deletions cmds/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,38 @@ func NewCmdCleanUpApp(f *cmdutil.Factory) *cobra.Command {
return cmd
}

func NewCmdCleanUpTenant(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "tenant",
Short: "Hard delete your tenant removing all pipelines, apps, jobs and releases",

Run: func(cmd *cobra.Command, args []string) {
var confirm string
if cmd.Flags().Lookup(yesFlag).Value.String() == "true" {
confirm = "y"
} else {
fmt.Fprintf(os.Stdout, `WARNING this is destructive and will remove all your pipelines, apps, jobs and releases. Continue? [y/N] `)
fmt.Scanln(&confirm)
}

if confirm == "y" {
util.Info("Removing all tenant pipelines...\n")
err := cleanUpTenant(f)
if err != nil {
util.Fatalf("Failed to remove tenant %v", err)
}
return
}
util.Info("Cancelled")
},
}
return cmd
}

func cleanUpTenant(f *cmdutil.Factory) error {
return nil
}

func deleteSystem(f *cmdutil.Factory) error {
var oc *oclient.Client

Expand Down
Loading

0 comments on commit 42e3d1b

Please sign in to comment.