From 42607e4afa60fbd830473525437deffdb87e647e Mon Sep 17 00:00:00 2001 From: Benjamin Guillet Date: Mon, 23 Nov 2015 17:30:22 -0800 Subject: [PATCH] Support multiple environments for Github deployments. Allow for environment aliases like prod for production for instance. --- cmd/empire/main.go | 6 +++--- cmd/empire/server.go | 6 +++--- empiretest/test.go | 2 +- server/github/github.go | 17 +++++++++++++---- server/server.go | 4 ++-- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cmd/empire/main.go b/cmd/empire/main.go index de8ffe001..b08a1e069 100644 --- a/cmd/empire/main.go +++ b/cmd/empire/main.go @@ -20,7 +20,7 @@ const ( FlagGithubApiURL = "github.api.url" FlagGithubWebhooksSecret = "github.webhooks.secret" - FlagGithubDeploymentsEnvironment = "github.deployments.environment" + FlagGithubDeploymentsEnvironments = "github.deployments.environment" FlagGithubDeploymentsImageTemplate = "github.deployments.template" FlagGithubDeploymentsTugboatURL = "github.deployments.tugboat.url" @@ -105,9 +105,9 @@ var Commands = []cli.Command{ EnvVar: "EMPIRE_GITHUB_WEBHOOKS_SECRET", }, cli.StringFlag{ - Name: FlagGithubDeploymentsEnvironment, + Name: FlagGithubDeploymentsEnvironments, Value: "", - Usage: "If provided, only github deployments to the specified environment will be handled.", + Usage: "If provided, only github deployments to the specified environments will be handled.", EnvVar: "EMPIRE_GITHUB_DEPLOYMENTS_ENVIRONMENT", }, cli.StringFlag{ diff --git a/cmd/empire/server.go b/cmd/empire/server.go index 3295d71a5..cc7a010db 100644 --- a/cmd/empire/server.go +++ b/cmd/empire/server.go @@ -4,15 +4,15 @@ import ( "fmt" "log" "net/http" + "strings" "time" - "golang.org/x/oauth2" - "github.com/codegangsta/cli" "github.com/remind101/empire" "github.com/remind101/empire/server" "github.com/remind101/empire/server/auth" "github.com/remind101/empire/server/auth/github" + "golang.org/x/oauth2" ) func runServer(c *cli.Context) { @@ -36,7 +36,7 @@ func newServer(c *cli.Context, e *empire.Empire) http.Handler { var opts server.Options opts.Authenticator = newAuthenticator(c, e) opts.GitHub.Webhooks.Secret = c.String(FlagGithubWebhooksSecret) - opts.GitHub.Deployments.Environment = c.String(FlagGithubDeploymentsEnvironment) + opts.GitHub.Deployments.Environments = strings.Split(c.String(FlagGithubDeploymentsEnvironments), ",") opts.GitHub.Deployments.ImageTemplate = c.String(FlagGithubDeploymentsImageTemplate) opts.GitHub.Deployments.TugboatURL = c.String(FlagGithubDeploymentsTugboatURL) diff --git a/empiretest/test.go b/empiretest/test.go index b0f4d0511..9558a55f4 100644 --- a/empiretest/test.go +++ b/empiretest/test.go @@ -58,8 +58,8 @@ func NewServer(t testing.TB, e *empire.Empire) *httptest.Server { } server.DefaultOptions.GitHub.Webhooks.Secret = "abcd" - server.DefaultOptions.GitHub.Deployments.Environment = "test" server.DefaultOptions.Authenticator = auth.Anyone(&empire.User{Name: "fake"}) + server.DefaultOptions.GitHub.Deployments.Environments = []string{"test"} return httptest.NewServer(server.New(e, server.DefaultOptions)) } diff --git a/server/github/github.go b/server/github/github.go index edd0d89e7..ead59942c 100644 --- a/server/github/github.go +++ b/server/github/github.go @@ -25,7 +25,7 @@ type Options struct { // If provided, specifies the environments that this Empire instance // should handle deployments for. - Environment string + Environments []string // ImageTemplate is used to determine the image to deploy. ImageTemplate string @@ -40,7 +40,7 @@ func New(e *empire.Empire, opts Options) httpx.Handler { d := newDeployer(e, opts) secret := opts.Secret - r.Handle("deployment", hookshot.Authorize(&DeploymentHandler{deployer: d, environment: opts.Environment}, secret)) + r.Handle("deployment", hookshot.Authorize(&DeploymentHandler{deployer: d, environments: opts.Environments}, secret)) r.Handle("ping", hookshot.Authorize(http.HandlerFunc(Ping), secret)) return r @@ -49,7 +49,7 @@ func New(e *empire.Empire, opts Options) httpx.Handler { // Deployment is an http.Handler for handling the `deployment` event. type DeploymentHandler struct { deployer - environment string + environments []string } func (h *DeploymentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -64,7 +64,7 @@ func (h *DeploymentHandler) ServeHTTPContext(ctx context.Context, w http.Respons return nil } - if p.Deployment.Environment != h.environment { + if !currentEnvironment(p.Deployment.Environment, h.environments) { w.WriteHeader(http.StatusNoContent) fmt.Fprintf(w, "Ignore deployment to environment: %s", p.Deployment.Environment) return nil @@ -79,6 +79,15 @@ func (h *DeploymentHandler) ServeHTTPContext(ctx context.Context, w http.Respons return nil } +func currentEnvironment(eventEnv string, environments []string) bool { + for _, env := range environments { + if env == eventEnv { + return true + } + } + return false +} + func Ping(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "Ok\n") } diff --git a/server/server.go b/server/server.go index 3492d7311..4391b1bb1 100644 --- a/server/server.go +++ b/server/server.go @@ -30,7 +30,7 @@ type Options struct { Secret string } Deployments struct { - Environment string + Environments []string ImageTemplate string TugboatURL string } @@ -44,7 +44,7 @@ func New(e *empire.Empire, options Options) http.Handler { // Mount GitHub webhooks g := github.New(e, github.Options{ Secret: options.GitHub.Webhooks.Secret, - Environment: options.GitHub.Deployments.Environment, + Environments: options.GitHub.Deployments.Environments, ImageTemplate: options.GitHub.Deployments.ImageTemplate, TugboatURL: options.GitHub.Deployments.TugboatURL, })