github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/actor/pushaction/application_config.go (about)

     1  package pushaction
     2  
     3  import (
     4  	"os"
     5  
     6  	"code.cloudfoundry.org/cli/actor/pushaction/manifest"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	log "github.com/sirupsen/logrus"
     9  )
    10  
    11  type ApplicationConfig struct {
    12  	CurrentApplication v2action.Application
    13  	DesiredApplication v2action.Application
    14  
    15  	CurrentRoutes []v2action.Route
    16  	DesiredRoutes []v2action.Route
    17  
    18  	AllResources []v2action.Resource
    19  	Archive      bool
    20  	Path         string
    21  
    22  	TargetedSpaceGUID string
    23  }
    24  
    25  func (config ApplicationConfig) CreatingApplication() bool {
    26  	return config.CurrentApplication.GUID == ""
    27  }
    28  
    29  func (config ApplicationConfig) UpdatingApplication() bool {
    30  	return !config.CreatingApplication()
    31  }
    32  
    33  func (actor Actor) ConvertToApplicationConfigs(orgGUID string, spaceGUID string, apps []manifest.Application) ([]ApplicationConfig, Warnings, error) {
    34  	var configs []ApplicationConfig
    35  	var warnings Warnings
    36  
    37  	log.Infof("iterating through %d app configuration(s)", len(apps))
    38  	for _, app := range apps {
    39  		config := ApplicationConfig{
    40  			TargetedSpaceGUID: spaceGUID,
    41  			Path:              app.Path,
    42  		}
    43  
    44  		log.Infoln("searching for app", app.Name)
    45  		appExists, foundApp, v2Warnings, err := actor.FindOrReturnPartialApp(app.Name, spaceGUID)
    46  		warnings = append(warnings, v2Warnings...)
    47  		if err != nil {
    48  			log.Errorln("app lookup:", err)
    49  			return nil, warnings, err
    50  		}
    51  
    52  		if appExists {
    53  			var configWarnings v2action.Warnings
    54  			config, configWarnings, err = actor.configureExistingApp(config, app, foundApp)
    55  			warnings = append(warnings, configWarnings...)
    56  			if err != nil {
    57  				log.Errorln("configuring existing app:", err)
    58  				return nil, warnings, err
    59  			}
    60  		} else {
    61  			log.Debug("using empty app as base")
    62  			config.DesiredApplication.Name = app.Name
    63  			config.DesiredApplication.SpaceGUID = spaceGUID
    64  		}
    65  
    66  		defaultRoute, routeWarnings, err := actor.GetRouteWithDefaultDomain(app.Name, orgGUID, spaceGUID, config.CurrentRoutes)
    67  		warnings = append(warnings, routeWarnings...)
    68  		if err != nil {
    69  			log.Errorln("getting default route:", err)
    70  			return nil, warnings, err
    71  		}
    72  
    73  		// TODO: when working with all of routes, append to current route
    74  		config.DesiredRoutes = []v2action.Route{defaultRoute}
    75  
    76  		config, err = actor.configureResources(config, app)
    77  		if err != nil {
    78  			log.Errorln("configuring resources", err)
    79  			return nil, warnings, err
    80  		}
    81  
    82  		configs = append(configs, config)
    83  	}
    84  
    85  	return configs, warnings, nil
    86  }
    87  
    88  func (actor Actor) FindOrReturnPartialApp(appName string, spaceGUID string) (bool, v2action.Application, v2action.Warnings, error) {
    89  	foundApp, v2Warnings, err := actor.V2Actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    90  	if _, ok := err.(v2action.ApplicationNotFoundError); ok {
    91  		log.Warnf("unable to find app %s in current space (GUID: %s)", appName, spaceGUID)
    92  		return false, v2action.Application{}, v2Warnings, nil
    93  	}
    94  	return true, foundApp, v2Warnings, err
    95  }
    96  
    97  func (actor Actor) configureExistingApp(config ApplicationConfig, app manifest.Application, foundApp v2action.Application) (ApplicationConfig, v2action.Warnings, error) {
    98  	log.Debugf("found app: %#v", foundApp)
    99  	config.CurrentApplication = foundApp
   100  	config.DesiredApplication = foundApp
   101  
   102  	log.Info("looking up application routes")
   103  	routes, warnings, err := actor.V2Actor.GetApplicationRoutes(foundApp.GUID)
   104  	if err != nil {
   105  		log.Errorln("existing routes lookup:", err)
   106  		return config, warnings, err
   107  	}
   108  	config.CurrentRoutes = routes
   109  	return config, warnings, nil
   110  }
   111  
   112  func (actor Actor) configureResources(config ApplicationConfig, app manifest.Application) (ApplicationConfig, error) {
   113  	if app.DockerImage == "" {
   114  		info, err := os.Stat(app.Path)
   115  		if err != nil {
   116  			return config, err
   117  		}
   118  
   119  		var resources []v2action.Resource
   120  		if info.IsDir() {
   121  			log.WithField("path_to_resources", app.Path).Info("determine directory resources to zip")
   122  			resources, err = actor.V2Actor.GatherDirectoryResources(app.Path)
   123  		} else {
   124  			config.Archive = true
   125  			log.WithField("path_to_resources", app.Path).Info("determine archive resources to zip")
   126  			resources, err = actor.V2Actor.GatherArchiveResources(app.Path)
   127  		}
   128  		if err != nil {
   129  			return config, err
   130  		}
   131  		config.AllResources = resources
   132  		log.WithField("number_of_files", len(resources)).Debug("completed file scan")
   133  	} else {
   134  		config.DesiredApplication.DockerImage = app.DockerImage
   135  	}
   136  
   137  	return config, nil
   138  }