github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/stop.go (about)

     1  // Copyright 2016 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //+build linux
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/rkt/rkt/stage0"
    23  
    24  	pkgPod "github.com/rkt/rkt/pkg/pod"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var (
    29  	cmdStop = &cobra.Command{
    30  		Use:   "stop --uuid-file=FILE | UUID ...",
    31  		Short: "Stop a pod",
    32  		Run:   runWrapper(runStop),
    33  	}
    34  	flagForce bool
    35  )
    36  
    37  func init() {
    38  	cmdRkt.AddCommand(cmdStop)
    39  	cmdStop.Flags().BoolVar(&flagForce, "force", false, "forced stopping")
    40  	cmdStop.Flags().StringVar(&flagUUIDFile, "uuid-file", "", "read pod UUID from file instead of argument")
    41  }
    42  
    43  func runStop(cmd *cobra.Command, args []string) (exit int) {
    44  	var podUUIDs []string
    45  	var errors int
    46  
    47  	ret := 0
    48  	switch {
    49  	case len(args) == 0 && flagUUIDFile != "":
    50  		podUUID, err := pkgPod.ReadUUIDFromFile(flagUUIDFile)
    51  		if err != nil {
    52  			stderr.PrintE("unable to resolve UUID from file", err)
    53  			ret = 1
    54  		} else {
    55  			podUUIDs = append(podUUIDs, podUUID)
    56  		}
    57  
    58  	case len(args) > 0 && flagUUIDFile == "":
    59  		podUUIDs = args
    60  
    61  	default:
    62  		cmd.Usage()
    63  		return 254
    64  	}
    65  
    66  	for _, podUUID := range podUUIDs {
    67  		p, err := pkgPod.PodFromUUIDString(getDataDir(), podUUID)
    68  		if err != nil {
    69  			errors++
    70  			stderr.PrintE("cannot get pod", err)
    71  			continue
    72  		}
    73  		defer p.Close()
    74  
    75  		if p.IsAfterRun() {
    76  			stdout.Printf("pod %q is already stopped", p.UUID)
    77  			continue
    78  		}
    79  
    80  		if p.State() != pkgPod.Running {
    81  			stderr.Error(fmt.Errorf("pod %q is not running", p.UUID))
    82  			errors++
    83  			continue
    84  		}
    85  
    86  		if err := stage0.StopPod(p.Path(), flagForce, p.UUID); err == nil {
    87  			stdout.Printf("%q", p.UUID)
    88  		} else {
    89  			stderr.PrintE(fmt.Sprintf("error stopping %q", p.UUID), err)
    90  			errors++
    91  		}
    92  	}
    93  
    94  	if errors > 0 {
    95  		stderr.Error(fmt.Errorf("failed to stop %d pod(s)", errors))
    96  		return 254
    97  	}
    98  
    99  	return ret
   100  }