feat: implement getPhaseScript and scaffold out plugin triggers

This commit is contained in:
Jose Diaz-Gonzalez
2020-07-18 14:02:53 -04:00
parent b6d5d291b0
commit 98bc513336
5 changed files with 102 additions and 33 deletions

View File

@@ -0,0 +1,47 @@
package appjson
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/dokku/dokku/plugins/common"
)
type AppJson struct {
Scripts struct {
Dokku string `json:"dokku"`
} `json:"scripts"`
}
// getPhaseScript extracts app.json from app image and returns the appropriate json key/value
func getPhaseScript(appName string, image string, phase string) (string, error) {
appJsonFile, err := ioutil.TempFile(os.TempDir(), fmt.Sprintf("dokku-%s-%s", common.MustGetEnv("DOKKU_PID"), "getPhaseScript"))
if err != nil {
return "", fmt.Errorf("Cannot create temporary file: %v", err)
}
defer os.Remove(appJsonFile.Name())
common.CopyFromImage(appName, image, "app.json", appJsonFile.Name())
if !common.FileExists(appJsonFile.Name()) {
return "", nil
}
b, err := ioutil.ReadAll(appJsonFile)
if err != nil {
return "", fmt.Errorf("Cannot read app.json file: %v", err)
}
var appJson AppJson
if err = json.Unmarshal(b, &appJson); err != nil {
return "", fmt.Errorf("Cannot parse app.json: %v", err)
}
return appJson.Scripts.Dokku, nil
}
func executeScript(appName string, imageTag string, phase string) error {
return nil
}

View File

@@ -1,16 +0,0 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/app-json/internal-functions"
trigger-app-json-post-deploy() {
declare desc="app-json scripts execution"
declare trigger="post-deploy"
declare APP="$1" IMAGE_TAG="$4"
local PHASE_SCRIPT_KEY="postdeploy"
execute_script "$APP" "$IMAGE_TAG" "$PHASE_SCRIPT_KEY"
}
trigger-app-json-post-deploy "$@"

View File

@@ -1,17 +0,0 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/app-json/internal-functions"
trigger-app-json-pre-deploy() {
declare desc="app-json scripts execution"
declare trigger="pre-deploy"
declare APP="$1" IMAGE_TAG="$2"
local PHASE_SCRIPT_KEY="predeploy"
execute_script "$APP" "$IMAGE_TAG" "$PHASE_SCRIPT_KEY"
execute_script "$APP" "$IMAGE_TAG" "release"
}
trigger-app-json-pre-deploy "$@"

View File

@@ -0,0 +1,36 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/app-json"
"github.com/dokku/dokku/plugins/common"
)
// main entrypoint to all triggers
func main() {
parts := strings.Split(os.Args[0], "/")
trigger := parts[len(parts)-1]
flag.Parse()
var err error
switch trigger {
case "post-deploy":
appName := flag.Arg(0)
imageTag := flag.Arg(3)
err = appjson.TriggerPostDeploy(appName, imageTag)
case "pre-deploy":
appName := flag.Arg(0)
imageTag := flag.Arg(1)
err = appjson.TriggerPreDeploy(appName, imageTag)
default:
common.LogFail(fmt.Sprintf("Invalid plugin trigger call: %s", trigger))
}
if err != nil {
common.LogFail(err.Error())
}
}

View File

@@ -0,0 +1,19 @@
package appjson
func TriggerPostDeploy(appName string, imageTag string) error {
if err := executeScript(appName, imageTag, "predeploy"); err != nil {
return err
}
if err := executeScript(appName, imageTag, "release"); err != nil {
return err
}
return nil
}
func TriggerPreDeploy(appName string, imageTag string) error {
if err := executeScript(appName, imageTag, "postdeploy"); err != nil {
return err
}
return nil
}