Skip to content

Commit

Permalink
Simplify devcontainer setup (home-assistant#1687)
Browse files Browse the repository at this point in the history
* Simplify devcontainer setup

* Add stable shellcheck

* Fix observer port
  • Loading branch information
ludeeus authored Nov 23, 2020
1 parent 4bbb66a commit 0b3c815
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 89 deletions.
57 changes: 22 additions & 35 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
FROM mcr.microsoft.com/vscode/devcontainers/base:debian-10
FROM mcr.microsoft.com/vscode/devcontainers/base:debian

WORKDIR /workspaces

# Default ENV
ENV LANG C.UTF-8
ENV DEBIAN_FRONTEND noninteractive

# Set shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Install docker
# https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/
RUN apt-get update && apt-get install -y --no-install-recommends \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
gnupg \
&& curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - \
&& add-apt-repository "deb https://download.docker.com/linux/debian $(lsb_release -cs) stable" \
&& apt-get update && apt-get install -y --no-install-recommends \
docker-ce \
docker-ce-cli \
containerd.io \
&& rm -rf /var/lib/apt/lists/*
# Set Docker daemon config
RUN \
mkdir -p /etc/docker \
&& echo '{"storage-driver": "vfs"}' > /etc/docker/daemon.json

# Install tools
ARG SHELLCHECK_VERSION=0.7.0
RUN apt-get update && apt-get install -y --no-install-recommends \
jq \
# Installa aditional tools
RUN \
apt-get update \
&& apt-get install -y --no-install-recommends \
dbus \
network-manager \
libpulse0 \
git \
tar \
xz-utils \
&& rm -rf /var/lib/apt/lists/* \
xz-utils

# Install docker
RUN curl -fsSL https://get.docker.com | sh -

# Install shellcheck
RUN \
curl -fLs \
"https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.linux.x86_64.tar.xz" \
| tar -xJ \
\
&& curl -SL "https://storage.googleapis.com/shellcheck/shellcheck-v${SHELLCHECK_VERSION}.linux.x86_64.tar.xz" | tar xJf - \
&& cp "shellcheck-v${SHELLCHECK_VERSION}/shellcheck" /usr/bin/ \
&& rm -rf shellcheck-v${SHELLCHECK_VERSION}
&& mv -f "./shellcheck-stable/shellcheck" "/usr/bin/shellcheck" \
&& rm -rf "./shellcheck-stable"

# Generate a machine-id for this container
RUN rm /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id

ENV DEBIAN_FRONTEND=dialog
RUN rm /etc/machine-id && dbus-uuidgen --ensure=/etc/machine-id
13 changes: 4 additions & 9 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
"name": "Home Assistant Add-Ons",
"context": "..",
"dockerFile": "Dockerfile",
"appPort": "7123:8123",
"runArgs": [
"-e",
"GIT_EDITOR=code --wait",
"--privileged"
],
"appPort": ["7123:8123", "7357:4357"],
"postStartCommand": "service docker start",
"runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"],
"extensions": [
"timonwong.shellcheck",
"esbenp.prettier-vscode"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/test_hassio/addons/local,type=bind,consistency=delegated",
"workspaceFolder": "/workspaces/test_hassio/addons/local"
}
}
74 changes: 40 additions & 34 deletions .devcontainer/start_supervisor.sh → .devcontainer/supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,58 +55,64 @@ function stop_docker() {
}


function install() {
docker pull homeassistant/amd64-hassio-supervisor:dev
function cleanup_lastboot() {
if [[ -f /tmp/supervisor_data/config.json ]]; then
echo "Cleaning up last boot"
cp /tmp/supervisor_data/config.json /tmp/config.json
jq -rM 'del(.last_boot)' /tmp/config.json > /tmp/supervisor_data/config.json
rm /tmp/config.json
fi
}

function cleanup_hass_data() {
rm -rf /workspaces/test_hassio/{apparmor,backup,config.json,dns,dns.json,homeassistant,homeassistant.json,ingress.json,share,ssl,tmp,updater.json}
rm -rf /workspaces/test_hassio/addons/{core,data,git}
}

function cleanup_docker() {
echo "Cleaning up stopped containers..."
docker rm "$(docker ps -a -q)"
}

function cleanup_lastboot() {
if [[ -f /workspaces/test_hassio/config.json ]]; then
echo "Cleaning up last boot"
cp /workspaces/test_hassio/config.json /tmp/config.json
jq -rM 'del(.last_boot)' /tmp/config.json > /workspaces/test_hassio/config.json
rm /tmp/config.json
fi
docker rm "$(docker ps -a -q)" || true
}

function run_supervisor() {
mkdir -p /tmp/supervisor_data
docker run --rm --privileged \
--name hassio_supervisor \
--security-opt seccomp=unconfined \
--security-opt apparmor:unconfined \
-v /run/docker.sock:/run/docker.sock \
-v /run/dbus:/run/dbus \
-v "/workspaces/test_hassio":/data \
-v /tmp/supervisor_data:/data \
-v "/workspaces/addons":/data/addons/local \
-v /etc/machine-id:/etc/machine-id:ro \
-e SUPERVISOR_SHARE="/workspaces/test_hassio" \
-e SUPERVISOR_SHARE="/tmp/supervisor_data" \
-e SUPERVISOR_NAME=hassio_supervisor \
-e SUPERVISOR_DEV=1 \
-e SUPERVISOR_MACHINE="qemux86-64" \
homeassistant/amd64-hassio-supervisor:dev
}

case "$1" in
"--cleanup")
echo "Cleaning up old environment"
cleanup_docker || true
cleanup_hass_data || true
exit 0;;
*)
echo "Creating development Supervisor environment"
start_docker
trap "stop_docker" ERR
cleanup_docker || true
cleanup_lastboot || true
install
run_supervisor
stop_docker;;
esac
function init_dbus() {
if pgrep dbus-daemon; then
echo "Dbus is running"
return 0
fi

echo "Startup dbus"
mkdir -p /var/lib/dbus
cp -f /etc/machine-id /var/lib/dbus/machine-id

# cleanups
mkdir -p /run/dbus
rm -f /run/dbus/pid

# run
dbus-daemon --system --print-address
}

echo "Start Test-Env"

start_docker
trap "stop_docker" ERR

cleanup_lastboot
cleanup_docker
init_dbus
run_supervisor
stop_docker
12 changes: 1 addition & 11 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{
"label": "Start Home Assistant",
"type": "shell",
"command": "/workspaces/test_hassio/addons/local/.devcontainer/start_supervisor.sh",
"command": "/workspaces/addons/.devcontainer/supervisor.sh",
"group": {
"kind": "test",
"isDefault": true,
Expand All @@ -16,16 +16,6 @@
"panel": "new"
},
"problemMatcher": []
},{
"label": "Cleanup stale Home Assistant environment",
"type": "shell",
"command": "/workspaces/test_hassio/addons/local/.devcontainer/start_supervisor.sh --cleanup",
"group": "test",
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": []
},{
"label": "Run Home Assistant CLI",
"type": "shell",
Expand Down

0 comments on commit 0b3c815

Please sign in to comment.