Merge pull request #3349 from dokku/3249-clean-clones

Ensure apps are cleanly cloned
This commit is contained in:
Jose Diaz-Gonzalez
2018-12-28 18:05:43 -05:00
committed by GitHub
10 changed files with 95 additions and 3 deletions

View File

@@ -559,6 +559,21 @@ set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `post-app-clone-setup`
- Description: Allows you to run commands after an app is setup, and before it is rebuild. This is useful for cleaning up tasks, or ensuring configuration from an old app is copied to the new app
- Invoked by: `dokku apps:clone`
- Arguments: `$OLD_APP_NAME $NEW_APP_NAME`
- Example:
```shell
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
# TODO
```
### `post-app-rename`
- Description: Allows you to run commands after an app was renamed.

View File

@@ -0,0 +1 @@
hook

View File

@@ -39,6 +39,7 @@ apps_clone_cmd() {
pushd "$DOKKU_ROOT/$OLD_APP/." > /dev/null
find ./* -not \( -name .cache \) | grep -v "./cache" | cpio -pdmu --quiet "$DOKKU_ROOT/$NEW_APP"
popd > /dev/null 2>&1 || pushd "/tmp" > /dev/null
plugn trigger post-app-clone-setup "$OLD_APP" "$NEW_APP"
if [[ -d "$NEW_CACHE_DIR" ]] && ! rmdir "$NEW_CACHE_DIR"; then
docker run "$DOKKU_GLOBAL_RUN_ARGS" --rm -v "$NEW_CACHE_HOST_DIR:/cache" "dokku/$OLD_APP" chmod 777 -R /cache
@@ -50,7 +51,7 @@ apps_clone_cmd() {
[[ -f "$DOKKU_ROOT/$NEW_APP/hooks/pre-receive" ]] && sed -i -e "s/git-hook $OLD_APP/git-hook $NEW_APP/g" "$DOKKU_ROOT/$NEW_APP/hooks/pre-receive"
[[ "$SKIP_REBUILD" == "true" ]] || ps_rebuild "$NEW_APP"
plugn trigger post-app-clone "$OLD_APP" "$NEW_APP"
echo "Cloning $OLD_APP to $NEW_APP... done"
dokku_log_info1_quiet "Cloning $OLD_APP to $NEW_APP... done"
}
apps_clone_cmd "$@"

View File

@@ -3,6 +3,7 @@
/triggers/*
/network-*
/install
/post-app-clone-setup
/post-create
/post-delete
/report
/report

View File

@@ -3,7 +3,7 @@ include ../../common.mk
GO_ARGS ?= -a
SUBCOMMANDS = subcommands/rebuild subcommands/rebuildall subcommands/report subcommands/set
TRIGGERS = triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-write-ipaddr triggers/network-write-port triggers/post-create triggers/post-delete triggers/report
TRIGGERS = triggers/install triggers/network-build-config triggers/network-compute-ports triggers/network-config-exists triggers/network-get-ipaddr triggers/network-get-listeners triggers/network-get-port triggers/network-get-property triggers/network-write-ipaddr triggers/network-write-port triggers/post-app-clone-setup triggers/post-create triggers/post-delete triggers/report
build-in-docker: clean
docker run --rm \
-v $$PWD/../..:$(GO_REPO_ROOT) \

View File

@@ -180,6 +180,29 @@ func HasNetworkConfig(appName string) bool {
return common.FileExists(ipfile) && common.FileExists(portfile)
}
// PostAppCloneSetup removes old IP and PORT files for a newly cloned app
func PostAppCloneSetup(appName string) bool {
dokkuRoot := common.MustGetEnv("DOKKU_ROOT")
appRoot := strings.Join([]string{dokkuRoot, appName}, "/")
success := true
ipFiles, _ := filepath.Glob(appRoot + "/IP.*")
for _, file := range ipFiles {
if err := os.Remove(file); err != nil {
common.LogWarn(fmt.Sprintf("Unable to remove file %s", file))
success = false
}
}
portFiles, _ := filepath.Glob(appRoot + "/PORT.*")
for _, file := range portFiles {
if err := os.Remove(file); err != nil {
common.LogWarn(fmt.Sprintf("Unable to remove file %s", file))
success = false
}
}
return success
}
// ReportSingleApp is an internal function that displays the app report for one or more apps
func ReportSingleApp(appName, infoFlag string) {
if err := common.VerifyAppName(appName); err != nil {

View File

@@ -0,0 +1,19 @@
package main
import (
"flag"
"os"
"github.com/dokku/dokku/plugins/network"
)
// cleanup network files for a new app clone
func main() {
flag.Parse()
appName := flag.Arg(1)
success := network.PostAppCloneSetup(appName)
if !success {
os.Exit(1)
}
}

View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
nginx_post_app_clone_setup() {
declare desc="removes nginx files when setting up a clone"
declare OLD_APP="$1" NEW_APP="$2"
local APP_DIR="$DOKKU_ROOT/$NEW_APP"
rm -rf "$APP_DIR/nginx.conf"
}
nginx_post_app_clone_setup "$@"

View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
scheduler-docker-local-post-app-clone-setup() {
declare desc="removes docker-local files when setting up a clone"
declare OLD_APP="$1" NEW_APP="$2"
local APP_DIR="$DOKKU_ROOT/$NEW_APP"
find "$APP_DIR" -type f -name 'CONTAINER.*' -exec rm {} \;
}
scheduler-docker-local-post-app-clone-setup "$@"

8
tests/unit/10_apps.bats Normal file → Executable file
View File

@@ -152,6 +152,10 @@ teardown () {
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url $TEST_APP` | grep 200"
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url great-test-name` | grep 404"
echo "output: $output"
echo "status: $status"
@@ -160,6 +164,10 @@ teardown () {
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "curl --silent --write-out '%{http_code}\n' `dokku url $TEST_APP` | grep 200"
echo "output: $output"
echo "status: $status"
assert_success
}
@test "(apps) apps:clone --ignore-existing" {