github.com/naphatkrit/deis@v1.12.3/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  	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  	for _, app := range apps {
    87  		fmt.Println(app.ID)
    88  	}
    89  	return nil
    90  }
    91  
    92  // AppInfo prints info about app.
    93  func AppInfo(appID string) error {
    94  	c, appID, err := load(appID)
    95  
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	app, err := apps.Get(c, appID)
   101  
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	fmt.Printf("=== %s Application\n", app.ID)
   107  	fmt.Println("updated: ", app.Updated)
   108  	fmt.Println("uuid:    ", app.UUID)
   109  	fmt.Println("created: ", app.Created)
   110  	fmt.Println("url:     ", app.URL)
   111  	fmt.Println("owner:   ", app.Owner)
   112  	fmt.Println("id:      ", app.ID)
   113  
   114  	fmt.Println()
   115  	// print the app processes
   116  	if err = PsList(app.ID, defaultLimit); err != nil {
   117  		return err
   118  	}
   119  
   120  	fmt.Println()
   121  	// print the app domains
   122  	if err = DomainsList(app.ID, defaultLimit); err != nil {
   123  		return err
   124  	}
   125  
   126  	fmt.Println()
   127  
   128  	return nil
   129  }
   130  
   131  // AppOpen opens an app in the default webbrowser.
   132  func AppOpen(appID string) error {
   133  	c, appID, err := load(appID)
   134  
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	app, err := apps.Get(c, appID)
   140  
   141  	if err != nil {
   142  		return err
   143  	}
   144  
   145  	u, err := url.Parse(app.URL)
   146  
   147  	if err != nil {
   148  		return err
   149  	}
   150  
   151  	u.Scheme = "http"
   152  
   153  	return webbrowser.Webbrowser(u.String())
   154  }
   155  
   156  // AppLogs returns the logs from an app.
   157  func AppLogs(appID string, lines int) error {
   158  	c, appID, err := load(appID)
   159  
   160  	if err != nil {
   161  		return err
   162  	}
   163  
   164  	logs, err := apps.Logs(c, appID, lines)
   165  
   166  	if err != nil {
   167  		return err
   168  	}
   169  
   170  	return printLogs(logs)
   171  }
   172  
   173  // printLogs prints each log line with a color matched to its category.
   174  func printLogs(logs string) error {
   175  	for _, log := range strings.Split(strings.Trim(logs, `\n`), `\n`) {
   176  		category := "unknown"
   177  		parts := strings.Split(strings.Split(log, ": ")[0], " ")
   178  		if len(parts) >= 2 {
   179  			category = parts[1]
   180  		}
   181  		colorVars := map[string]string{
   182  			"Color": chooseColor(category),
   183  			"Log":   log,
   184  		}
   185  		fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
   186  	}
   187  
   188  	return nil
   189  }
   190  
   191  // AppRun runs a one time command in the app.
   192  func AppRun(appID, command string) error {
   193  	c, appID, err := load(appID)
   194  
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	fmt.Printf("Running '%s'...\n", command)
   200  
   201  	out, err := apps.Run(c, appID, command)
   202  
   203  	if err != nil {
   204  		return err
   205  	}
   206  
   207  	fmt.Print(out.Output)
   208  	os.Exit(out.ReturnCode)
   209  	return nil
   210  }
   211  
   212  // AppDestroy destroys an app.
   213  func AppDestroy(appID, confirm string) error {
   214  	gitSession := false
   215  
   216  	c, err := client.New()
   217  
   218  	if err != nil {
   219  		return err
   220  	}
   221  
   222  	if appID == "" {
   223  		appID, err = git.DetectAppName(c.ControllerURL.Host)
   224  
   225  		if err != nil {
   226  			return err
   227  		}
   228  
   229  		gitSession = true
   230  	}
   231  
   232  	if confirm == "" {
   233  		fmt.Printf(` !    WARNING: Potentially Destructive Action
   234   !    This command will destroy the application: %s
   235   !    To proceed, type "%s" or re-run this command with --confirm=%s
   236  
   237  > `, appID, appID, appID)
   238  
   239  		fmt.Scanln(&confirm)
   240  	}
   241  
   242  	if confirm != appID {
   243  		return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm)
   244  	}
   245  
   246  	startTime := time.Now()
   247  	fmt.Printf("Destroying %s...\n", appID)
   248  
   249  	if err = apps.Delete(c, appID); err != nil {
   250  		return err
   251  	}
   252  
   253  	fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
   254  
   255  	if gitSession {
   256  		return git.DeleteRemote(appID)
   257  	}
   258  
   259  	return nil
   260  }
   261  
   262  // AppTransfer transfers app ownership to another user.
   263  func AppTransfer(appID, username string) error {
   264  	c, appID, err := load(appID)
   265  
   266  	if err != nil {
   267  		return err
   268  	}
   269  
   270  	fmt.Printf("Transferring %s to %s... ", appID, username)
   271  
   272  	err = apps.Transfer(c, appID, username)
   273  
   274  	if err != nil {
   275  		return err
   276  	}
   277  
   278  	fmt.Println("done")
   279  
   280  	return nil
   281  }