github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_add.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  	"github.com/rkt/rkt/common"
    21  	pkgPod "github.com/rkt/rkt/pkg/pod"
    22  	"github.com/rkt/rkt/rkt/image"
    23  	"github.com/rkt/rkt/stage0"
    24  	"github.com/rkt/rkt/store/imagestore"
    25  	"github.com/rkt/rkt/store/treestore"
    26  
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var (
    31  	cmdAppAdd = &cobra.Command{
    32  		Use:   "add UUID IMAGEID ...",
    33  		Short: "Add an app to a pod",
    34  		Long:  "This adds an application available in the local image store to a running mutable pod.",
    35  		Run:   runWrapper(runAppAdd),
    36  	}
    37  )
    38  
    39  func init() {
    40  	cmdApp.AddCommand(cmdAppAdd)
    41  	addAppFlags(cmdAppAdd)
    42  	addIsolatorFlags(cmdAppAdd, false)
    43  
    44  	// Add per-app volume mounts only for sandbox for now
    45  	cmdAppAdd.Flags().Var((*appMountVolume)(&rktApps), "mnt-volume", "Configure a per-app mount and volume directly")
    46  
    47  	// Disable interspersed flags to stop parsing after the first non flag
    48  	// argument. All the subsequent parsing will be done by parseApps.
    49  	// This is needed to correctly handle image args
    50  	cmdAppAdd.Flags().SetInterspersed(false)
    51  }
    52  
    53  func runAppAdd(cmd *cobra.Command, args []string) (exit int) {
    54  	if len(args) < 2 {
    55  		stderr.Print("must provide the pod UUID and an IMAGEID")
    56  		return 254
    57  	}
    58  
    59  	err := parseApps(&rktApps, args[1:], cmd.Flags(), true)
    60  	if err != nil {
    61  		stderr.PrintE("error parsing app image arguments", err)
    62  		return 254
    63  	}
    64  
    65  	if rktApps.Count() > 1 {
    66  		stderr.Print("must give only one app")
    67  		return 254
    68  	}
    69  
    70  	p, err := pkgPod.PodFromUUIDString(getDataDir(), args[0])
    71  	if err != nil {
    72  		stderr.PrintE("problem retrieving pod", err)
    73  		return 254
    74  	}
    75  	defer p.Close()
    76  
    77  	if p.State() != pkgPod.Running {
    78  		stderr.Printf("pod %q isn't currently running", p.UUID)
    79  		return 254
    80  	}
    81  
    82  	if !p.IsSupervisorReady() {
    83  		stderr.Printf("supervisor for pod %q is not yet ready", p.UUID)
    84  		return 254
    85  	}
    86  
    87  	s, err := imagestore.NewStore(storeDir())
    88  	if err != nil {
    89  		stderr.PrintE("cannot open store", err)
    90  		return 254
    91  	}
    92  
    93  	ts, err := treestore.NewStore(treeStoreDir(), s)
    94  	if err != nil {
    95  		stderr.PrintE("cannot open treestore", err)
    96  		return 254
    97  	}
    98  
    99  	fn := &image.Finder{
   100  		S:  s,
   101  		Ts: ts,
   102  		Ks: getKeystore(),
   103  
   104  		PullPolicy: image.PullPolicyNever,
   105  	}
   106  
   107  	img, err := fn.FindImage(args[1], "")
   108  	if err != nil {
   109  		stderr.PrintE("error finding images", err)
   110  		return 254
   111  	}
   112  	rktApps.Last().ImageID = *img
   113  
   114  	podPID, err := p.ContainerPid1()
   115  	if err != nil {
   116  		stderr.PrintE(fmt.Sprintf("unable to determine the pid for pod %q", p.UUID), err)
   117  		return 254
   118  	}
   119  
   120  	ccfg := stage0.CommonConfig{
   121  		DataDir:   getDataDir(),
   122  		Store:     s,
   123  		TreeStore: ts,
   124  		UUID:      p.UUID,
   125  		Debug:     globalFlags.Debug,
   126  	}
   127  
   128  	rktgid, err := common.LookupGid(common.RktGroup)
   129  	if err != nil {
   130  		stderr.Printf("group %q not found, will use default gid when rendering images", common.RktGroup)
   131  		rktgid = -1
   132  	}
   133  
   134  	cfg := stage0.AddConfig{
   135  		CommonConfig: &ccfg,
   136  		Image:        *img,
   137  		Apps:         &rktApps,
   138  		RktGid:       rktgid,
   139  		UsesOverlay:  p.UsesOverlay(),
   140  		PodPath:      p.Path(),
   141  		PodPID:       podPID,
   142  	}
   143  
   144  	if globalFlags.Debug {
   145  		stage0.InitDebug()
   146  	}
   147  
   148  	err = stage0.AddApp(cfg)
   149  	if err != nil {
   150  		stderr.PrintE("error adding app to pod", err)
   151  		return 254
   152  	}
   153  
   154  	return 0
   155  }