func (w *TemplateWriterBase) WriteTemplate(templateName string, filename string, context interface{}) error {
	tmpl, err := template.ParseFiles(filepath.Join(w.TemplateDir, templateName))
	if err != nil {
		return err
	}

	destFullPath := filepath.Join(w.OutputDir, filename)
	if !w.shouldOverwriteExistingFile(destFullPath) {
		newName := filename + ".old"
		err = os.Rename(destFullPath, filepath.Join(w.OutputDir, newName))
		if err != nil {
			return err
		}
		common.PrintlnL2("Renaming %s to %s...", filename, newName)
	}

	destFile, err := os.Create(destFullPath)
	if err != nil {
		return err
	}
	defer func() {
		if err := destFile.Close(); err != nil {
			common.PrintlnError("Cannot close file %s due to: %s", filename, err.Error())
		}
	}()

	common.PrintlnL2("Writing %s...", filename)
	err = tmpl.Execute(destFile, context)
	if err != nil {
		return err
	}

	return w.prependToFile(destFullPath, templateHeader)
}
Ejemplo n.º 2
0
func (a *Analyzer) GuessPackages() *common.Lister {
	packages := common.NewLister()

	if runsExpress, _ := common.GetDependencyVersion(a.PackageJSON, "express"); runsExpress {
		common.PrintlnL2("Found Express")
	}
	if hasScript, script := common.GetScriptsStart(a.PackageJSON); hasScript {
		common.PrintlnL2("Found Script: %s", script)
	}

	return packages
}
Ejemplo n.º 3
0
func (a *AnalyzerBase) ConfirmDatabases(foundDbs *common.Lister) *common.Lister {
	var dbs common.Lister
	for _, db := range foundDbs.Items {
		if !a.ShouldPrompt {
			common.PrintlnL2("Found %s", db)
		}
		if common.AskYesOrNo(fmt.Sprintf("Found %s, confirm?", db), true, a.ShouldPrompt) {
			dbs.Add(db)
		}
	}

	var message string
	var defaultValue bool
	if len(foundDbs.Items) > 0 {
		message = "Add any other databases?"
		defaultValue = false
	} else {
		message = "No databases found. Add manually?"
		defaultValue = true
	}

	if common.AskYesOrNo(message, defaultValue, a.ShouldPrompt) && a.ShouldPrompt {
		common.PrintlnL1("  See http://help.cloud66.com/building-your-stack/docker-service-configuration#database-configs for complete list of possible values")
		common.PrintlnL1("  Example: 'mysql elasticsearch' ")
		common.PrintL1("> ")

		reader := bufio.NewReader(os.Stdin)
		otherDbs, err := reader.ReadString('\n')
		if err == nil {
			dbs.Add(strings.Fields(otherDbs)...)
		}
	}
	return &dbs
}
Ejemplo n.º 4
0
func fetch(url string, mod *time.Time) (io.ReadCloser, error) {
	common.PrintlnL2("Downloading from %s", url)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	if mod != nil {
		req.Header.Add("If-Modified-Since", mod.Format(http.TimeFormat))
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return nil, err
	}
	if mod != nil && resp.StatusCode == 304 {
		return nil, errors.New("item moved")
	}
	if resp.StatusCode != 200 {
		err := fmt.Errorf("bad http status from %s: %v", url, resp.Status)
		return nil, err
	}
	if s := resp.Header.Get("Last-Modified"); mod != nil && s != "" {
		t, err := time.Parse(http.TimeFormat, s)
		if err == nil {
			*mod = t
		}
	}
	return resp.Body, nil
}
Ejemplo n.º 5
0
func (a *Analyzer) GuessPackages() *common.Lister {
	packages := common.NewLister()
	common.PrintlnL2("Analyzing dependencies")
	if hasRmagick, _ := common.GetGemVersion(a.Gemfile, "rmagick", "refile-mini_magick", "mini_magick"); hasRmagick {
		packages.Add("imagemagick", "libmagickwand-dev")
		common.PrintlnL2("Found Image Magick")
	}

	if hasSqlite, _ := common.GetGemVersion(a.Gemfile, "sqlite"); hasSqlite {
		packages.Add("libsqlite3-dev")
		common.PrintlnL2("Found sqlite")
	}

	if hasMemcache, _ := common.GetGemVersion(a.Gemfile, "dalli"); hasMemcache {
		packages.Add("memcached")
		common.PrintlnL2("Found Memcache")
	}
	return packages
}
Ejemplo n.º 6
0
func (b *AnalyzerBase) DetectWebServer(a Analyzer, command string, servers []WebServer) (hasFound bool, webserver WebServer) {
	for _, server := range servers {
		for _, name := range server.Names() {
			if a.HasPackage(name) || strings.HasPrefix(command, name) {
				common.PrintlnL2("Found %s", name)
				return true, server
			}
		}
	}
	return false, nil
}
Ejemplo n.º 7
0
func (a *AnalyzerBase) analyzeProcfile() ([]*common.Service, error) {
	services := []*common.Service{}
	procfilePath := filepath.Join(a.RootDir, "Procfile")
	if !common.FileExists(procfilePath) {
		a.Messages.Add("No Procfile was detected. It is strongly advised to add one in order to specify the commands to run your services.")
		return services, nil
	}

	common.PrintlnL2("Parsing Procfile")
	procs, err := common.ParseProcfile(procfilePath)
	if err != nil {
		return nil, err
	}

	for _, proc := range procs {
		common.PrintlnL2("Found Procfile item %s", proc.Name)
		services = append(services, &common.Service{Name: proc.Name, Command: proc.Command})
	}
	return services, nil
}
Ejemplo n.º 8
0
func (a *Analyzer) FindDatabases() *common.Lister {
	dbs := common.NewLister()
	if hasMysql, _ := common.GetGemVersion(a.Gemfile, "mysql2"); hasMysql {
		dbs.Add("mysql")
	}

	if hasPg, _ := common.GetGemVersion(a.Gemfile, "pg"); hasPg {
		dbs.Add("postgresql")
	}

	if hasRedis, _ := common.GetGemVersion(a.Gemfile, "redis", "redis-rails"); hasRedis {
		dbs.Add("redis")
	}

	if hasMongoDB, _ := common.GetGemVersion(a.Gemfile, "mongo", "mongo_mapper", "dm-mongo-adapter", "mongoid"); hasMongoDB {
		dbs.Add("mongodb")
	}

	if hasElasticsearch, _ := common.GetGemVersion(a.Gemfile, "elasticsearch", "tire", "flex", "chewy"); hasElasticsearch {
		dbs.Add("elasticsearch")
	}

	if hasDatabaseYaml := common.FileExists("config/database.yml"); hasDatabaseYaml {
		common.PrintlnL2("Found config/database.yml")
		a.Messages.Add(
			fmt.Sprintf("%s %s-> %s",
				"database.yml: Make sure you are using environment variables.",
				common.MsgReset, "http://help.cloud66.com/deployment/environment-variables"))
	}

	if hasMongoIdYaml := common.FileExists("config/mongoid.yml"); hasMongoIdYaml {
		common.PrintlnL2("Found config/mongoid.yml")
		a.Messages.Add(
			fmt.Sprintf("%s %s-> %s",
				"mongoid.yml: Make sure you are using environment variables.",
				common.MsgReset, "http://help.cloud66.com/deployment/environment-variables"))
	}
	return dbs
}
Ejemplo n.º 9
0
// downloading templates from github and putting them into homedir
func getTempaltes(tempDir string) error {
	common.PrintlnL0("Checking templates in %s", tempDir)

	var tv templateDefinition
	err := fetchJSON(strings.Replace(templatePath, "{{.branch}}", flagBranch, -1), nil, &tv)
	if err != nil {
		return err
	}

	// is there a local copy?
	if _, err := os.Stat(filepath.Join(tempDir, "templates.json")); os.IsNotExist(err) {
		// no file. downloading
		common.PrintlnL1("No local templates found. Downloading now.")
		err := os.MkdirAll(tempDir, 0777)
		if err != nil {
			return err
		}

		err = downloadTemplates(tempDir, tv)
		if err != nil {
			return err
		}
	}

	// load the local json
	templatesLocal, err := ioutil.ReadFile(filepath.Join(tempDir, "templates.json"))
	if err != nil {
		return err
	}
	var localTv templateDefinition
	err = json.Unmarshal(templatesLocal, &localTv)
	if err != nil {
		return err
	}

	// compare
	if localTv.Version != tv.Version {
		common.PrintlnL2("Newer templates found. Downloading them now")
		// they are different, we need to download the new ones
		err = downloadTemplates(tempDir, tv)
		if err != nil {
			return err
		}
	} else {
		common.PrintlnL1("Local templates are up to date")
	}

	return nil
}