Created
October 26, 2016 14:05
-
-
Save fearoffish/8013d4d432e6f75696e80f041b443884 to your computer and use it in GitHub Desktop.
Resin init script for Home-Assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Script options (exit script on command fail). | |
# | |
set -e | |
# | |
# Define default Variables. | |
# | |
USER="homeassistant" | |
GROUP="homeassistant" | |
GIT_REPO=${GIT_REPO} | |
CONFIG_DIR="/data/home-assistant-config" | |
COMMAND_OPTIONS_DEFAULT="-c ${CONFIG_DIR}" | |
UID_DEFAULT="1000" | |
GID_DEFAULT="1000" | |
COMMAND="hass --open-ui ${COMMAND_OPTIONS:=${COMMAND_OPTIONS_DEFAULT}}" | |
UID_ACTUAL=$(id -u ${USER}) | |
GID_ACTUAL=$(id -g ${GROUP}) | |
# | |
# Display settings on standard out. | |
# | |
echo "container settings" | |
echo "==================" | |
echo | |
echo " Username: ${USER}" | |
echo " Groupname: ${GROUP}" | |
echo " UID actual: ${UID_ACTUAL}" | |
echo " GID actual: ${GID_ACTUAL}" | |
echo " UID prefered: ${UID:=${UID_DEFAULT}}" | |
echo " GID prefered: ${GID:=${GID_DEFAULT}}" | |
echo " Command: ${COMMAND}" | |
echo | |
# So dirty....SO SO DIRTY | |
echo "Creating SSH key" | |
mkdir -p ~/.ssh | |
JUST_KEY=`echo $GIT_SSH_KEY | sed 's/-----BEGIN RSA PRIVATE KEY----- //' | sed 's/ -----END RSA PRIVATE KEY-----//'` | |
echo "-----BEGIN RSA PRIVATE KEY-----" > ~/.ssh/id_rsa | |
echo $JUST_KEY | tr ' ' '\n' >> ~/.ssh/id_rsa | |
echo "-----END RSA PRIVATE KEY-----" >> ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-keyscan github.com >> ~/.ssh/known_hosts | |
echo "Cloning ${GIT_REPO}" | |
rm -rf ${CONFIG_DIR} | |
git clone ${GIT_REPO} ${CONFIG_DIR} | |
# | |
# Change UID / GID of user. | |
# | |
echo "Updating UID / GID... " | |
if [[ ${GID_ACTUAL} -ne ${GID} -o ${UID_ACTUAL} -ne ${UID} ]] | |
then | |
echo "change user / group" | |
deluser ${USER} | |
addgroup -g ${GID} ${GROUP} | |
adduser -u ${UID} -G ${GROUP} -h /etc/bind -g "${USER} User" -s /bin/sh -D ${USER} | |
echo "[DONE]" | |
echo "Set owner and permissions for old uid/gid files" | |
find / -user ${UID_ACTUAL} -exec chown ${USER} {} \; | |
find / -group ${GID_ACTUAL} -exec chgrp ${GROUP} {} \; | |
echo "[DONE]" | |
else | |
echo "[NOTHING DONE]" | |
fi | |
# | |
# Set owner and permissions. | |
# | |
echo "Set owner and permissions... " | |
chown -R ${USER}:${GROUP} /home/${USER} | |
chown -R ${USER}:${GROUP} ${CONFIG_DIR} | |
chmod -R o-rwx /home/${USER} | |
echo "[DONE]" | |
# | |
# Start application. | |
# | |
echo "Start application..." | |
exec ${COMMAND} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment