Skip to content

Commit

Permalink
Implement cron api
Browse files Browse the repository at this point in the history
  • Loading branch information
jushar committed May 29, 2017
1 parent d2b69ca commit c2b5f85
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
16 changes: 6 additions & 10 deletions common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package common
import "github.com/jinzhu/configor"

type Deployment struct {
Name string `required:"true"`
Description string `default:""`
Secret string `required:"true"`
User string `default:"root"`
Autodeploy struct {
Cron string
OnlyOnChange bool
ScriptNoChange []string
}
Script []string `required:"true"`
Name string `required:"true"`
Description string `default:""`
Secret string `required:"true"`
User string `default:"root"`
CronDeploy string `yaml:"cron_deploy"`
Script []string `required:"true"`
}

type Config struct {
Expand Down
6 changes: 1 addition & 5 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ deployments:
secret: deploy2secret
description: "My test deploy service 2"
user: vm
autodeploy:
cron: "@midnight"
only_on_change: true
script_no_change:
- echo "Script that is executed if nothing changed"
cron_deploy: "@every 5s"
script:
- echo "Hello World from mydeploy2"
- whoami
57 changes: 37 additions & 20 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"

"github.com/Jusonex/deployron/common"
"github.com/robfig/cron"
)

var config *common.Config
Expand Down Expand Up @@ -41,6 +42,18 @@ func main() {
// TODO: Make this more fine-grained
os.Chmod(config.Service.Unixsocket, 0777)

// Register cron jobs
cron := cron.New()
for _, deployment := range config.Deployments {
if deployment.CronDeploy != "" {
cron.AddFunc(deployment.CronDeploy, func() {
fmt.Println("[CRON] Launching '" + deployment.Name + "'")
executeDeployScript(deployment.Name)
})
}
}
cron.Start()

fmt.Println("Waiting for commands")

// Wait for commands
Expand Down Expand Up @@ -71,30 +84,34 @@ func main() {
func processMessage(message *common.Message) {
switch message.Identifier {
case "EXC_DEPLOY":
deployment := config.FindDeploymentByName(message.Parameter)
executeDeployScript(message.Parameter)
}
}

if deployment == nil {
fmt.Fprintf(os.Stderr, "Invalid deployment service name passed")
return
}
func executeDeployScript(name string) {
deployment := config.FindDeploymentByName(name)

var commandBuffer bytes.Buffer
for _, line := range deployment.Script {
commandBuffer.WriteString(line)
commandBuffer.WriteString("; ")
}
if deployment == nil {
fmt.Fprintf(os.Stderr, "Invalid deployment service name passed")
return
}

// Prepare deploy script for execution
cmd := exec.Command("su", "-s", "/bin/sh", "-c", commandBuffer.String(), deployment.User)
var commandBuffer bytes.Buffer
for _, line := range deployment.Script {
commandBuffer.WriteString(line)
commandBuffer.WriteString("; ")
}

// Redirect stdout, stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Prepare deploy script for execution
cmd := exec.Command("su", "-s", "/bin/sh", "-c", commandBuffer.String(), deployment.User)

// Run deploy script
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Executing the script failed: %s\n", err.Error())
}
// Redirect stdout, stderr
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

// Run deploy script
err := cmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Executing the script failed: %s\n", err.Error())
}
}

0 comments on commit c2b5f85

Please sign in to comment.