Skip to content

Commit

Permalink
Merge pull request #10 from Sharpz7/dev
Browse files Browse the repository at this point in the history
Version  0.1.10
  • Loading branch information
Sharpz7 authored Aug 12, 2021
2 parents 558cea3 + 621ec3d commit 5b9ed51
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.env
.env
.vscode
__debug_bin
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION=0.1.9
VERSION=0.1.10
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Installation
===========
On linux, just run:
```console
adam@console~$ sudo curl -s -L https://github.com/Sharpz7/sharpcd/releases/download/0.1.9/install.sh | sudo bash
adam@console~$ sudo curl -s -L https://github.com/Sharpz7/sharpcd/releases/download/0.1.10/install.sh | sudo bash
```

Maintainers and Developers
Expand Down
1 change: 1 addition & 0 deletions gopack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ packages:
- golang.org/x/crypto/bcrypt
- github.com/joho/godotenv
- golang.org/x/term
- github.com/hashicorp/go-version
devpackages:
- golang.org/x/sys/unix
16 changes: 8 additions & 8 deletions sharpcd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tasks:
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/developer/testing/basic.yml
compose: /sharpcd/dev/testing/basic.yml

registry_task:
name: Registry
Expand All @@ -15,48 +15,48 @@ tasks:
envfile: .env
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/developer/testing/registry.yml
compose: /sharpcd/dev/testing/registry.yml

env_task:
name: Enviroment Test Fail
type: docker
envfile: .env
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/developer/testing/env.yml
compose: /sharpcd/dev/testing/env.yml

external_task:
name: External Docker Parts
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/developer/testing/external.yml
compose: /sharpcd/dev/testing/external.yml

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

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

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

Restart_task:
name: Test Log exit
type: docker
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/developer/testing/restart.yml
compose: /sharpcd/dev/testing/restart.yml
39 changes: 23 additions & 16 deletions sharpdev.yml
Original file line number Diff line number Diff line change
@@ -1,43 +1,50 @@
version: 0.1
version: 1.0
envfile: .env

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

scripts:
build: go build -o ./internal/sharpcd ./src
revert: git revert ${SHARP_ARG_1}..HEAD
build: |
go build -o ./internal/sharpcd ./src
revert: git revert $_ARG1..HEAD
list: git branch
archive: |
git tag archive/${SHARP_ARG_1} ${SHARP_ARG_1} &&
git branch -d ${SHARP_ARG_1}
git tag archive/$_ARG1 $_ARG1
git branch -d $_ARG1
server: |
sudo apt-get install -y lsof &&
sudo kill $(sudo lsof -t -i:5666) > /dev/null 2>&1 || true &&
sharpdev build &&
sudo apt-get install -y lsof
sudo kill $(sudo lsof -t -i:5666) > /dev/null 2>&1 || true
sharpdev build
sudo ./internal/sharpcd server
client: |
sudo docker-compose -f "./internal/sharpcd-data/docker/external_task/docker-compose.yml" down &&
$(sudo docker volume rm memes) > /dev/null 2>&1 || true &&
$(sudo docker network rm memes) > /dev/null 2>&1 || true &&
sharpdev build &&
sudo docker-compose -f "./internal/sharpcd-data/docker/external_task/docker-compose.yml" down
$(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 $PASSD
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}
job: curl -k -X POST -d SECRETD https://localhost:5666/api/job/$_ARG1
logs: curl -k -X POST -d SECRETD https://localhost:5666/api/logs/$_ARG1
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}
token: ./internal/sharpcd changetoken $_ARG1
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: |
curl -H 'Authorization: token ${SHARP_ARG_1}' \
curl -H 'Authorization: token $_ARG1' \
-H 'Accept: application/vnd.github.v3.raw' \
-O \
-L https://raw.githubusercontent.com/Sharpz7/stuandbob/main/docker-compose.yml
test: |
git checkout dev
git push origin dev
sharpdev client
10 changes: 9 additions & 1 deletion src/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func client() {
Compose: task.Compose,
Enviroment: getEnviroment(task.Envfile),
Registry: task.Registry,
Secret: getSec()}
Secret: getSec(),
Version: sharpCDVersion}

// Make POST request and let user know if successful
body, code := post(payload, task.SharpURL)
Expand All @@ -59,6 +60,7 @@ func client() {
} else {
fmt.Println(body.Message)
fmt.Printf("Task %s Failed!\n\n", task.Name)
os.Exit(1)
}
}

Expand Down Expand Up @@ -125,6 +127,7 @@ func postCommChecks(t task, id string) error {
Secret: getSec()}
buildingTriggered := false
runningTriggered := false
lastIssue := ""
counter := 0

fmt.Println("Waiting on server response...")
Expand Down Expand Up @@ -175,6 +178,11 @@ func postCommChecks(t task, id string) error {
return errors.New("Bad Task")
}

if lastIssue != job.Issue {
fmt.Println("No fatal Issue found: " + job.Issue)
lastIssue = job.Issue
}

if counter > 7 {
fmt.Println("Task has started Properly!")
return nil
Expand Down
6 changes: 4 additions & 2 deletions src/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"log"
"os"
Expand All @@ -11,14 +12,15 @@ import (
// Checks for client err
// Records as Fatal
func handle(e error, msg string) {
// Try and get SHARPDEV var
// Try and get DEV var
godotenv.Load()

if e != nil {
if os.Getenv("SHARPDEV") == "TRUE" {
if os.Getenv("DEV") == "TRUE" {
fmt.Println(e)
}
log.Fatal(msg)
flag.Usage()
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"net/url"
"path"

"github.com/hashicorp/go-version"
)

// Hander for requests made to /api/
Expand Down Expand Up @@ -48,6 +50,10 @@ func httpHandleTask(w http.ResponseWriter, r *http.Request) {
err := checkURL(payload.GitURL)
handleStatus(err, statCode.BannedURL, &status)

// Check Version
err = checkVersion(payload.Version)
handleStatus(err, statCode.WrongVersion, &status)

// Get task Type
err = checkTaskType(payload)
handleStatus(err, statCode.CommDoesNotExist, &status)
Expand Down Expand Up @@ -84,6 +90,9 @@ func getFailMessage(status int) string {
case statCode.IncorrectSecret:
return "SharpCD: Incorrect Secret"

case statCode.WrongVersion:
return "SharpCD: Wrong Client Version, expected " + sharpCDVersion + " or above."

case statCode.NotPostMethod:
return "SharpCD: Only accepting POST requests"

Expand Down Expand Up @@ -129,6 +138,17 @@ func checkMethod(method string) error {
return nil
}

func checkVersion(clientVersion string) error {
v1, err := version.NewVersion(clientVersion)
v2, err := version.NewVersion(sharpCDVersion)

if v1.LessThan(v2) {
err = errors.New("Wrong Client Version")
}

return err
}

// Checks if URLs are okay
func checkURL(taskURL string) error {

Expand Down
33 changes: 28 additions & 5 deletions src/jobs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -80,8 +81,8 @@ func (job *taskJob) Run() {
err := cmd.Run()
handleAPI(err, job, "Job Exited With Error")

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

}
}

Expand Down Expand Up @@ -109,10 +110,9 @@ func (job *taskJob) DockerCmd() *exec.Cmd {

if f.Token != "" {
req.Header.Set("Authorization", "token "+f.Token)
req.Header.Set("Accept", "application/vnd.github.v3.raw")
}

req.Header.Set("Accept", "application/vnd.github.v3.raw")

resp, err := http.DefaultClient.Do(req)
if err != nil {
handleAPI(err, job, "Failed to get compose URL")
Expand All @@ -124,8 +124,30 @@ func (job *taskJob) DockerCmd() *exec.Cmd {
// Make directory for docker and logs and save file
os.Mkdir(folder.Docker+id, 0777)
os.Mkdir(logsLoc, 0777)
err = ioutil.WriteFile(composeLoc, file, 0777)
handleAPI(err, job, "Failed to write to file")

if strings.Contains(string(file), "404") {
err = errors.New("404 in Compose File")
text := "Github Token invalid or wrong compose URL"
handleAPI(err, job, text)
job.Issue = text
}

// If Token was valid and compose file not empty
if err == nil {

// Ensure ComposeLoc is Empty
_, err = os.Stat(composeLoc)
if err == nil {
err = os.Remove(composeLoc)
if err != nil {
handleAPI(err, job, "Failed to Remove")
}
}

// Write to file
err = ioutil.WriteFile(composeLoc, file, 0777)
handleAPI(err, job, "Failed to write to file")
}

if job.Registry != "" {
job.dockerLogin()
Expand Down Expand Up @@ -153,6 +175,7 @@ func (job *taskJob) DockerCmd() *exec.Cmd {

// Get logging Running
cmd := exec.Command("docker-compose", "-f", composeLoc, "logs", "-f", "--no-color")

outfile, err := os.Create(logsLoc + "/info.log")
handleAPI(err, job, "Failed to create log file")
cmd.Env = job.insertEnviroment()
Expand Down
22 changes: 18 additions & 4 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func main() {
switch arg1 {
case "server":
server()
case "help":
flag.Usage()
case "setsecret":
setSec()
case "addfilter":
Expand All @@ -56,17 +58,29 @@ func main() {
case "changetoken":
changeToken()
case "version":
fmt.Println("Version: 0.1.9")
fmt.Println("Version: " + sharpCDVersion)
default:
log.Fatal("This subcommand does not exist!")
flag.Usage()
}
}
return
}

func getDir() string {
ex, err := os.Executable()
handle(err, "Failed to get dir")
exPath := filepath.Dir(ex)
var exPath string
var err error

if os.Getenv("DEV") == "TRUE" {
exPath, err = os.Getwd()
handle(err, "Failed to get dir")

} else {
ex, err := os.Executable()
handle(err, "Failed to get dir")
exPath = filepath.Dir(ex)

}

return exPath
}
Loading

0 comments on commit 5b9ed51

Please sign in to comment.