From 637377f81d81b1f359663e456c7623a61bc237a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 18 Oct 2021 10:44:28 +0200 Subject: [PATCH] Replace devcontainer (#3175) --- .devcontainer/Dockerfile | 64 --------------- .devcontainer/devcontainer.json | 15 ++-- .vscode/tasks.json | 2 +- scripts/common.sh | 61 --------------- scripts/supervisor.sh | 133 -------------------------------- 5 files changed, 10 insertions(+), 265 deletions(-) delete mode 100644 .devcontainer/Dockerfile delete mode 100644 scripts/common.sh delete mode 100755 scripts/supervisor.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile deleted file mode 100644 index 43cea0e29..000000000 --- a/.devcontainer/Dockerfile +++ /dev/null @@ -1,64 +0,0 @@ -FROM mcr.microsoft.com/vscode/devcontainers/python:0-3.9 - -ENV \ - DEBIAN_FRONTEND=noninteractive \ - VCN_VERSION=0.9.8 - -SHELL ["/bin/bash", "-c"] - -WORKDIR /workspaces - -# Set Docker daemon config -RUN \ - mkdir -p /etc/docker \ - && echo '{"storage-driver": "vfs"}' > /etc/docker/daemon.json - -# Install Node/Yarn for Frontent -RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ - && apt-get update \ - && apt-get update && apt-get install -y --no-install-recommends \ - curl \ - git \ - apt-utils \ - apt-transport-https \ - && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list \ - && apt-get update && apt-get install -y --no-install-recommends \ - nodejs \ - yarn \ - && curl -o - https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash \ - && rm -rf /var/lib/apt/lists/* -ENV NVM_DIR /root/.nvm - -# 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 \ - gpg-agent \ - && 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/* - -# Install tools -RUN apt-get update && apt-get install -y --no-install-recommends \ - jq \ - dbus \ - network-manager \ - apparmor-utils \ - libpulse0 \ - && curl -Lo /bin/vcn https://github.com/codenotary/vcn/releases/download/${VCN_VERSION}/vcn-${VCN_VERSION}-linux-amd64-static \ - && chmod a+x /bin/vcn \ - && rm -rf /var/lib/apt/lists/* - -# Install Python dependencies from requirements.txt if it exists -COPY requirements.txt requirements_tests.txt ./ -RUN pip3 install -U setuptools pip \ - && pip3 install -r requirements.txt -r requirements_tests.txt \ - && pip3 install tox \ - && rm -f requirements.txt requirements_tests.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index b5629a19e..93a605aca 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,11 +1,9 @@ { "name": "Supervisor dev", - "context": "..", - "dockerFile": "Dockerfile", - "appPort": "9123:8123", - "postCreateCommand": "pre-commit install", + "image": "ghcr.io/home-assistant/devcontainer:supervisor", + "appPort": ["9123:8123", "7357:4357"], + "postCreateCommand": "bash devcontainer_bootstrap", "runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"], - "containerEnv": { "NVM_DIR": "/usr/local/share/nvm" }, "extensions": [ "ms-python.python", "ms-python.vscode-pylance", @@ -13,7 +11,12 @@ "esbenp.prettier-vscode" ], "settings": { - "terminal.integrated.shell.linux": "/bin/bash", + "terminal.integrated.profiles.linux": { + "zsh": { + "path": "/usr/bin/zsh" + } + }, + "terminal.integrated.defaultProfile.linux": "zsh", "editor.formatOnPaste": false, "editor.formatOnSave": true, "editor.formatOnType": true, diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 0e5e67486..a0994d4d7 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -4,7 +4,7 @@ { "label": "Run Supervisor", "type": "shell", - "command": "./scripts/supervisor.sh", + "command": "supervisor_run", "group": { "kind": "test", "isDefault": true diff --git a/scripts/common.sh b/scripts/common.sh deleted file mode 100644 index e123a4c5f..000000000 --- a/scripts/common.sh +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/bash - -function start_docker() { - local starttime - local endtime - - update-alternatives --set iptables /usr/sbin/iptables-legacy || echo "Fails adjust iptables" - update-alternatives --set ip6tables /usr/sbin/iptables-legacy || echo "Fails adjust ip6tables" - - echo "Starting docker." - dockerd 2> /dev/null & - DOCKER_PID=$! - - echo "Waiting for docker to initialize..." - starttime="$(date +%s)" - endtime="$(date +%s)" - until docker info >/dev/null 2>&1; do - if [[ $((endtime - starttime)) -le $DOCKER_TIMEOUT ]]; then - sleep 1 - endtime=$(date +%s) - else - echo "Timeout while waiting for docker to come up" - exit 1 - fi - done - echo "Docker was initialized" -} - -function stop_docker() { - local starttime - local endtime - - echo "Stopping in container docker..." - if [ "$DOCKER_PID" -gt 0 ] && kill -0 "$DOCKER_PID" 2> /dev/null; then - starttime="$(date +%s)" - endtime="$(date +%s)" - - # Now wait for it to die - kill "$DOCKER_PID" - while kill -0 "$DOCKER_PID" 2> /dev/null; do - if [[ $((endtime - starttime)) -le $DOCKER_TIMEOUT ]]; then - sleep 1 - endtime=$(date +%s) - else - echo "Timeout while waiting for container docker to die" - exit 1 - fi - done - else - echo "Your host might have been left with unreleased resources" - fi -} - -function cleanup_lastboot() { - if [[ -f /workspaces/test_supervisor/config.json ]]; then - echo "Cleaning up last boot" - cp /workspaces/test_supervisor/config.json /tmp/config.json - jq -rM 'del(.last_boot)' /tmp/config.json > /workspaces/test_supervisor/config.json - rm /tmp/config.json - fi -} diff --git a/scripts/supervisor.sh b/scripts/supervisor.sh deleted file mode 100755 index be52fd740..000000000 --- a/scripts/supervisor.sh +++ /dev/null @@ -1,133 +0,0 @@ -#!/bin/bash -source "${BASH_SOURCE[0]%/*}/common.sh" - -set -eE - -DOCKER_TIMEOUT=30 -DOCKER_PID=0 - -function build_supervisor() { - docker pull homeassistant/amd64-builder:dev - - docker run --rm \ - --privileged \ - -v /run/docker.sock:/run/docker.sock \ - -v "$(pwd):/data" \ - homeassistant/amd64-builder:dev \ - --generic latest \ - --target /data \ - --test \ - --amd64 \ - --no-cache -} - - -function cleanup_docker() { - echo "Cleaning up stopped containers..." - docker rm $(docker ps -a -q) || true -} - - -function run_supervisor() { - mkdir -p /workspaces/test_supervisor - - echo "Start Supervisor" - docker run --rm --privileged \ - --name hassio_supervisor \ - --privileged \ - --security-opt seccomp=unconfined \ - --security-opt apparmor=unconfined \ - -v /run/docker.sock:/run/docker.sock:rw \ - -v /run/dbus:/run/dbus:ro \ - -v /run/udev:/run/udev:ro \ - -v "/workspaces/test_supervisor":/data:rw \ - -v /etc/machine-id:/etc/machine-id:ro \ - -v /workspaces/supervisor:/usr/src/supervisor \ - -e SUPERVISOR_SHARE="/workspaces/test_supervisor" \ - -e SUPERVISOR_NAME=hassio_supervisor \ - -e SUPERVISOR_DEV=1 \ - -e SUPERVISOR_MACHINE="qemux86-64" \ - homeassistant/amd64-hassio-supervisor:latest - -} - - -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 -} - - -function init_udev() { - if pgrep systemd-udevd; then - echo "udev is running" - return 0 - fi - - echo "Startup udev" - - # cleanups - mkdir -p /run/udev - - # run - /lib/systemd/systemd-udevd --daemon - sleep 3 - udevadm trigger && udevadm settle -} - -function init_os-agent() { - if pgrep os-agent; then - echo "os-agent is running" - return 0 - fi - - if [ ! -f /usr/sbin/os-agent ]; then - curl -Lo /usr/sbin/os-agent https://github.com/home-assistant/os-agent/releases/latest/download/os-agent-debian-amd64.bin - curl -Lo /etc/dbus-1/system.d/io.hass.conf https://raw.githubusercontent.com/home-assistant/os-agent/main/contrib/io.hass.conf - chmod a+x /usr/sbin/os-agent - fi - - /usr/sbin/os-agent & -} - -echo "Run Supervisor" - -start_docker -trap "stop_docker" ERR - - -if [ "$( docker container inspect -f '{{.State.Status}}' hassio_supervisor )" == "running" ]; then - echo "Restarting Supervisor" - docker rm -f hassio_supervisor - init_dbus - init_udev - init_os-agent - cleanup_lastboot - run_supervisor - stop_docker - -else - echo "Starting Supervisor" - docker system prune -f - build_supervisor - cleanup_lastboot - cleanup_docker - init_dbus - init_udev - init_os-agent - run_supervisor - stop_docker -fi