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