2016-04-01 14:30:17 -05:00
|
|
|
// Copyright 2016 Tim Shannon. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-04-17 20:43:27 -05:00
|
|
|
"fmt"
|
2016-04-01 14:30:17 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-05 16:59:52 -05:00
|
|
|
"strconv"
|
2016-04-01 14:30:17 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2016-04-19 14:52:55 -05:00
|
|
|
"git.townsourced.com/townsourced/ironsmith/datastore"
|
2016-04-01 14:30:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Project life cycle:
|
|
|
|
(Load Project file) -> (Fetch) -> (Build) -> (Test) -> (Release) - > (Sleep for polling period) ->
|
|
|
|
(Reload Project File) -> (Fetch) -> etc...
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// load is the beginning of the cycle. Loads / reloads the project file to make sure that the scripts are up-to-date
|
|
|
|
// call's fetch and triggers the next poll if one exists
|
|
|
|
func (p *Project) load() {
|
2016-04-13 15:53:49 -05:00
|
|
|
p.processing.Lock() // ensure only one cycle is running at a time per project
|
|
|
|
defer p.processing.Unlock()
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setStage(stageLoad)
|
2016-04-25 14:41:03 -05:00
|
|
|
p.setVersion("Version not yet set")
|
2016-04-25 16:02:01 -05:00
|
|
|
p.start = time.Time{}
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
if p.filename == "" {
|
|
|
|
p.errHandled(errors.New("Invalid project file name"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
if _, ok := projects.get(p.id()); !ok {
|
2016-04-01 14:30:17 -05:00
|
|
|
// project has been deleted
|
|
|
|
// don't continue polling
|
2016-04-05 16:59:52 -05:00
|
|
|
// move project data to deleted folder with a timestamp
|
2016-04-13 15:53:49 -05:00
|
|
|
if p.errHandled(p.close()) {
|
|
|
|
return
|
|
|
|
}
|
2016-04-05 16:59:52 -05:00
|
|
|
p.errHandled(os.Rename(p.dir(), filepath.Join(dataDir, deletedProjectDir,
|
|
|
|
strconv.FormatInt(time.Now().Unix(), 10), p.id())))
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filepath.Join(projectDir, enabledProjectDir, p.filename))
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
new := &Project{}
|
|
|
|
if p.errHandled(json.Unmarshal(data, new)) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setData(new)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
p.fetch()
|
|
|
|
|
2016-04-13 15:53:49 -05:00
|
|
|
p.setStage(stageWait)
|
|
|
|
|
2016-04-01 14:30:17 -05:00
|
|
|
//full cycle completed
|
|
|
|
|
|
|
|
if p.poll > 0 {
|
|
|
|
//start polling
|
2016-04-13 15:53:49 -05:00
|
|
|
go func() {
|
|
|
|
time.AfterFunc(p.poll, p.load)
|
|
|
|
}()
|
2016-04-01 14:30:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
// fetch first runs the fetch script into a temporary directory
|
|
|
|
// then it runs the version script in the temp directory to see if there is a newer version of the
|
|
|
|
// fetched code, if there is then the temp dir is renamed to the version name
|
2016-04-01 14:30:17 -05:00
|
|
|
func (p *Project) fetch() {
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setStage(stageFetch)
|
2016-04-25 16:02:01 -05:00
|
|
|
p.start = time.Now()
|
2016-04-05 16:59:52 -05:00
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
if p.Fetch == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
tempDir := filepath.Join(p.dir(), strconv.FormatInt(time.Now().Unix(), 10))
|
|
|
|
|
|
|
|
if p.errHandled(os.MkdirAll(tempDir, 0777)) {
|
|
|
|
return
|
2016-04-01 14:30:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
//fetch project
|
2016-04-20 11:05:59 -05:00
|
|
|
fetchResult, err := runCmd(p.Fetch, tempDir, p.Environment)
|
2016-04-05 16:59:52 -05:00
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetched succesfully, determine version
|
2016-04-20 11:05:59 -05:00
|
|
|
version, err := runCmd(p.Version, tempDir, p.Environment)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setVersion(strings.TrimSpace(string(version)))
|
2016-04-01 14:30:17 -05:00
|
|
|
|
2016-04-14 11:29:56 -05:00
|
|
|
// check if this specific version has attempted a build yet
|
|
|
|
lVer, err := p.ds.LastVersion(stageBuild)
|
2016-04-01 14:30:17 -05:00
|
|
|
if err != datastore.ErrNotFound && p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-14 11:29:56 -05:00
|
|
|
if p.version == "" || p.version == lVer.Version {
|
2016-04-05 16:59:52 -05:00
|
|
|
// no new build clean up temp dir
|
|
|
|
p.errHandled(os.RemoveAll(tempDir))
|
2016-04-01 14:30:17 -05:00
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
vlog("No new version found for Project: %s Version: %s.\n", p.id(), p.version)
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
//remove any existing data that matches version hash
|
|
|
|
if p.errHandled(os.RemoveAll(p.workingDir())) {
|
|
|
|
return
|
2016-04-01 14:30:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
//new version move tempdir to workingDir
|
|
|
|
if p.errHandled(os.Rename(tempDir, p.workingDir())) {
|
|
|
|
// cleanup temp dir if rename failed
|
|
|
|
p.errHandled(os.RemoveAll(tempDir))
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
//log fetch results
|
2016-04-05 21:08:08 -05:00
|
|
|
if p.errHandled(p.ds.AddLog(p.version, p.stage, string(fetchResult))) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
// continue to build
|
2016-04-01 14:30:17 -05:00
|
|
|
p.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
// build runs the build scripts to build the project which should result in the a single file
|
|
|
|
// configured in the ReleaseFile section of the project file
|
|
|
|
func (p *Project) build() {
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setStage(stageBuild)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
if p.Build == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-20 11:05:59 -05:00
|
|
|
output, err := runCmd(p.Build, p.workingDir(), p.Environment)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:08:08 -05:00
|
|
|
if p.errHandled(p.ds.AddLog(p.version, p.stage, string(output))) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// built successfully, onto test stage
|
|
|
|
p.test()
|
|
|
|
}
|
|
|
|
|
|
|
|
// test runs the test scripts
|
|
|
|
func (p *Project) test() {
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setStage(stageTest)
|
2016-04-07 09:49:54 -05:00
|
|
|
|
|
|
|
if p.Test == "" {
|
|
|
|
return
|
|
|
|
}
|
2016-04-20 11:05:59 -05:00
|
|
|
output, err := runCmd(p.Test, p.workingDir(), p.Environment)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:08:08 -05:00
|
|
|
if p.errHandled(p.ds.AddLog(p.version, p.stage, string(output))) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests passed, onto release
|
|
|
|
p.release()
|
|
|
|
}
|
|
|
|
|
|
|
|
// release runs the release scripts and builds the release file
|
|
|
|
func (p *Project) release() {
|
2016-04-06 16:59:24 -05:00
|
|
|
p.setStage(stageRelease)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
2016-04-07 09:49:54 -05:00
|
|
|
if p.Release == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-20 11:05:59 -05:00
|
|
|
output, err := runCmd(p.Release, p.workingDir(), p.Environment)
|
2016-04-01 14:30:17 -05:00
|
|
|
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:08:08 -05:00
|
|
|
if p.errHandled(p.ds.AddLog(p.version, p.stage, string(output))) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//get release file
|
2016-04-05 16:59:52 -05:00
|
|
|
f, err := os.Open(filepath.Join(p.workingDir(), p.ReleaseFile))
|
2016-04-01 14:30:17 -05:00
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buff, err := ioutil.ReadAll(f)
|
|
|
|
if p.errHandled(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:02:01 -05:00
|
|
|
if p.errHandled(p.ds.AddRelease(p.version, filepath.Base(p.ReleaseFile), buff)) {
|
2016-04-01 14:30:17 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-17 20:43:27 -05:00
|
|
|
p.setStage(stageReleased)
|
|
|
|
|
|
|
|
if p.errHandled(p.ds.AddLog(p.version, p.stage,
|
2016-04-25 16:02:01 -05:00
|
|
|
fmt.Sprintf("Project %s Version %s built, tested, and released successfully and took %s.\n", p.id(), p.version,
|
|
|
|
time.Now().Sub(p.start)))) {
|
2016-04-17 20:43:27 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
//build successfull, remove working dir
|
|
|
|
p.errHandled(os.RemoveAll(p.workingDir()))
|
|
|
|
|
2016-04-17 21:14:04 -05:00
|
|
|
vlog("Project %s Version %s built, tested, and released successfully.\n", p.id(), p.version)
|
2016-04-01 14:30:17 -05:00
|
|
|
}
|