github.com/chasestarr/deis@v1.13.5-0.20170519182049-1d9e59fbdbfc/client/cmd/apps.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "os" 6 "sort" 7 "strings" 8 "time" 9 10 "github.com/deis/deis/pkg/prettyprint" 11 12 "github.com/deis/deis/client/controller/api" 13 "github.com/deis/deis/client/controller/client" 14 "github.com/deis/deis/client/controller/models/apps" 15 "github.com/deis/deis/client/controller/models/config" 16 "github.com/deis/deis/client/pkg/git" 17 "github.com/deis/deis/client/pkg/webbrowser" 18 ) 19 20 // AppCreate creates an app. 21 func AppCreate(id string, buildpack string, remote string, noRemote bool) error { 22 c, err := client.New() 23 if err != nil { 24 return err 25 } 26 27 fmt.Print("Creating Application... ") 28 quit := progress() 29 app, err := apps.New(c, id) 30 31 quit <- true 32 <-quit 33 34 if err != nil { 35 return err 36 } 37 38 fmt.Printf("done, created %s\n", app.ID) 39 40 if buildpack != "" { 41 configValues := api.Config{ 42 Values: map[string]interface{}{ 43 "BUILDPACK_URL": buildpack, 44 }, 45 } 46 if _, err = config.Set(c, app.ID, configValues); err != nil { 47 return err 48 } 49 } 50 51 if !noRemote { 52 if err = git.CreateRemote(c.ControllerURL.Host, remote, app.ID); err != nil { 53 if err.Error() == "exit status 128" { 54 fmt.Println("To replace the existing git remote entry, run:") 55 fmt.Printf(" git remote rename deis deis.old && deis git:remote -a %s\n", app.ID) 56 } 57 return err 58 } 59 } 60 61 fmt.Println("remote available at", git.RemoteURL(c.ControllerURL.Host, app.ID)) 62 63 return nil 64 } 65 66 // AppsList lists apps on the Deis controller. 67 func AppsList(results int) error { 68 c, err := client.New() 69 70 if err != nil { 71 return err 72 } 73 74 if results == defaultLimit { 75 results = c.ResponseLimit 76 } 77 78 apps, count, err := apps.List(c, results) 79 80 if err != nil { 81 return err 82 } 83 84 fmt.Printf("=== Apps%s", limitCount(len(apps), count)) 85 86 sort.Sort(apps) 87 for _, app := range apps { 88 fmt.Println(app.ID) 89 } 90 return nil 91 } 92 93 // AppInfo prints info about app. 94 func AppInfo(appID string) error { 95 c, appID, err := load(appID) 96 97 if err != nil { 98 return err 99 } 100 101 app, err := apps.Get(c, appID) 102 103 if err != nil { 104 return err 105 } 106 107 fmt.Printf("=== %s Application\n", app.ID) 108 fmt.Println("updated: ", app.Updated) 109 fmt.Println("uuid: ", app.UUID) 110 fmt.Println("created: ", app.Created) 111 fmt.Println("url: ", app.URL) 112 fmt.Println("owner: ", app.Owner) 113 fmt.Println("id: ", app.ID) 114 115 fmt.Println() 116 // print the app processes 117 if err = PsList(app.ID, defaultLimit); err != nil { 118 return err 119 } 120 121 fmt.Println() 122 // print the app domains 123 if err = DomainsList(app.ID, defaultLimit); err != nil { 124 return err 125 } 126 127 fmt.Println() 128 129 return nil 130 } 131 132 // AppOpen opens an app in the default webbrowser. 133 func AppOpen(appID string) error { 134 c, appID, err := load(appID) 135 136 if err != nil { 137 return err 138 } 139 140 app, err := apps.Get(c, appID) 141 142 if err != nil { 143 return err 144 } 145 146 u := app.URL 147 if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) { 148 u = "http://" + u 149 } 150 151 return webbrowser.Webbrowser(u) 152 } 153 154 // AppLogs returns the logs from an app. 155 func AppLogs(appID string, lines int) error { 156 c, appID, err := load(appID) 157 158 if err != nil { 159 return err 160 } 161 162 logs, err := apps.Logs(c, appID, lines) 163 164 if err != nil { 165 return err 166 } 167 168 return printLogs(logs) 169 } 170 171 // printLogs prints each log line with a color matched to its category. 172 func printLogs(logs string) error { 173 for _, log := range strings.Split(logs, "\n") { 174 category := "unknown" 175 parts := strings.Split(strings.Split(log, ": ")[0], " ") 176 if len(parts) >= 2 { 177 category = parts[1] 178 } 179 colorVars := map[string]string{ 180 "Color": chooseColor(category), 181 "Log": log, 182 } 183 fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars)) 184 } 185 186 return nil 187 } 188 189 // AppRun runs a one time command in the app. 190 func AppRun(appID, command string) error { 191 c, appID, err := load(appID) 192 193 if err != nil { 194 return err 195 } 196 197 fmt.Printf("Running '%s'...\n", command) 198 199 out, err := apps.Run(c, appID, command) 200 201 if err != nil { 202 return err 203 } 204 205 fmt.Print(out.Output) 206 os.Exit(out.ReturnCode) 207 return nil 208 } 209 210 // AppDestroy destroys an app. 211 func AppDestroy(appID, confirm string) error { 212 gitSession := false 213 214 c, err := client.New() 215 216 if err != nil { 217 return err 218 } 219 220 if appID == "" { 221 appID, err = git.DetectAppName(c.ControllerURL.Host) 222 223 if err != nil { 224 return err 225 } 226 227 gitSession = true 228 } 229 230 if confirm == "" { 231 fmt.Printf(` ! WARNING: Potentially Destructive Action 232 ! This command will destroy the application: %s 233 ! To proceed, type "%s" or re-run this command with --confirm=%s 234 235 > `, appID, appID, appID) 236 237 fmt.Scanln(&confirm) 238 } 239 240 if confirm != appID { 241 return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm) 242 } 243 244 startTime := time.Now() 245 fmt.Printf("Destroying %s...\n", appID) 246 247 if err = apps.Delete(c, appID); err != nil { 248 return err 249 } 250 251 fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds())) 252 253 if gitSession { 254 return git.DeleteRemote(appID) 255 } 256 257 return nil 258 } 259 260 // AppTransfer transfers app ownership to another user. 261 func AppTransfer(appID, username string) error { 262 c, appID, err := load(appID) 263 264 if err != nil { 265 return err 266 } 267 268 fmt.Printf("Transferring %s to %s... ", appID, username) 269 270 err = apps.Transfer(c, appID, username) 271 272 if err != nil { 273 return err 274 } 275 276 fmt.Println("done") 277 278 return nil 279 }