Skip to content

Commit

Permalink
Added env vars to all compose commads
Browse files Browse the repository at this point in the history
Stopping will always be picked up

Added proper erroring
  • Loading branch information
Sharpz7 committed Jun 6, 2020
1 parent 05fd398 commit f8a6755
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 62 deletions.
74 changes: 37 additions & 37 deletions sharpcd.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
version: 1

tasks:
# basic_task:
# name: Basic
# type: docker
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/basic.yml
basic_task:
name: Basic
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/basic.yml

env_task:
name: Enviroment Test Fail
Expand All @@ -16,38 +16,38 @@ tasks:
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/env.yml

# env_task2:
# name: Enviroment Test Pass
# type: docker
# envfile: ./testing/.env
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/env.yml
env_task2:
name: Enviroment Test Pass
type: docker
envfile: ./testing/.env
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/env.yml

# basic_task:
# name: Basic 2
# type: docker
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/basic.yml
basic_task:
name: Basic 2
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/basic.yml

# file_task:
# name: No Compose File
# type: docker
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/basic
file_task:
name: No Compose File
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/basic

# Delayed_task:
# name: Delayed stop
# type: docker
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/delayed.yml
Delayed_task:
name: Delayed stop
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/delayed.yml

# Restart_task:
# name: Test Log exit
# type: docker
# sharpurl: https://localhost:5666
# giturl: https://raw.githubusercontent.com/Sharpz7/
# compose: /sharpcd/testing/testing/restart.yml
Restart_task:
name: Test Log exit
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/testing/testing/restart.yml
1 change: 1 addition & 0 deletions sharpdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ scripts:
logs: curl -k -X POST -d SECRET https://localhost:5666/api/logs/${SHARP_ARG_1}
filter: ./internal/sharpcd addfilter https://raw.githubusercontent.com/Sharpz7/
remove: ./internal/sharpcd removefilter https://raw.githubusercontent.com/Sharpz7/
alljobs: curl -k -X POST -d SECRET https://localhost:5666/api/jobs

8 changes: 5 additions & 3 deletions src/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ func postCommChecks(t task, id string) error {
errored := job.Status == jobStatus.Errored
logsError := strings.Contains(logFile, "exited with code")

if ((stopped || logsError) && buildingTriggered) || errored {
if ((stopped || logsError) && runningTriggered) || errored {
fmt.Println("Task stopped running! Error Message:")
fmt.Println(job.ErrMsg)

fmt.Println("Logs File:")
fmt.Println(logFile)
if runningTriggered {
fmt.Println("Logs File:")
fmt.Println(logFile)
}
return errors.New("Bad Task")
}

Expand Down
37 changes: 22 additions & 15 deletions src/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"os"
"os/exec"
"time"
)

// Pointer to variable storing all jobs
Expand All @@ -30,9 +31,9 @@ func createJob(payload postData) {

// Stores info on the job from the task
newJob := taskJob{
Name: payload.Name,
Type: payload.Type,
URL: payload.GitURL + payload.Compose,
Name: payload.Name,
Type: payload.Type,
URL: payload.GitURL + payload.Compose,
Enviroment: payload.Enviroment}

// If a job with that ID already exists
Expand Down Expand Up @@ -75,6 +76,7 @@ func (job *taskJob) Run() {

// Run Command
job.Status = jobStatus.Running

err := cmd.Run()
handleAPI(err, job, "Job Exited With Error")

Expand All @@ -99,6 +101,9 @@ func (job *taskJob) Stop() {

err := cmd.Run()
handleAPI(err, job, "Failed to Stop Job")

// Sleeps to API can pick it up
time.Sleep(2 * time.Second)
}

// Stop sequence for a docker job
Expand All @@ -107,6 +112,7 @@ func (job *taskJob) DockerStop() *exec.Cmd {

// Stopping the container
cmd := exec.Command("docker-compose", "-f", composeLoc, "down")
cmd.Env = job.insertEnviroment()

return cmd
}
Expand All @@ -133,18 +139,12 @@ func (job *taskJob) DockerCmd() *exec.Cmd {
err = ioutil.WriteFile(composeLoc, file, 0777)
handleAPI(err, job, "Failed to write to file")


// Remove any previous containers
out, err := exec.Command("docker-compose", "-f", composeLoc, "down").CombinedOutput()
handleAPI(err, job, string(out))

// Make sure Config Is valid
out, err = exec.Command("docker-compose", "-f", composeLoc, "config").CombinedOutput()
handleAPI(err, job, string(out))

// pull lastest images
out, err = exec.Command("docker-compose", "-f", composeLoc, "pull").CombinedOutput()
handleAPI(err, job, string(out))
job.buildCommand("-f", composeLoc, "down")
// Make sure config is valid
job.buildCommand("-f", composeLoc, "config")
// Pull new images
job.buildCommand("-f", composeLoc, "pull")

// Get logging Running
cmd := exec.Command("docker-compose", "-f", composeLoc, "up", "--no-color")
Expand All @@ -165,5 +165,12 @@ func (job *taskJob) insertEnviroment() []string {
environ = append(environ, str)
}

return environ
return append(os.Environ(), environ...)
}

func (job *taskJob) buildCommand(args ...string) {
cmd := exec.Command("docker-compose", args...)
cmd.Env = job.insertEnviroment()
out, err := cmd.CombinedOutput()
handleAPI(err, job, string(out))
}
14 changes: 7 additions & 7 deletions src/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ var jobStatus = jobStats{
Stopping: "stopping"}

type taskJob struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
ErrMsg string `json:"err_msg"`
URL string `json:"url"`
Enviroment map[string]string
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Status string `json:"status"`
ErrMsg string `json:"err_msg"`
URL string `json:"url"`
Enviroment map[string]string `json:"-"`
}

type allTaskJobs struct {
Expand Down

0 comments on commit f8a6755

Please sign in to comment.