github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_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  package main
    16  
    17  import (
    18  	"fmt"
    19  
    20  	pkgPod "github.com/rkt/rkt/pkg/pod"
    21  	"github.com/rkt/rkt/stage0"
    22  
    23  	"github.com/appc/spec/schema/types"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  var (
    28  	cmdAppStop = &cobra.Command{
    29  		Use:   "stop UUID --app=NAME",
    30  		Short: "Stop an app in a pod",
    31  		Long:  "This stops an application running inside a mutable pod.",
    32  		Run:   runWrapper(runAppStop),
    33  	}
    34  )
    35  
    36  func init() {
    37  	cmdAppStop.Flags().StringVar(&flagAppName, "app", "", "app to stop")
    38  	cmdApp.AddCommand(cmdAppStop)
    39  }
    40  
    41  func runAppStop(cmd *cobra.Command, args []string) (exit int) {
    42  	if len(args) < 1 {
    43  		stderr.Print("must provide the pod UUID")
    44  		return 254
    45  	}
    46  
    47  	if flagAppName == "" {
    48  		stderr.Print("must provide the app to remove")
    49  		return 254
    50  	}
    51  
    52  	appName, err := types.NewACName(flagAppName)
    53  	if err != nil {
    54  		stderr.PrintE("invalid app name", err)
    55  		return 254
    56  	}
    57  
    58  	p, err := pkgPod.PodFromUUIDString(getDataDir(), args[0])
    59  	if err != nil {
    60  		stderr.PrintE("problem retrieving pod", err)
    61  		return 254
    62  	}
    63  	defer p.Close()
    64  
    65  	if p.IsAfterRun() {
    66  		stdout.Printf("pod %q is already stopped", p.UUID)
    67  		return 0
    68  	}
    69  
    70  	if p.State() != pkgPod.Running {
    71  		stderr.Printf("pod %q isn't currently running", p.UUID)
    72  		return 254
    73  	}
    74  
    75  	if !p.IsSupervisorReady() {
    76  		stderr.Printf("supervisor for pod %q is not yet ready", p.UUID)
    77  		return 254
    78  	}
    79  
    80  	podPID, err := p.ContainerPid1()
    81  	if err != nil {
    82  		stderr.PrintE(fmt.Sprintf("unable to determine the pid for pod %q", p.UUID), err)
    83  		return 254
    84  	}
    85  
    86  	cfg := stage0.CommonConfig{
    87  		DataDir: getDataDir(),
    88  		UUID:    p.UUID,
    89  		Debug:   globalFlags.Debug,
    90  	}
    91  
    92  	scfg := stage0.StopConfig{
    93  		CommonConfig: &cfg,
    94  		PodPath:      p.Path(),
    95  		AppName:      appName,
    96  		PodPID:       podPID,
    97  	}
    98  
    99  	if globalFlags.Debug {
   100  		stage0.InitDebug()
   101  	}
   102  
   103  	err = stage0.StopApp(scfg)
   104  	if err != nil {
   105  		stderr.PrintE("error stopping app", err)
   106  		return 254
   107  	}
   108  
   109  	return 0
   110  }