feat: add trigger to allow retrieving resource properties on the fly

This commit is contained in:
Jose Diaz-Gonzalez
2019-04-08 11:31:18 -04:00
parent 243d5ecc43
commit c2898ec040
4 changed files with 49 additions and 2 deletions

View File

@@ -6,3 +6,4 @@
/report
/docker-args-process-deploy
/docker-args-process-run
/resource-get-property

View File

@@ -3,7 +3,7 @@ include ../../common.mk
GO_ARGS ?= -a
SUBCOMMANDS = subcommands/limit subcommands/limit-clear subcommands/report subcommands/reserve subcommands/reserve-clear
TRIGGERS = triggers/docker-args-process-deploy triggers/install triggers/post-delete triggers/report
TRIGGERS = triggers/docker-args-process-deploy triggers/install triggers/post-delete triggers/report triggers/resource-get-property
build-in-docker: clean
docker run --rm \
-v $$PWD/../..:$(GO_REPO_ROOT) \
@@ -23,7 +23,7 @@ subcommands/%: src/subcommands/*/%.go
go build $(GO_ARGS) -o $@ $<
clean:
rm -rf commands subcommands triggers docker-args-process-deploy docker-args-process-run install post-delete report
rm -rf commands subcommands triggers docker-args-process-deploy docker-args-process-run install post-delete report resource-get-property
src-clean:
rm -rf .gitignore src triggers vendor Makefile *.go

View File

@@ -1,6 +1,7 @@
package resource
import (
"errors"
"fmt"
"os"
"reflect"
@@ -66,6 +67,25 @@ func ReportSingleApp(appName, infoFlag string) {
common.LogFail(fmt.Sprintf("Invalid flag passed, valid flags: %s", strings.Join(strkeys, ", ")))
}
func GetResourceValue(appName string, procType string, resourceType string, prefix string) (string, error) {
resources, err := common.PropertyGetAll("resource", appName)
if err != nil {
return "", err
}
for key, value := range resources {
parts := strings.SplitN(strings.TrimPrefix(key, prefix), ".", 2)
if parts[0] != resourceType {
return "", err
}
if parts[1] == prefix {
return value, nil
}
}
return "", errors.New("No value found")
}
func times(str string, n int) (out string) {
for i := 0; i < n; i++ {
out += str

View File

@@ -0,0 +1,26 @@
package main
import (
"flag"
"fmt"
"os"
"github.com/dokku/dokku/plugins/common"
"github.com/dokku/dokku/plugins/resource"
)
// writes the resource property to stdout for a given app container
func main() {
flag.Parse()
appName := flag.Arg(0)
procType := flag.Arg(1)
resourceType := flag.Arg(1)
property := flag.Arg(1)
value, err := resource.GetResourceValue(appName, procType, resourceType, property)
if err != nil {
common.LogFail(err.Error())
}
fmt.Fprintln(os.Stdout, value)
}