Skip to content

Commit

Permalink
Bugfix: properly remove old releases.
Browse files Browse the repository at this point in the history
API Updates
  • Loading branch information
wayneeseguin committed Nov 3, 2011
1 parent 236a32c commit a5a30bc
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 87 deletions.
194 changes: 111 additions & 83 deletions shell/functions
Expand Up @@ -4,16 +4,17 @@ set +o noclobber # Why yes, I did mean to clobber that file.


hook() hook()
{ {
trace_filter "deploy" || set -o xtrace
local _hook local _hook
_hook="$1" _hook="$1"


(( _hooks_flag )) || return 0 (( _hooks_flag )) || return 0


# System # System
if paths exist "${shared_path}/deploy" if path exists "${shared_path}/deploy"
then then
if paths exist "${shared_path}/deploy" && if path exists "${shared_path}/deploy" &&
files are executable "${shared_path}/deploy/$_hook" file is executable "${shared_path}/deploy/$_hook"
then then
log step "${_hook} ( ${shared_path##${project_path}/}/deploy/ )" log step "${_hook} ( ${shared_path##${project_path}/}/deploy/ )"
hook_source "${shared_path}/deploy/$_hook" &> >(tee -a "${deploy_log_path}/${_hook}.log") hook_source "${shared_path}/deploy/$_hook" &> >(tee -a "${deploy_log_path}/${_hook}.log")
Expand All @@ -23,26 +24,26 @@ hook()
fi fi


# System, Environment specific # System, Environment specific
if [[ -n "${environment:-}" ]] && if variable is nonempty environment &&
files are executable "$shared_path/deploy/${environment}/${_hook}" file is executable "$shared_path/deploy/${environment}/${_hook}"
then then
log step "${_hook} ( ${shared_path##${project_path}/}/deploy/${environment}/ )" log step "${_hook} ( ${shared_path##${project_path}/}/deploy/${environment}/ )"
hook_source "$shared_path/deploy/${environment}/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log") hook_source "$shared_path/deploy/${environment}/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success log step success
fi fi


# Project # Project
if paths exist "$shared_path/$project" && if path exists "$shared_path/$project" &&
files are executable "$shared_path/$project/config/deploy/${_hook}" file is executable "$shared_path/$project/config/deploy/${_hook}"
then then
log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/ )" log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/ )"
hook_source "$shared_path/$project/config/deploy/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log") hook_source "$shared_path/$project/config/deploy/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
log step success log step success
fi fi


# Project, environment # Project, environment
if [[ -n "${environment}" ]] && if variable is nonempty environment &&
files are executable "$shared_path/$project/config/deploy/$environment/${_hook}" file is executable "$shared_path/$project/config/deploy/$environment/${_hook}"
then then
log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/${environment}/ )" log step "${_hook} ( ${shared_path##${project_path}/}/${project}/config/deploy/${environment}/ )"
hook_source "$shared_path/$project/config/deploy/$environment/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log") hook_source "$shared_path/$project/config/deploy/$environment/${_hook}" &> >(tee -a "${deploy_log_path}/${_hook}.log")
Expand All @@ -67,16 +68,20 @@ after_step()


update() update()
{ {
trace_filter "deploy" || set -o xtrace

update_repository && release update_repository && release
} }


update_repository() update_repository()
{ {
trace_filter "deploy" || set -o xtrace

log step "update_repository ( ${shared_path##${project_path}/}/${project} )" log step "update_repository ( ${shared_path##${project_path}/}/${project} )"
fetch_uri "$repository_url" "$shared_path/$project" "${branch:-}" fetch_uri "$repository_url" "$shared_path/$project" "${branch:-}"
log step success log step success


paths enter "${shared_path}/${project}" path enter "${shared_path}/${project}"


log " TODO: **** move revision handling to fetch_uri ****" log " TODO: **** move revision handling to fetch_uri ****"


Expand Down Expand Up @@ -129,16 +134,19 @@ update_repository()


stage() stage()
{ {
trace_filter "deploy" || set -o xtrace

log step "stage ~ staging ${project} ${branch:-} ${revision:-} ( ${stage_path##${project_path}/} )" \ log step "stage ~ staging ${project} ${branch:-} ${revision:-} ( ${stage_path##${project_path}/} )" \
rsync -ag --exclude=".${scm_type}/" "${shared_path}/${project}/" "${stage_path}" rsync -ag --exclude=".${scm_type}/" "${shared_path}/${project}/" "${stage_path}"


paths enter "$stage_path" paths enter "$stage_path"

paths create "$stage_path/public" paths create "$stage_path/public"
} }


fail_stage() fail_stage()
{ {
trace_filter "deploy" || set -o xtrace

if paths exist "${stage_path}" if paths exist "${stage_path}"
then then
log step "stage fail ~ Removing stage release ( $stage_path )" \ log step "stage fail ~ Removing stage release ( $stage_path )" \
Expand All @@ -148,6 +156,8 @@ fail_stage()


release() release()
{ {
trace_filter "deploy" || set -o xtrace

typeset _prefix typeset _prefix
_prefix="${shared_path%/*}" _prefix="${shared_path%/*}"


Expand All @@ -164,89 +174,69 @@ release()
name "${project}" \ name "${project}" \
version ${version:=$(date +"%Y-%m-%dT%H:%M:%S")} version ${version:=$(date +"%Y-%m-%dT%H:%M:%S")}


paths remove "$stage_path" path remove "$stage_path"

paths enter "$release_path"
}

fail_release()
{
if paths exist "$stage_path"
then
log step "discard ~ Discarding stage ( $discard_path/$timestamp )"
paths move \
from "$stage_path" \
to "$discard_path/$timestamp"
log step success
fi

if variables are nonempty "${initial_release}"
then
paths exist "$initial_release" || return 0
else
return 0
fi

paths create "$discard_path" "$deploy_log_path"


# TODO: versionedfs last # switch to last version path enter "$release_path"
log step "Reverting to previous release $previous"
paths move from "$previous" to "${release_path%/}"
log step success
} }


configure() configure()
{ {
trace_filter "deploy" || set -o xtrace

local _name _file _path _paths _prefix _shared_public_path local _name _file _path _paths _prefix _shared_public_path


_prefix="${shared_path%/*}" _prefix="${shared_path%/*}"
_shared_public_path="${shared_path}/public" _shared_public_path="${shared_path}/public"


paths exist "${shared_path}/config" || return 0 if ! path exists "${shared_path}/config"
then
return 0
fi


paths enter "${shared_path}/config" path enter "${shared_path}/config"


for _file in "${shared_path}"/config/* for _file in "${shared_path}"/config/*
do do
_name="${_file##*/}" _name="${_file##*/}"


log step " link ~ ${_name} ( ${shared_path##${project_path}/}/config/${_name} => $stage_path/config/${_name} )" log step " link ~ ${_name} ( ${shared_path##${project_path}/}/config/${_name} => $stage_path/config/${_name} )"


paths remove "$stage_path/config/${_name}" path remove "$stage_path/config/${_name}"
files link symbolic \
file link symbolic \
from "$shared_path/config/${_name}" \ from "$shared_path/config/${_name}" \
to "$stage_path/config/${_name}" to "$stage_path/config/${_name}"


log step success log step success
done done


if paths exist "$shared_path/db" if path exists "$shared_path/db"
then then
log step " link ~ db path ( ${shared_path##${project_path}/}/db => ${stage_path##${project_path}/}/db )" log step " link ~ db path ( ${shared_path##${project_path}/}/db => ${stage_path##${project_path}/}/db )"
paths remove "$stage_path/db" path remove "$stage_path/db"
files link symbolic \ file link symbolic from "$shared_path/db" to "$stage_path/db"
from "$shared_path/db" \
to "$stage_path/db"
log step success log step success
fi fi


for _path in tmp log pids #files for _path in tmp log pids #files
do do
if paths exist "${stage_path}/${_path}" if path exists "${stage_path}/${_path}"
then then
paths remove "${stage_path}/${_path}" path remove "${stage_path}/${_path}"
fi fi
log step " link ~ ${_path} ( ${shared_path##${project_path}/}/${_path} => ${stage_path##${project_path}/}/${_path} )" log step " link ~ ${_path} ( ${shared_path##${project_path}/}/${_path} => ${stage_path##${project_path}/}/${_path} )"


paths remove "${stage_path}/${_path}" path remove "${stage_path}/${_path}"
files link symbolic \ file link symbolic \
from "${shared_path}/${_path}" \ from "${shared_path}/${_path}" \
to "${stage_path}/${_path}" to "${stage_path}/${_path}"
log step success log step success
done done


paths exist "$shared_path/public/" || if ! path exists "$shared_path/public/"
error "\nPlease run the folloing command first and then retry:\n\tsm rails setup" then
log error "\nPlease run the folloing command first and then retry:\n\tsm rails setup"
fi


_paths=($( find "${shared_path}/public" -mindepth 1 -maxdepth 1 -type d )) _paths=($( find "${shared_path}/public" -mindepth 1 -maxdepth 1 -type d ))


Expand All @@ -256,60 +246,71 @@ configure()


log step " link ~ public ${_path} ( ${shared_path##${project_path}/}/public/${_path} => ${stage_path##${project_path}/}/public/${_path} )" log step " link ~ public ${_path} ( ${shared_path##${project_path}/}/public/${_path} => ${stage_path##${project_path}/}/public/${_path} )"


paths remove "${stage_path}/public/${_path}" path remove "${stage_path}/public/${_path}"
files link symbolic \ file link symbolic \
from "${_shared_public_path}/${_path}" \ from "${_shared_public_path}/${_path}" \
to "${stage_path}/public/${_path}" to "${stage_path}/public/${_path}"


log step success log step success
done done
} }


remove_releases()
{
typeset _release
while (( $# > 0 ))
do
_release="${1}"
shift
log "Removing old release ${_release}"
paths remove "${_release}"
done
}

record() record()
{ # TODO: What else could we record that would be useful? { # TODO: What else could we record that would be useful?
trace_filter "deploy" || set -o xtrace
paths exist "${release_path}" || return 0 # Release failed, nothing to do... paths exist "${release_path}" || return 0 # Release failed, nothing to do...


if [[ -n "${revision:-}" ]] if variable is nonempty revision
then then
files write string "${revision}" to "${release_path}/revision" file write string "${revision}" to "${release_path}/revision"
fi fi


if [[ -n "${branch:-}" ]] if variable is nonempty branch
then then
files write string "${branch}" to "${release_path}/branch" file write string "${branch}" to "${release_path}/branch"
fi fi


if [[ -n "${repository_url}" ]] # TODO: get this from the .uri file. if variable is nonempty repository_url # TODO: get this from the .uri file.
then then
files write string "$repository_url" to "${release_path}/repository_url" file write string "$repository_url" to "${release_path}/repository_url"
fi fi


if [[ -n "${timestamp:-}" ]] if variable is nonempty timestamp
then then
files write string "${timestamp}" to "${release_path}/timestamp" files write string "${timestamp}" to "${release_path}/timestamp"
fi fi
} }


remove_releases()
{
trace_filter "deploy" || set -o xtrace

local _release

path enter "${versions_path}"

while (( $# > 0 ))
do
_release="${1/:/\/}"
shift
log "Removing old release ${_release}"
path remove "${_release}"
done
}

cleanup() cleanup()
{ {
paths exist "$project_path/previous" || return 0 # No old releases trace_filter "deploy" || set -o xtrace
array is nonempty old_releases || return 0 # No old releases


paths enter "${project_path}/previous" if array is empty old_releases || ! path exists "${versions_path}"
remove_releases "${old_releases[@]}" || then
log "There was an error removing old releases." return 0 # No old releases
fi

if ! remove_releases "${old_releases[@]}"
then
log warn "There was an error removing old releases (${old_releases[*]})."
fi
} }


do_deploy() do_deploy()
Expand Down Expand Up @@ -338,9 +339,9 @@ do_deploy()
"root# sm sets install networking ; sm rsync package install" "root# sm sets install networking ; sm rsync package install"
fi fi


if paths exist "${release_path}" if path exists "${release_path}"
then # If release path already exists, start inside it. then # If release path already exists, start inside it.
paths enter "$release_path" path enter "$release_path"
fi fi


log " Deploy started at ${timestamp}" log " Deploy started at ${timestamp}"
Expand All @@ -363,7 +364,7 @@ revert_on_error()
trap "backtrace \"An errorr occured while reverting.\"" ERR trap "backtrace \"An errorr occured while reverting.\"" ERR
trap "backtrace \"An errorr occured while reverting.\"" ZERR trap "backtrace \"An errorr occured while reverting.\"" ZERR
backtrace "A command has returned an not handled error code (${result})." no_exit 2>"${log_file}" backtrace "A command has returned an not handled error code (${result})." no_exit 2>"${log_file}"
log "\nAn error occured, reverting...\n for details check deploy.$timestamp.log\n" >&2 log "\nAn error occured, reverting...\n for details check the logfiles in ${deploy_log_path}/\n" >&2
retreat >&2 retreat >&2
kill -s USR2 $APP_PID #to exit application not current subshell kill -s USR2 $APP_PID #to exit application not current subshell
} }
Expand All @@ -378,3 +379,30 @@ do_revert()
log " Reverting ..." log " Reverting ..."
retreat "deploy" "${deploy_steps[@]}" retreat "deploy" "${deploy_steps[@]}"
} }

fail_release()
{
trace_filter "deploy" || set -o xtrace

if path exists "$stage_path"
then
log step "discard ~ Discarding stage ( $discard_path/$timestamp )"
path move from "$stage_path" to "$discard_path/$timestamp"
log step success
fi

if variable is nonempty initial_release
then
paths exist "$initial_release" || return 0
else
return 0
fi

paths create "$discard_path" "$deploy_log_path"

# TODO: versionedfs last # switch to last version
log step "Reverting to previous release $previous"
path move from "$previous" to "${release_path%/}"
log step success
}

0 comments on commit a5a30bc

Please sign in to comment.