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

     1  // Copyright 2014 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 "github.com/coreos/rkt/Godeps/_workspace/src/github.com/spf13/cobra"
    20  
    21  var (
    22  	cmdStatus = &cobra.Command{
    23  		Use:   "status [--wait] UUID",
    24  		Short: "Check the status of a rkt pod",
    25  		Run:   runWrapper(runStatus),
    26  	}
    27  	flagWait bool
    28  )
    29  
    30  const (
    31  	overlayStatusDirTemplate = "overlay/%s/upper/rkt/status"
    32  	regularStatusDir         = "stage1/rootfs/rkt/status"
    33  	cmdStatusName            = "status"
    34  )
    35  
    36  func init() {
    37  	cmdRkt.AddCommand(cmdStatus)
    38  	cmdStatus.Flags().BoolVar(&flagWait, "wait", false, "toggle waiting for the pod to exit")
    39  }
    40  
    41  func runStatus(cmd *cobra.Command, args []string) (exit int) {
    42  	if len(args) != 1 {
    43  		cmd.Usage()
    44  		return 1
    45  	}
    46  
    47  	podUUID, err := resolveUUID(args[0])
    48  	if err != nil {
    49  		stderr("Unable to resolve UUID: %v", err)
    50  		return 1
    51  	}
    52  
    53  	p, err := getPod(podUUID)
    54  	if err != nil {
    55  		stderr("Unable to get pod: %v", err)
    56  		return 1
    57  	}
    58  	defer p.Close()
    59  
    60  	if flagWait {
    61  		if err := p.waitExited(); err != nil {
    62  			stderr("Unable to wait for pod: %v", err)
    63  			return 1
    64  		}
    65  	}
    66  
    67  	if err = printStatus(p); err != nil {
    68  		stderr("Unable to print status: %v", err)
    69  		return 1
    70  	}
    71  
    72  	return 0
    73  }
    74  
    75  // printStatus prints the pod's pid and per-app status codes
    76  func printStatus(p *pod) error {
    77  	stdout("state=%s", p.getState())
    78  
    79  	if p.isRunning() {
    80  		stdout("networks=%s", fmtNets(p.nets))
    81  	}
    82  
    83  	if !p.isEmbryo && !p.isPreparing && !p.isPrepared && !p.isAbortedPrepare && !p.isGarbage && !p.isGone {
    84  		pid, err := p.getPID()
    85  		if err != nil {
    86  			return err
    87  		}
    88  
    89  		stats, err := p.getExitStatuses()
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		stdout("pid=%d\nexited=%t", pid, p.isExited)
    95  		for app, stat := range stats {
    96  			stdout("app-%s=%d", app, stat)
    97  		}
    98  	}
    99  	return nil
   100  }