Skip to content

Commit cfecd53

Browse files
author
marcel
committed
A cont-init.d script for the official openHAB Docker container.
* Download an install the helper libraries. * Copy the initial configuration.[groovy|js|py] in place if it does not exist. This will script will download the helper libraries to the /openhab/automation/openhab-helper-libraries directory when this directory does not exist. Before making any changes, the script does a sanity check to make sure that the required changes can be made. In case of a problem the script will abort before making any changes to the system. Updating the helper libraries is as easy as deleting the /openhab/automation/openhab-helper-libraries directory and restarting the container. The script creates the following directory structure (where 'core' is a symlink the the indicated core directory in the openhab-helper-library): /openhab/conf/automation/jsr223/groovy/community /openhab/conf/automation/jsr223/groovy/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/jsr223/groovy/core /openhab/conf/automation/jsr223/groovy/personal /openhab/conf/automation/jsr223/javascript/community /openhab/conf/automation/jsr223/javascript/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/jsr223/javascript/core /openhab/conf/automation/jsr223/javascript/personal /openhab/conf/automation/jsr223/python/community /openhab/conf/automation/jsr223/python/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/jsr223/python/core /openhab/conf/automation/jsr223/python/personal /openhab/conf/automation/lib/groovy/community /openhab/conf/automation/lib/groovy/configuration.groovy /openhab/conf/automation/lib/groovy/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/lib/groovy/core /openhab/conf/automation/lib/groovy/personal /openhab/conf/automation/lib/javascript/community /openhab/conf/automation/lib/javascript/configuration.js /openhab/conf/automation/lib/javascript/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/lib/javascript/core /openhab/conf/automation/lib/javascript/personal /openhab/conf/automation/lib/python/community /openhab/conf/automation/lib/python/configuration.py /openhab/conf/automation/lib/python/core -> /openhab/conf/automation/openhab-helper-libraries/Core/automation/lib/python/core /openhab/conf/automation/lib/python/personal /openhab/conf/automation/openhab-helper-libraries
1 parent d316364 commit cfecd53

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#!/bin/bash -x
2+
3+
OPENHAB_AUTOMATION="${OPENHAB_CONF}/automation"
4+
OPENHAB_HL_AUTOMATION="${OPENHAB_AUTOMATION}/openhab-helper-libraries/Core/automation"
5+
OPENHAB_HL_URL="https://github.com/openhab-scripters/openhab-helper-libraries/archive/master.zip"
6+
7+
declare -A LANGUAGES=( ["groovy"]="groovy" ["javascript"]="js" ["python"]="py" )
8+
9+
function verify_directory_structure() {
10+
11+
# before making any changes let's first verify that we can make the required changes
12+
verify_directory "${OPENHAB_AUTOMATION}"
13+
for SUBDIR in jsr223 lib; do
14+
verify_directory "${OPENHAB_AUTOMATION}/${SUBDIR}"
15+
for LANGUAGE in "${!LANGUAGES[@]}"; do
16+
verify_directory "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/community"
17+
verify_directory "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/personal"
18+
19+
verify_symlink "${OPENHAB_AUTOMATION}/${SUBDIR}/${LANGUAGE}/core" "${OPENHAB_HL_AUTOMATION}/${SUBDIR}/${LANGUAGE}/core"
20+
done
21+
done
22+
}
23+
24+
function create_directory_structure() {
25+
create_directory "${OPENHAB_AUTOMATION}"
26+
for SUBDIR in jsr223 lib; do
27+
create_directory "${OPENHAB_AUTOMATION}/${SUBDIR}"
28+
for LANGUAGE in "${!LANGUAGES[@]}"; do
29+
create_directory "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/community"
30+
chmod g+w "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/community"
31+
create_directory "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/personal"
32+
chmod g+w "${OPENHAB_AUTOMATION}/${SUBDIR}/$LANGUAGE/personal"
33+
34+
create_symlink "${OPENHAB_AUTOMATION}/${SUBDIR}/${LANGUAGE}/core" "${OPENHAB_HL_AUTOMATION}/${SUBDIR}/${LANGUAGE}/core"
35+
done
36+
done
37+
}
38+
39+
function verify_directory() {
40+
local DIRECTORY=$1
41+
if [ -L "${DIRECTORY}" ]; then
42+
echo "ERROR: Cannot create directory ${DIRECTORY}. A symlink with that link name already exists."
43+
exit 1
44+
elif [ -f "${DIRECTORY}" ]; then
45+
echo "ERROR: Cannot create directory ${DIRECTORY}. A file with that name already exists."
46+
exit 1
47+
fi
48+
}
49+
50+
function create_directory() {
51+
local DIRECTORY=$1
52+
if [ ! -d "${DIRECTORY}" ]; then
53+
mkdir -p "${DIRECTORY}"
54+
if [ $? -ne 0 ]; then
55+
echo "ERROR: Could not create directory ${DIRECTORY}."
56+
exit 1
57+
fi
58+
else
59+
echo "Directory ${DIRECTORY} already exists."
60+
fi
61+
}
62+
63+
function verify_symlink() {
64+
local LINK_NAME=$1
65+
local TARGET=$2
66+
if [ -L "${LINK_NAME}" ]; then
67+
local LINK_TARGET="$(readlink ${LINK_NAME})"
68+
if [ "${LINK_TARGET}" != "${TARGET}" ]; then
69+
echo "ERROR: A symlink with ${LINK_NAME} already exists pointing to a different target."
70+
exit 1
71+
fi
72+
elif [ -e "${LINK_NAME}" ]; then
73+
echo "ERROR: File or directory with name ${LINK_NAME} already exists."
74+
exit 1
75+
fi
76+
}
77+
78+
function create_symlink() {
79+
local LINK_NAME=$1
80+
local TARGET=$2
81+
if [ ! -L "${LINK_NAME}" ]; then
82+
ln -s "${TARGET}" "${LINK_NAME}"
83+
if [ $? -ne 0 ]; then
84+
echo "ERROR: Could not create symlink ${LINK_NAME} to ${TARGET}."
85+
exit 1
86+
fi
87+
else
88+
echo "Symlink ${LINK_NAME} already exists."
89+
fi
90+
}
91+
92+
function create_initial_configuration() {
93+
for LANGUAGE in "${!LANGUAGES[@]}"; do
94+
if [ ! -f "${OPENHAB_AUTOMATION}/lib/${LANGUAGE}/configuration.${LANGUAGES[$LANGUAGE]}" ]; then
95+
cp "${OPENHAB_HL_AUTOMATION}/lib/${LANGUAGE}/configuration.${LANGUAGES[$LANGUAGE]}.example" "${OPENHAB_AUTOMATION}/lib/${LANGUAGE}/configuration.${LANGUAGES[$LANGUAGE]}"
96+
fi
97+
done
98+
}
99+
100+
function download_helper_libraries() {
101+
# Download the Helper Libraries for openHAB Scripted Automation
102+
echo "Downloading openhab-helper-libraries-master archive from Github."
103+
wget -nv -O /tmp/openhab-helper-libraries-master.zip "${OPENHAB_HL_URL}"
104+
if [ $? -ne 0 ]; then
105+
echo "ERROR: Failed to download the helper libraries form Github."
106+
exit 1
107+
fi
108+
109+
unzip -q /tmp/openhab-helper-libraries-master.zip -d /tmp -x \
110+
"openhab-helper-libraries-master/.github/*" \
111+
"openhab-helper-libraries-master/.gitignore" \
112+
"openhab-helper-libraries-master/docs/*" \
113+
"openhab-helper-libraries-master/Docker/*" \
114+
"openhab-helper-libraries-master/Sphinx/*"
115+
if [ $? != 0 ]; then
116+
echo "ERROR: Failed to extract the helper libraries zip file."
117+
exit 1
118+
fi
119+
120+
rm /tmp/openhab-helper-libraries-master.zip
121+
}
122+
123+
function install_helper_libraries() {
124+
mv /tmp/openhab-helper-libraries-master "${OPENHAB_AUTOMATION}/openhab-helper-libraries"
125+
# update ownership
126+
chown -R openhab:openhab "${OPENHAB_AUTOMATION}"
127+
}
128+
129+
function enable_next_generation_rule_engine() {
130+
# Enable the Next Generation Rule Engine
131+
set +e
132+
MISC_LINE=$(grep '^[[:space:]]\?misc' ${OPENHAB_CONF}/services/addons.cfg)
133+
if [ $? -eq 0 ]; then
134+
# ensure we have ruleengine enabled
135+
if [[ ${MISC_LINE} == *"ruleengine"* ]]; then
136+
echo "New rule engine is already included in the addons.cfg."
137+
else
138+
sed -i 's/misc\s\?=\s\?/misc = ruleengine,/' ${OPENHAB_CONF}/services/addons.cfg
139+
fi
140+
else
141+
# Just append last line
142+
echo "Append 'misc = ruleengine' to ${OPENHAB_CONF}/services/addons.cfg."
143+
echo "misc = ruleengine" >> ${OPENHAB_CONF}/services/addons.cfg
144+
fi
145+
}
146+
147+
148+
if [ ! -d "${OPENHAB_AUTOMATION}/openhab-helper-libraries" ]; then
149+
# verify if installation is possible
150+
verify_directory_structure
151+
download_helper_libraries
152+
153+
# make the required changes and install the libraries
154+
create_directory_structure
155+
install_helper_libraries
156+
157+
# create initial configuration if required
158+
create_initial_configuration
159+
160+
# enable the next genereation rule engine if required
161+
enable_next_generation_rule_engine
162+
else
163+
echo "Helper Libraries for openHAB Scripted Automation already installed."
164+
fi
165+

0 commit comments

Comments
 (0)