func (j *Job) Run() error { defer j.parseLogs() j.JobId = fmt.Sprintf("%s-%x", j.Name, sha1.Sum([]byte(j.Name+time.Now().String()))) j.logFile = fmt.Sprintf("%s/%s.log", env.Get("LOG_DIR"), j.JobId) log, err := os.OpenFile(j.logFile, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { j.Error = err return err } defer log.Close() j.log = log fmt.Fprintf(log, "New build %s - %s\n", j.JobId, time.Now().String()) workdir := fmt.Sprintf("/tmp/%s", j.JobId) defer os.RemoveAll(workdir) if err := j.clone(workdir); err != nil { j.Error = err return err } if err := j.command(workdir); err != nil { j.Error = err return err } return nil }
func main() { app := happyngine.NewAPI() // Setup seed rand.Seed(time.Now().UnixNano()) // Setup Origin if origin := env.Get("ALLOW_ORIGIN"); len(origin) > 0 { app.Headers["Access-Control-Allow-Origin"] = origin } if cacheControl := env.Get("CACHE_CONTROL"); len(cacheControl) > 0 { app.Headers["Cache-Control"] = cacheControl } // Register actions app.AddRoute("GET", "/:bucket/:file", newGetFileAction) // Setup custuom 404 handler app.Error404Handler = func(ctx *happyngine.Context, err interface{}) { ctx.Send(http.StatusNotFound, `not found 404`) } // Setup custuom panic handler app.PanicHandler = func(ctx *happyngine.Context, err interface{}) { ctx.Send(500, `{"error":"internal_error"}`) trace := make([]byte, 1024) runtime.Stack(trace, true) ctx.Criticalln(err, string(trace)) } log.Debugln("Running...") if err := app.Run(":8080"); err != nil { log.Criticalln("app.Run:", err) } }
func GetJobLogs(id string) ([]byte, error) { data, err := ioutil.ReadFile(fmt.Sprintf("%s/%s.log", env.Get("LOG_DIR"), id)) if err != nil { if strings.Contains(err.Error(), "no such file or directory") { return nil, nil } return nil, err } return data, nil }
func init() { var auth = aws.Auth{ AccessKey: env.Get("AWS_ACCESS_KEY_ID"), SecretKey: env.Get("AWS_SECRET_ACCESS_KEY"), } regionName := env.Get("AWS_DEFAULT_REGION") conn := sqs.New(auth, aws.Regions[regionName]) var err error queue, err = conn.GetQueue(env.Get("AWS_SQS_QUEUE")) if err != nil { panic(err) } if cnt := env.GetInt("TASK_BATCH_SIZE"); cnt > 0 { batchSize = cnt } go taskRunner() }
func GetJob(name string) (*Job, error) { data, err := ioutil.ReadFile(env.Get("JOB_FILE")) if err != nil { return nil, err } jobs := make(map[string]Job) if err := yaml.Unmarshal(data, jobs); err != nil { return nil, err } for jobName, job := range jobs { if jobName == name { job.Name = jobName return &job, nil } } return nil, nil }
func (this *getFileAction) getFile() ([]byte, bool) { bucket := this.Form.Elem("bucket").FormValue() file := this.Form.Elem("file").FormValue() u := url.URL{ Scheme: "http", Host: fmt.Sprintf("s3-%s.amazonaws.com", env.Get("AWS_DEFAULT_REGION")), Path: fmt.Sprintf("%s/%s", bucket, file), } q := u.Query() for name := range this.Context.Request.URL.Query() { q.Set(name, this.Context.Request.URL.Query().Get(name)) } u.RawQuery = q.Encode() resp, err := http.Get(u.String()) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { panic(err) } if resp.StatusCode >= 300 { contentType := resp.Header.Get("Content-Type") this.SendByte(resp.StatusCode, body, fmt.Sprintf("Content-Type: %s", contentType)) return nil, false } return body, true }