refactor: move all golang code for managing data directories to common plugin

Additionally, ensure the golang plugins handle setup/teardown of plugin and app-specific data directories during install, rename, and clone phases.
This commit is contained in:
Jose Diaz-Gonzalez
2023-01-08 23:24:15 -05:00
parent 38fdb87c79
commit 6cbe93c245
14 changed files with 153 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/report subcommands/set
TRIGGERS = triggers/app-json-process-deploy-parallelism triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/post-deploy triggers/pre-deploy triggers/report
TRIGGERS = triggers/app-json-process-deploy-parallelism triggers/install triggers/post-app-clone-setup triggers/post-app-rename triggers/post-app-rename-setup triggers/post-create triggers/post-delete triggers/post-deploy triggers/pre-deploy triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = app-json

View File

@@ -45,7 +45,7 @@ type Formation struct {
// GetAppjsonDirectory returns the directory containing a given app's extracted app.json file
func GetAppjsonDirectory(appName string) string {
return filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json", appName)
return common.GetAppDataDirectory("app-json", appName)
}
// GetAppjsonPath returns the path to a given app's extracted app.json file

View File

@@ -455,12 +455,12 @@ func createdContainerID(appName string, dockerArgs []string, image string, comma
}
func refreshAppJSON(appName string, image string) error {
baseDirectory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json")
baseDirectory := common.GetDataDirectory("app-json")
if !common.DirectoryExists(baseDirectory) {
return errors.New("Run 'dokku plugin:install' to ensure the correct directories exist")
}
directory := GetAppjsonDirectory(appName)
directory := common.GetAppDataDirectory("app-json", appName)
if !common.DirectoryExists(directory) {
if err := os.MkdirAll(directory, 0755); err != nil {
return err
@@ -514,7 +514,7 @@ func injectDokkuScale(appName string, image string) error {
return err
}
baseDirectory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json")
baseDirectory := common.GetDataDirectory("app-json")
if !common.DirectoryExists(baseDirectory) {
return errors.New("Run 'dokku plugin:install' to ensure the correct directories exist")
}

View File

@@ -28,6 +28,10 @@ func main() {
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
err = appjson.TriggerPostAppCloneSetup(oldAppName, newAppName)
case "post-app-rename":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
err = appjson.TriggerPostAppRename(oldAppName, newAppName)
case "post-app-rename-setup":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)

View File

@@ -3,7 +3,6 @@ package appjson
import (
"fmt"
"os"
"path/filepath"
"github.com/dokku/dokku/plugins/common"
)
@@ -34,18 +33,17 @@ func TriggerAppJSONProcessDeployParallelism(appName string, processType string)
return nil
}
// TriggerInstall initializes app restart policies
// TriggerInstall initializes app-json directory structures
func TriggerInstall() error {
if err := common.PropertySetup("app-json"); err != nil {
return fmt.Errorf("Unable to install the app-json plugin: %s", err.Error())
}
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json")
if err := os.MkdirAll(directory, 0755); err != nil {
if err := common.SetupAppData("app-json"); err != nil {
return err
}
return common.SetPermissions(directory, 0755)
return nil
}
// TriggerPostAppCloneSetup creates new app-json files
@@ -55,7 +53,12 @@ func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
return err
}
return nil
return common.CloneAppData("app-json", oldAppName, newAppName)
}
// TriggerPostAppRename removes the old app data
func TriggerPostAppRename(oldAppName string, newAppName string) error {
return common.RemoveAppDataDirectory("app-json", oldAppName)
}
// TriggerPostAppRenameSetup renames app-json files
@@ -68,13 +71,17 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
return err
}
return nil
return common.CloneAppData("app-json", oldAppName, newAppName)
}
// TriggerPostCreate ensures apps the correct data directory structure
func TriggerPostCreate(appName string) error {
return common.CreateAppDataDirectory("app-json", appName)
}
// TriggerPostDelete destroys the app-json data for a given app container
func TriggerPostDelete(appName string) error {
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "app-json", appName)
dataErr := os.RemoveAll(directory)
dataErr := os.RemoveAll(common.GetAppDataDirectory("app-json", appName))
propertyErr := common.PropertyDestroy("app-json", appName)
if dataErr != nil {

82
plugins/common/data.go Normal file
View File

@@ -0,0 +1,82 @@
package common
import (
"fmt"
"os"
"path/filepath"
"github.com/otiai10/copy"
)
// CreateAppDataDirectory creates a data directory for the given plugin/app combination with the correct permissions
func CreateAppDataDirectory(pluginName, appName string) error {
directory := GetAppDataDirectory(pluginName, appName)
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := SetPermissions(directory, 0755); err != nil {
return err
}
return nil
}
// CreateDataDirectory creates a data directory for the given plugin/app combination with the correct permissions
func CreateDataDirectory(pluginName string) error {
directory := GetDataDirectory(pluginName)
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := SetPermissions(directory, 0755); err != nil {
return err
}
return nil
}
// GetAppDataDirectory returns the path to the data directory for the given plugin/app combination
func GetAppDataDirectory(pluginName string, appName string) string {
return filepath.Join(GetDataDirectory(pluginName), appName)
}
// GetDataDirectory returns the path to the data directory for the specified plugin
func GetDataDirectory(pluginName string) string {
return filepath.Join(MustGetEnv("DOKKU_LIB_ROOT"), "data", pluginName)
}
// RemoveAppDataDirectory removes the path to the data directory for the given plugin/app combination
func RemoveAppDataDirectory(pluginName, appName string) error {
return os.RemoveAll(GetAppDataDirectory(pluginName, appName))
}
func CloneAppData(pluginName string, oldAppName string, newAppName string) error {
oldDataDir := GetAppDataDirectory(pluginName, oldAppName)
newDataDir := GetAppDataDirectory(pluginName, newAppName)
if err := copy.Copy(oldDataDir, newDataDir); err != nil {
return fmt.Errorf("Unable to clone app data to new location: %v", err.Error())
}
return nil
}
// SetupAppData creates
func SetupAppData(pluginName string) error {
if err := CreateDataDirectory(pluginName); err != nil {
return err
}
apps, err := UnfilteredDokkuApps()
if err != nil {
return nil
}
for _, appName := range apps {
if err := CreateAppDataDirectory(pluginName, appName); err != nil {
return err
}
}
return nil
}

View File

@@ -1,5 +1,5 @@
SUBCOMMANDS = subcommands/failed subcommands/report subcommands/set subcommands/vector-logs subcommands/vector-start subcommands/vector-stop
TRIGGERS = triggers/docker-args-process-deploy triggers/install triggers/logs-get-property triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/report
TRIGGERS = triggers/docker-args-process-deploy triggers/install triggers/logs-get-property triggers/post-app-clone-setup triggers/post-app-rename triggers/post-app-rename-setup triggers/post-create triggers/post-delete triggers/report
BUILD = commands subcommands triggers
PLUGIN_NAME = logs

View File

@@ -223,7 +223,7 @@ func writeVectorConfig() error {
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
b = bytes.Replace(b, []byte("\\u002B"), []byte("+"), -1)
vectorConfig := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "logs", "vector.json")
vectorConfig := filepath.Join(common.GetDataDirectory("logs"), "vector.json")
if err := common.WriteSliceToFile(vectorConfig, []string{string(b)}); err != nil {
return err
}

View File

@@ -31,6 +31,10 @@ func main() {
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
err = logs.TriggerPostAppCloneSetup(oldAppName, newAppName)
case "post-app-rename":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)
err = logs.TriggerPostAppRename(oldAppName, newAppName)
case "post-app-rename-setup":
oldAppName := flag.Arg(0)
newAppName := flag.Arg(1)

View File

@@ -75,7 +75,7 @@ func CommandSet(appName string, property string, value string) error {
common.CommandPropertySet("logs", appName, property, value, DefaultProperties, GlobalProperties)
if property == "vector-sink" {
common.LogVerboseQuiet(fmt.Sprintf("Writing updated vector config to %s", filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "logs", "vector.json")))
common.LogVerboseQuiet(fmt.Sprintf("Writing updated vector config to %s", filepath.Join(common.GetDataDirectory("logs"), "vector.json")))
return writeVectorConfig()
}
return nil

View File

@@ -69,18 +69,13 @@ func TriggerDockerArgsProcessDeploy(appName string) error {
return nil
}
// TriggerInstall initializes app restart policies
// TriggerInstall initializes log configuration
func TriggerInstall() error {
if err := common.PropertySetup("logs"); err != nil {
return fmt.Errorf("Unable to install the logs plugin: %s", err.Error())
}
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "logs")
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := common.SetPermissions(directory, 0755); err != nil {
if err := common.SetupAppData("logs"); err != nil {
return err
}
@@ -118,7 +113,12 @@ func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
return err
}
return nil
return common.CloneAppData("logs", oldAppName, newAppName)
}
// TriggerPostAppRename removes the old app data
func TriggerPostAppRename(oldAppName string, newAppName string) error {
return common.RemoveAppDataDirectory("logs", oldAppName)
}
// TriggerPostAppRenameSetup renames logs files
@@ -131,10 +131,22 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
return err
}
return nil
return common.CloneAppData("logs", oldAppName, newAppName)
}
// TriggerPostCreate ensures apps the correct data directory structure
func TriggerPostCreate(appName string) error {
return common.CreateAppDataDirectory("logs", appName)
}
// TriggerPostDelete destroys the logs property for a given app container
func TriggerPostDelete(appName string) error {
return common.PropertyDestroy("logs", appName)
dataErr := os.RemoveAll(common.GetAppDataDirectory("logs", appName))
propertyErr := common.PropertyDestroy("logs", appName)
if dataErr != nil {
return dataErr
}
return propertyErr
}

View File

@@ -66,7 +66,7 @@ func getProcfileCommand(procfilePath string, processType string, port int) (stri
}
func getProcfilePath(appName string) string {
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps", appName)
directory := common.GetAppDataDirectory("ps", appName)
return filepath.Join(directory, "Procfile")
}

View File

@@ -104,7 +104,7 @@ func CommandRestore(appName string, allApps bool, parallelCount int) error {
// CommandRetire ensures old containers are retired
func CommandRetire(appName string) error {
lockFile := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps", "retire")
lockFile := filepath.Join(common.GetDataDirectory("ps"), "retire")
scheduler := ""
if appName == "" {
scheduler = common.GetGlobalScheduler()

View File

@@ -116,12 +116,7 @@ func TriggerInstall() error {
return fmt.Errorf("Unable to install the ps plugin: %s", err.Error())
}
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps")
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := common.SetPermissions(directory, 0755); err != nil {
if err := common.SetupAppData("ps"); err != nil {
return err
}
@@ -130,6 +125,12 @@ func TriggerInstall() error {
return nil
}
for _, appName := range apps {
if err := common.CreateAppDataDirectory("ps", appName); err != nil {
return err
}
}
for _, appName := range apps {
policies, err := getRestartPolicy(appName)
if err != nil {
@@ -188,12 +189,15 @@ func TriggerPostAppCloneSetup(oldAppName string, newAppName string) error {
return err
}
// TODO: Copy data dir
return nil
return common.CloneAppData("ps", oldAppName, newAppName)
}
// TriggerPostAppRename rebuilds the renamed app
func TriggerPostAppRename(oldAppName string, newAppName string) error {
if err := common.RemoveAppDataDirectory("ps", oldAppName); err != nil {
return err
}
if os.Getenv("SKIP_REBUILD") == "true" {
return nil
}
@@ -211,8 +215,7 @@ func TriggerPostAppRenameSetup(oldAppName string, newAppName string) error {
return err
}
// TODO: Move data dir
return nil
return common.CloneAppData("ps", oldAppName, newAppName)
}
// TriggerPostCreate ensures apps have a default restart policy
@@ -222,12 +225,7 @@ func TriggerPostCreate(appName string) error {
return err
}
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps", appName)
if err := os.MkdirAll(directory, 0755); err != nil {
return err
}
if err := common.SetPermissions(directory, 0755); err != nil {
if err := common.CreateAppDataDirectory("ps", appName); err != nil {
return err
}
@@ -242,8 +240,7 @@ func TriggerPostCreate(appName string) error {
// TriggerPostDelete destroys the ps properties for a given app container
func TriggerPostDelete(appName string) error {
directory := filepath.Join(common.MustGetEnv("DOKKU_LIB_ROOT"), "data", "ps", appName)
dataErr := os.RemoveAll(directory)
dataErr := common.RemoveAppDataDirectory("ps", appName)
propertyErr := common.PropertyDestroy("ps", appName)
if dataErr != nil {