-
Notifications
You must be signed in to change notification settings - Fork 1
/
stage-manager
108 lines (86 loc) · 2.48 KB
/
stage-manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
# Configuration options
REPO_LOC="/var/www/release" # Your main application repository
SPAWN_DIR="stage" # Name of the folder where your environments will reside
# Global variables
BASE_DIR=$(dirname "$(readlink -f "$0")")
MODE=$1
NAME=$2
stage-create()
{
if [ -d "$BASE_DIR/$SPAWN_DIR/$NAME" ]; then
echo "Error: $NAME already exists as a testing environment."
exit 1
fi
echo "Creating core repository:"
${BASE_DIR}/git-new-workdir "$REPO_LOC" "$BASE_DIR/$SPAWN_DIR/$NAME"
cd "$BASE_DIR/$SPAWN_DIR/$NAME"
if [ -f "$BASE_DIR/.bootstrap" ]; then
# Put all stuff that you need to perform to make your application useable in this file
. ${BASE_DIR}/.bootstrap
fi
echo -e "New testing environment is available @ \033[0;32m$(pwd)\033[0m"
}
stage-list()
{
echo -e "Available testing environments:\n"
for D in "$BASE_DIR/$SPAWN_DIR/"*; do
if [ -d "$D" ]; then
# We're not interested in the folder if it's not a Git repository
if [ ! -d "$D/.git" ]; then
continue
fi
BRANCH="$(git --git-dir=${D}/.git symbolic-ref --short -q HEAD)"
TAG="$(git --git-dir=${D}/.git describe --exact-match --abbrev=0 2>/dev/null)"
# "git describe" will error out if the commit is not tied to an annotated tag
if [ "$?" -ne 0 ]; then
TAG="unknown"
fi
if [ -n "$BRANCH" ]; then
LABEL="$BRANCH [\033[0;31mbranch\033[0m]"
else
LABEL="$TAG [\033[0;32mtag\033[0m]"
fi
echo -e "$(basename ${D})\t=>\t$LABEL"
fi
done
}
stage-destroy()
{
if [ ! -d "$BASE_DIR/$SPAWN_DIR/$NAME" ]; then
echo "Error: $NAME could not be found."
exit 1
fi
rm -rf "$BASE_DIR/$SPAWN_DIR/$NAME"
echo "Testing environment has been removed sucessfully!"
}
stage-help()
{
# What's my name again?
SCRIPT_NAME=$(basename ${BASH_SOURCE[0]})
echo "Usage: $SCRIPT_NAME <add|help|list|remove> [name]"
}
if [ ! -d "$BASE_DIR/$SPAWN_DIR" ]; then
mkdir "$BASE_DIR/$SPAWN_DIR"
fi
if [ ! -x "$BASE_DIR/git-new-workdir" ]; then
echo "Error: git-new-workdir should be in the working directory and executable."
exit 1
fi
if [ "$#" -lt "2" ] && [ "$1" != "list" ]; then
MODE=
fi
case "$MODE" in
"add")
stage-create
;;
"list")
stage-list
;;
"remove")
stage-destroy
;;
*)
stage-help
;;
esac