Skip to content

Commit

Permalink
Merge pull request #20 from Sharpz7/dev
Browse files Browse the repository at this point in the history
Ready to Launch v3.6
  • Loading branch information
Sharpz7 authored Jun 6, 2022
2 parents ea0d0e0 + ff1a6b8 commit 97ae445
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 28 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

# SharpCD || Continuous Development for your server!

![](https://files.mcaq.me/zbnf.png)
SharpCD is a simple, yet powerful, continuous development tool for your server.
It allows you to easily deploy, manage and track docker-compose projects from any location.

![](https://files.mcaq.me/r4844.png)

# Example Config

Everything is controlled by the `sharpcd.yml` file. You can also use
remote configuration files, allowing for easy deployment of multiple projects that can
have multiple dependencies.

```yml
version: 1

Expand All @@ -30,7 +38,7 @@ tasks:
compose: /sharpcd/dev/testing/registry.yml

env_task:
name: Enviroment Test Fail
name: Enviroment Test
type: docker
envfile: .env
sharpurl: https://localhost:5666
Expand Down Expand Up @@ -67,13 +75,13 @@ Args of SharpCD:

Sub Command Trak:

- sharpcd trak alljobs {location} {type}
- sharpcd trak alljobs {location}
Get info on all jobs

- sharpcd trak job {location} {type} {job_id}
- sharpcd trak job {location} {job_id}
Get info on job with logging

- sharpcd trak list {location} {type}
- sharpcd trak list {location}
Get all jobs running on sharpcd server

- sharpcd trak logs {location} {job_id}
Expand Down
4 changes: 4 additions & 0 deletions installs/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ then

read -p "Enter your repository name: " filter_loc
sudo sharpcd addfilter $filter_loc

echo "To get a github token, visit https://github.com/settings/tokens"
read -p "Enter your GitHub token: " token
sudo sharpcd changetoken $token
fi


Expand Down
4 changes: 2 additions & 2 deletions sharpcd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ tasks:
sharpurl: https://localhost:5666
giturl: https://raw.githubusercontent.com/Sharpz7/
compose: /sharpcd/dev/testing/trak.yml
depends:
- file_task
# depends:
# - file_task
1 change: 1 addition & 0 deletions sharpdev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ scripts:
test: |
sharpdev server
sharpdev client
sharpdev client_test_different_url
sharpdev client_test_password
sharpdev kill
Expand Down
6 changes: 6 additions & 0 deletions src/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func runTask(id string, task task, tasksRun *[]string, con config, level int) (r
if len(task.Depends) != 0 {
for _, taskDep := range task.Depends {
level++

// if task is not in con.Tasks, error
if _, ok := con.Tasks[taskDep]; !ok {
fmt.Println("Task", taskDep, "is not in your config.yml")
os.Exit(1)
}
runTask(taskDep, con.Tasks[taskDep], tasksRun, con, level)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ func handleAPI(e error, job *taskJob, msg string) {
job.Status = jobStatus.Errored
}

fmt.Println("DEBUG:", job.ErrMsg, job.Status, e)
if e != nil {
jobText := fmt.Sprintf("{%s}:", job.ID)
fmt.Println("DEBUG [Error Handler]:", jobText, msg, job.Status, e)
fmt.Println()
}
}
25 changes: 24 additions & 1 deletion src/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/url"
"path"
"strings"

"github.com/hashicorp/go-version"
)
Expand All @@ -20,6 +21,9 @@ func httpHandleAPI(w http.ResponseWriter, r *http.Request) {
// Do Common Checks
commonHandlerChecks(r, &status)

// Check if job exists
checkJobExists(r, &status)

resp := response{}
w.WriteHeader(status)

Expand All @@ -30,7 +34,7 @@ func httpHandleAPI(w http.ResponseWriter, r *http.Request) {
if status == statCode.Accepted {

} else {
resp.Message = getFailMessage(status) + " Message: " + resp.Message
resp.Message = getFailMessage(status) + "\nMessage: " + resp.Message
}

json.NewEncoder(w).Encode(resp)
Expand Down Expand Up @@ -101,6 +105,9 @@ func getFailMessage(status int) string {
case statCode.FailedLogRead:
return "SharpCD: Could not read log file for job"

case statCode.JobDoesNotExist:
return "SharpCD: Job does not exist"

default:
return "No Fail Message"
}
Expand Down Expand Up @@ -128,6 +135,22 @@ func commonHandlerChecks(r *http.Request, status *int) postData {
return payload
}

func checkJobExists(r *http.Request, status *int) {

path := strings.Split(r.URL.Path[5:], "/")
jobID := path[1]

if path[0] == "job" {
for _, job := range allJobs {
if job.ID == jobID {
return
}
}

*status = statCode.JobDoesNotExist
}
}

// Check the correct http method is used
func checkMethod(method string) error {
if method != "POST" {
Expand Down
12 changes: 8 additions & 4 deletions src/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (job *taskJob) Run() {
logsLoc := folder.Docker + id
composeLoc := folder.Docker + id + "/docker-compose.yml"

// Flush Logs
_, err := os.Create(logsLoc + "/info.log")
handleAPI(err, job, "Failed to empty log file")

// Mark as building
job.Status = jobStatus.Building

Expand Down Expand Up @@ -112,7 +116,7 @@ func (job *taskJob) Run() {

job.Status = jobStatus.Stopped

fmt.Println("Stopped:", job.ID, "Kill:", job.Kill)
fmt.Println("DEBUG:", job.ID+" was stopped.")

go job.DockerLog(cmd)

Expand All @@ -122,15 +126,15 @@ func (job *taskJob) Run() {
// Log the dockerfile until the job has been killed
func (job *taskJob) DockerLog(cmd *exec.Cmd) {

fmt.Println("Running extended logs")
fmt.Println("Running extended logs for", job.ID)

for job.Kill == false {
cmd.Run()

time.Sleep(1 * time.Second)
}

fmt.Println("Exited:", job.ID)
fmt.Println("DEBUG:", job.ID+" was stopped. (Killed by job.Kill)")
}

// Get cmd for a Docker Job
Expand Down Expand Up @@ -173,7 +177,7 @@ func (job *taskJob) DockerSetup() {

if strings.Contains(string(file), "404") {
err = errors.New("404 in Compose File")
text := "Github Token invalid or wrong compose URL"
text := "Github Token invalid or wrong compose URL: \n" + string(file) + "\n" + url + "\n"
handleAPI(err, job, text)
job.Issue = text
}
Expand Down
13 changes: 3 additions & 10 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,9 @@ func getDir() string {
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)

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

return exPath
}
4 changes: 3 additions & 1 deletion src/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type statusCodes struct {
CommDoesNotExist int
FailedLogRead int
WrongVersion int
JobDoesNotExist int
}

var statCode = statusCodes{
Expand All @@ -27,7 +28,8 @@ var statCode = statusCodes{
FailedLogRead: 614,
BannedURL: 616,
CommDoesNotExist: 617,
WrongVersion: 618}
WrongVersion: 618,
JobDoesNotExist: 619}

var (
upgrader = websocket.Upgrader{
Expand Down
20 changes: 16 additions & 4 deletions src/trak.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func liveFeed() {

urlJobs = con.Trak[location] + "/api/jobs/"

// Insert needed data
secret := getSec()

// Only needed for single job requests
if trakArg == trakOne {
if len(flag.Args()) < 4 {
Expand All @@ -149,10 +152,18 @@ func liveFeed() {

urlJob = con.Trak[location] + "/api/job/" + jobID
urlLog = con.Trak[location] + "/api/logsfeed/" + jobID
}

// Insert needed data
secret := getSec()
// Tests to check if job exists
apiOutput, code := getAPIOutput(urlJob, secret)

if code == statCode.Accepted {
fmt.Printf("Connection to API...")
} else {
fmt.Println(apiOutput.Message)
fmt.Println()
os.Exit(1)
}
}

// Tests to ensure you can actually reach the server
apiOutput, code := getAPIOutput(urlJobs, secret)
Expand All @@ -161,7 +172,8 @@ func liveFeed() {
fmt.Printf("Connection to API...")
} else {
fmt.Println(apiOutput.Message)
fmt.Printf("APi Connection Failed!\n")
fmt.Println()
fmt.Printf("API Connection Failed!\n")
os.Exit(1)
}

Expand Down

0 comments on commit 97ae445

Please sign in to comment.