From 9f577c515511e04dc8314900173ab713dea1ec26 Mon Sep 17 00:00:00 2001 From: Andrey Khrolenok Date: Wed, 22 Nov 2023 04:14:25 +0300 Subject: [PATCH] Make local hosted devcontainer docker image --- .devcontainer/Dockerfile | 16 ++++++ .devcontainer/container/container | 3 ++ .devcontainer/container/container.mk | 12 +++++ .../common/homeassistant/check-config.sh | 3 ++ .../common/homeassistant/set-version.sh | 8 +++ .../helpers/common/homeassistant/start.sh | 40 +++++++++++++++ .../container/helpers/common/paths.sh | 9 ++++ .../container/helpers/integration/init.sh | 21 ++++++++ .../container/makefiles/integration.mk | 20 ++++++++ .devcontainer/install/cleanup.sh | 10 ++++ .devcontainer/install/container.sh | 24 +++++++++ .devcontainer/install/init.sh | 5 ++ .devcontainer/install/integration.sh | 50 +++++++++++++++++++ .github/ISSUE_TEMPLATE/bug.yml | 2 +- scripts/devcontainer | 3 +- 15 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/container/container create mode 100644 .devcontainer/container/container.mk create mode 100644 .devcontainer/container/helpers/common/homeassistant/check-config.sh create mode 100644 .devcontainer/container/helpers/common/homeassistant/set-version.sh create mode 100644 .devcontainer/container/helpers/common/homeassistant/start.sh create mode 100644 .devcontainer/container/helpers/common/paths.sh create mode 100644 .devcontainer/container/helpers/integration/init.sh create mode 100644 .devcontainer/container/makefiles/integration.mk create mode 100644 .devcontainer/install/cleanup.sh create mode 100644 .devcontainer/install/container.sh create mode 100644 .devcontainer/install/init.sh create mode 100644 .devcontainer/install/integration.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..7a53f74 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -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"] diff --git a/.devcontainer/container/container b/.devcontainer/container/container new file mode 100644 index 0000000..e9c6237 --- /dev/null +++ b/.devcontainer/container/container @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +make --file /opt/container/container.mk "${*:-"help"}" diff --git a/.devcontainer/container/container.mk b/.devcontainer/container/container.mk new file mode 100644 index 0000000..78110c6 --- /dev/null +++ b/.devcontainer/container/container.mk @@ -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 diff --git a/.devcontainer/container/helpers/common/homeassistant/check-config.sh b/.devcontainer/container/helpers/common/homeassistant/check-config.sh new file mode 100644 index 0000000..de91bf3 --- /dev/null +++ b/.devcontainer/container/helpers/common/homeassistant/check-config.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +hass -c /config --script check_config \ No newline at end of file diff --git a/.devcontainer/container/helpers/common/homeassistant/set-version.sh b/.devcontainer/container/helpers/common/homeassistant/set-version.sh new file mode 100644 index 0000000..ade4e9e --- /dev/null +++ b/.devcontainer/container/helpers/common/homeassistant/set-version.sh @@ -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 \ No newline at end of file diff --git a/.devcontainer/container/helpers/common/homeassistant/start.sh b/.devcontainer/container/helpers/common/homeassistant/start.sh new file mode 100644 index 0000000..8770e79 --- /dev/null +++ b/.devcontainer/container/helpers/common/homeassistant/start.sh @@ -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 diff --git a/.devcontainer/container/helpers/common/paths.sh b/.devcontainer/container/helpers/common/paths.sh new file mode 100644 index 0000000..d2ed67d --- /dev/null +++ b/.devcontainer/container/helpers/common/paths.sh @@ -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 +} \ No newline at end of file diff --git a/.devcontainer/container/helpers/integration/init.sh b/.devcontainer/container/helpers/integration/init.sh new file mode 100644 index 0000000..754444e --- /dev/null +++ b/.devcontainer/container/helpers/integration/init.sh @@ -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 \ No newline at end of file diff --git a/.devcontainer/container/makefiles/integration.mk b/.devcontainer/container/makefiles/integration.mk new file mode 100644 index 0000000..87fa68b --- /dev/null +++ b/.devcontainer/container/makefiles/integration.mk @@ -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 \ No newline at end of file diff --git a/.devcontainer/install/cleanup.sh b/.devcontainer/install/cleanup.sh new file mode 100644 index 0000000..f196398 --- /dev/null +++ b/.devcontainer/install/cleanup.sh @@ -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 diff --git a/.devcontainer/install/container.sh b/.devcontainer/install/container.sh new file mode 100644 index 0000000..82122a7 --- /dev/null +++ b/.devcontainer/install/container.sh @@ -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 diff --git a/.devcontainer/install/init.sh b/.devcontainer/install/init.sh new file mode 100644 index 0000000..e0c2704 --- /dev/null +++ b/.devcontainer/install/init.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env sh + +uname -m +printenv + diff --git a/.devcontainer/install/integration.sh b/.devcontainer/install/integration.sh new file mode 100644 index 0000000..d0b86d4 --- /dev/null +++ b/.devcontainer/install/integration.sh @@ -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 diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml index 9c65fef..1de4f0f 100644 --- a/.github/ISSUE_TEMPLATE/bug.yml +++ b/.github/ISSUE_TEMPLATE/bug.yml @@ -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 diff --git a/scripts/devcontainer b/scripts/devcontainer index 35a6db5..b586313 100755 --- a/scripts/devcontainer +++ b/scripts/devcontainer @@ -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" @@ -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