From c2b5f85c5340d92c2877d39c34edab13c3938c8a Mon Sep 17 00:00:00 2001 From: Jusonex Date: Mon, 29 May 2017 15:40:05 +0200 Subject: [PATCH] Implement cron api --- common/config.go | 16 +++++-------- config.yml | 6 +---- service/service.go | 57 ++++++++++++++++++++++++++++++---------------- 3 files changed, 44 insertions(+), 35 deletions(-) diff --git a/common/config.go b/common/config.go index 0570f9c..cbce2a3 100644 --- a/common/config.go +++ b/common/config.go @@ -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 { diff --git a/config.yml b/config.yml index 6122660..3c81881 100644 --- a/config.yml +++ b/config.yml @@ -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 diff --git a/service/service.go b/service/service.go index 5501b63..85f8dd2 100644 --- a/service/service.go +++ b/service/service.go @@ -9,6 +9,7 @@ import ( "syscall" "github.com/Jusonex/deployron/common" + "github.com/robfig/cron" ) var config *common.Config @@ -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 @@ -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()) } }