-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitscript.sh
170 lines (141 loc) · 5.11 KB
/
initscript.sh
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
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/sh
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "$(date): \"${last_command}\" command filed with exit code $?."' EXIT
#set -x # for debugging
### BEGIN INIT INFO
# Provides: @@@APPNAME@@@
# Required-Start: $syslog $remote_fs
# Required-Stop: $syslog $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
USER=root
CONTAINER_ENGINE=podman
COMPOSE_ENGINE=podman-compose
APP_NAME="@@@APPNAME@@@"
APP_HOME="/opt/plcnext/appshome"
APP_ID="@@@APPIDENTIFIER@@@"
##________Usefull environment variables________##
export APP_UNIQUE_NAME="@@@APPNAME@@@_@@@APPIDENTIFIER@@@" # unique AppName to use
export APP_PATH="/opt/plcnext/apps/${APP_ID}" # mountpoint for ro app file
export APP_TMP_PATH="/var/tmp/appsdata/${APP_ID}" # app temporary data storage
export APP_DATA_PATH="${APP_HOME}/data/${APP_ID}" # app persistent data storage
export APP_LOG="${APP_DATA_PATH}/${APP_NAME}.log" # logfile
ORGANIZATION_NAME=localhost
#add path to app-binaries
export PATH=$PATH:$APP_HOME/bin
#export env files
export $(grep -v '^#' $APP_PATH/.env | xargs)
start ()
{
if [ ! -e "$APP_LOG" ]
then
touch $APP_LOG
fi
echo "$(date): Executing start()" >> $APP_LOG
# When start() is executed then
# either
# the image is not loaded and the container not started.
# Then load the docker image from the app directory and run container with
# restart option to start it again after each boot of the controller.
# or
# the container is already loaded into the container cache.
# It will be started by the container engine automatically.
# copy user configuration file to the app persistent storage if needed
echo "$(date) Executing start()" >> $APP_LOG
echo "Installing podman-compose" >> $APP_LOG
podman load -i $APP_PATH/images/PodmanCompose.tar.gz >> $APP_LOG
podman tag 81cb2d500ca0 k8s.gcr.io/pause:3.5 >> $APP_LOG
# Check app, install, run
if [ -e "$APP_DATA_PATH/dockerapp_install" ]; then
# app is already installed and container exists
# just start it
echo "$NAME is already installed. podman will start it automatically" >> $APP_LOG
chmod -R 777 $APP_DATA_PATH
cd $APP_DATA_PATH
# Start UP Podman
podman-compose down
podman-compose up -d
else
echo "$NAME is not installed --> load images and install" >> $APP_LOG
echo "Copy content" >> $APP_LOG
cp -r $APP_PATH "$APP_HOME/data"
chmod -R 777 $APP_DATA_PATH
echo "Load images" >> $APP_LOG
podman load -i $APP_DATA_PATH/images/${IMAGE_FILE_NAME}.tar.gz >> $APP_LOG 2>&1
if [ ! $? -eq 0 ]
then
stop
echo "$(date): Wasn't able to load ${IMAGE_FILE_NAME}." >> $APP_LOG
exit 201
fi
# Remove tmp tar file from container load
rm /media/rfs/rw/data/system/containers/docker-tar* >> $APP_LOG 2>&1
if [ ! $? -eq 0 ]
then
echo "$(date): Wasn't able to remove with command: '$0'." >> $APP_LOG
fi
echo "Start compose" >> $APP_LOG
cd $APP_DATA_PATH
podman-compose up -d >> $APP_LOG 2>&1
if [ ! $? -eq 0 ]
then
stop
echo "$(date): Wasn't able to start podman compose." >> $APP_LOG
exit 201
fi
# set app is installed
touch $APP_DATA_PATH/dockerapp_install
echo "Installation finished" >> $APP_LOG
fi
echo "$(date) start() finished" >> $APP_LOG
# write Podman events to syslog
logger -f /run/libpod/events/events.log
rm -f /run/libpod/events/events.log
}
stop ()
{
echo "$(date) Executing stop()" >> $APP_LOG
# stop() is called when App is stopped by the AppManager e.g. via WBM
# in this case the container needs to be stopped and removed from
# the container cache. The goal is to keep the controller clean.
# stop() is also called when the system will shutdown.
# In this case the container should not be removed.
# The container should just be started when the controller starts up again.
if [ -e "$APP_DATA_PATH/dockerapp_install" ]; then
# Distinguish whether the controller is in shutdown phase
# if not shutdown then the app is explicitly stopped -> remove container and image
currentRunlevel=$(runlevel | cut -d ' ' -f2)
echo current runlevel=$currentRunlevel >> $APP_LOG
if [ "$currentRunlevel" -ne "6" ]; then
echo "Stop $NAME" >> $APP_LOG
cd $APP_DATA_PATH
echo "Remove compose and all used images."
podman-compose down
podman image rm --force ${IMAGE_NAME}:${IMAGE_TAG}
rm $APP_DATA_PATH/dockerapp_install
fi
else
echo "Stop $NAME: container not installed" >> $APP_LOG
# the container is not installed.
# nothing to be done
fi
echo "$(date) stop() finished" >> $APP_LOG
# write Podman events to syslog
logger -f /run/libpod/events/events.log
rm -f /run/libpod/events/events.log
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
esac