github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_rm.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  	cmdAppRm = &cobra.Command{
    29  		Use:   "rm UUID --app=NAME",
    30  		Short: "Remove an app from a pod",
    31  		Long:  "This removes an application from a mutable pod, stopping it beforehand if still running.",
    32  		Run:   runWrapper(runAppRm),
    33  	}
    34  )
    35  
    36  func init() {
    37  	cmdAppRm.Flags().StringVar(&flagAppName, "app", "", "app to remove")
    38  	cmdApp.AddCommand(cmdAppRm)
    39  }
    40  
    41  func runAppRm(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  	p, err := pkgPod.PodFromUUIDString(getDataDir(), args[0])
    53  	if err != nil {
    54  		stderr.PrintE("problem retrieving pod", err)
    55  		return 254
    56  	}
    57  	defer p.Close()
    58  
    59  	appName, err := types.NewACName(flagAppName)
    60  	if err != nil {
    61  		stderr.PrintE("invalid app name", err)
    62  	}
    63  
    64  	var podPID int
    65  	if p.State() == pkgPod.Running {
    66  		if !p.IsSupervisorReady() {
    67  			stderr.Printf("pod %q is running but its supervisor is not ready yet", p.UUID)
    68  			return 254
    69  		}
    70  
    71  		podPID, err = p.ContainerPid1()
    72  		if err != nil {
    73  			stderr.PrintE(fmt.Sprintf("unable to determine the pid for pod %q", p.UUID), err)
    74  			return 254
    75  		}
    76  	}
    77  
    78  	ccfg := stage0.CommonConfig{
    79  		DataDir: getDataDir(),
    80  		UUID:    p.UUID,
    81  		Debug:   globalFlags.Debug,
    82  	}
    83  
    84  	cfg := stage0.RmConfig{
    85  		CommonConfig: &ccfg,
    86  		UsesOverlay:  p.UsesOverlay(),
    87  		AppName:      appName,
    88  		PodPath:      p.Path(),
    89  		PodPID:       podPID,
    90  	}
    91  
    92  	if globalFlags.Debug {
    93  		stage0.InitDebug()
    94  	}
    95  
    96  	err = stage0.RmApp(cfg)
    97  	if err != nil {
    98  		stderr.PrintE("error removing app", err)
    99  		return 254
   100  	}
   101  
   102  	return 0
   103  }