Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ ACME_URL=https://acme-staging.api.letsencrypt.org/directory
GITHUB_API_HOST=https://api.github.com
GITHUB_API_TOKEN=c3c6280f5c5d504a00765fbc598fbf818b90cec7
WEBHOOK_HOST=https://localhost:3000
SENTRY_URL=
1 change: 1 addition & 0 deletions apiserver/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
GitHubAPIHost = os.Getenv("GITHUB_API_HOST")
GitHubAPIToken = os.Getenv("GITHUB_API_TOKEN")
WebhookHost = os.Getenv("WEBHOOK_HOST")
SentryURL = os.Getenv("SENTRY_URL")
)

func init() {
Expand Down
24 changes: 19 additions & 5 deletions apiserver/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"
"strings"

"github.com/getsentry/raven-go"
"github.com/nitrous-io/rise-server/apiserver/common"
"github.com/nitrous-io/rise-server/apiserver/models/oauthtoken"
"github.com/nitrous-io/rise-server/apiserver/models/project"
"github.com/nitrous-io/rise-server/apiserver/models/user"
Expand All @@ -21,6 +23,10 @@ const (
CurrentProjectKey = "current_project"
)

func init() {
raven.SetDSN(common.SentryURL)
}

func CurrentToken(c *gin.Context) *oauthtoken.OauthToken {
ti, exists := c.Get(CurrentTokenKey)
if ti == nil || !exists {
Expand Down Expand Up @@ -66,16 +72,24 @@ func InternalServerError(c *gin.Context, err error, msg ...string) {
errHash string
)

req := c.Request

if err != nil {
appErr := err
if len(msg) > 0 {
errMsg = fmt.Sprintf("%s: %s", msg, err.Error())
} else {
errMsg = err.Error()
errMsg = fmt.Sprintf("%s: %s", msg[0], err.Error())
}
errMsg = appErr.Error()
errHash = fmt.Sprintf("%x", sha1.Sum([]byte(errMsg)))
}

req := c.Request
raven.CaptureError(appErr, map[string]string{
"app": "api-server",
"error_hash": errHash,
"method": req.Method,
"url": req.URL.String(),
"ip": c.ClientIP(),
})
}

fields := log.Fields{
"req": fmt.Sprintf("%s %s", req.Method, req.URL.String()),
Expand Down
69 changes: 34 additions & 35 deletions apiserver/controllers/deployments/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Create(c *gin.Context) {

db, err := dbconn.DB()
if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to get a db connection")
controllers.InternalServerError(c, err, "failed to obtain a db connection")
return
}

Expand All @@ -54,7 +54,7 @@ func Create(c *gin.Context) {
if proj.ActiveDeploymentID != nil {
var prevDepl deployment.Deployment
if err := db.Where("id = ?", proj.ActiveDeploymentID).First(&prevDepl).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to fetch a previous deployment")
controllers.InternalServerError(c, err, "failed to fetch a previous deployment")
return
}

Expand Down Expand Up @@ -116,20 +116,20 @@ func Create(c *gin.Context) {
if part.FormName() == "payload" {
ver, err := proj.NextVersion(db)
if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to get next deployment version number")
controllers.InternalServerError(c, err, "failed to get next deployment version number")
return
}

depl.Version = ver
if err := db.Create(depl).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to create a deployment record in DB")
controllers.InternalServerError(c, err, "failed to create a deployment record in DB")
return
}

br := bufio.NewReader(part)
partHead, err := br.Peek(512)
if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to get header from payload")
controllers.InternalServerError(c, err, "failed to get header from payload")
return
}

Expand All @@ -152,7 +152,7 @@ func Create(c *gin.Context) {

hr := hasher.NewReader(br)
if err := s3client.Upload(uploadKey, hr, "", "private"); err != nil {
controllers.InternalServerError(c, err, "deployments: failed to upload to S3")
controllers.InternalServerError(c, err, "failed to upload to S3")
return
}

Expand All @@ -162,7 +162,7 @@ func Create(c *gin.Context) {
UploadedPath: uploadKey,
}
if err := db.Create(bun).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to create a raw bundle record in DB")
controllers.InternalServerError(c, err, "failed to create a raw bundle record in DB")
return
}

Expand All @@ -174,13 +174,13 @@ func Create(c *gin.Context) {
case viaCachedBundle:
ver, err := proj.NextVersion(db)
if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to get next deployment version number")
controllers.InternalServerError(c, err, "failed to get next deployment version number")
return
}

depl.Version = ver
if err := db.Create(depl).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to create a deployment record in DB")
controllers.InternalServerError(c, err, "failed to create a deployment record in DB")
return
}

Expand All @@ -206,7 +206,7 @@ func Create(c *gin.Context) {
})
return
}
controllers.InternalServerError(c, err, "deployments: failed to find a raw bundle")
controllers.InternalServerError(c, err, "failed to find a raw bundle")
return
}
depl.RawBundleID = &bun.ID
Expand Down Expand Up @@ -253,14 +253,14 @@ func Create(c *gin.Context) {

ver, err := proj.NextVersion(db)
if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to get next deployment version number")
controllers.InternalServerError(c, err, "failed to get next deployment version number")
return
}

depl.TemplateID = &tmpl.ID
depl.Version = ver
if err := db.Create(depl).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to create a deployment record in DB")
controllers.InternalServerError(c, err, "deployments: failed to create a deployment record in DB")
return
}

Expand All @@ -275,7 +275,7 @@ func Create(c *gin.Context) {
UploadedPath: bundlePath,
}
if err := db.Create(bun).Error; err != nil {
controllers.InternalServerError(c, err, "deployments: failed to create a raw bundle record in DB")
controllers.InternalServerError(c, err, "failed to create a raw bundle record in DB")
return
}

Expand All @@ -290,7 +290,7 @@ func Create(c *gin.Context) {
}

if err := depl.UpdateState(db, deployment.StateUploaded); err != nil {
controllers.InternalServerError(c, err, "deployments: failed to update deployment state to be uploaded")
controllers.InternalServerError(c, err, "failed to update deployment state to be uploaded")
return
}

Expand All @@ -309,12 +309,12 @@ func Create(c *gin.Context) {
}

if err != nil {
controllers.InternalServerError(c, err, "deployments: failed to connect to job queue")
controllers.InternalServerError(c, err, "failed to create a job")
return
}

if err := j.Enqueue(); err != nil {
controllers.InternalServerError(c, err, "deployments: failed to enqueue a job")
controllers.InternalServerError(c, err, "failed to enqueue a job")
return
}

Expand All @@ -324,7 +324,7 @@ func Create(c *gin.Context) {
}

if err := depl.UpdateState(db, newState); err != nil {
controllers.InternalServerError(c, err, "deployments: failed to update deployment state to be "+newState)
controllers.InternalServerError(c, err, "failed to update deployment state to be "+newState)
return
}

Expand Down Expand Up @@ -366,7 +366,7 @@ func Show(c *gin.Context) {

db, err := dbconn.DB()
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to obtain a db connection")
return
}

Expand All @@ -380,7 +380,7 @@ func Show(c *gin.Context) {
})
return
}
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch the deployment from DB")
return
}

Expand All @@ -403,7 +403,7 @@ func Download(c *gin.Context) {

db, err := dbconn.DB()
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to obtain a db connection")
return
}

Expand All @@ -416,7 +416,7 @@ func Download(c *gin.Context) {
})
return
}
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch the deployment from DB")
return
}

Expand All @@ -437,16 +437,16 @@ func Download(c *gin.Context) {
})
return
}
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch the raw bundle from DB")
return
}

exists, err := s3client.Exists(bun.UploadedPath)
if err != nil {
log.Warnf("failed to check existence of %q on S3, err: %v", bun.UploadedPath, err)
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, fmt.Sprintf("failed to check existence of %q on S3", bun.UploadedPath))
return
}

if !exists {
log.Warnf("deployment raw bundle %q does not exist in S3", bun.UploadedPath)
c.JSON(http.StatusGone, gin.H{
Expand All @@ -458,8 +458,7 @@ func Download(c *gin.Context) {

url, err := s3client.PresignedURL(bun.UploadedPath, presignExpiryDuration)
if err != nil {
log.Printf("error generating presigned URL to %q, err: %v", bun.UploadedPath, err)
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, fmt.Sprintf("error generating presigned URL to %q", bun.UploadedPath))
return
}

Expand All @@ -483,21 +482,21 @@ func Rollback(c *gin.Context) {

db, err := dbconn.DB()
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to obtain a db connection")
return
}

var currentDepl deployment.Deployment
if err := db.First(&currentDepl, *proj.ActiveDeploymentID).Error; err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch an active deployment from DB")
return
}

var depl *deployment.Deployment
if c.PostForm("version") == "" {
depl, err = currentDepl.PreviousCompletedDeployment(db)
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch a previous deployment from DB")
return
}

Expand Down Expand Up @@ -528,7 +527,7 @@ func Rollback(c *gin.Context) {
return
}

controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch a deployment for specified version from DB")
return
}

Expand All @@ -547,17 +546,17 @@ func Rollback(c *gin.Context) {
})

if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to create a job")
return
}

if err := j.Enqueue(); err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to enqueue a job")
return
}

if err := depl.UpdateState(db, deployment.StatePendingRollback); err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to update a deployment to be panding rollback")
return
}

Expand Down Expand Up @@ -593,13 +592,13 @@ func Index(c *gin.Context) {

db, err := dbconn.DB()
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to obtain a db connection")
return
}

depls, err := deployment.CompletedDeployments(db, proj.ID, proj.MaxDeploysKept)
if err != nil {
controllers.InternalServerError(c, err)
controllers.InternalServerError(c, err, "failed to fetch completed deployments from DB")
return
}

Expand Down
11 changes: 11 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"syscall"
"time"

"github.com/getsentry/raven-go"
"github.com/nitrous-io/rise-server/apiserver/common"
"github.com/nitrous-io/rise-server/builder/builder"
"github.com/nitrous-io/rise-server/pkg/mqconn"
"github.com/nitrous-io/rise-server/shared/queues"
Expand All @@ -15,6 +17,8 @@ import (
)

func main() {
raven.SetDSN(common.SentryURL)

run()
os.Exit(1)
}
Expand Down Expand Up @@ -94,6 +98,13 @@ func run() {
// failure
log.Warnln("Work failed", err, string(d.Body))

if err != builder.ErrProjectLocked {
raven.CaptureError(err, map[string]string{
"app": "builder",
"body": string(d.Body),
})
}

if err == builder.ErrRecordNotFound || err == builder.ErrUnarchiveFailed {
if err := d.Ack(false); err != nil {
log.WithFields(log.Fields{"queue": queueName}).Warnln("Failed to Ack message:", err)
Expand Down
Loading