Docker image which provides a full Eressea installation to host a local game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

559 lines
18 KiB

3 weeks ago
  1. #!/bin/bash
  2. base_dir="$(dirname "$0")"
  3. # -------------------
  4. # -- Helper functions
  5. function ini_sec() {
  6. /eressea/server/bin/inifile /data/game-1/eressea.ini add $1
  7. }
  8. function ini_add() {
  9. /eressea/server/bin/inifile /data/game-1/eressea.ini add $1:$2 $3
  10. }
  11. function ini_get() {
  12. /eressea/server/bin/inifile /data/game-1/eressea.ini get $1:$2
  13. }
  14. function get_turn() {
  15. turn=0
  16. [ -e /data/game-1/turn ] && turn=$(cat /data/game-1/turn)
  17. [ -z $turn ] && turn=0
  18. }
  19. # -----------------
  20. # -- Main-commands
  21. cmd_help() {
  22. usage() {
  23. echo ""
  24. echo "Usage: $0 COMMAND [-h] [arguments]"
  25. echo ""
  26. echo "supported COMMANDs:"
  27. echo " addpwd adds password in newfactions"
  28. echo " bash start a bash shell"
  29. echo " generate generate eressea.ini and all relevant files"
  30. echo " help show this help"
  31. echo " mail process incoming e-mail"
  32. echo " map generate/edit game map"
  33. echo " run execute Eressea game turn"
  34. echo " shutdown remove temporaray Eressea environment"
  35. echo " startup create temporaray Eressea environment which is necessary for scripts"
  36. echo ""
  37. echo "arguments:"
  38. echo "-h ... show more details of command"
  39. exit 2
  40. }
  41. usage
  42. }
  43. cmd_bash() {
  44. usage() {
  45. [ -n "$1" ] && echo -e "\n$1"
  46. echo ""
  47. echo "Starts an interactive bash shell."
  48. echo "Usage: $0 bash [-h] [-n]"
  49. echo "-h ... show this help"
  50. echo "-n ... start a 'naked' bash, which means eressea is not setup"
  51. echo " can be done later by invoking '/eressea/start.sh startup'"
  52. echo " before you exit the shell, call '/eressea/start.sh shutdown'"
  53. exit 2
  54. }
  55. args=$(getopt --name shutdown -o hn -- "$@")
  56. if [ $? != 0 ]; then
  57. usage
  58. exit
  59. fi
  60. eval set -- "$args"
  61. naked=0
  62. while :; do
  63. case "$1" in
  64. -h) usage ; shift ;;
  65. -n) naked=1; shift ;;
  66. --) shift ; break ;;
  67. esac
  68. done
  69. [ ${naked} == 0 ] && cmd_startup || echo "started bash without any Eressea environment setup"
  70. /bin/bash
  71. [ ${naked} == 0 ] && cmd_shutdown
  72. }
  73. cmd_startup() {
  74. usage() {
  75. [ -n "$1" ] && echo -e "\n$1"
  76. echo ""
  77. echo "Setup an environment which enables Eressea to find a runtime environment where all scripts are functional."
  78. echo "Usage: $0 startup [-h]"
  79. echo "-h ... show this help"
  80. exit 2
  81. }
  82. args=$(getopt --name shutdown -o h -- "$@")
  83. if [ $? != 0 ]; then
  84. usage
  85. exit
  86. fi
  87. eval set -- "$args"
  88. while :; do
  89. case "$1" in
  90. -h) usage ; shift ;;
  91. --) shift ; break ;;
  92. esac
  93. done
  94. mkdir -p /data/config
  95. mkdir -p /data/log
  96. ln -sf /data/config/muttrc ~/.muttrc
  97. ln -sf /data/config/fetchmailrc ~/.fetchmailrc
  98. ln -sf /data/config/procmailrc ~/.procmailrc
  99. ln -sf /eressea/server /data/server
  100. ln -sf /eressea/orders-php /data/orders-php
  101. mkdir -p /data/game-1
  102. mkdir -p /data/game-1/backup
  103. ln -sf /data/server/scripts/config.lua /data/game-1/config.lua
  104. ln -sf /data/server/bin/eressea /data/game-1/eressea
  105. ln -sf /data/server/scripts/reports.lua /data/game-1/reports.lua
  106. ln -sf /data/server/scripts/run-turn.lua /data/game-1/run-turn.lua
  107. cd /data/game-1
  108. echo "Eressea environment setup complete"
  109. }
  110. cmd_shutdown() {
  111. usage() {
  112. [ -n "$1" ] && echo -e "\n$1"
  113. echo ""
  114. echo "Removes temporary Eressea environment from mapped /data folder."
  115. echo "Usage: $0 shutdown [-h]"
  116. echo "-h ... show this help"
  117. exit 2
  118. }
  119. args=$(getopt --name shutdown -o h -- "$@")
  120. if [ $? != 0 ]; then
  121. usage
  122. exit
  123. fi
  124. eval set -- "$args"
  125. while :; do
  126. case "$1" in
  127. -h) usage ; shift ;;
  128. --) shift ; break ;;
  129. esac
  130. done
  131. [ -e /data/game-1/config.lua ] && rm /data/game-1/config.lua
  132. [ -e /data/game-1/eressea ] && rm /data/game-1/eressea
  133. [ -e /data/game-1/reports.lua ] && rm /data/game-1/reports.lua
  134. [ -e /data/game-1/run-turn.lua ] && rm /data/game-1/run-turn.lua
  135. [ -e /data/server ] && rm /data/server
  136. [ -e /data/orders-php ] && rm /data/orders-php
  137. [ -e ~/.muttrc ] && rm -f ~/.muttrc
  138. [ -e ~/.fetchmailrc ] && rm -f ~/.fetchmailrc
  139. [ -e ~/.procmailrc ] && rm -f ~/.procmailrc
  140. [ -e /data/config/logrotate ] && logrotate /data/config/logrotate
  141. echo "Eressea environment successfully removed"
  142. }
  143. cmd_generate() {
  144. usage() {
  145. [ -n "$1" ] && echo -e "\n$1"
  146. echo ""
  147. echo "Generates eressea.ini file and all other necessary files/folders needed by this Docker container."
  148. echo "Usage: $0 generate [options]"
  149. echo ""
  150. echo "General option:"
  151. echo "-f force generating of files - delete will be done without prompt!"
  152. echo "-g generate all other necessary files and folders (values are defined by eressea.ini and mail.ini)"
  153. echo "-h show this help"
  154. echo "-i generate eressea.ini and mail.ini file"
  155. echo ""
  156. echo "If eressea.ini is not available, option -i is mandatory. All of the following options are also necessray:"
  157. echo " --from <email> e-mail address Eressea postbox"
  158. echo " --imap_server <addr> IMAP server address. If not provided, value of smtp_server is used."
  159. echo " --imap_user <user> user for IMAP server. If not provided, value of smtp_user is used."
  160. echo " --imap_pass <password> password of IMAP user. If not provided, value of smtp_pass is used."
  161. echo " --smtp_server <addr> SMTP server address. If not provided, value of imap_server is used."
  162. echo " --smtp_user <user> user for SMTP server. If not provided, value of imap_user is used."
  163. echo " --smtp_pass <password> password of SMTP user. If not provided, value of imap_pass is used."
  164. echo ""
  165. echo " Optional:"
  166. echo " --game_name <name> name of self hosted Eressea game. Default=MyEressea"
  167. echo " --realname name> real name used for e-Mails. Default=Game Server <game_name>"
  168. echo " --smtp_port <port> port of SMTP server, Default=587"
  169. echo " --imap_port <port> port of IMAP server, Default=993"
  170. echo " --rules <ruleset> ruleset, Defaule=e3"
  171. exit 2
  172. }
  173. args=$(getopt --name generate -o fghi --long game_name:,from:,realname:,smtp_server:,smtp_port:,smtp_user:,smtp_pass:,imap_server:,imap_port:,imap_user:,imap_pass:,rules: -- "$@")
  174. if [ $? != 0 ]; then
  175. usage
  176. exit
  177. fi
  178. eval set -- "$args"
  179. force=0
  180. do_gen=0
  181. do_ini=0
  182. game_name="MyEressea"
  183. imap_port=993
  184. smtp_port=587
  185. rules="e3"
  186. while :; do
  187. case "$1" in
  188. -f) force=1 ; shift ;;
  189. -g) do_gen=1 ; shift ;;
  190. -h) usage ; shift ;;
  191. -i) do_ini=1 ; shift ;;
  192. --game_name) game_name="$2" ; shift 2 ;;
  193. --from) from="$2" ; shift 2 ;;
  194. --realname) realname="$2" ; shift 2 ;;
  195. --smtp_server) smtp_server="$2" ; shift 2 ;;
  196. --smtp_port) smtp_port="$2" ; shift 2 ;;
  197. --smtp_user) smtp_user="$2" ; shift 2 ;;
  198. --smtp_pass) smtp_pass="$2" ; shift 2 ;;
  199. --imap_server) imap_server="$2" ; shift 2 ;;
  200. --imap_port) imap_port="$2" ; shift 2 ;;
  201. --imap_user) imap_user="$2" ; shift 2 ;;
  202. --imap_pass) imap_pass="$2" ; shift 2 ;;
  203. --rules) rules="$2" ; shift 2 ;;
  204. --) shift ; break ;;
  205. esac
  206. done
  207. if [ $do_ini == 1 ]; then
  208. mkdir -p /data/game-1
  209. [ -e /data/game-1/eressea.ini ] && [ $force == 0 ] && usage "eressea.ini already exists. Add option -f"
  210. [ -z "$imap_server" ] && [ -n "$smtp_server" ] && imap_server=$smtp_server
  211. [ -z "$imap_user" ] && [ -n "$smtp_user" ] && imap_user=$smtp_user
  212. [ -z "$imap_pass" ] && [ -n "$smtp_pass" ] && imap_pass=$smtp_pass
  213. [ -z "$smtp_server" ] && [ -n "$imap_server" ] && smtp_server=$imap_server
  214. [ -z "$smtp_user" ] && [ -n "$imap_user" ] && smtp_user=$imap_user
  215. [ -z "$smtp_pass" ] && [ -n "$imap_pass" ] && smtp_pass=$imap_pass
  216. [ -z "$realname" ] && realname="Game Server ${game_name}"
  217. [ -z "$game_name" ] || [ -z "$from" ] || [ -z "$realname" ] || [ -z "$rules" ] || \
  218. [ -z "$smtp_server" ] || [ -z "$smtp_port" ] || [ -z "$smtp_user" ] || [ -z "$smtp_pass" ] || \
  219. [ -z "$imap_server" ] || [ -z "$imap_port" ] || [ -z "$imap_user" ] || [ -z "$imap_pass" ] && \
  220. usage "not all options relevant for eressea.ini were provided"
  221. [ -e /data/game-1/eressea.ini ] && rm -f /data/game-1/eressea.ini
  222. touch /data/game-1/eressea.ini
  223. ini_sec game
  224. ini_add game locales de,en
  225. ini_add game id 1
  226. ini_add game start 0
  227. ini_add game email $from
  228. ini_add game name $game_name
  229. ini_add game seed `shuf -i 0-9999 -n1`
  230. ini_add game dbname eressea.db
  231. ini_add game dbswap :memory:
  232. ini_add game mailcmd `echo "print '$game_name'.upper()" | python`
  233. ini_sec lua
  234. ini_add lua install /data/server
  235. ini_add lua paths /data/server/scripts:/data/server/lunit
  236. ini_add lua rules $rules
  237. echo "eressea.ini generated"
  238. [ -e /data/config/mail.ini ] && rm -f /data/config/mail.ini
  239. mkdir -p /data/config
  240. echo "[smtp]" > /data/config/mail.ini
  241. echo "server = $smtp_server" >> /data/config/mail.ini
  242. echo "port = $smtp_port" >> /data/config/mail.ini
  243. echo "user = $smtp_user" >> /data/config/mail.ini
  244. echo "pass = $smtp_pass" >> /data/config/mail.ini
  245. echo "[imap]" >> /data/config/mail.ini
  246. echo "server = $imap_server" >> /data/config/mail.ini
  247. echo "port = $imap_port" >> /data/config/mail.ini
  248. echo "user = $imap_user" >> /data/config/mail.ini
  249. echo "pass = $imap_pass" >> /data/config/mail.ini
  250. echo "[general]" >> /data/config/mail.ini
  251. echo "realname = $realname" >> /data/config/mail.ini
  252. echo "mail.ini generated"
  253. fi
  254. if [ $do_gen == 1 ]; then
  255. [ ! -e /data/game-1/eressea.ini ] && usage "eressea.ini missing. Use option -i"
  256. if [ $force == 1 ]; then
  257. echo "existing game data deleted"
  258. for node in /data/game-1/*
  259. do
  260. [ $node == "/data/game-1/eressea.ini" ] && continue
  261. [ -d $node ] && rm -rf $node || rm -f $node
  262. done;
  263. else
  264. echo "existing game data is not touched. Missing files are recreated"
  265. fi
  266. mkdir -p /data/config
  267. tmpfile=$(mktemp ini.XXX)
  268. cat /data/game-1/eressea.ini > $tmpfile
  269. cat /data/config/mail.ini >> $tmpfile
  270. [ -e /data/config/fetchmailrc ] && [ $force == 0 ] && rm -f /data/config/fetchmailrc
  271. [ ! -e /data/config/fetchmailrc ] && j2 -f ini /eressea/template-config/fetchmailrc $tmpfile > /data/config/fetchmailrc
  272. chmod 700 /data/config/fetchmailrc
  273. [ -e /data/config/procmailrc ] && [ $force == 0 ] && rm -f /data/config/procmailrc
  274. [ ! -e /data/config/procmailrc ] && j2 -f ini /eressea/template-config/procmailrc $tmpfile > /data/config/procmailrc
  275. [ -e /data/config/muttrc ] && [ $force == 0 ] && rm -f /data/config/muttrc
  276. [ ! -e /data/config/muttrc ] && j2 -f ini /eressea/template-config/muttrc $tmpfile > /data/config/muttrc
  277. [ -e /data/config/logrotate ] && [ $force == 0 ] && rm -f /data/config/logrotate
  278. [ ! -e /data/config/logrotate ] && cp /eressea/template-config/logrotate /data/config/logrotate
  279. rm -f $tmpfile
  280. mkdir -p /data/mail/cache
  281. mkdir -p /data/mail/postbox/inbox/{cur,new,tmp}
  282. mkdir -p /data/mail/postbox/sent/{cur,new,tmp}
  283. mkdir -p /data/mail/postbox/draft/{cur,new,tmp}
  284. mkdir -p /data/mail/certificates
  285. mkdir -p /data/game-1/data
  286. mkdir -p /data/game-1/reports
  287. mkdir -p /data/game-1/backup
  288. [ ! -e /data/game-1/newfactions ] && touch /data/game-1/newfactions
  289. [ ! -e /data/game-1/turn ] && echo 0 > /data/game-1/turn
  290. echo "all basic game files generated"
  291. echo "next step would be to create a new game file / map; therefore use '$0 map -h'"
  292. fi
  293. [ $do_gen == 0 ] && [ $do_ini == 0 ] && usage "nothing to generate. Use option -i or -g"
  294. }
  295. cmd_map() {
  296. usage() {
  297. [ -n "$1" ] && echo -e "\n$1"
  298. echo ""
  299. echo "Generate or edit Eressea map."
  300. echo "Usage: $0 map [options]"
  301. echo "-h ... show this help"
  302. echo "-n ... generate new map. If map already exists, it will be overwritten!"
  303. echo "-w ... width of new map - only relevant together with -n"
  304. echo "-e ... height of new map - only relevant together with -n"
  305. echo "-t ... turn"
  306. echo "-s ... save map when editor is closed"
  307. exit 2
  308. }
  309. args=$(getopt --name shutdown -o hnw:e:t:s -- "$@")
  310. if [ $? != 0 ]; then
  311. usage
  312. exit
  313. fi
  314. eval set -- "$args"
  315. create_new=0
  316. export ERESSEA_MAP_WIDTH=60
  317. export ERESSEA_MAP_HEIGHT=40
  318. get_turn
  319. save=0
  320. while :; do
  321. case "$1" in
  322. -h) usage ; shift ;;
  323. -n) create_new=1 ; shift ;;
  324. -w) ERESSEA_MAP_WIDTH=$2 ; shift 2 ;;
  325. -e) ERESSEA_MAP_HEIGHT=$2; shift 2 ;;
  326. -t) turn=$2 ; shift 2 ;;
  327. -s) save=1 ; shift ;;
  328. --) shift ; break ;;
  329. esac
  330. done
  331. cmd_startup
  332. if [ $create_new == 1 ]; then
  333. ini_sec game
  334. ini_add game seed `shuf -i 0-9999 -n1`
  335. ./eressea -v 0 -t $turn /eressea/lua-scripts/newgame.lua
  336. echo "created new game map with size ${ERESSEA_MAP_WIDTH}x${ERESSEA_MAP_HEIGHT}"
  337. fi
  338. if [ $save == 1 ]; then
  339. ./eressea -v 0 -t $turn /eressea/lua-scripts/modifymap.lua
  340. else
  341. ./eressea -v 0 -t $turn /data/server/scripts/map.lua
  342. fi
  343. cmd_shutdown
  344. }
  345. cmd_addpwd() {
  346. usage() {
  347. [ -n "$1" ] && echo -e "\n$1"
  348. echo ""
  349. echo "In file 'newfactions' on lines without password a secure one is generated and inserted."
  350. echo "Usage: $0 addpwd [options]"
  351. echo "-f ... force usage of new password"
  352. echo "-h ... show this help"
  353. exit 2
  354. }
  355. args=$(getopt --name shutdown -o hf -- "$@")
  356. if [ $? != 0 ]; then
  357. usage
  358. exit
  359. fi
  360. eval set -- "$args"
  361. force=0
  362. while :; do
  363. case "$1" in
  364. -h) usage ; shift ;;
  365. -f) force=1; shift ;;
  366. --) shift ; break ;;
  367. esac
  368. done
  369. [ ! -e "/data/game-1/newfactions" ] && echo "file newfactions does not exist" && exit
  370. tmpfile=$(mktemp newfactions.XXX)
  371. touch $tmpfile
  372. while IFS=" " read -r email race language pass alliance
  373. do
  374. [ -z "$email" ] && continue
  375. echo "found player $email ($race), language $language`[ -n "$alliance" ] && echo " in alliance with $alliance"`"
  376. if [ -z "$pass" ] || [ $force == 1 ]; then
  377. new_pass="`pwgen -c -n -y 8 1`"
  378. echo " new password $new_pass`[ -n "$pass" ] && echo " replaces former password $pass"`"
  379. else
  380. new_pass=$pass
  381. echo " uses password $pass"
  382. fi
  383. echo "$email $race $language $new_pass`[ -n "$alliance" ] && echo " $alliance"`" >> $tmpfile
  384. done < /data/game-1/newfactions
  385. rm -f /data/game-1/newfactions
  386. mv $tmpfile /data/game-1/newfactions
  387. }
  388. cmd_mail() {
  389. usage() {
  390. [ -n "$1" ] && echo -e "\n$1"
  391. echo ""
  392. echo "Process incoming e-mails"
  393. echo "Usage: $0 mail [options]"
  394. echo "-c ... check orders and send email to player"
  395. echo "-f ... fetch mail from server and pre-process them"
  396. echo "-h ... show this help"
  397. exit 2
  398. }
  399. args=$(getopt --name shutdown -o hcf -- "$@")
  400. if [ $? != 0 ]; then
  401. usage
  402. exit
  403. fi
  404. eval set -- "$args"
  405. fetch=0
  406. check=0
  407. while :; do
  408. case "$1" in
  409. -f) fetch=1; shift ;;
  410. -c) check=1; shift ;;
  411. -h) usage ; shift ;;
  412. --) shift ; break ;;
  413. esac
  414. done
  415. [ $(expr $fetch + $check) == 0 ] && usage "either option -f or -c is necessary"
  416. cmd_startup
  417. if [ $fetch == 1 ]; then
  418. touch /data/log/fetchmail.log
  419. fetchmail >> /data/log/fetchmail.log 2>&1
  420. fi
  421. if [ $check == 1 ]; then
  422. mkdir -p /data/game-1/orders.dir
  423. rules="$(ini_get lua rules)"
  424. /data/orders-php/check-orders.sh 1 $rules
  425. fi
  426. cmd_shutdown
  427. }
  428. cmd_run() {
  429. usage() {
  430. [ -n "$1" ] && echo -e "\n$1"
  431. echo ""
  432. echo "Execute next game turn"
  433. echo "Usage: $0 rung [options]"
  434. echo "-h ... show this help"
  435. exit 2
  436. }
  437. args=$(getopt --name shutdown -o h -- "$@")
  438. if [ $? != 0 ]; then
  439. usage
  440. exit
  441. fi
  442. eval set -- "$args"
  443. while :; do
  444. case "$1" in
  445. -h) usage ; shift ;;
  446. --) shift ; break ;;
  447. esac
  448. done
  449. cmd_startup
  450. mkdir -p $HOME/log
  451. mkdir -p /data/game-1/orders.dir
  452. touch /data/log/eressea.cron.log
  453. ln -sf /data/log/eressea.cron.log $HOME/log/eressea.cron.log
  454. get_turn
  455. enable_empty_orders="no"
  456. [ "$turn" == "0" ] && enable_empty_orders="yes"
  457. /eressea/run-eressea.sh 1
  458. cmd_shutdown
  459. }
  460. # ----------------
  461. # -- Main function
  462. COMMAND="$1"
  463. shift
  464. case $COMMAND in
  465. "help" | "startup" | "shutdown" | "bash" | "generate" | "map" | "addpwd" | "mail" | "run") eval cmd_$COMMAND "$@" ;;
  466. *) cmd_help ;;
  467. esac