refactor: move the env files to the new path on upgrade

Refs #1558
This commit is contained in:
Jose Diaz-Gonzalez
2024-03-14 03:05:46 -04:00
parent 38c6b180af
commit f2f6e0b473
6 changed files with 92 additions and 1 deletions

View File

@@ -541,6 +541,10 @@ func PropertySetup(pluginName string) error {
}
func PropertySetupApp(pluginName string, appName string) error {
if err := PropertySetup(pluginName); err != nil {
return err
}
if err := makePluginAppPropertyPath(pluginName, appName); err != nil {
return fmt.Errorf("Unable to create %s config directory for %s: %s", pluginName, appName, err.Error())
}

View File

@@ -5,3 +5,4 @@
/config-*
/config_sub
/post-*
/install

View File

@@ -1,6 +1,6 @@
GOARCH ?= amd64
SUBCOMMANDS = subcommands/bundle subcommands/clear subcommands/export subcommands/get subcommands/import subcommands/keys subcommands/show subcommands/set subcommands/unset
TRIGGERS = triggers/config-export triggers/config-get triggers/config-get-global triggers/config-set triggers/config-unset triggers/post-app-clone-setup triggers/post-app-rename-setup
TRIGGERS = triggers/config-export triggers/config-get triggers/config-get-global triggers/install triggers/config-set triggers/config-unset triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-create triggers/post-delete
BUILD = commands config_sub subcommands triggers
PLUGIN_NAME = config

View File

@@ -51,6 +51,8 @@ func main() {
restart = flag.Arg(1)
}
err = config.TriggerConfigUnset(appName, key, common.ToBool(restart))
case "install":
err = config.TriggerInstall()
case "post-app-clone-setup":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
@@ -59,6 +61,12 @@ func main() {
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
err = config.TriggerPostAppRenameSetup(oldAppName, newAppName)
case "post-create":
appName := flag.Arg(0)
err = config.TriggerPostCreate(appName)
case "post-delete":
appName := flag.Arg(0)
err = config.TriggerPostDelete(appName)
default:
err = fmt.Errorf("Invalid plugin trigger call: %s", trigger)
}

View File

@@ -2,7 +2,11 @@ package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/dokku/dokku/plugins/common"
)
// TriggerConfigExport returns a global config value by key
@@ -56,6 +60,65 @@ func TriggerConfigUnset(appName string, key string, restart bool) error {
return nil
}
// TriggerInstall runs the install step for the config plugin
func TriggerInstall() error {
if err := common.PropertySetup("config"); err != nil {
return fmt.Errorf("Unable to install the config plugin: %s", err.Error())
}
apps, err := common.UnfilteredDokkuApps()
if err != nil {
return nil
}
// migrate all created-at values from app mod-time to property
for _, appName := range apps {
oldEnvFile := filepath.Join(common.AppRoot(appName) + "ENV")
isMigrated := common.PropertyGetDefault("config", appName, "env-migrated", "")
// delete the old file on the next install
if isMigrated == "true" {
if err := os.RemoveAll(oldEnvFile); err != nil {
return fmt.Errorf("Unable to remove old ENV file: %s", err.Error())
}
continue
}
// skip if the file doesn't exist
if _, err := os.Stat(oldEnvFile); err != nil {
if err := common.PropertyWrite("config", appName, "env-migrated", "true"); err != nil {
return fmt.Errorf("Unable to set env-migrated property: %s", err.Error())
}
continue
}
if err := common.PropertySetupApp("config", appName); err != nil {
return fmt.Errorf("Unable to setup app environment: %s", err.Error())
}
// merge in the old env into the new env
oldEnv, err := loadFromFile(appName, oldEnvFile)
if err != nil {
return fmt.Errorf("Unable to load old environment: %s", err.Error())
}
env, err := LoadAppEnv(appName)
if err != nil {
return fmt.Errorf("Unable to load app environment: %s", err.Error())
}
env.Merge(oldEnv)
if err := env.Write(); err != nil {
return fmt.Errorf("Unable to write app environment: %s", err.Error())
}
if err := common.PropertyWrite("config", appName, "env-migrated", "true"); err != nil {
return fmt.Errorf("Unable to set env-migrated property: %s", err.Error())
}
}
return nil
}
// TriggerPostAppCloneSetup creates new buildpacks files
func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
oldEnv, err := LoadAppEnv(oldAppName)
@@ -95,3 +158,13 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
return nil
}
// TriggerPostCreate ensures apps have the correct config structure
func TriggerPostCreate(appName string) error {
return common.PropertySetupApp("config", appName)
}
// TriggerPostDelete destroys the config data for a given app container
func TriggerPostDelete(appName string) error {
return common.PropertyDestroy("config", appName)
}