func guessApplicationID(c util.Settings, e *util.Environment, name string) string { id, _ := c.String("application-id") if id == "" { id = name } return id }
// NewDeployOptions constructor func NewDeployOptions(c util.Settings, e *util.Environment) (*PipelineOptions, error) { pipelineOpts, err := NewPipelineOptions(c, e) if err != nil { return nil, err } // default to last build output if none defined target, _ := c.String("target") if target == "" { found, err := util.Exists("./.wercker/latest/output") if err == nil && found { util.RootLogger().Println("No target specified, using recent build output.") pipelineOpts.ProjectPath, _ = filepath.Abs("./.wercker/latest/output") } } // if the deploy target path does not have a wercker.yml, use the current one werckerYml, _ := c.String("wercker-yml") if werckerYml == "" { found, _ := util.Exists(filepath.Join(pipelineOpts.ProjectPath, "wercker.yml")) if !found { pipelineOpts.WerckerYml = "./wercker.yml" } } if pipelineOpts.RunID == "" { pipelineOpts.RunID = uuid.NewRandom().String() } return pipelineOpts, nil }
func guessProjectURL(c util.Settings, e *util.Environment) string { target, _ := c.String("target") if !looksLikeURL(target) { return "" } return target }
func guessGitCommit(c util.Settings, e *util.Environment) string { commit, _ := c.String("git-commit") if commit != "" { return commit } projectPath := guessProjectPath(c, e) if projectPath == "" { return "" } cwd, err := os.Getwd() if err != nil { return "" } defer os.Chdir(cwd) os.Chdir(projectPath) git, err := exec.LookPath("git") if err != nil { return "" } var out bytes.Buffer cmd := exec.Command(git, "rev-parse", "HEAD") cmd.Stdout = &out err = cmd.Run() if err != nil { return "" } return strings.Trim(out.String(), "\n") }
// Some logic to guess the application name func guessApplicationName(c util.Settings, e *util.Environment) (string, error) { applicationName, _ := c.String("application-name") if applicationName != "" { return applicationName, nil } // Otherwise, check our build target, it can be a url... target, _ := c.String("target") projectURL := "" if strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://") { projectURL = target base := path.Base(projectURL) // Special handling for github tarballs if base == "tarball" { base = path.Base(path.Dir(projectURL)) } ext := path.Ext(base) base = base[:len(ext)] return base, nil } // ... or a file path if target == "" { target = "." } stat, err := os.Stat(target) if err != nil || !stat.IsDir() { return "", fmt.Errorf("target '%s' is not a directory", target) } abspath, err := filepath.Abs(target) if err != nil { return "", err } return filepath.Base(abspath), nil }
func guessTag(c util.Settings, e *util.Environment) string { tag, _ := c.String("tag") if tag == "" { tag = guessGitBranch(c, e) } tag = strings.Replace(tag, "/", "_", -1) return tag }
func guessProjectID(c util.Settings, e *util.Environment) string { projectID, _ := c.String("project-id") if projectID != "" { return projectID } // If this was going to fail it already failed and we exited name, _ := guessApplicationName(c, e) return name }
func guessProjectPath(c util.Settings, e *util.Environment) string { target, _ := c.String("target") if looksLikeURL(target) { return "" } if target == "" { target = "." } abs, _ := filepath.Abs(target) return abs }
func guessGitRepository(c util.Settings, e *util.Environment) string { repository, _ := c.String("git-repository") if repository != "" { return repository } // repository, err := guessApplicationName(c, env) // if err != nil { // return "" // } return repository }
func guessGitOwner(c util.Settings, e *util.Environment) string { owner, _ := c.String("git-owner") if owner != "" { return owner } u, err := user.Current() if err == nil { owner = u.Username } return owner }
func guessApplicationOwnerName(c util.Settings, e *util.Environment) string { name, _ := c.String("application-owner-name") if name == "" { u, err := user.Current() if err == nil { name = u.Username } } if name == "" { name = "wercker" } return name }
// NewGitOptions constructor func NewGitOptions(c util.Settings, e *util.Environment, globalOpts *GlobalOptions) (*GitOptions, error) { gitBranch := guessGitBranch(c, e) gitCommit := guessGitCommit(c, e) gitDomain, _ := c.String("git-domain") gitOwner := guessGitOwner(c, e) gitRepository := guessGitRepository(c, e) return &GitOptions{ GlobalOptions: globalOpts, GitBranch: gitBranch, GitCommit: gitCommit, GitDomain: gitDomain, GitOwner: gitOwner, GitRepository: gitRepository, }, nil }
// NewReporterOptions constructor func NewReporterOptions(c util.Settings, e *util.Environment, globalOpts *GlobalOptions) (*ReporterOptions, error) { shouldReport, _ := c.Bool("report") reporterHost, _ := c.String("wercker-host") reporterKey, _ := c.String("wercker-token") if shouldReport { if reporterKey == "" { return nil, errors.New("wercker-token is required") } if reporterHost == "" { return nil, errors.New("wercker-host is required") } } return &ReporterOptions{ GlobalOptions: globalOpts, ReporterHost: reporterHost, ReporterKey: reporterKey, ShouldReport: shouldReport, }, nil }
// NewKeenOptions constructor func NewKeenOptions(c util.Settings, e *util.Environment, globalOpts *GlobalOptions) (*KeenOptions, error) { keenMetrics, _ := c.Bool("keen-metrics") keenProjectWriteKey, _ := c.String("keen-project-write-key") keenProjectID, _ := c.String("keen-project-id") if keenMetrics { if keenProjectWriteKey == "" { return nil, errors.New("keen-project-write-key is required") } if keenProjectID == "" { return nil, errors.New("keen-project-id is required") } } return &KeenOptions{ GlobalOptions: globalOpts, KeenProjectID: keenProjectID, KeenProjectWriteKey: keenProjectWriteKey, ShouldKeenMetrics: keenMetrics, }, nil }
// NewAWSOptions constructor func NewAWSOptions(c util.Settings, e *util.Environment, globalOpts *GlobalOptions) (*AWSOptions, error) { awsAccessKeyID, _ := c.String("aws-access-key") awsRegion, _ := c.String("aws-region") awsSecretAccessKey, _ := c.String("aws-secret-key") s3Bucket, _ := c.String("s3-bucket") return &AWSOptions{ GlobalOptions: globalOpts, AWSAccessKeyID: awsAccessKeyID, AWSRegion: awsRegion, AWSSecretAccessKey: awsSecretAccessKey, S3Bucket: s3Bucket, S3PartSize: 100 * 1024 * 1024, // 100 MB }, nil }
// NewPullOptions constructor func NewPullOptions(c util.Settings, e *util.Environment) (*PullOptions, error) { globalOpts, err := NewGlobalOptions(c, e) if err != nil { return nil, err } // dockerOpts, err := NewDockerOptions(c, e, globalOpts) // if err != nil { // return nil, err // } repository, _ := c.String("target") output, _ := c.String("output") outputDir, err := filepath.Abs(output) if err != nil { return nil, err } branch, _ := c.String("branch") status, _ := c.String("status") result, _ := c.String("result") load, _ := c.Bool("load") force, _ := c.Bool("force") return &PullOptions{ GlobalOptions: globalOpts, // DockerOptions: dockerOpts, Repository: repository, Branch: branch, Status: status, Result: result, Output: outputDir, Load: load, Force: force, }, nil }
// NewDockerOptions constructor func NewDockerOptions(c util.Settings, e *util.Environment) (*DockerOptions, error) { dockerHost, _ := c.String("docker-host") dockerTLSVerify, _ := c.String("docker-tls-verify") dockerCertPath, _ := c.String("docker-cert-path") dockerDNS, _ := c.StringSlice("docker-dns") dockerLocal, _ := c.Bool("docker-local") speculativeOptions := &DockerOptions{ DockerHost: dockerHost, DockerTLSVerify: dockerTLSVerify, DockerCertPath: dockerCertPath, DockerDNS: dockerDNS, DockerLocal: dockerLocal, } // We're going to try out a few settings and set DockerHost if // one of them works, it they don't we'll get a nice error when // requireDockerEndpoint triggers later on guessAndUpdateDockerOptions(speculativeOptions, e) return speculativeOptions, nil }
// NewPipelineOptions big-ass constructor func NewPipelineOptions(c util.Settings, e *util.Environment) (*PipelineOptions, error) { globalOpts, err := NewGlobalOptions(c, e) if err != nil { return nil, err } // dockerOpts, err := NewDockerOptions(c, e, globalOpts) // if err != nil { // return nil, err // } awsOpts, err := NewAWSOptions(c, e, globalOpts) if err != nil { return nil, err } gitOpts, err := NewGitOptions(c, e, globalOpts) if err != nil { return nil, err } keenOpts, err := NewKeenOptions(c, e, globalOpts) if err != nil { return nil, err } reporterOpts, err := NewReporterOptions(c, e, globalOpts) if err != nil { return nil, err } buildID, _ := c.String("build-id") deployID, _ := c.String("deploy-id") pipelineID := "" if deployID != "" { pipelineID = deployID } else { pipelineID = buildID } deployTarget, _ := c.String("deploy-target") pipeline, _ := c.String("pipeline") applicationName, err := guessApplicationName(c, e) if err != nil { return nil, err } applicationID := guessApplicationID(c, e, applicationName) applicationOwnerName := guessApplicationOwnerName(c, e) applicationStartedByName, _ := c.String("application-started-by-name") if applicationStartedByName == "" { applicationStartedByName = applicationOwnerName } repository, _ := c.String("commit") shouldCommit := (repository != "") tag := guessTag(c, e) message := guessMessage(c, e) shouldStoreLocal, _ := c.Bool("store-local") shouldStoreS3, _ := c.Bool("store-s3") workingDir, _ := c.String("working-dir") workingDir, _ = filepath.Abs(workingDir) guestRoot, _ := c.String("guest-root") mntRoot, _ := c.String("mnt-root") reportRoot, _ := c.String("report-root") projectID := guessProjectID(c, e) projectPath := guessProjectPath(c, e) projectURL := guessProjectURL(c, e) if projectPath == workingDir { return nil, fmt.Errorf("Project path can't be the same as the working dir") } // These timeouts are given in minutes but we store them as milliseconds commandTimeoutFloat, _ := c.Float64("command-timeout") commandTimeout := int(commandTimeoutFloat * 1000 * 60) noResponseTimeoutFloat, _ := c.Float64("no-response-timeout") noResponseTimeout := int(noResponseTimeoutFloat * 1000 * 60) shouldArtifacts, _ := c.Bool("artifacts") // TODO(termie): switch negative flag shouldRemove, _ := c.Bool("no-remove") shouldRemove = !shouldRemove sourceDir, _ := c.String("source-dir") attachOnError, _ := c.Bool("attach-on-error") directMount, _ := c.Bool("direct-mount") enableDevSteps, _ := c.Bool("enable-dev-steps") publishPorts, _ := c.StringSlice("publish") enableVolumes, _ := c.Bool("enable-volumes") werckerYml, _ := c.String("wercker-yml") return &PipelineOptions{ GlobalOptions: globalOpts, AWSOptions: awsOpts, // DockerOptions: dockerOpts, GitOptions: gitOpts, KeenOptions: keenOpts, ReporterOptions: reporterOpts, HostEnv: e, BuildID: buildID, DeployID: deployID, PipelineID: pipelineID, DeployTarget: deployTarget, Pipeline: pipeline, ApplicationID: applicationID, ApplicationName: applicationName, ApplicationOwnerName: applicationOwnerName, ApplicationStartedByName: applicationStartedByName, Message: message, Tag: tag, Repository: repository, ShouldCommit: shouldCommit, ShouldStoreLocal: shouldStoreLocal, ShouldStoreS3: shouldStoreS3, WorkingDir: workingDir, GuestRoot: guestRoot, MntRoot: mntRoot, ReportRoot: reportRoot, ProjectID: projectID, ProjectURL: projectURL, ProjectPath: projectPath, CommandTimeout: commandTimeout, NoResponseTimeout: noResponseTimeout, ShouldArtifacts: shouldArtifacts, ShouldRemove: shouldRemove, SourceDir: sourceDir, AttachOnError: attachOnError, DirectMount: directMount, EnableDevSteps: enableDevSteps, PublishPorts: publishPorts, EnableVolumes: enableVolumes, WerckerYml: werckerYml, }, nil }
func guessMessage(c util.Settings, e *util.Environment) string { message, _ := c.String("message") return message }