forked from DockerDemos/DwarfFortressServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunme
executable file
·158 lines (137 loc) · 3.44 KB
/
runme
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
export DOCKER_DEFAULT_PLATFORM=linux/amd64
VERSION="$(awk '/Version:/ {print $2",",$4}' README.md |sed 's/_//g')"
DATETIME=$(date +"%s")
COLWIDTH="$(tput cols)"
RIGHTCOL="$((COLWIDTH -30))"
if [ -f ./config ] ; then
. ./config
else
echo "No source config file found"
LOCAL_PORT='80'
SAVE_DIR='/tmp/DFServer'
IMAGENAME='dfserver'
fi
f_help () {
echo -e "Usage: ./runme [OPTIIONS]\n
Dwarf Fortress in a Docker container\n
Options:
--rebuild-image\tRebuild the Docker image before running
--no-cache\t\tDo not use Docker's image cache when building
--pull-latest\t\tPull the latest code from the Github; implies --rebuild-image
--no-persist\t\tDon't mount a volume to save DwarfFortress data into
--help\t\tPrint this message
--version\t\tPrint the version info and exit
"
}
command_exists() {
command -v "$@" > /dev/null 2>&1
}
f_err () {
echo -e "$1"
exit 1
}
write_fail () {
printf "$1"
printf "%${RIGHTCOL}s\n" "[FAILED]"
}
write_no () {
printf "$1"
printf "%${RIGHTCOL}s\n" "[NO]"
}
write_ok () {
printf "$1"
printf "%${RIGHTCOL}s\n" "[OK]"
}
pull_latest () {
if ! command_exists git ; then
write_no "Checking for Git"
echo 'WARNING: Skipping "--pull-latest". Git not installed or in $PATH'
elif ! $(git status > /dev/null 2>&1) ; then
write_no echo "Checking for Git Repo information"
echo 'WARNING: Skipping "--pull-latest". Unable to find .git/config'
else
write_ok "Checking for Git"
write_ok "Checking for Git Repo information"
echo 'Pulling latest code from Github'
GIT=$(git pull 2>&1) || \
f_err "Unable to pull from repository\n\m ${GIT}"
fi
}
rebuild_image () {
IMAGE="$(docker images ${IMAGENAME} |grep latest)"
if [[ ! -z $IMAGE ]] ; then
RMI=$(docker rmi ${IMAGENAME}:latest 2>&1) || \
f_err "Unable to remove ${IMAGENAME}:latest tag\n\n ${RMI}"
fi
if [[ ! -z $CACHE ]] ; then
echo 'Building image without Docker cache'
fi
echo "Building new image - ${IMAGENAME}:latest"
BUILD=$(docker build $CACHE -t ${IMAGENAME}:${DATETIME} . 2>&1) || \
f_err "Unable to build image ${IMAGENAME}:${DATETIME}\n\n ${BUILD}"
TAG=$(docker tag ${IMAGENAME}:${DATETIME} ${IMAGENAME}:latest 2>&1) || \
f_err "Unable to tag image ${IMAGENAME}:latest\n\n ${TAG}"
}
CACHE=''
VOLMNT="-v ${SAVE_DIR}:/df_linux/data/save"
while [[ $# > 0 ]] ; do
OPT="$1"
case $OPT in
--rebuild-image)
REBUILD=true
shift
;;
--no-cache)
CACHE='--no-cache'
REBUILD=true
shift
;;
--pull-latest)
LATEST=true
REBUILD=true
shift
;;
--no-persist)
VOLMNT=''
shift
;;
--version)
echo Dwarf Fortress Server version $VERSION
exit 0
shift
;;
--help)
f_help
exit 0
shift
;;
*)
# Unknown
;;
esac
done
# Make sure Docker is installed, or there's nothign to do
if ! command_exists docker ; then
write_no "Checking for docker"
if ! command_exists lxc-docker ; then
write_no "Checking for lxc-docker"
f_err "Dwarf Fortress Server requires Docker to run."
fi
else
write_ok "Checking for docker"
fi
if [[ $REBUILD ]] ; then
if [[ $LATEST ]] ; then
pull_latest
fi
rebuild_image
fi
if [[ -z $VOLMNT ]] ; then
echo 'Not setting up persistent save location'
fi
DFS=$(docker run -p ${LOCAL_PORT}:6080 $VOLMNT -d ${IMAGENAME}:latest 2>&1) && \
write_ok "Starting Dwarf Fortress server" && exit 0
# If we didn't Exit 0 above, then exit 1
write_fail "Starting Dwarf Fortress server"
f_err "$DFS"