github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/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  		return git.CreateRemote(c.ControllerURL.Host, remote, app.ID)
    50  	}
    51  
    52  	fmt.Println("remote available at", git.RemoteURL(c.ControllerURL.Host, app.ID))
    53  
    54  	return nil
    55  }
    56  
    57  // AppsList lists apps on the Deis controller.
    58  func AppsList(results int) error {
    59  	c, err := client.New()
    60  
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	if results == defaultLimit {
    66  		results = c.ResponseLimit
    67  	}
    68  
    69  	apps, count, err := apps.List(c, results)
    70  
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	fmt.Printf("=== Apps%s", limitCount(len(apps), count))
    76  
    77  	for _, app := range apps {
    78  		fmt.Println(app.ID)
    79  	}
    80  	return nil
    81  }
    82  
    83  // AppInfo prints info about app.
    84  func AppInfo(appID string) error {
    85  	c, appID, err := load(appID)
    86  
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	app, err := apps.Get(c, appID)
    92  
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	fmt.Printf("=== %s Application\n", app.ID)
    98  	fmt.Println("updated: ", app.Updated)
    99  	fmt.Println("uuid:    ", app.UUID)
   100  	fmt.Println("created: ", app.Created)
   101  	fmt.Println("url:     ", app.URL)
   102  	fmt.Println("owner:   ", app.Owner)
   103  	fmt.Println("id:      ", app.ID)
   104  
   105  	fmt.Println()
   106  	// print the app processes
   107  	if err = PsList(app.ID, defaultLimit); err != nil {
   108  		return err
   109  	}
   110  
   111  	fmt.Println()
   112  	// print the app domains
   113  	if err = DomainsList(app.ID, defaultLimit); err != nil {
   114  		return err
   115  	}
   116  
   117  	fmt.Println()
   118  
   119  	return nil
   120  }
   121  
   122  // AppOpen opens an app in the default webbrowser.
   123  func AppOpen(appID string) error {
   124  	c, appID, err := load(appID)
   125  
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	app, err := apps.Get(c, appID)
   131  
   132  	if err != nil {
   133  		return err
   134  	}
   135  
   136  	u, err := url.Parse(app.URL)
   137  
   138  	if err != nil {
   139  		return err
   140  	}
   141  
   142  	u.Scheme = "http"
   143  
   144  	return webbrowser.Webbrowser(u.String())
   145  }
   146  
   147  // AppLogs returns the logs from an app.
   148  func AppLogs(appID string, lines int) error {
   149  	c, appID, err := load(appID)
   150  
   151  	if err != nil {
   152  		return err
   153  	}
   154  
   155  	logs, err := apps.Logs(c, appID, lines)
   156  
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	for _, log := range strings.Split(strings.Trim(logs, `\n`), `\n`) {
   162  		catagory := strings.Split(strings.Split(log, ": ")[0], " ")[1]
   163  		colorVars := map[string]string{
   164  			"Color": chooseColor(catagory),
   165  			"Log":   log,
   166  		}
   167  		fmt.Println(prettyprint.ColorizeVars("{{.V.Color}}{{.V.Log}}{{.C.Default}}", colorVars))
   168  	}
   169  
   170  	return nil
   171  }
   172  
   173  // AppRun runs a one time command in the app.
   174  func AppRun(appID, command string) error {
   175  	c, appID, err := load(appID)
   176  
   177  	if err != nil {
   178  		return err
   179  	}
   180  
   181  	fmt.Printf("Running '%s'...\n", command)
   182  
   183  	out, err := apps.Run(c, appID, command)
   184  
   185  	if err != nil {
   186  		return err
   187  	}
   188  
   189  	fmt.Print(out.Output)
   190  	os.Exit(out.ReturnCode)
   191  	return nil
   192  }
   193  
   194  // AppDestroy destroys an app.
   195  func AppDestroy(appID, confirm string) error {
   196  	gitSession := false
   197  
   198  	c, err := client.New()
   199  
   200  	if err != nil {
   201  		return err
   202  	}
   203  
   204  	if appID == "" {
   205  		appID, err = git.DetectAppName(c.ControllerURL.Host)
   206  
   207  		if err != nil {
   208  			return err
   209  		}
   210  
   211  		gitSession = true
   212  	}
   213  
   214  	if confirm == "" {
   215  		fmt.Printf(` !    WARNING: Potentially Destructive Action
   216   !    This command will destroy the application: %s
   217   !    To proceed, type "%s" or re-run this command with --confirm=%s
   218  
   219  > `, appID, appID, appID)
   220  
   221  		fmt.Scanln(&confirm)
   222  	}
   223  
   224  	if confirm != appID {
   225  		return fmt.Errorf("App %s does not match confirm %s, aborting.", appID, confirm)
   226  	}
   227  
   228  	startTime := time.Now()
   229  	fmt.Printf("Destroying %s...\n", appID)
   230  
   231  	if err = apps.Delete(c, appID); err != nil {
   232  		return err
   233  	}
   234  
   235  	fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
   236  
   237  	if gitSession {
   238  		return git.DeleteRemote(appID)
   239  	}
   240  
   241  	return nil
   242  }