github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_start.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  	cmdAppStart = &cobra.Command{
    29  		Use:   "start UUID --app=NAME",
    30  		Short: "Start an app in a pod",
    31  		Long: `This starts an existing application inside a mutable pod.
    32  In order to be started, the application must have been previously added to the
    33  pod, e.g. via "rkt app add".`,
    34  		Run: runWrapper(runAppStart),
    35  	}
    36  )
    37  
    38  func init() {
    39  	cmdAppStart.Flags().StringVar(&flagAppName, "app", "", "app to start")
    40  	cmdApp.AddCommand(cmdAppStart)
    41  }
    42  
    43  func runAppStart(cmd *cobra.Command, args []string) (exit int) {
    44  	if len(args) < 1 {
    45  		stderr.Print("must provide the pod UUID")
    46  		return 254
    47  	}
    48  
    49  	if flagAppName == "" {
    50  		stderr.Print("must provide the app to start")
    51  		return 254
    52  	}
    53  
    54  	p, err := pkgPod.PodFromUUIDString(getDataDir(), args[0])
    55  	if err != nil {
    56  		stderr.PrintE("problem retrieving pod", err)
    57  		return 254
    58  	}
    59  	defer p.Close()
    60  
    61  	if p.State() != pkgPod.Running {
    62  		stderr.Printf("pod %q isn't currently running", p.UUID)
    63  		return 254
    64  	}
    65  
    66  	if !p.IsSupervisorReady() {
    67  		stderr.Printf("supervisor for pod %q is not yet ready", p.UUID)
    68  		return 254
    69  	}
    70  
    71  	appName, err := types.NewACName(flagAppName)
    72  	if err != nil {
    73  		stderr.PrintE("invalid app name", err)
    74  		return 254
    75  	}
    76  
    77  	podPID, err := p.ContainerPid1()
    78  	if err != nil {
    79  		stderr.PrintE(fmt.Sprintf("unable to determine the pid for pod %q", p.UUID), err)
    80  		return 254
    81  	}
    82  
    83  	cfg := stage0.CommonConfig{
    84  		DataDir: getDataDir(),
    85  		UUID:    p.UUID,
    86  		Debug:   globalFlags.Debug,
    87  	}
    88  
    89  	scfg := stage0.StartConfig{
    90  		CommonConfig: &cfg,
    91  		PodPath:      p.Path(),
    92  		AppName:      appName,
    93  		PodPID:       podPID,
    94  	}
    95  
    96  	if globalFlags.Debug {
    97  		stage0.InitDebug()
    98  	}
    99  
   100  	err = stage0.StartApp(scfg)
   101  	if err != nil {
   102  		stderr.PrintE("error starting app", err)
   103  		return 254
   104  	}
   105  
   106  	return 0
   107  }