Skip to content

Commit 03ce42a

Browse files
committed
🦸‍ Home Assistant Supervised in Docker
0 parents  commit 03ce42a

File tree

20 files changed

+539
-0
lines changed

20 files changed

+539
-0
lines changed

.github/workflows/build.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Build Docker Image
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: Version
8+
required: true
9+
10+
jobs:
11+
docker:
12+
runs-on: ubuntu-latest
13+
steps:
14+
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: Set up QEMU
19+
uses: docker/setup-qemu-action@v3
20+
21+
- name: Set up Docker Buildx
22+
uses: docker/setup-buildx-action@v3
23+
24+
- name: Login to GitHub Container Registry
25+
uses: docker/login-action@v3
26+
with:
27+
registry: ghcr.io
28+
username: ${{ github.repository_owner }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Build and push
32+
uses: docker/build-push-action@v5
33+
with:
34+
context: .
35+
platforms: linux/amd64,linux/arm64,linux/arm/v7
36+
push: true
37+
tags: |
38+
ghcr.io/${{ github.repository_owner }}/hass-super:latest
39+
ghcr.io/${{ github.repository_owner }}/hass-super:${{ github.event.inputs.version }}

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM debian:bookworm
2+
3+
ENV container=docker \
4+
DOCKER_STORAGE_DRIVER=vfs \
5+
DEBIAN_FRONTEND=noninteractive
6+
7+
COPY build /tmp/build
8+
RUN run-parts --report --exit-on-error /tmp/build/scripts && rm -rfv /tmp/build
9+
10+
EXPOSE 22
11+
VOLUME ["/usr/share/hassio", "/var/lib/docker"]
12+
WORKDIR /root
13+
14+
ENTRYPOINT ["/opt/init-wrapper/sbin/entrypoint.sh"]
15+
CMD ["/sbin/init"]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#! /bin/sh
2+
3+
set -ex
4+
5+
if [ -f /usr/bin/dockerd ]; then
6+
exit 0
7+
fi
8+
9+
if [ $(cat /etc/timezone) = "Asia/Shanghai" ]; then
10+
sed -i 's@download.docker.com@mirrors.ustc.edu.cn/docker-ce@g' /etc/apt/sources.list.d/docker.list
11+
apt-get update
12+
fi
13+
14+
apt-get install --no-install-recommends -y docker-ce
15+
docker version
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#! /bin/sh
2+
3+
set -ex
4+
5+
if [ -f /usr/bin/os-agent ]; then
6+
exit 0
7+
fi
8+
9+
dpkg -i /opt/os-agent.deb
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /bin/sh
2+
3+
set -ex
4+
5+
ARCH=$(uname -m)
6+
case ${ARCH} in
7+
"arm" | "armv6l" | "armv7l")
8+
export MACHINE=${MACHINE:=qemuarm}
9+
;;
10+
"aarch64")
11+
export MACHINE=${MACHINE:=qemuarm-64}
12+
;;
13+
esac
14+
15+
xargs -0 -n1 < /proc/1/environ | sed "s/=\(.*\)/='\1'/" | while read line; do
16+
export "$line"
17+
done
18+
env >> /tmp/hassio.log
19+
20+
mount --make-shared "${DATA_SHARE:=/usr/share/hassio}"
21+
22+
if [ -f /usr/sbin/hassio-supervisor ]; then
23+
exit 0
24+
fi
25+
26+
deb="/opt/hassio.deb"
27+
if [ $(cat /etc/timezone) = "Asia/Shanghai" ]; then
28+
deb="/opt/hasscn.deb"
29+
fi
30+
dpkg --force-confdef --force-confold -i $deb
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /bin/sh
2+
3+
set -e
4+
5+
## environment variables
6+
##
7+
## - DEFAULT_LANG
8+
## - DEFAULT_TZ
9+
10+
if [ -n "${DEFAULT_LANG}" ]; then
11+
sed -i -e "s/LANG=C/LANG=${DEFAULT_LANG}/" /etc/default/locale
12+
fi
13+
14+
if [ -n "${DEFAULT_TZ}" ]; then
15+
echo "${DEFAULT_TZ}" > /etc/timezone
16+
# workaround: see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=813226
17+
[ -L /etc/localtime ] && rm /etc/localtime
18+
dpkg-reconfigure -f noninteractive tzdata 1>/dev/null 2>/dev/null
19+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/sh
2+
3+
export DEBIAN_FRONTEND=noninteractive
4+
5+
##
6+
## regenerate ssh host keys
7+
##
8+
if dpkg-query -s openssh-server 1>/dev/null 2>/dev/null; then
9+
rm /etc/ssh/ssh_host_*_key /etc/ssh/ssh_host_*_key.pub
10+
dpkg-reconfigure openssh-server 1>/dev/null 2>/dev/null
11+
fi
12+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#! /bin/sh
2+
3+
## environment variables
4+
##
5+
## - CUSTOM_GROUP : group name
6+
## - CUSTOM_GROUP_GID : group id
7+
## - CUSTOM_USER : user name
8+
## - CUSTOM_USER_UID : user id
9+
## - CUSTOM_USER_GECOS : gecos field for user
10+
## - CUSTOM_USER_SHELL : login shell for user
11+
##
12+
13+
if [ -n "${CUSTOM_USER}" ]; then
14+
## create group
15+
if [ -n "${CUSTOM_GROUP}" ]; then
16+
group=${CUSTOM_GROUP}
17+
else
18+
group=${CUSTOM_USER}
19+
fi
20+
addgroup ${CUSTOM_GROUP_GID+--gid ${CUSTOM_GROUP_GID}} ${group} 1>/dev/null 2>/dev/null
21+
22+
## create user (without password, without sudo priv)
23+
adduser \
24+
--disabled-password \
25+
--quiet \
26+
--gecos "${CUSTOM_USER_GECOS}" \
27+
--shell ${CUSTOM_USER_SHELL-/bin/bash} \
28+
${CUSTOM_USER_UID+--uid ${CUSTOM_USER_UID}} \
29+
--ingroup ${group} \
30+
${CUSTOM_USER} 1>/dev/null 2>/dev/null
31+
32+
## add sudo priv
33+
adduser ${CUSTOM_USER} sudo 1>/dev/null 2>/dev/null
34+
adduser ${CUSTOM_USER} adm 1>/dev/null 2>/dev/null
35+
fi
36+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#! /bin/sh
2+
3+
## environment variables
4+
##
5+
## - USER_PASSWORD : debian user password
6+
## - USER_SSH_KEY_URI : ssh public key uri (or github account) for debian user
7+
## - ROOT_PASSWORD : root password
8+
## - ROOT_SSH_KEY_URI : ssh public key uri (or github account) for root
9+
## - CUSTOM_USER : custom user account
10+
## - CUSTOM_USER_PASSWORD : password for custom user
11+
## - CUSTOM_USER_SSH_KEY_URI : ssh public key uri (or github account) for custom user
12+
##
13+
14+
change_password (){
15+
user=$1
16+
password=$2
17+
if [ -n "${user}" -a -n "${password}" ]; then
18+
echo "${user}:${password}" | chpasswd 1>/dev/null 2>/dev/null
19+
fi
20+
unset user password
21+
}
22+
23+
add_ssh_key (){
24+
user=$1
25+
ssh_key_uri=$2
26+
if [ "$user" = "root" ]; then
27+
home=/root
28+
sudo=
29+
else
30+
home=/home/$user
31+
sudo="sudo -u $user"
32+
fi
33+
if [ -n "${ssh_key_uri}" ]; then
34+
if [ ! -d $home/.ssh ]; then
35+
$sudo mkdir -m 0700 $home/.ssh
36+
fi
37+
if [ ! -f $home/.ssh/authorized_keys ]; then
38+
$sudo bash -c "umask 066 && touch $home/.ssh/authorized_keys"
39+
fi
40+
41+
$sudo bash -c "echo '' >> $home/.ssh/authorized_keys"
42+
$sudo bash -c "echo '## ${ssh_key_uri}' >> $home/.ssh/authorized_keys"
43+
case "${ssh_key_uri}" in
44+
https?://*)
45+
curl -s -L -o - "${ssh_key_uri}" | $sudo bash -c "cat >> $home/.ssh/authorized_keys"
46+
;;
47+
*)
48+
if echo "${ssh_key_uri}" | grep -E '^[0-9a-zA-Z-]+$' 1>/dev/null 2>/dev/null; then
49+
curl -s -L -o - "https://github.com/${ssh_key_uri}.keys" | $sudo bash -c "cat >> $home/.ssh/authorized_keys"
50+
fi
51+
;;
52+
esac
53+
$sudo bash -c "echo '' >> $home/.ssh/authorized_keys"
54+
fi
55+
unset user ssh_key_uri home sudo
56+
}
57+
58+
## debian user
59+
change_password debian "${USER_PASSWORD}"
60+
add_ssh_key debian "${USER_SSH_KEY_URI}"
61+
62+
## root
63+
change_password root "${ROOT_PASSWORD}"
64+
add_ssh_key root "${ROOT_SSH_KEY_URI}"
65+
66+
## custom user
67+
change_password "${CUSTOM_USER}" "${CUSTOM_USER_PASSWORD}"
68+
add_ssh_key "${CUSTOM_USER}" "${CUSTOM_USER_SSH_KEY_URI}"
69+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#! /bin/sh
2+
3+
## environment variables
4+
##
5+
## - APT_LINE : "keep", "jp", "http://foobar.example.org/debian/"
6+
## - APT_UPDATE : "yes", "true", "on", 1 -> run apt-get update after init
7+
## - APT_INSTALL_PACKAGES : "pkg1 pkg2=1.2-3 pkg3- ..."
8+
## - APT_INSTALL_RECOMMENDS : "yes", "true", "on", 1
9+
## - APT_INSTALL_SUGGESTS : "yes", "true", "on", 1
10+
## - APT_HTTP_PROXY : "http://x.x.x.x:3142/"
11+
##
12+
13+
set -e
14+
post_init_script=/opt/init-wrapper/post-init.d/${0##*/}
15+
16+
if [ -z "${APT_LINE}" ]; then
17+
APT_LINE=keep
18+
if [ "$DEFAULT_TZ" = "Asia/Shanghai" ]; then
19+
APT_LINE=cn
20+
fi
21+
fi
22+
23+
change_apt_line (){
24+
uri=$1
25+
sed -i -e "s@http://deb\.debian\.org/debian@${uri}@" /etc/apt/sources.list.d/debian.sources
26+
}
27+
28+
clear_post_init (){
29+
if [ -f ${post_init_script} ]; then
30+
: > ${post_init_script}
31+
fi
32+
}
33+
34+
write_post_init (){
35+
echo "#! /bin/sh" > ${post_init_script}
36+
37+
case "${APT_UPDATE}" in
38+
1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN])
39+
echo "apt-get update";;
40+
*)
41+
echo "#apt-get update";;
42+
esac \
43+
>> ${post_init_script}
44+
45+
options=
46+
case "${APT_INSTALL_RECOMMENDS}" in
47+
1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN])
48+
options="$options --install-recommends";;
49+
*)
50+
options="$options --no-install-recommends";;
51+
esac
52+
case "${APT_INSTALL_SUGGESTS}" in
53+
1|[tT][rR][uU][eE]|[yY][eE][sS]|[oO][nN])
54+
options="$options --install-suggests";;
55+
*)
56+
options="$options --no-install-suggests";;
57+
esac
58+
if [ -n "${APT_INSTALL_PACKAGES}" ]; then
59+
echo "apt-get install -y ${options} ${APT_INSTALL_PACKAGES}"
60+
else
61+
echo "#apt-get install -y ${options} ${APT_INSTALL_PACKAGES}"
62+
fi \
63+
>> ${post_init_script}
64+
65+
echo "exit 0" \
66+
>> ${post_init_script}
67+
68+
chmod +x ${post_init_script}
69+
}
70+
71+
if [ -n "${APT_LINE}" ]; then
72+
case "${APT_LINE}" in
73+
keep)
74+
# do nothing (use deb.debian.org)
75+
:
76+
;;
77+
cn)
78+
change_apt_line http://mirrors.ustc.edu.cn/debian
79+
;;
80+
jp)
81+
change_apt_line http://ftp.jp.debian.org/debian
82+
;;
83+
*)
84+
change_apt_line "${APT_LINE}"
85+
;;
86+
esac
87+
fi
88+
89+
if [ -n "${APT_HTTP_PROXY}" ]; then
90+
echo "Acquire::http::proxy \"${APT_HTTP_PROXY}\";" > /etc/apt/apt.conf.d/proxy.conf
91+
echo "Acquire::https::proxy \"DIRECT\";" >> /etc/apt/apt.conf.d/proxy.conf
92+
fi
93+
94+
clear_post_init
95+
write_post_init
96+

0 commit comments

Comments
 (0)