Skip to content

Commit

Permalink
More testing and bugfixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharpz7 committed Jun 6, 2020
1 parent 6a8278c commit 05fd398
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 72 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 Task
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 Task 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 Task
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: No Compose File Task
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
2 changes: 2 additions & 0 deletions sharpdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ scripts:
sharpdev build &&
./internal/sharpcd --secret Secret123
job: curl -k -X POST -d SECRET https://localhost:5666/api/job/${SHARP_ARG_1}
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/

5 changes: 3 additions & 2 deletions src/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func client() {
// Make POST request and let user know if successful
body, code := post(payload, task.SharpURL)
if code == statCode.Accepted {
fmt.Printf("Task %s succesfully sent!\n\n", task.Name)
fmt.Printf("Task [%s] succesfully sent!\n", task.Name)
fmt.Println("=================")
err = postCommChecks(task, id)
if err != nil {
Expand Down Expand Up @@ -161,9 +161,10 @@ func postCommChecks(t task, id string) error {
}

stopped := job.Status == jobStatus.Stopped
errored := job.Status == jobStatus.Errored
logsError := strings.Contains(logFile, "exited with code")

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

Expand Down
65 changes: 39 additions & 26 deletions src/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ func createJob(payload postData) {
newJob := taskJob{
Name: payload.Name,
Type: payload.Type,
URL: payload.GitURL + payload.Compose}

// Load Env Vars in
loadEnv(payload.Enviroment)
URL: payload.GitURL + payload.Compose,
Enviroment: payload.Enviroment}

// If a job with that ID already exists
if job := getJob(payload.ID); job != nil {
Expand All @@ -44,7 +42,6 @@ func createJob(payload postData) {
newJob.ID = job.ID

// Stop the old job
job.Status = jobStatus.Stopping
job.Stop()

// Replace and run the new job
Expand All @@ -70,23 +67,30 @@ func (job *taskJob) Run() {
// Run the correct job Type
switch job.Type {
case "docker":
cmd = job.DockerRun()
cmd = job.DockerCmd()
}

// Mark as Running
job.Status = jobStatus.Running
err := cmd.Run()
handleAPI(err, job, "Job Exited With Error")
// If setting up the command went fine
if job.Status != jobStatus.Errored {

// Run Command
job.Status = jobStatus.Running
err := cmd.Run()
handleAPI(err, job, "Job Exited With Error")

// When finished, mark as stopped
job.Status = jobStatus.Stopped
// When finished, mark as stopped
job.Status = jobStatus.Stopped
}
}

// Stop a job Task
func (job *taskJob) Stop() {

var cmd *exec.Cmd

// Mark Job as stopping, Clear error Message
job.Status = jobStatus.Stopping

// Makes sure to run the correct stop sequence
switch job.Type {
case "docker":
Expand All @@ -107,24 +111,15 @@ func (job *taskJob) DockerStop() *exec.Cmd {
return cmd
}

// Load in Enviroment from postData
func loadEnv(data map[string]string) {
for key, val := range data {
os.Setenv(key, val)
}
}

// Run sequence for a Docker job
func (job *taskJob) DockerRun() *exec.Cmd {
// Get cmd for a Docker Job
func (job *taskJob) DockerCmd() *exec.Cmd {

// All Location Data
id := job.ID
url := job.URL
logsLoc := folder.Logs + id
composeLoc := folder.Docker + id + "/docker-compose.yml"

job.Status = jobStatus.Building

// Make url, read the compose file
resp, err := http.Get(url)
handleAPI(err, job, "Failed to get compose URL")
Expand All @@ -133,14 +128,21 @@ func (job *taskJob) DockerRun() *exec.Cmd {
handleAPI(err, job, "Failed to read compose file")

// Make directory for docker and logs and save file
os.Mkdir(folder.Docker + id, 0777)
os.Mkdir(folder.Docker+id, 0777)
os.Mkdir(logsLoc, 0777)
err = ioutil.WriteFile(composeLoc, file, 0777)
handleAPI(err, job, "Failed to write to file")

// Build Commands

// 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))

Expand All @@ -149,8 +151,19 @@ func (job *taskJob) DockerRun() *exec.Cmd {

outfile, err := os.Create(logsLoc + "/info.log")
handleAPI(err, job, "Failed to create log file")
cmd.Env = os.Environ()
cmd.Env = job.insertEnviroment()
cmd.Stdout = outfile

return cmd
}

func (job *taskJob) insertEnviroment() []string {
var environ []string

for key, val := range job.Enviroment {
str := key + "=" + val
environ = append(environ, str)
}

return environ
}
13 changes: 7 additions & 6 deletions src/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +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"`
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
}

type allTaskJobs struct {
Expand Down
2 changes: 1 addition & 1 deletion testing/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
hello_world:
image: ubuntu
command: tail -F anything
command: tail -f /dev/null

environment:
TEST: ${TEST:?err}

0 comments on commit 05fd398

Please sign in to comment.