这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contrib/build-dokku.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ RUN PLUGIN_MAKE_TARGET=${PLUGIN_MAKE_TARGET} \
IS_RELEASE=${IS_RELEASE} \
SKIP_GO_CLEAN=true \
make version copyfiles \
&& rm -rf plugins/common/*.go plugins/common/glide* plugins/common/vendor/ \
&& make deb-dokku deb-sigil \
rpm-dokku rpm-sigil

Expand Down
1 change: 1 addition & 0 deletions plugins/common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/prop
10 changes: 10 additions & 0 deletions plugins/common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
BUILD = prop
PLUGIN_NAME = common

clean-prop:
rm -rf prop

prop: clean-prop **/**/prop.go
go build $(GO_ARGS) -o prop src/prop/prop.go

include ../../common.mk
9 changes: 9 additions & 0 deletions plugins/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ func MustGetEnv(key string) (val string) {
return
}

// GetenvWithDefault returns env variable or defaultValue if it's not set
func GetenvWithDefault(key string, defaultValue string) (val string) {
val = os.Getenv(key)
if val == "" {
val = defaultValue
}
return
}

// ReadFirstLine gets the first line of a file that has contents and returns it
// if there are no contents, an empty string is returned
// will also return an empty string if the file does not exist
Expand Down
110 changes: 85 additions & 25 deletions plugins/common/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func PropertyGetDefault(pluginName, appName, property, defaultValue string) (val

// PropertyListAdd adds a property to a list at an optionally specified index
func PropertyListAdd(pluginName string, appName string, property string, value string, index int) error {
if err := PropertyTouch(pluginName, appName, property); err != nil {
if err := propertyTouch(pluginName, appName, property); err != nil {
return err
}

Expand Down Expand Up @@ -182,6 +182,33 @@ func PropertyListGet(pluginName string, appName string, property string) (lines
return lines, nil
}

// PropertyListLength returns the length of a property list
func PropertyListLength(pluginName string, appName string, property string) (length int, err error) {
if !PropertyExists(pluginName, appName, property) {
return length, nil
}

propertyPath := getPropertyPath(pluginName, appName, property)
file, err := os.Open(propertyPath)
if err != nil {
return length, err
}
defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

if err = scanner.Err(); err != nil {
return length, fmt.Errorf("Unable to read %s config value for %s.%s: %s", pluginName, appName, property, err.Error())
}

length = len(lines)
return length, nil
}

// PropertyListGetByIndex returns an entry within property list by index
func PropertyListGetByIndex(pluginName string, appName string, property string, index int) (propertyValue string, err error) {
lines, err := PropertyListGet(pluginName, appName, property)
Expand Down Expand Up @@ -262,9 +289,45 @@ func PropertyListRemove(pluginName string, appName string, property string, valu
return nil
}

// PropertyListRemoveByPrefix removes a value by prefix from a property list
func PropertyListRemoveByPrefix(pluginName string, appName string, property string, prefix string) error {
lines, err := PropertyListGet(pluginName, appName, property)
if err != nil {
return err
}

propertyPath := getPropertyPath(pluginName, appName, property)
file, err := os.OpenFile(propertyPath, os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return err
}

found := false
w := bufio.NewWriter(file)
for _, line := range lines {
if strings.HasPrefix(line, prefix) {
found = true
continue
}
fmt.Fprintln(w, line)
}
if err = w.Flush(); err != nil {
return fmt.Errorf("Unable to write %s config value %s.%s: %s", pluginName, appName, property, err.Error())
}

file.Chmod(0600)
setPermissions(propertyPath, 0600)

if !found {
return errors.New("Property not found, nothing was removed")
}

return nil
}

// PropertyListSet sets a value within a property list at a specified index
func PropertyListSet(pluginName string, appName string, property string, value string, index int) error {
if err := PropertyTouch(pluginName, appName, property); err != nil {
if err := propertyTouch(pluginName, appName, property); err != nil {
return err
}

Expand Down Expand Up @@ -310,8 +373,8 @@ func PropertyListSet(pluginName string, appName string, property string, value s
return nil
}

// PropertyTouch ensures a given application property file exists
func PropertyTouch(pluginName string, appName string, property string) error {
// propertyTouch ensures a given application property file exists
func propertyTouch(pluginName string, appName string, property string) error {
if err := makePluginAppPropertyPath(pluginName, appName); err != nil {
return fmt.Errorf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error())
}
Expand All @@ -332,7 +395,7 @@ func PropertyTouch(pluginName string, appName string, property string) error {

// PropertyWrite writes a value for a given application property
func PropertyWrite(pluginName string, appName string, property string, value string) error {
if err := PropertyTouch(pluginName, appName, property); err != nil {
if err := propertyTouch(pluginName, appName, property); err != nil {
return err
}

Expand All @@ -350,10 +413,13 @@ func PropertyWrite(pluginName string, appName string, property string, value str
}

// PropertySetup creates the plugin config root
func PropertySetup(pluginName string) (err error) {
func PropertySetup(pluginName string) error {
pluginConfigRoot := getPluginConfigPath(pluginName)
if err = os.MkdirAll(pluginConfigRoot, 0755); err != nil {
return
if err := os.MkdirAll(pluginConfigRoot, 0755); err != nil {
return err
}
if err := setPermissions(path.Join(MustGetEnv("DOKKU_LIB_ROOT"), "config"), 0755); err != nil {
return err
}
return setPermissions(pluginConfigRoot, 0755)
}
Expand All @@ -374,46 +440,40 @@ func getPluginConfigPath(pluginName string) string {
}

// makePluginAppPropertyPath ensures that a property path exists
func makePluginAppPropertyPath(pluginName string, appName string) (err error) {
func makePluginAppPropertyPath(pluginName string, appName string) error {
pluginAppConfigRoot := getPluginAppPropertyPath(pluginName, appName)
if err = os.MkdirAll(pluginAppConfigRoot, 0755); err != nil {
return
if err := os.MkdirAll(pluginAppConfigRoot, 0755); err != nil {
return err
}
return setPermissions(pluginAppConfigRoot, 0755)
}

// setPermissions sets the proper owner and filemode for a given file
func setPermissions(path string, fileMode os.FileMode) (err error) {
if err = os.Chmod(path, fileMode); err != nil {
func setPermissions(path string, fileMode os.FileMode) error {
if err := os.Chmod(path, fileMode); err != nil {
return err
}

systemGroup := os.Getenv("DOKKU_SYSTEM_GROUP")
systemUser := os.Getenv("DOKKU_SYSTEM_USER")
if systemGroup == "" {
systemGroup = "dokku"
}
if systemUser == "" {
systemUser = "dokku"
}
systemGroup := GetenvWithDefault("DOKKU_SYSTEM_GROUP", "dokku")
systemUser := GetenvWithDefault("DOKKU_SYSTEM_USER", "dokku")

group, err := user.LookupGroup(systemGroup)
if err != nil {
return
return err
}
user, err := user.Lookup(systemUser)
if err != nil {
return
return err
}

uid, err := strconv.Atoi(user.Uid)
if err != nil {
return
return err
}

gid, err := strconv.Atoi(group.Gid)
if err != nil {
return
return err
}
return os.Chown(path, uid, gid)
}
129 changes: 63 additions & 66 deletions plugins/common/property-functions
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,99 @@
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

fn-plugin-property-delete() {
declare desc="delete a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3"

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
fn-plugin-property-get() {
declare desc="returns the value for a given property"
declare PLUGIN="$1" APP="$2" KEY="$3" DEFAULT="$4"

rm -f "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
fn-plugin-property-get-default "$PLUGIN" "$APP" "$KEY" "$DEFAULT"
}

fn-plugin-property-destroy() {
declare desc="destroy the properties for an app"
declare PLUGIN="$1" APP="$2"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "destroy" "$PLUGIN" "$APP" "$KEY"
}

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]]; then
return 1
fi

if [[ "$APP" == "_all_" ]]; then
rm -rf "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
else
rm -rf "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}"
fi
fn-plugin-property-delete() {
declare desc="delete a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "del" "$PLUGIN" "$APP" "$KEY"
}

fn-plugin-property-exists() {
declare desc="returns whether the property store has a value for an app"
declare PLUGIN="$1" APP="$2" KEY="$3" DEFAULT="$4"
local CONFIG_VALUE

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
declare PLUGIN="$1" APP="$2" KEY="$3"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "exists" "$PLUGIN" "$APP" "$KEY"
}

if [[ ! -f "${DOKKU_LIB_ROOT:?}/config/${PLUGIN}/${APP}/${KEY}" ]]; then
return 1
fi
fn-plugin-property-get-all() {
declare desc="returns a map of all properties for a given app"
declare PLUGIN="$1" APP="$2"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "get-all" "$PLUGIN" "$APP"
}

fn-plugin-property-get() {
declare desc="returns the property store value for an app"
fn-plugin-property-get-default() {
declare desc="returns the value for a given property with a specified default value"
declare PLUGIN="$1" APP="$2" KEY="$3" DEFAULT="$4"
local CONFIG_VALUE

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi

CONFIG_VALUE="$(fn-plugin-property-read "$PLUGIN" "$APP" "$KEY" || true)"
if [[ -z "$CONFIG_VALUE" ]]; then
CONFIG_VALUE="$DEFAULT"
fi
echo "$CONFIG_VALUE"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "get-with-default" "$PLUGIN" "$APP" "$KEY" "$DEFAULT"
}

fn-plugin-property-setup() {
declare desc="creates the plugin config root"
declare PLUGIN="$1"
fn-plugin-property-list-add() {
declare desc="adds a property to a list at an optionally specified index"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4" INDEX="$5"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "rpush" "$PLUGIN" "$APP" "$KEY" "$VALUE" "$INDEX"
}

if [[ -z "$PLUGIN" ]]; then
return 1
fi
fn-plugin-property-list-get() {
declare desc="returns a property list"
declare PLUGIN="$1" APP="$2" KEY="$3"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lrange" "$PLUGIN" "$APP" "$KEY"
}

mkdir -p "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
chmod 0755 "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
fn-plugin-property-list-get-by-index() {
declare desc="returns an entry within property list by index"
declare PLUGIN="$1" APP="$2" KEY="$3" INDEX="$4"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lindex" "$PLUGIN" "$APP" "$KEY" "$INDEX"
}

local dokku_system_group=${DOKKU_SYSTEM_GROUP:="dokku"}
local dokku_system_user=${DOKKU_SYSTEM_USER:="dokku"}
chown "${dokku_system_user}:${dokku_system_group}" "${DOKKU_LIB_ROOT}/config/${PLUGIN}"
fn-plugin-property-list-get-by-value() {
declare desc="returns an entry within property list by value"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lismember" "$PLUGIN" "$APP" "$KEY" "$VALUE"
}

fn-plugin-property-read() {
declare desc="read a key from the property store for an app"
fn-plugin-property-list-length() {
declare desc="returns a property list"
declare PLUGIN="$1" APP="$2" KEY="$3"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "llen" "$PLUGIN" "$APP" "$KEY"
}

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi
fn-plugin-property-list-remove() {
declare desc="removes a value from a property list"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lrem" "$PLUGIN" "$APP" "$KEY" "$VALUE"
}

if [[ ! -f "${DOKKU_LIB_ROOT:?}/config/${PLUGIN}/${APP}/${KEY}" ]]; then
return 1
fi
fn-plugin-property-list-remove-by-prefix() {
declare desc="removes a value from a property list"
declare PLUGIN="$1" APP="$2" KEY="$3" PREFIX="$4"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lrem-by-prefix" "$PLUGIN" "$APP" "$KEY" "$PREFIX"
}

cat "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
fn-plugin-property-list-set() {
declare desc="sets a value within a property list at a specified index"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4" INDEX="$5"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "lset" "$PLUGIN" "$APP" "$KEY" "$VALUE" "$INDEX"
}

fn-plugin-property-write() {
declare desc="read a key from the property store for an app"
declare PLUGIN="$1" APP="$2" KEY="$3" VALUE="$4"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "set" "$PLUGIN" "$APP" "$KEY" "$VALUE"
}

if [[ -z "$PLUGIN" ]] || [[ -z "$APP" ]] || [[ -z "$KEY" ]]; then
return 1
fi

mkdir -p "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}"
echo "$VALUE" >"${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
chmod 600 "${DOKKU_LIB_ROOT}/config/${PLUGIN}/${APP}/${KEY}"
fn-plugin-property-setup() {
declare desc="creates the plugin config root"
declare PLUGIN="$1"
"$PLUGIN_CORE_AVAILABLE_PATH/common/prop" "setup" "$PLUGIN"
}
Loading