Beispiel #1
0
func CmdList(databaseName string, page, pageSize int, id IDb, is services.IServices) error {
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	jobs, err := id.List(page, pageSize, service)
	if err != nil {
		return err
	}
	sort.Sort(SortedJobs(*jobs))
	for _, job := range *jobs {
		logrus.Printf("%s %s (status = %s)", job.ID, job.CreatedAt, job.Status)
	}
	if len(*jobs) == pageSize && page == 1 {
		logrus.Println("(for older backups, try with --page 2 or adjust --page-size)")
	}
	if len(*jobs) == 0 && page == 1 {
		logrus.Println("No backups created yet for this service.")
	} else if len(*jobs) == 0 {
		logrus.Println("No backups found with the given parameters.")
	}
	return nil
}
Beispiel #2
0
func CmdDeploy(svcName, target string, iw IWorker, is services.IServices, ij jobs.IJobs) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName)
	}
	logrus.Printf("Initiating a worker for service %s (procfile target = \"%s\")", svcName, target)
	workers, err := iw.Retrieve(service.ID)
	if err != nil {
		return err
	}
	if _, ok := workers.Workers[target]; ok {
		logrus.Printf("Worker with target %s for service %s is already running, deploying another worker", target, svcName)
	}
	workers.Workers[target]++
	err = iw.Update(service.ID, workers)
	if err != nil {
		return err
	}
	err = ij.DeployTarget(target, service.ID)
	if err != nil {
		return err
	}
	logrus.Printf("Successfully deployed a worker for service %s with target %s", svcName, target)
	return nil
}
Beispiel #3
0
func CmdShow(name string, is ISites, iservices services.IServices) error {
	serviceProxy, err := iservices.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	sites, err := is.List(serviceProxy.ID)
	if err != nil {
		return err
	}
	var site *models.Site
	for _, s := range *sites {
		if s.Name == name {
			site = &s
			break
		}
	}
	if site == nil {
		return fmt.Errorf("Could not find a site with the label \"%s\". You can list sites with the \"catalyze sites list\" command.", name)
	}
	site, err = is.Retrieve(site.ID, serviceProxy.ID)
	if err != nil {
		return err
	}
	table, err := simpletable.New(simpletable.HeadersForType(models.Site{}), []models.Site{*site})
	if err != nil {
		return err
	}
	table.Write(logrus.StandardLogger().Out)
	return nil
}
Beispiel #4
0
func CmdList(is ISites, iservices services.IServices) error {
	serviceProxy, err := iservices.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	sites, err := is.List(serviceProxy.ID)
	if err != nil {
		return err
	}
	if sites == nil || len(*sites) == 0 {
		logrus.Println("No sites found")
		return nil
	}
	svcs, err := iservices.List()
	svcMap := map[string]string{}
	for _, s := range *svcs {
		svcMap[s.ID] = s.Label
	}

	data := [][]string{{"NAME", "CERT", "UPSTREAM SERVICE"}}
	for _, s := range *sites {
		data = append(data, []string{s.Name, s.Cert, svcMap[s.UpstreamService]})
	}

	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
	table.SetBorder(false)
	table.SetRowLine(false)
	table.SetCenterSeparator("")
	table.SetColumnSeparator("")
	table.SetRowSeparator("")
	table.AppendBulk(data)
	table.Render()
	return nil
}
Beispiel #5
0
func CmdAdd(name, keyPath, svcName string, id IDeployKeys, is services.IServices) error {
	if strings.ContainsAny(name, config.InvalidChars) {
		return fmt.Errorf("Invalid SSH key name. Names must not contain the following characters: %s", config.InvalidChars)
	}
	if _, err := os.Stat(keyPath); os.IsNotExist(err) {
		return fmt.Errorf("A file does not exist at path '%s'", keyPath)
	}
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	if service.Type != "code" {
		return fmt.Errorf("You can only add deploy keys to code services, not %s services", service.Type)
	}
	key, err := ioutil.ReadFile(keyPath)
	if err != nil {
		return err
	}
	k, err := id.ParsePublicKey(key)
	if err != nil {
		return err
	}
	key = ssh.MarshalAuthorizedKey(k)
	return id.Add(name, "ssh", string(key), service.ID)
}
Beispiel #6
0
func CmdBackup(databaseName string, skipPoll bool, id IDb, is services.IServices, ij jobs.IJobs) error {
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	job, err := id.Backup(service)
	if err != nil {
		return err
	}
	logrus.Printf("Backup started (job ID = %s)", job.ID)
	if !skipPoll {
		// all because logrus treats print, println, and printf the same
		logrus.StandardLogger().Out.Write([]byte("Polling until backup finishes."))
		status, err := ij.PollTillFinished(job.ID, service.ID)
		if err != nil {
			return err
		}
		job.Status = status
		logrus.Printf("\nEnded in status '%s'", job.Status)
		err = id.DumpLogs("backup", job, service)
		if err != nil {
			return err
		}
		if job.Status != "finished" {
			return fmt.Errorf("Job finished with invalid status %s", job.Status)
		}
	}
	logrus.Printf("You can download your backup with the \"catalyze db download %s %s ./output_file_path\" command", databaseName, job.ID)
	return nil
}
Beispiel #7
0
func CmdSet(svcName, defaultSvcID string, variables []string, iv IVars, is services.IServices) error {
	if svcName != "" {
		service, err := is.RetrieveByLabel(svcName)
		if err != nil {
			return err
		}
		if service == nil {
			return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
		}
		defaultSvcID = service.ID
	}
	envVarsMap := make(map[string]string, len(variables))
	r := regexp.MustCompile("^[a-zA-Z_]+[a-zA-Z0-9_]*$")
	for _, envVar := range variables {
		pieces := strings.SplitN(envVar, "=", 2)
		if len(pieces) != 2 {
			return fmt.Errorf("Invalid variable format. Expected <key>=<value> but got %s", envVar)
		}
		name, value := pieces[0], pieces[1]
		if !r.MatchString(name) {
			return fmt.Errorf("Invalid environment variable name '%s'. Environment variable names must only contain letters, numbers, and underscores and must not start with a number.", name)
		}
		envVarsMap[name] = value
	}

	err := iv.Set(defaultSvcID, envVarsMap)
	if err != nil {
		return err
	}
	// TODO add in the service label in the redeploy example once we take in the service label in
	// this command
	logrus.Println("Set. For these environment variables to take effect, you will need to redeploy your service with \"catalyze redeploy\"")
	return nil
}
Beispiel #8
0
func CmdRollback(svcName, releaseName string, ij jobs.IJobs, irs releases.IReleases, is services.IServices) error {
	if strings.ContainsAny(releaseName, config.InvalidChars) {
		return fmt.Errorf("Invalid release name. Names must not contain the following characters: %s", config.InvalidChars)
	}
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	logrus.Printf("Rolling back %s to %s", svcName, releaseName)
	release, err := irs.Retrieve(releaseName, service.ID)
	if err != nil {
		return err
	}
	if release == nil {
		return fmt.Errorf("Could not find a release with the name \"%s\". You can list releases for this code service with the \"catalyze releases list %s\" command.", releaseName, svcName)
	}
	err = ij.DeployRelease(releaseName, service.ID)
	if err != nil {
		return err
	}
	logrus.Println("Rollback successful! Check the status with \"catalyze status\" and your logging dashboard for updates.")
	return nil
}
Beispiel #9
0
// CmdDomain prints out the namespace plus domain of the given environment
func CmdDomain(envID string, ie environments.IEnvironments, is services.IServices, isites sites.ISites) error {
	env, err := ie.Retrieve(envID)
	if err != nil {
		return err
	}
	serviceProxy, err := is.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	sites, err := isites.List(serviceProxy.ID)
	if err != nil {
		return err
	}
	domain := ""
	for _, site := range *sites {
		if strings.HasPrefix(site.Name, env.Namespace) {
			domain = site.Name
		}
	}
	if domain == "" {
		return errors.New("Could not determine the temporary domain name of your environment")
	}
	logrus.Println(domain)
	return nil
}
Beispiel #10
0
func CmdRm(svcName, target string, iw IWorker, is services.IServices, ip prompts.IPrompts, ij jobs.IJobs) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName)
	}
	err = ip.YesNo(fmt.Sprintf("Removing the worker target %s for service %s will automatically stop all existing worker jobs with that target, would you like to proceed? (y/n) ", target, svcName))
	if err != nil {
		return err
	}
	jobs, err := ij.RetrieveByTarget(service.ID, target, 1, 1000)
	if err != nil {
		return err
	}
	for _, j := range *jobs {
		err = ij.Delete(j.ID, service.ID)
		if err != nil {
			return err
		}
	}
	workers, err := iw.Retrieve(service.ID)
	if err != nil {
		return err
	}
	delete(workers.Workers, target)
	err = iw.Update(service.ID, workers)
	if err != nil {
		return err
	}
	logrus.Printf("Successfully removed all workers with target %s for service %s", target, svcName)
	return nil
}
Beispiel #11
0
func CmdDownload(databaseName, backupID, filePath string, force bool, id IDb, ip prompts.IPrompts, is services.IServices) error {
	err := ip.PHI()
	if err != nil {
		return err
	}
	if !force {
		if _, err := os.Stat(filePath); err == nil {
			return fmt.Errorf("File already exists at path '%s'. Specify `--force` to overwrite", filePath)
		}
	} else {
		os.Remove(filePath)
	}
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	err = id.Download(backupID, filePath, service)
	if err != nil {
		return err
	}
	logrus.Printf("%s backup downloaded successfully to %s", databaseName, filePath)
	logrus.Printf("You can also view logs for this backup with the \"catalyze db logs %s %s\" command", databaseName, backupID)
	return nil
}
Beispiel #12
0
func CmdRm(name string, is ISites, iservices services.IServices) error {
	serviceProxy, err := iservices.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	sites, err := is.List(serviceProxy.ID)
	if err != nil {
		return err
	}
	var site *models.Site
	for _, s := range *sites {
		if s.Name == name {
			site = &s
			break
		}
	}
	if site == nil {
		return fmt.Errorf("Could not find a site with the label \"%s\". You can list sites with the \"catalyze sites list\" command.", name)
	}
	err = is.Rm(site.ID, serviceProxy.ID)
	if err != nil {
		return err
	}
	logrus.Println("Site removed")
	logrus.Println("To make your changes go live, you must redeploy your service proxy with the \"catalyze redeploy service_proxy\" command")
	return nil
}
Beispiel #13
0
func CmdList(svcName string, id IDeployKeys, is services.IServices) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	if service.Type != "code" {
		return fmt.Errorf("You can only list deploy keys for code services, not %s services", service.Type)
	}

	keys, err := id.List(service.ID)
	if err != nil {
		return err
	}
	if keys == nil || len(*keys) == 0 {
		logrus.Println("No deploy-keys found")
		return nil
	}

	invalidKeys := map[string]string{}

	data := [][]string{{"NAME", "TYPE", "FINGERPRINT"}}
	for _, key := range *keys {
		if key.Type != "ssh" {
			continue
		}
		s, err := id.ParsePublicKey([]byte(key.Key))
		if err != nil {
			invalidKeys[key.Name] = err.Error()
			continue
		}
		h := sha256.New()
		h.Write(s.Marshal())
		fingerprint := base64.StdEncoding.EncodeToString(h.Sum(nil))
		data = append(data, []string{key.Name, key.Type, fmt.Sprintf("SHA256:%s", strings.TrimRight(fingerprint, "="))})
	}

	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
	table.SetBorder(false)
	table.SetRowLine(false)
	table.SetCenterSeparator("")
	table.SetColumnSeparator("")
	table.SetRowSeparator("")
	table.AppendBulk(data)
	table.Render()

	if len(invalidKeys) > 0 {
		logrus.Println("\nInvalid Keys:")
		for keyName, reason := range invalidKeys {
			logrus.Printf("%s: %s", keyName, reason)
		}
	}
	return nil
}
Beispiel #14
0
func CmdImport(databaseName, filePath, mongoCollection, mongoDatabase string, id IDb, is services.IServices, ij jobs.IJobs) error {
	if _, err := os.Stat(filePath); os.IsNotExist(err) {
		return fmt.Errorf("A file does not exist at path '%s'", filePath)
	}
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	logrus.Printf("Backing up \"%s\" before performing the import", databaseName)
	job, err := id.Backup(service)
	if err != nil {
		return err
	}
	logrus.Printf("Backup started (job ID = %s)", job.ID)

	// all because logrus treats print, println, and printf the same
	logrus.StandardLogger().Out.Write([]byte("Polling until backup finishes."))
	status, err := ij.PollTillFinished(job.ID, service.ID)
	if err != nil {
		return err
	}
	job.Status = status
	logrus.Printf("\nEnded in status '%s'", job.Status)
	err = id.DumpLogs("backup", job, service)
	if err != nil {
		return err
	}
	if job.Status != "finished" {
		return fmt.Errorf("Job finished with invalid status %s", job.Status)
	}
	logrus.Printf("Importing '%s' into %s (ID = %s)", filePath, databaseName, service.ID)
	job, err = id.Import(filePath, mongoCollection, mongoDatabase, service)
	if err != nil {
		return err
	}
	// all because logrus treats print, println, and printf the same
	logrus.StandardLogger().Out.Write([]byte(fmt.Sprintf("Processing import (job ID = %s).", job.ID)))

	status, err = ij.PollTillFinished(job.ID, service.ID)
	if err != nil {
		return err
	}
	job.Status = status
	logrus.Printf("\nImport complete (end status = '%s')", job.Status)
	err = id.DumpLogs("restore", job, service)
	if err != nil {
		return err
	}
	if job.Status != "finished" {
		return fmt.Errorf("Finished with invalid status %s", job.Status)
	}
	return nil
}
Beispiel #15
0
func CmdConsole(svcName, command string, ic IConsole, is services.IServices) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.\n", svcName)
	}
	return ic.Open(command, service)
}
Beispiel #16
0
func CmdStatus(envID string, is IStatus, ie environments.IEnvironments, iservices services.IServices) error {
	env, err := ie.Retrieve(envID)
	if err != nil {
		return err
	}
	svcs, err := iservices.ListByEnvID(env.ID, env.Pod)
	if err != nil {
		return err
	}
	return is.Status(env, svcs)
}
Beispiel #17
0
func CmdList(svcName string, iw IWorker, is services.IServices, ij jobs.IJobs) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName)
	}
	workers, err := iw.Retrieve(service.ID)
	if err != nil {
		return err
	}

	jobs, err := ij.RetrieveByType(service.ID, "worker", 1, 1000)
	if err != nil {
		return err
	}
	type workerJob struct {
		scale   int
		running int
	}
	var workerJobs = map[string]*workerJob{}
	for target, scale := range workers.Workers {
		workerJobs[target] = &workerJob{scale, 0}
	}
	if len(workerJobs) == 0 {
		logrus.Printf("No workers found for service %s", svcName)
		return nil
	}
	for _, j := range *jobs {
		if _, ok := workerJobs[j.Target]; !ok {
			workerJobs[j.Target] = &workerJob{0, 0}
		}
		if j.Status == "running" {
			workerJobs[j.Target].running = 1
		}
	}

	data := [][]string{{"TARGET", "SCALE", "RUNNING JOBS"}}
	for target, wj := range workerJobs {
		data = append(data, []string{target, fmt.Sprintf("%d", wj.scale), fmt.Sprintf("%d", wj.running)})
	}

	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
	table.SetBorder(false)
	table.SetRowLine(false)
	table.SetCenterSeparator("")
	table.SetColumnSeparator("")
	table.SetRowSeparator("")
	table.AppendBulk(data)
	table.Render()
	return nil
}
Beispiel #18
0
func CmdWorker(svcName, defaultSvcID, target string, iw IWorker, is services.IServices, ij jobs.IJobs) error {
	if svcName != "" {
		service, err := is.RetrieveByLabel(svcName)
		if err != nil {
			return err
		}
		if service == nil {
			return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services list\" command.", svcName)
		}
		svcName = service.Label
	}
	return CmdDeploy(svcName, target, iw, is, ij)
}
Beispiel #19
0
func CmdShow(svcName string, is services.IServices) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	if service.Source == "" {
		return fmt.Errorf("No git remote found for the \"%s\" service.", svcName)
	}
	logrus.Println(service.Source)
	return nil
}
Beispiel #20
0
func CmdLogs(databaseName, backupID string, id IDb, is services.IServices, ij jobs.IJobs) error {
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	job, err := ij.Retrieve(backupID, service.ID, false)
	if err != nil {
		return err
	}
	return id.DumpLogs("backup", job, service)
}
Beispiel #21
0
func CmdRm(hostname string, ic ICerts, is services.IServices) error {
	if strings.ContainsAny(hostname, config.InvalidChars) {
		return fmt.Errorf("Invalid cert hostname. Hostnames must not contain the following characters: %s", config.InvalidChars)
	}
	service, err := is.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	err = ic.Rm(hostname, service.ID)
	if err != nil {
		return err
	}
	logrus.Printf("Removed '%s'", hostname)
	return nil
}
Beispiel #22
0
func CmdExport(databaseName, filePath string, force bool, id IDb, ip prompts.IPrompts, is services.IServices, ij jobs.IJobs) error {
	err := ip.PHI()
	if err != nil {
		return err
	}
	if !force {
		if _, err := os.Stat(filePath); err == nil {
			return fmt.Errorf("File already exists at path '%s'. Specify `--force` to overwrite", filePath)
		}
	} else {
		os.Remove(filePath)
	}
	service, err := is.RetrieveByLabel(databaseName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", databaseName)
	}
	job, err := id.Backup(service)
	if err != nil {
		return err
	}
	logrus.Printf("Export started (job ID = %s)", job.ID)
	// all because logrus treats print, println, and printf the same
	logrus.StandardLogger().Out.Write([]byte("Polling until export finishes."))
	status, err := ij.PollTillFinished(job.ID, service.ID)
	if err != nil {
		return err
	}
	job.Status = status
	logrus.Printf("\nEnded in status '%s'", job.Status)
	if job.Status != "finished" {
		id.DumpLogs("backup", job, service)
		return fmt.Errorf("Job finished with invalid status %s", job.Status)
	}

	err = id.Export(filePath, job, service)
	if err != nil {
		return err
	}
	err = id.DumpLogs("backup", job, service)
	if err != nil {
		return err
	}
	logrus.Printf("%s exported successfully to %s", service.Name, filePath)
	return nil
}
Beispiel #23
0
func CmdRm(name, svcName string, id IDeployKeys, is services.IServices) error {
	if strings.ContainsAny(name, config.InvalidChars) {
		return fmt.Errorf("Invalid SSH key name. Names must not contain the following characters: %s", config.InvalidChars)
	}
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	if service.Type != "code" {
		return fmt.Errorf("You can only remove a deploy keys from code services, not %s services", service.Type)
	}
	return id.Rm(name, "ssh", service.ID)
}
Beispiel #24
0
func CmdCreate(hostname, pubKeyPath, privKeyPath string, selfSigned, resolve bool, ic ICerts, is services.IServices, issl ssl.ISSL) error {
	if strings.ContainsAny(hostname, config.InvalidChars) {
		return fmt.Errorf("Invalid cert hostname. Hostnames must not contain the following characters: %s", config.InvalidChars)
	}
	if _, err := os.Stat(pubKeyPath); os.IsNotExist(err) {
		return fmt.Errorf("A cert does not exist at path '%s'", pubKeyPath)
	}
	if _, err := os.Stat(privKeyPath); os.IsNotExist(err) {
		return fmt.Errorf("A private key does not exist at path '%s'", privKeyPath)
	}
	err := issl.Verify(pubKeyPath, privKeyPath, hostname, selfSigned)
	var pubKeyBytes []byte
	var privKeyBytes []byte
	if err != nil && !ssl.IsHostnameMismatchErr(err) {
		if ssl.IsIncompleteChainErr(err) && resolve {
			pubKeyBytes, err = issl.Resolve(pubKeyPath)
			if err != nil {
				return fmt.Errorf("Could not resolve the incomplete certificate chain. If this is a self signed certificate, please re-run this command with the '-s' option: %s", err.Error())
			}
		} else {
			return err
		}
	}
	service, err := is.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	if pubKeyBytes == nil {
		pubKeyBytes, err = ioutil.ReadFile(pubKeyPath)
		if err != nil {
			return err
		}
	}
	if privKeyBytes == nil {
		privKeyBytes, err = ioutil.ReadFile(privKeyPath)
		if err != nil {
			return err
		}
	}
	err = ic.Create(hostname, string(pubKeyBytes), string(privKeyBytes), service.ID)
	if err != nil {
		return err
	}
	logrus.Printf("Created '%s'", hostname)
	logrus.Println("To make use of your cert, you need to add a site with the \"catalyze sites create\" command")
	return nil
}
Beispiel #25
0
// CmdLogs is a way to stream logs from Kibana to your local terminal. This is
// useful because Kibana is hard to look at because it splits every single
// log statement into a separate block that spans multiple lines so it's
// not very cohesive. This is intended to be similar to the `heroku logs`
// command.
func CmdLogs(queryString string, follow bool, hours, minutes, seconds int, envID string, settings *models.Settings, il ILogs, ip prompts.IPrompts, ie environments.IEnvironments, is services.IServices, isites sites.ISites) error {
	if follow && (hours > 0 || minutes > 0 || seconds > 0) {
		logrus.Warnln("Specifying \"logs -f\" in combination with \"--hours\", \"--minutes\", or \"--seconds\" has been deprecated!")
		logrus.Warnln("Please specify either \"-f\" or use \"--hours\", \"--minutes\", \"--seconds\" but not both. Support for \"-f\" and a specified time frame will be removed in a later version.")
	}
	env, err := ie.Retrieve(envID)
	if err != nil {
		return err
	}
	serviceProxy, err := is.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	sites, err := isites.List(serviceProxy.ID)
	if err != nil {
		return err
	}
	domain := ""
	for _, site := range *sites {
		if strings.HasPrefix(site.Name, env.Namespace) {
			domain = site.Name
			break
		}
	}
	if domain == "" {
		return errors.New("Could not determine the fully qualified domain name of your environment. Please contact Catalyze Support at [email protected] with this error message to resolve this issue.")
	}
	if follow {
		if err := il.Watch(queryString, domain, settings.SessionToken); err != nil {
			logrus.Debugf("Error attempting to stream logs from logwatch: %s", err)
		} else {
			return nil
		}
	}
	from := 0
	offset := time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute + time.Duration(seconds)*time.Second
	timestamp := time.Now().In(time.UTC).Add(-1 * offset)
	from, timestamp, err = il.Output(queryString, settings.SessionToken, domain, follow, hours, minutes, seconds, from, timestamp, time.Now(), env)
	if err != nil {
		return err
	}
	if follow {
		return il.Stream(queryString, settings.SessionToken, domain, follow, hours, minutes, seconds, from, timestamp, env)
	}
	return nil
}
Beispiel #26
0
func CmdAdd(svcName, remote string, ig IGit, is services.IServices) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	if service.Source == "" {
		return fmt.Errorf("No git remote found for the \"%s\" service.", svcName)
	}
	err = ig.Add(remote, service.Source)
	if err != nil {
		return err
	}
	logrus.Printf("\"%s\" remote added.", remote)
	return nil
}
Beispiel #27
0
func CmdRm(svcName, releaseName string, ir IReleases, is services.IServices) error {
	if strings.ContainsAny(releaseName, config.InvalidChars) {
		return fmt.Errorf("Invalid release name. Names must not contain the following characters: %s", config.InvalidChars)
	}
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}
	err = ir.Rm(releaseName, service.ID)
	if err != nil {
		return err
	}
	logrus.Printf("Release '%s' has been successfully removed.", releaseName)
	return nil
}
Beispiel #28
0
func CmdRake(svcName, taskName, defaultSvcID string, ir IRake, is services.IServices) error {
	if svcName != "" {
		service, err := is.RetrieveByLabel(svcName)
		if err != nil {
			return err
		}
		if service == nil {
			return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
		}
		defaultSvcID = service.ID
	}
	logrus.Printf("Executing Rake task: %s", taskName)
	err := ir.Run(taskName, defaultSvcID)
	if err != nil {
		return err
	}
	logrus.Println("Rake task output viewable in your logging dashboard")
	return nil
}
Beispiel #29
0
func CmdList(svcName string, ir IReleases, is services.IServices) error {
	service, err := is.RetrieveByLabel(svcName)
	if err != nil {
		return err
	}
	if service == nil {
		return fmt.Errorf("Could not find a service with the label \"%s\". You can list services with the \"catalyze services\" command.", svcName)
	}

	rls, err := ir.List(service.ID)
	if err != nil {
		return err
	}

	if rls == nil || len(*rls) == 0 {
		logrus.Println("No releases found")
		return nil
	}

	sort.Sort(SortedReleases(*rls))
	const dateForm = "2006-01-02T15:04:05"
	data := [][]string{{"Release Name", "Created At", "Notes"}}
	for _, r := range *rls {
		name := r.Name
		if r.Name == service.ReleaseVersion {
			name = fmt.Sprintf("*%s", r.Name)
		}
		t, _ := time.Parse(dateForm, r.CreatedAt)
		data = append(data, []string{name, t.Local().Format(time.Stamp), r.Notes})
	}

	table := tablewriter.NewWriter(logrus.StandardLogger().Out)
	table.SetBorder(false)
	table.SetRowLine(false)
	table.SetCenterSeparator("")
	table.SetColumnSeparator("")
	table.SetRowSeparator("")
	table.AppendBulk(data)
	table.Render()

	logrus.Println("\n* denotes the current release")
	return nil
}
Beispiel #30
0
func CmdList(ic ICerts, is services.IServices) error {
	service, err := is.RetrieveByLabel("service_proxy")
	if err != nil {
		return err
	}
	certs, err := ic.List(service.ID)
	if err != nil {
		return err
	}
	if certs == nil || len(*certs) == 0 {
		logrus.Println("No certs found")
		return nil
	}
	logrus.Println("NAME")
	for _, cert := range *certs {
		logrus.Println(cert.Name)
	}
	return nil
}