提交 4fe9ca5f 编写于 作者: M Medya Gh

break done delete and prune into two funcs better error handling

上级 e1ffa5fb
......@@ -118,11 +118,17 @@ func runDelete(cmd *cobra.Command, args []string) {
if errs != nil && len(errs) > 0 { // it will error if there is no container to delete
glog.Infof("error delete containers by label %q (might be okay): %+v", delLabel, err)
}
errs = oci.DeleteAllVolumesByLabel(oci.Docker, delLabel)
if errs != nil && len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error delete volumes by label %q (might be okay): %+v", delLabel, errs)
}
errs = oci.PruneAllVolumesByLabel(oci.Docker, delLabel)
if errs != nil && len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error pruning volumes by label %q (might be okay): %+v", delLabel, errs)
}
errs = DeleteProfiles(profilesToDelete)
if len(errs) > 0 {
HandleDeletionErrors(errs)
......@@ -185,13 +191,19 @@ func DeleteProfiles(profiles []*pkg_config.Profile) []error {
func deleteProfile(profile *pkg_config.Profile) error {
viper.Set(pkg_config.MachineProfile, profile.Name)
errs := oci.DeleteAllContainersByLabel(oci.Docker, fmt.Sprintf("%s=%s", oci.ProfileLabelKey, profile.Name))
delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, profile.Name)
errs := oci.DeleteAllContainersByLabel(oci.Docker, delLabel)
if errs != nil { // it will error if there is no container to delete
glog.Infof("no left over kic container for %s found to delete. %+v", profile.Name, errs)
glog.Infof("error deleting containers (might be okay):\n%v", profile.Name, errs)
}
errs = oci.DeleteAllVolumesByLabel(oci.Docker, fmt.Sprintf("%s=%s", oci.ProfileLabelKey, profile.Name))
errs = oci.DeleteAllVolumesByLabel(oci.Docker, delLabel)
if errs != nil { // it will not error if there is nothing to delete
glog.Warningf("error deleting left docker volumes. To see the list of volumes run: 'docker volume ls' \n:%+v", errs)
glog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
}
errs = oci.PruneAllVolumesByLabel(oci.Docker, delLabel)
if errs != nil && len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error pruning volume (might be okay):\n%v", delLabel, errs)
}
api, err := machine.NewAPIClient()
......
......@@ -35,7 +35,7 @@ import (
)
// DeleteAllContainersByLabel deletes all containers that have a specific label
// if there are no containers found with the label, it will return nil
// if there no containers found with the given label, it will return nil
func DeleteAllContainersByLabel(ociBin string, label string) []error {
var deleteErrs []error
if ociBin == Docker {
......@@ -431,13 +431,15 @@ func listContainersByLabel(ociBinary string, label string) ([]string, error) {
}
cmd := exec.Command(ociBinary, "ps", "-a", "--filter", fmt.Sprintf("label=%s", label), "--format", "{{.Names}}")
stdout, err := cmd.Output()
outs := strings.Split(strings.Replace(string(stdout), "\r", "", -1), "\n")
s := bufio.NewScanner(bytes.NewReader(stdout))
var names []string
for _, o := range outs {
if strings.TrimSpace(o) != "" {
names = append(names, strings.TrimSpace(o))
for s.Scan() {
n := strings.TrimSpace(s.Text())
if n != "" {
names = append(names, n)
}
}
return names, err
}
......
......@@ -17,6 +17,8 @@ limitations under the License.
package oci
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strings"
......@@ -27,10 +29,9 @@ import (
// DeleteAllVolumesByLabel deletes all volumes that have a specific label
// if there is no volume to delete it will return nil
// example: docker volume prune -f --filter label=name.minikube.sigs.k8s.io=minikube
func DeleteAllVolumesByLabel(ociBin string, label string) []error {
var deleteErrs []error
glog.Infof("trying to prune all %s volumes with label %s", ociBin, label)
glog.Infof("trying to delete all %s volumes with label %s", ociBin, label)
if ociBin == Docker {
if err := PointToHostDockerDaemon(); err != nil {
return []error{errors.Wrap(err, "point host docker-daemon")}
......@@ -39,20 +40,34 @@ func DeleteAllVolumesByLabel(ociBin string, label string) []error {
vs, err := allVolumesByLabel(ociBin, label)
if err != nil {
glog.Infof("error listing volumes by label %q: %v", label, err)
return []error{fmt.Errorf("listing volumes by label %q: %v", label, err)}
}
for _, v := range vs {
cmd := exec.Command(ociBin, "volume", "rm", "--force", v)
if out, err := cmd.CombinedOutput(); err != nil {
glog.Infof("error deleting volume %s: output: %s", v, string(out))
deleteErrs = append(deleteErrs, err)
deleteErrs = append(deleteErrs, fmt.Errorf("deleting volume %s: output: %s", v, string(out)))
}
}
return deleteErrs
}
// PruneAllVolumesByLabel deletes all volumes that have a specific label
// if there is no volume to delete it will return nil
// example: docker volume prune -f --filter label=name.minikube.sigs.k8s.io=minikube
func PruneAllVolumesByLabel(ociBin string, label string) []error {
var deleteErrs []error
glog.Infof("trying to prune all %s volumes with label %s", ociBin, label)
if ociBin == Docker {
if err := PointToHostDockerDaemon(); err != nil {
return []error{errors.Wrap(err, "point host docker-daemon")}
}
}
// try to prune afterwards just in case delete didn't go through
cmd := exec.Command(ociBin, "volume", "prune", "-f", "--filter", "label="+label)
if out, err := cmd.CombinedOutput(); err != nil {
deleteErrs = append(deleteErrs, errors.Wrapf(err, "prune volume %s", string(out)))
deleteErrs = append(deleteErrs, errors.Wrapf(err, "prune volume by label %s: %s", label, string(out)))
}
return deleteErrs
}
......@@ -62,13 +77,13 @@ func DeleteAllVolumesByLabel(ociBin string, label string) []error {
func allVolumesByLabel(ociBin string, label string) ([]string, error) {
cmd := exec.Command(ociBin, "volume", "ls", "--filter", "label="+label, "--format", "{{.Name}}")
stdout, err := cmd.Output()
outs := strings.Split(strings.Replace(string(stdout), "\r", "", -1), "\n")
s := bufio.NewScanner(bytes.NewReader(stdout))
var vols []string
for _, o := range outs {
if strings.TrimSpace(o) != "" {
vols = append(vols, strings.TrimSpace(o))
for s.Scan() {
v := strings.TrimSpace(s.Text())
if v != "" {
vols = append(vols, v)
}
}
return vols, err
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册