Skip to content

Commit

Permalink
Add bootstrap script ability to devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Mar 22, 2021
1 parent f98ef39 commit 0776957
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .devcontainer/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

ROOT="$( cd "$( dirname "$(readlink -f "$0")" )/.." >/dev/null 2>&1 && pwd )"

GITHUB_TOKEN=$(grep github_token ${ROOT}/secrets.yaml | cut -d' ' -f2)
FILES=$(grep "{GITHUB_TOKEN}" ${ROOT}/requirements.txt | sed "s/{GITHUB_TOKEN}/${GITHUB_TOKEN}/g")

python3 -m pip install --upgrade "${FILES}"
23 changes: 22 additions & 1 deletion bin/devcontainer
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ port="127.0.0.1:9123:8123"
image="ludeeus/container:integration-debian"
volume="${ROOT}:${workdir}"

if [[ "${1}" == "--public" ]]; then
shift
log.info "Attention! The container is creating externally accessible."
port=$(echo "${port}" | sed "s/127.0.0.1/0.0.0.0/")
fi

cmd="${1:-menu}"

docker_start() {
tmp=$(echo "${port}" | cut -d":" -f-2)
tmp=$(${docker} ps | grep "${tmp}" | awk "{print \$NF}")
if [ -n "${tmp}" ] ; then
if test -n "${tmp}" ; then
log.info "Stop container ${tmp}..."
${docker} stop "${tmp}"
fi
Expand All @@ -33,6 +39,13 @@ docker_start() {
${docker} start "${container}"
}

bootstrap() {
if test -f "${ROOT}/.devcontainer/bootstrap.sh"; then
log.info "Execute bootstrap.sh..."
${docker} exec -it -w "${workdir}" "${container}" .devcontainer/bootstrap.sh "$1"
fi
}

if ! ${docker} ps -a | grep -wq ${container} && [[ "${cmd}" != "down" ]]; then
log.info "Create container..."
${docker} create -it --name "${container}" -p "${port}" -v "${volume}" "${image}"
Expand All @@ -41,6 +54,7 @@ if ! ${docker} ps -a | grep -wq ${container} && [[ "${cmd}" != "down" ]]; then

log.info "Initialize container..."
${docker} exec -it -w "${workdir}" "${container}" container install
bootstrap "install"
fi

if [[ "${cmd}" == "menu" ]]; then
Expand Down Expand Up @@ -88,6 +102,9 @@ case "${cmd}" in
log.info "Interactive mode..."
${docker} exec -it "${container}" bash
;;
"bootstrap" )
bootstrap "${cmd}"
;;
* )
if ! ${docker} ps | grep -wq ${container}; then
docker_start
Expand All @@ -97,5 +114,9 @@ case "${cmd}" in
log.info "After Home Assistant initialization you can access to system on http://localhost:9123/"
fi
${docker} exec -it -w "${workdir}" "${container}" container "${cmd}"

if [[ "${cmd}" == "upgrade" ]]; then
bootstrap "${cmd}"
fi
;;
esac
2 changes: 1 addition & 1 deletion tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def test_failed_config_flow(hass: HomeAssistant, error_on_get_data):


# Our config flow also has an options flow, so we must test it as well.
async def test_options_flow(hass):
async def test_options_flow(hass: HomeAssistant):
"""Test an options flow."""
# Create a new MockConfigEntry and add to HASS (we're bypassing config
# flow entirely)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.integration_blueprint import async_setup_entry
Expand All @@ -13,7 +14,7 @@
from .const import MOCK_CONFIG


async def test_switch_services(hass):
async def test_switch_services(hass: HomeAssistant):
"""Test switch services."""
# Create a mock entry so we don't have to go through config flow
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
Expand Down

0 comments on commit 0776957

Please sign in to comment.