Skip to content

Commit

Permalink
Merge pull request #8 from Sharpz7/developer
Browse files Browse the repository at this point in the history
Updated to version 0.1.8: Private Docker logins
  • Loading branch information
Sharpz7 authored Jan 25, 2021
2 parents 7746d2c + 05d6750 commit 2002130
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
8 changes: 5 additions & 3 deletions sharpdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: 0.1
envfile: .env

values:
SECRETD: '{\"secret\":\"$PASSD\"}'
SECRET: '{\"secret\":\"$PASS\"}'

scripts:
Expand All @@ -23,13 +24,14 @@ scripts:
$(sudo docker volume rm memes) > /dev/null 2>&1 || true &&
$(sudo docker network rm memes) > /dev/null 2>&1 || true &&
sharpdev build &&
./internal/sharpcd --secret Secret123
./internal/sharpcd --secret $PASSD
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}
job: curl -k -X POST -d SECRETD https://localhost:5666/api/job/${SHARP_ARG_1}
logs: curl -k -X POST -d SECRETD 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/
token: ./internal/sharpcd changetoken ${SHARP_ARG_1}
alljobsd: curl -k -X POST -d SECRETD https://localhost:5666/api/jobs
alljobs: curl -k -X POST -d SECRET https://173.212.252.82:5666/api/jobs

file: |
Expand Down
7 changes: 0 additions & 7 deletions src/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ func postCommChecks(t task, id string) error {
payload := postData{
Secret: getSec()}
buildingTriggered := false
stoppingTriggered := false
runningTriggered := false
counter := 0

Expand All @@ -145,12 +144,6 @@ func postCommChecks(t task, id string) error {
errored := job.Status == jobStatus.Errored
building := job.Status == jobStatus.Building && !buildingTriggered
running := job.Status == jobStatus.Running && !runningTriggered
stopping := job.Status == jobStatus.Stopping && !stoppingTriggered

if stopping {
stoppingTriggered = true
fmt.Println("The Task already exists on server. Stopping old job...")
}

if building {
buildingTriggered = true
Expand Down
54 changes: 16 additions & 38 deletions src/jobs.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"time"
)

// Pointer to variable storing all jobs
Expand Down Expand Up @@ -46,9 +46,6 @@ func createJob(payload postData) {
// Assign its ID to the old job ID
newJob.ID = job.ID

// Stop the old job
job.Stop()

// Replace and run the new job
*job = newJob
job.Run()
Expand Down Expand Up @@ -89,38 +86,6 @@ func (job *taskJob) Run() {
}
}

// 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":
cmd = job.DockerStop()
}

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
func (job *taskJob) DockerStop() *exec.Cmd {
composeLoc := folder.Docker + job.ID + "/docker-compose.yml"

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

return cmd
}

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

Expand Down Expand Up @@ -175,11 +140,15 @@ func (job *taskJob) DockerCmd() *exec.Cmd {
if err != nil {
return nil
}

// Remove any previous containers
job.buildCommand("-f", composeLoc, "down")

// Run Code
job.buildCommand("-f", composeLoc, "up", "-d")

if job.Registry != "" {
job.dockerLogout()
}
}

// Get logging Running
Expand All @@ -204,7 +173,15 @@ func (job *taskJob) insertEnviroment() []string {
}

func (job *taskJob) dockerLogin() {
cmd := exec.Command("docker", "login", job.Registry, "-u", job.Enviroment["DOCKER_USER"], "-p", job.Enviroment["DOCKER_PASS"])
cmd := exec.Command("docker", "login", "-u", job.Enviroment["DOCKER_USER"], "-p", job.Enviroment["DOCKER_PASS"], job.Registry)
out, err := cmd.CombinedOutput()
errMsg := string(out)
fmt.Println(errMsg)
handleAPI(err, job, errMsg)
}

func (job *taskJob) dockerLogout() {
cmd := exec.Command("docker", "logout", job.Registry)
out, err := cmd.CombinedOutput()
errMsg := string(out)
handleAPI(err, job, errMsg)
Expand All @@ -215,6 +192,7 @@ func (job *taskJob) buildCommand(args ...string) error {
cmd := exec.Command("docker-compose", args...)
cmd.Env = job.insertEnviroment()
out, err := cmd.CombinedOutput()
fmt.Println(string(out))

// Add conditions for volumes and networks
if strings.Contains(string(out), "404") {
Expand Down

0 comments on commit 2002130

Please sign in to comment.