Skip to content

Commit

Permalink
Make local hosted devcontainer docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Nov 22, 2023
1 parent 813e74f commit 9f577c5
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 2 deletions.
16 changes: 16 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG BUILD_FROM BUILD_FROM_TAG
FROM python:3.11-slim

ENV DEVCONTAINER=true

COPY ./container /container
COPY ./install /install

ARG OS_VARIANT CONTAINER_TYPE
RUN \
bash /install/init.sh \
&& bash /install/container.sh \
&& bash /install/integration.sh \
&& bash /install/cleanup.sh

CMD ["bash"]
3 changes: 3 additions & 0 deletions .devcontainer/container/container
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

make --file /opt/container/container.mk "${*:-"help"}"
12 changes: 12 additions & 0 deletions .devcontainer/container/container.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
MAKEFLAGS += --no-print-directory
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
.DEFAULT_GOAL := help

include /opt/container/makefiles/*.mk

help: ## Show help
@printf " \033[1m%s\033[0m\n %s\033[32m\033[0m\n %s\033[32m\033[0m \n\n" "container" "Custom CLI used in this container" "https://github.com/ludeeus/container";
@printf " \033[1m%s\033[0m\n %s\033[32m\033[0m \n\n" "usage:" "container [command]";
@printf " \033[1m%s\033[0m\n" "where [command] is one of:";
@awk 'BEGIN {FS = ":.*##";} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m container %-25s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST);
@echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

hass -c /config --script check_config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

read -p 'Set Home Assistant version: ' -r version
python3 -m pip --disable-pip-version-check install --upgrade homeassistant=="$version"

if [[ -n "$POST_SET_VERSION_HOOK" ]]; then
"$POST_SET_VERSION_HOOK" "$version"
fi
40 changes: 40 additions & 0 deletions .devcontainer/container/helpers/common/homeassistant/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
# shellcheck source=/dev/null

source /opt/container/helpers/common/paths.sh
mkdir -p /config

if test -f ".devcontainer/configuration.yaml"; then
echo "Copy configuration.yaml"
ln -sf "$(workspacePath).devcontainer/configuration.yaml" /config/configuration.yaml || echo ".devcontainer/configuration.yaml are missing"
fi

if test -f ".devcontainer/ui-lovelace.yaml"; then
echo "Copy ui-lovelace.yaml"
ln -sf "$(workspacePath).devcontainer/ui-lovelace.yaml" /config/ui-lovelace.yaml || echo ""
fi

if test -f ".devcontainer/secrets.yaml"; then
echo "Copy secrets.yaml"
ln -sf "$(workspacePath).devcontainer/secrets.yaml" /config/secrets.yaml || echo ""
fi

if test -d "custom_components"; then
echo "Symlink the custom component directory"

if test -d "custom_components"; then
rm -R /config/custom_components
fi

ln -sf "$(workspacePath)custom_components/" /config/custom_components || echo "Could not copy the custom_component" exit 1
elif test -f "__init__.py"; then
echo "Having the component in the root is currently not supported"
fi

echo "Start Home Assistant"
if ! [ -x "$(command -v hass)" ]; then
echo "Home Assistant is not installed, running installation."
python3 -m pip --disable-pip-version-check install --upgrade git+https://github.com/home-assistant/home-assistant.git@dev
fi
hass --script ensure_config -c /config
hass -c /config
9 changes: 9 additions & 0 deletions .devcontainer/container/helpers/common/paths.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

function workspacePath {
if [[ -n "$WORKSPACE_DIRECTORY" ]]; then
echo "${WORKSPACE_DIRECTORY}/"
else
echo "$(find /workspaces -mindepth 1 -maxdepth 1 -type d | tail -1)/"
fi
}
21 changes: 21 additions & 0 deletions .devcontainer/container/helpers/integration/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# shellcheck source=/dev/null

source /opt/container/helpers/common/paths.sh


if test -d "$(GetWorkspaceName).git"; then
echo ".git exsist in $(GetWorkspaceName), existing initializing"
exit 1
fi

echo "Initializing dev env for integration"
rm -R /tmp/init > /dev/null 2>&1

git clone https://github.com/custom-components/integration-blueprint.git /tmp/init

rm -R /tmp/init/.git
rm -R /tmp/init/.devcontainer
cp -a /tmp/init/. "$(GetWorkspaceName)"
cd "$(GetWorkspaceName)" || exit 1
git init
20 changes: 20 additions & 0 deletions .devcontainer/container/makefiles/integration.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
start: ## Start Home Assistant with the integration loaded
@bash /opt/container/helpers/common/homeassistant/start.sh

set-version: ## Set Home Assistant version
@bash /opt/container/helpers/common/homeassistant/set-version.sh

install: ## Install Home Assistant dev in the container
@python3 -m pip --disable-pip-version-check install --upgrade git+https://github.com/home-assistant/home-assistant.git@dev

upgrade: ## Upgrade Home Assistant to latest dev in the container
install

run:
start

check-config: ## Check Home Assistant config
@hass -c /config --script check_config

init: ## Initialize the dev env
@bash /opt/container/helpers/integration/init.sh
10 changes: 10 additions & 0 deletions .devcontainer/install/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

echo -e "\\033[0;34mRunning cleanup script 'cleanup.sh'\\033[0m"

apt-get clean -y
rm -fr /var/lib/apt/lists/*
rm -fr /tmp/* /var/{cache,log}/*

rm -fr /container
rm -fr /install
24 changes: 24 additions & 0 deletions .devcontainer/install/container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash
set -e
echo -e "\\033[0;34mRunning install script 'container.sh'\\033[0m"

export DEBIAN_FRONTEND=noninteractive

apt-get update
apt-get install -y --no-install-recommends \
make

mkdir -p /opt/container/makefiles
mkdir -p /opt/container/helpers
touch /opt/container/makefiles/dummy.mk

cp /container/container.mk /opt/container/container.mk
cp -r /container/helpers/common /opt/container/helpers/common

cp /container/container /usr/bin/container
chmod +x /usr/bin/container

cp /container/makefiles/integration.mk /opt/container/makefiles/integration.mk
cp -r /container/helpers/integration /opt/container/helpers/integration

container help
5 changes: 5 additions & 0 deletions .devcontainer/install/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh

uname -m
printenv

50 changes: 50 additions & 0 deletions .devcontainer/install/integration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash
set -e
echo -e "\\033[0;34mRunning install script 'integration.sh'\\033[0m"

export DEBIAN_FRONTEND=noninteractive

apt-get update
apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
curl \
ffmpeg \
gcc \
git \
jq \
libavcodec-dev \
libavdevice-dev \
libavfilter-dev \
libavformat-dev \
libavutil-dev \
libbz2-dev \
libcap-dev \
libffi-dev \
libjpeg-dev \
liblzma-dev \
libncurses5-dev \
libncursesw5-dev \
libpcap-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libswresample-dev \
libswscale-dev \
llvm \
shellcheck \
tar \
tk-dev \
wget \
xz-utils \
zlib1g-dev


mkdir -p /config/custom_components

python3 -m pip --disable-pip-version-check install --upgrade \
git+https://github.com/home-assistant/home-assistant.git@dev
python3 -m pip --disable-pip-version-check install --upgrade wheel setuptools

# Fix issue https://github.com/home-assistant/core/issues/95192
python3 -m pip --disable-pip-version-check install --upgrade git+https://github.com/boto/botocore urllib3~=1.26
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ body:
- type: textarea
attributes:
label: "Debug logs"
description: "To enable debug logs check this https://www.home-assistant.io/integrations/logger/, this **needs** to include _everything_ from startup of Home Assistant to the point where you encounter the issue."
description: "To enable debug logs check this https://www.home-assistant.io/integrations/logger/, this **needs** to install _everything_ from startup of Home Assistant to the point where you encounter the issue."
render: text
validations:
required: true
Expand Down
3 changes: 2 additions & 1 deletion scripts/devcontainer
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ workdir="/workspaces/${workspace}"

container="dev-${workspace}"
port="127.0.0.1:9123:8123"
image="python:3.11-slim-bookworm"
image="devcontainer"
volume="${ROOT}:${workdir}"

cmd="menu"
Expand Down Expand Up @@ -80,6 +80,7 @@ bootstrap() {

if ! ${docker} ps -a | grep -wq ${container} && [[ "${cmd}" != "down" ]]; then
log.info "Create container..."
${docker} build -t "${image}" "${ROOT}/.devcontainer/"
${docker} create -it --name "${container}" -p "${port}" -v "${volume}" "${image}"

docker_start
Expand Down

0 comments on commit 9f577c5

Please sign in to comment.