github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/rkt/run_prepared.go (about)

     1  // Copyright 2015 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  	"github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/cobra"
    21  	"github.com/coreos/rkt/common"
    22  	"github.com/coreos/rkt/stage0"
    23  	"github.com/coreos/rkt/store"
    24  )
    25  
    26  const (
    27  	cmdRunPreparedName = "run-prepared"
    28  )
    29  
    30  var (
    31  	cmdRunPrepared = &cobra.Command{
    32  		Use:   "run-prepared UUID",
    33  		Short: "Run a prepared application pod in rkt",
    34  		Long:  "UUID must have been acquired via `rkt prepare`",
    35  		Run:   runWrapper(runRunPrepared),
    36  	}
    37  )
    38  
    39  func init() {
    40  	cmdRkt.AddCommand(cmdRunPrepared)
    41  
    42  	cmdRunPrepared.Flags().Var(&flagNet, "net", "configure the pod's networking. optionally pass a list of user-configured networks to load and arguments to pass to them. syntax: --net[=n[:args]][,]")
    43  	cmdRunPrepared.Flags().Lookup("net").NoOptDefVal = "default"
    44  	cmdRunPrepared.Flags().BoolVar(&flagInteractive, "interactive", false, "the pod is interactive")
    45  	cmdRunPrepared.Flags().BoolVar(&flagMDSRegister, "mds-register", false, "register pod with metadata service")
    46  }
    47  
    48  func runRunPrepared(cmd *cobra.Command, args []string) (exit int) {
    49  	if len(args) != 1 {
    50  		cmd.Usage()
    51  		return 1
    52  	}
    53  
    54  	podUUID, err := resolveUUID(args[0])
    55  	if err != nil {
    56  		stderr("Unable to resolve UUID: %v", err)
    57  		return 1
    58  	}
    59  
    60  	s, err := store.NewStore(globalFlags.Dir)
    61  	if err != nil {
    62  		stderr("prepared-run: cannot open store: %v", err)
    63  		return 1
    64  	}
    65  
    66  	p, err := getPod(podUUID)
    67  	if err != nil {
    68  		stderr("prepared-run: cannot get pod: %v", err)
    69  		return 1
    70  	}
    71  
    72  	if !p.isPrepared {
    73  		stderr("prepared-run: pod %q is not prepared", podUUID.String())
    74  		return 1
    75  	}
    76  
    77  	if flagInteractive {
    78  		ac, err := p.getAppCount()
    79  		if err != nil {
    80  			stderr("prepared-run: cannot get pod's app count: %v", err)
    81  			return 1
    82  		}
    83  		if ac > 1 {
    84  			stderr("prepared-run: interactive option only supports pods with one app")
    85  			return 1
    86  		}
    87  	}
    88  
    89  	// Make sure we have a metadata service available before we move to
    90  	// run state so that the user can rerun the command without needing
    91  	// to prepare the image again.
    92  	if flagMDSRegister {
    93  		if err := stage0.CheckMdsAvailability(); err != nil {
    94  			stderr("prepare-run: %v", err)
    95  			return 1
    96  		}
    97  	}
    98  
    99  	if err := p.xToRun(); err != nil {
   100  		stderr("prepared-run: cannot transition to run: %v", err)
   101  		return 1
   102  	}
   103  
   104  	lfd, err := p.Fd()
   105  	if err != nil {
   106  		stderr("prepared-run: unable to get lock fd: %v", err)
   107  		return 1
   108  	}
   109  
   110  	apps, err := p.getApps()
   111  	if err != nil {
   112  		stderr("prepared-run: unable to get app list: %v", err)
   113  		return 1
   114  	}
   115  
   116  	rktgid, err := common.LookupGid(common.RktGroup)
   117  	if err != nil {
   118  		stderr("prepared-run: group %q not found, will use default gid when rendering images", common.RktGroup)
   119  		rktgid = -1
   120  	}
   121  
   122  	rcfg := stage0.RunConfig{
   123  		CommonConfig: stage0.CommonConfig{
   124  			Store: s,
   125  			UUID:  p.uuid,
   126  			Debug: globalFlags.Debug,
   127  		},
   128  		Net:         flagNet,
   129  		LockFd:      lfd,
   130  		Interactive: flagInteractive,
   131  		MDSRegister: flagMDSRegister,
   132  		Apps:        apps,
   133  		RktGid:      rktgid,
   134  	}
   135  	if globalFlags.Debug {
   136  		stage0.InitDebug()
   137  	}
   138  	stage0.Run(rcfg, p.path(), globalFlags.Dir) // execs, never returns
   139  	return 1
   140  }