github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/rkt/app_status.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 "encoding/json" 19 "fmt" 20 "strings" 21 "time" 22 23 "github.com/rkt/rkt/api/v1" 24 rkt "github.com/rkt/rkt/lib" 25 "github.com/spf13/cobra" 26 ) 27 28 var ( 29 cmdAppStatus = &cobra.Command{ 30 Use: "status UUID --app=APP_NAME [--format=json]", 31 Short: "Check the status of an app in the given pod", 32 Long: "This will print detailed status of an app", 33 Run: runWrapper(runAppStatus), 34 } 35 ) 36 37 func init() { 38 cmdApp.AddCommand(cmdAppStatus) 39 cmdAppStatus.Flags().StringVar(&flagAppName, "app", "", "choose app within the pod, this flag must be set") 40 cmdAppStatus.Flags().Var(&flagFormat, "format", "choose the output format, allowed format includes 'json', 'json-pretty'. If empty, then the result is printed as key value pairs") 41 } 42 43 func printApp(app *v1.App) { 44 stdout.Printf("name=%s\n", app.Name) 45 stdout.Printf("state=%s\n", app.State) 46 stdout.Printf("image_id=%s\n", app.ImageID) 47 if app.CreatedAt != nil { 48 stdout.Printf("created_at=%v\n", time.Unix(0, *(app.CreatedAt))) 49 } 50 if app.StartedAt != nil { 51 stdout.Printf("started_at=%v\n", time.Unix(0, *(app.StartedAt))) 52 } 53 if app.FinishedAt != nil { 54 stdout.Printf("finished_at=%v\n", time.Unix(0, *(app.FinishedAt))) 55 } 56 if app.ExitCode != nil { 57 stdout.Printf("exit_code=%d\n", *(app.ExitCode)) 58 } 59 60 if len(app.Mounts) > 0 { 61 stdout.Printf("mounts=") 62 var mnts []string 63 for _, mnt := range app.Mounts { 64 mnts = append(mnts, fmt.Sprintf("%s:%s:(read_only:%v)", mnt.HostPath, mnt.ContainerPath, mnt.ReadOnly)) 65 } 66 stdout.Printf(strings.Join(mnts, ",")) 67 stdout.Println() 68 } 69 70 if len(app.UserAnnotations) > 0 { 71 stdout.Printf("user_annotations=") 72 var annos []string 73 for key, value := range app.UserAnnotations { 74 annos = append(annos, fmt.Sprintf("%s:%s", key, value)) 75 } 76 stdout.Printf(strings.Join(annos, ",")) 77 stdout.Println() 78 } 79 80 if len(app.UserLabels) > 0 { 81 stdout.Printf("user_labels=") 82 var labels []string 83 for key, value := range app.UserLabels { 84 labels = append(labels, fmt.Sprintf("%s:%s", key, value)) 85 } 86 stdout.Printf(strings.Join(labels, ",")) 87 stdout.Println() 88 } 89 } 90 91 func runAppStatus(cmd *cobra.Command, args []string) (exit int) { 92 if len(args) != 1 || flagAppName == "" { 93 cmd.Usage() 94 return 1 95 } 96 97 apps, err := rkt.AppsForPod(args[0], getDataDir(), flagAppName) 98 if err != nil { 99 stderr.PrintE("error getting app status", err) 100 return 1 101 } 102 103 if len(apps) == 0 { 104 stderr.Error(fmt.Errorf("cannot find app %q in the pod", flagAppName)) 105 return 1 106 } 107 108 // Must have only 1 app. 109 if len(apps) != 1 { 110 stderr.Error(fmt.Errorf("find more than one app with the name %q", flagAppName)) 111 return 1 112 } 113 114 // TODO(yifan): Print yamls. 115 switch flagFormat { 116 case outputFormatJSON: 117 result, err := json.Marshal(apps[0]) 118 if err != nil { 119 stderr.PrintE("error marshaling the app status", err) 120 return 1 121 } 122 stdout.Print(string(result)) 123 case outputFormatPrettyJSON: 124 result, err := json.MarshalIndent(apps[0], "", "\t") 125 if err != nil { 126 stderr.PrintE("error marshaling the app status", err) 127 return 1 128 } 129 stdout.Print(string(result)) 130 default: 131 printApp(apps[0]) 132 } 133 134 return 0 135 }