mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-08-31 11:50:21 +00:00
Compare commits
146 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a0c813bfc1 | ||
![]() |
5f7b3a7087 | ||
![]() |
6426f02a2c | ||
![]() |
7fef92c480 | ||
![]() |
c64744dedf | ||
![]() |
72a2088931 | ||
![]() |
db54556b0f | ||
![]() |
a2653d8462 | ||
![]() |
ef778238f6 | ||
![]() |
4cc0ddc35d | ||
![]() |
a0429179a0 | ||
![]() |
5cfb45c668 | ||
![]() |
a53b7041f5 | ||
![]() |
f534fae293 | ||
![]() |
f7cbd968d2 | ||
![]() |
844d76290c | ||
![]() |
8c8122eee0 | ||
![]() |
d63f0d5e0b | ||
![]() |
96f4ba5d25 | ||
![]() |
72e64676da | ||
![]() |
883e54f989 | ||
![]() |
c2d4be3304 | ||
![]() |
de737ddb91 | ||
![]() |
11ec6dd9ac | ||
![]() |
df7541e397 | ||
![]() |
95ac53d780 | ||
![]() |
e8c4b32a65 | ||
![]() |
eca535c978 | ||
![]() |
9088810b49 | ||
![]() |
172a7053ed | ||
![]() |
3d5bd2adef | ||
![]() |
cb03d039f4 | ||
![]() |
bb31b1bc6e | ||
![]() |
727532858e | ||
![]() |
c0868d9dac | ||
![]() |
ce26e1dac6 | ||
![]() |
c74f87ca12 | ||
![]() |
043111b91c | ||
![]() |
5c579e557c | ||
![]() |
f8f51740c1 | ||
![]() |
176b63df52 | ||
![]() |
e1979357a5 | ||
![]() |
030527a4f2 | ||
![]() |
cca74da1f3 | ||
![]() |
928aff342f | ||
![]() |
60a97235df | ||
![]() |
c77779cf9d | ||
![]() |
9351796ba8 | ||
![]() |
bef0f023d4 | ||
![]() |
3116f183f5 | ||
![]() |
16b71a22d1 | ||
![]() |
5f4581042c | ||
![]() |
6976a4cf2e | ||
![]() |
68d86b3b7b | ||
![]() |
d7d34d36c8 | ||
![]() |
68da328cc5 | ||
![]() |
78870186d7 | ||
![]() |
d634273b48 | ||
![]() |
2d970eee02 | ||
![]() |
1f0ea3c6f7 | ||
![]() |
d736913f7f | ||
![]() |
3e95a9d282 | ||
![]() |
7cd7259992 | ||
![]() |
87385cf28e | ||
![]() |
3a00c94325 | ||
![]() |
38d5d2307f | ||
![]() |
a0c12e7228 | ||
![]() |
b6625ad909 | ||
![]() |
6f01341055 | ||
![]() |
6762a4153a | ||
![]() |
31200df89f | ||
![]() |
18e422ca77 | ||
![]() |
1b362716e3 | ||
![]() |
1e49129197 | ||
![]() |
a8f818fca5 | ||
![]() |
0f600da096 | ||
![]() |
b04efe4eac | ||
![]() |
7361d39231 | ||
![]() |
059c0df16c | ||
![]() |
6f6b849335 | ||
![]() |
a390500309 | ||
![]() |
7c576da32c | ||
![]() |
6d021c1659 | ||
![]() |
37c1c89d44 | ||
![]() |
010043f116 | ||
![]() |
b1010c3c61 | ||
![]() |
7f0204bfc3 | ||
![]() |
a508cc5efd | ||
![]() |
65c90696d5 | ||
![]() |
b9f47898d6 | ||
![]() |
26f554e46a | ||
![]() |
b57889c84f | ||
![]() |
77fd1b4017 | ||
![]() |
ab6745bc99 | ||
![]() |
a5ea3cae72 | ||
![]() |
8bcd1b4efd | ||
![]() |
a24657e565 | ||
![]() |
b7721420fa | ||
![]() |
6c564fe4fd | ||
![]() |
012bfd7e6c | ||
![]() |
a70f81aa01 | ||
![]() |
1376a38de5 | ||
![]() |
1827ecda65 | ||
![]() |
994c981228 | ||
![]() |
5bbfbf44ae | ||
![]() |
ace58ba735 | ||
![]() |
f9840306a0 | ||
![]() |
322b3bbb4e | ||
![]() |
501318f468 | ||
![]() |
0234f38b23 | ||
![]() |
8743e0072f | ||
![]() |
a79e06afa7 | ||
![]() |
682b8e0535 | ||
![]() |
d70aa5f9a9 | ||
![]() |
1c815dcad1 | ||
![]() |
afa467a32b | ||
![]() |
274218d48e | ||
![]() |
7e73df26ab | ||
![]() |
ef8fc80c95 | ||
![]() |
05c39144e3 | ||
![]() |
f5cd35af47 | ||
![]() |
c69ecdafd0 | ||
![]() |
fa90c247ec | ||
![]() |
0cd7bd47bb | ||
![]() |
36d48d19fc | ||
![]() |
9322b68d47 | ||
![]() |
e11ff64b15 | ||
![]() |
3776dabfcf | ||
![]() |
d4e5831f0f | ||
![]() |
7b3b478e88 | ||
![]() |
f5afe13e91 | ||
![]() |
49ce468d83 | ||
![]() |
b26551c812 | ||
![]() |
394ba580d2 | ||
![]() |
2f7a54f5fd | ||
![]() |
360e085926 | ||
![]() |
042921925d | ||
![]() |
dcf024387b | ||
![]() |
e1232bc9e7 | ||
![]() |
d96598b5dd | ||
![]() |
2605f85668 | ||
![]() |
2c8e6ca0cd | ||
![]() |
0225f574be | ||
![]() |
34090bf2eb | ||
![]() |
5ae585ce13 | ||
![]() |
2bb10a32d7 |
@@ -7,34 +7,32 @@
|
||||
"appPort": ["9123:8123", "7357:4357"],
|
||||
"postCreateCommand": "bash devcontainer_bootstrap",
|
||||
"runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"],
|
||||
"extensions": [
|
||||
"ms-python.python",
|
||||
"ms-python.vscode-pylance",
|
||||
"visualstudioexptteam.vscodeintellicode",
|
||||
"esbenp.prettier-vscode"
|
||||
],
|
||||
"mounts": ["type=volume,target=/var/lib/docker"],
|
||||
"settings": {
|
||||
"terminal.integrated.profiles.linux": {
|
||||
"zsh": {
|
||||
"path": "/usr/bin/zsh"
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": [
|
||||
"ms-python.python",
|
||||
"ms-python.pylint",
|
||||
"ms-python.vscode-pylance",
|
||||
"visualstudioexptteam.vscodeintellicode",
|
||||
"esbenp.prettier-vscode"
|
||||
],
|
||||
"settings": {
|
||||
"terminal.integrated.profiles.linux": {
|
||||
"zsh": {
|
||||
"path": "/usr/bin/zsh"
|
||||
}
|
||||
},
|
||||
"terminal.integrated.defaultProfile.linux": "zsh",
|
||||
"editor.formatOnPaste": false,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnType": true,
|
||||
"files.trimTrailingWhitespace": true,
|
||||
"python.pythonPath": "/usr/local/bin/python3",
|
||||
"python.formatting.provider": "black",
|
||||
"python.formatting.blackArgs": ["--target-version", "py311"],
|
||||
"python.formatting.blackPath": "/usr/local/bin/black"
|
||||
}
|
||||
},
|
||||
"terminal.integrated.defaultProfile.linux": "zsh",
|
||||
"editor.formatOnPaste": false,
|
||||
"editor.formatOnSave": true,
|
||||
"editor.formatOnType": true,
|
||||
"files.trimTrailingWhitespace": true,
|
||||
"python.pythonPath": "/usr/local/bin/python3",
|
||||
"python.linting.pylintEnabled": true,
|
||||
"python.linting.enabled": true,
|
||||
"python.formatting.provider": "black",
|
||||
"python.formatting.blackArgs": ["--target-version", "py310"],
|
||||
"python.formatting.blackPath": "/usr/local/bin/black",
|
||||
"python.linting.banditPath": "/usr/local/bin/bandit",
|
||||
"python.linting.flake8Path": "/usr/local/bin/flake8",
|
||||
"python.linting.mypyPath": "/usr/local/bin/mypy",
|
||||
"python.linting.pylintPath": "/usr/local/bin/pylint",
|
||||
"python.linting.pydocstylePath": "/usr/local/bin/pydocstyle"
|
||||
}
|
||||
}
|
||||
},
|
||||
"mounts": ["type=volume,target=/var/lib/docker"]
|
||||
}
|
||||
|
28
.github/workflows/builder.yml
vendored
28
.github/workflows/builder.yml
vendored
@@ -53,7 +53,7 @@ jobs:
|
||||
requirements: ${{ steps.requirements.outputs.changed }}
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -70,13 +70,13 @@ jobs:
|
||||
- name: Get changed files
|
||||
id: changed_files
|
||||
if: steps.version.outputs.publish == 'false'
|
||||
uses: jitterbit/get-changed-files@v1
|
||||
uses: masesgroup/retrieve-changed-files@v3.0.0
|
||||
|
||||
- name: Check if requirements files changed
|
||||
id: requirements
|
||||
run: |
|
||||
if [[ "${{ steps.changed_files.outputs.all }}" =~ (requirements.txt|build.json) ]]; then
|
||||
echo "::set-output name=changed::true"
|
||||
echo "changed=true" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
|
||||
build:
|
||||
@@ -92,7 +92,7 @@ jobs:
|
||||
arch: ${{ fromJson(needs.init.outputs.architectures) }}
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -106,13 +106,13 @@ jobs:
|
||||
|
||||
- name: Build wheels
|
||||
if: needs.init.outputs.requirements == 'true'
|
||||
uses: home-assistant/wheels@2023.04.0
|
||||
uses: home-assistant/wheels@2023.10.5
|
||||
with:
|
||||
abi: cp311
|
||||
tag: musllinux_1_2
|
||||
arch: ${{ matrix.arch }}
|
||||
wheels-key: ${{ secrets.WHEELS_KEY }}
|
||||
apk: "libffi-dev;openssl-dev"
|
||||
apk: "libffi-dev;openssl-dev;yaml-dev"
|
||||
skip-binary: aiohttp
|
||||
env-file: true
|
||||
requirements: "requirements.txt"
|
||||
@@ -125,13 +125,13 @@ jobs:
|
||||
|
||||
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
|
||||
if: needs.init.outputs.publish == 'true'
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
with:
|
||||
python-version: ${{ env.DEFAULT_PYTHON }}
|
||||
|
||||
- name: Install Cosign
|
||||
if: needs.init.outputs.publish == 'true'
|
||||
uses: sigstore/cosign-installer@v3.1.2
|
||||
uses: sigstore/cosign-installer@v3.3.0
|
||||
with:
|
||||
cosign-release: "v2.0.2"
|
||||
|
||||
@@ -149,7 +149,7 @@ jobs:
|
||||
|
||||
- name: Login to GitHub Container Registry
|
||||
if: needs.init.outputs.publish == 'true'
|
||||
uses: docker/login-action@v2.2.0
|
||||
uses: docker/login-action@v3.0.0
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
@@ -160,7 +160,7 @@ jobs:
|
||||
run: echo "BUILD_ARGS=--test" >> $GITHUB_ENV
|
||||
|
||||
- name: Build supervisor
|
||||
uses: home-assistant/builder@2023.08.0
|
||||
uses: home-assistant/builder@2023.12.0
|
||||
with:
|
||||
args: |
|
||||
$BUILD_ARGS \
|
||||
@@ -178,7 +178,7 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
if: needs.init.outputs.publish == 'true'
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
|
||||
- name: Initialize git
|
||||
if: needs.init.outputs.publish == 'true'
|
||||
@@ -203,11 +203,11 @@ jobs:
|
||||
timeout-minutes: 60
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
|
||||
- name: Build the Supervisor
|
||||
if: needs.init.outputs.publish != 'true'
|
||||
uses: home-assistant/builder@2023.08.0
|
||||
uses: home-assistant/builder@2023.12.0
|
||||
with:
|
||||
args: |
|
||||
--test \
|
||||
@@ -324,7 +324,7 @@ jobs:
|
||||
if [ "$(echo $test | jq -r '.result')" != "ok" ]; then
|
||||
exit 1
|
||||
fi
|
||||
echo "::set-output name=slug::$(echo $test | jq -r '.data.slug')"
|
||||
echo "slug=$(echo $test | jq -r '.data.slug')" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Uninstall SSH add-on
|
||||
run: |
|
||||
|
59
.github/workflows/ci.yaml
vendored
59
.github/workflows/ci.yaml
vendored
@@ -9,7 +9,7 @@ on:
|
||||
|
||||
env:
|
||||
DEFAULT_PYTHON: "3.11"
|
||||
PRE_COMMIT_HOME: ~/.cache/pre-commit
|
||||
PRE_COMMIT_CACHE: ~/.cache/pre-commit
|
||||
|
||||
concurrency:
|
||||
group: "${{ github.workflow }}-${{ github.ref }}"
|
||||
@@ -25,10 +25,10 @@ jobs:
|
||||
name: Prepare Python dependencies
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python
|
||||
id: python
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
with:
|
||||
python-version: ${{ env.DEFAULT_PYTHON }}
|
||||
- name: Restore Python virtual environment
|
||||
@@ -49,7 +49,8 @@ jobs:
|
||||
id: cache-precommit
|
||||
uses: actions/cache@v3.3.2
|
||||
with:
|
||||
path: ${{ env.PRE_COMMIT_HOME }}
|
||||
path: ${{ env.PRE_COMMIT_CACHE }}
|
||||
lookup-only: true
|
||||
key: |
|
||||
${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
||||
restore-keys: |
|
||||
@@ -66,9 +67,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -87,7 +88,7 @@ jobs:
|
||||
- name: Run black
|
||||
run: |
|
||||
. venv/bin/activate
|
||||
black --target-version py38 --check supervisor tests setup.py
|
||||
black --target-version py311 --check supervisor tests setup.py
|
||||
|
||||
lint-dockerfile:
|
||||
name: Check Dockerfile
|
||||
@@ -95,7 +96,7 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Register hadolint problem matcher
|
||||
run: |
|
||||
echo "::add-matcher::.github/workflows/matchers/hadolint.json"
|
||||
@@ -110,9 +111,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -132,7 +133,7 @@ jobs:
|
||||
id: cache-precommit
|
||||
uses: actions/cache@v3.3.2
|
||||
with:
|
||||
path: ${{ env.PRE_COMMIT_HOME }}
|
||||
path: ${{ env.PRE_COMMIT_CACHE }}
|
||||
key: |
|
||||
${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
||||
- name: Fail job if cache restore failed
|
||||
@@ -154,9 +155,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -186,9 +187,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -208,7 +209,7 @@ jobs:
|
||||
id: cache-precommit
|
||||
uses: actions/cache@v3.3.2
|
||||
with:
|
||||
path: ${{ env.PRE_COMMIT_HOME }}
|
||||
path: ${{ env.PRE_COMMIT_CACHE }}
|
||||
key: |
|
||||
${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
||||
- name: Fail job if cache restore failed
|
||||
@@ -227,9 +228,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -249,7 +250,7 @@ jobs:
|
||||
id: cache-precommit
|
||||
uses: actions/cache@v3.3.2
|
||||
with:
|
||||
path: ${{ env.PRE_COMMIT_HOME }}
|
||||
path: ${{ env.PRE_COMMIT_CACHE }}
|
||||
key: |
|
||||
${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
||||
- name: Fail job if cache restore failed
|
||||
@@ -271,9 +272,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -303,9 +304,9 @@ jobs:
|
||||
needs: prepare
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
@@ -325,7 +326,7 @@ jobs:
|
||||
id: cache-precommit
|
||||
uses: actions/cache@v3.3.2
|
||||
with:
|
||||
path: ${{ env.PRE_COMMIT_HOME }}
|
||||
path: ${{ env.PRE_COMMIT_CACHE }}
|
||||
key: |
|
||||
${{ runner.os }}-pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}
|
||||
- name: Fail job if cache restore failed
|
||||
@@ -344,14 +345,14 @@ jobs:
|
||||
name: Run tests Python ${{ needs.prepare.outputs.python-version }}
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
- name: Install Cosign
|
||||
uses: sigstore/cosign-installer@v3.1.2
|
||||
uses: sigstore/cosign-installer@v3.3.0
|
||||
with:
|
||||
cosign-release: "v2.0.2"
|
||||
- name: Restore Python virtual environment
|
||||
@@ -402,9 +403,9 @@ jobs:
|
||||
needs: ["pytest", "prepare"]
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Set up Python ${{ needs.prepare.outputs.python-version }}
|
||||
uses: actions/setup-python@v4.7.0
|
||||
uses: actions/setup-python@v5.0.0
|
||||
id: python
|
||||
with:
|
||||
python-version: ${{ needs.prepare.outputs.python-version }}
|
||||
|
2
.github/workflows/lock.yml
vendored
2
.github/workflows/lock.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
lock:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: dessant/lock-threads@v4.0.1
|
||||
- uses: dessant/lock-threads@v5.0.1
|
||||
with:
|
||||
github-token: ${{ github.token }}
|
||||
issue-inactive-days: "30"
|
||||
|
6
.github/workflows/release-drafter.yml
vendored
6
.github/workflows/release-drafter.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
||||
name: Release Drafter
|
||||
steps:
|
||||
- name: Checkout the repository
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -33,10 +33,10 @@ jobs:
|
||||
|
||||
echo Current version: $latest
|
||||
echo New target version: $datepre.$newpost
|
||||
echo "::set-output name=version::$datepre.$newpost"
|
||||
echo "version=$datepre.$newpost" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Run Release Drafter
|
||||
uses: release-drafter/release-drafter@v5.24.0
|
||||
uses: release-drafter/release-drafter@v5.25.0
|
||||
with:
|
||||
tag: ${{ steps.version.outputs.version }}
|
||||
name: ${{ steps.version.outputs.version }}
|
||||
|
4
.github/workflows/sentry.yaml
vendored
4
.github/workflows/sentry.yaml
vendored
@@ -10,9 +10,9 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.0.0
|
||||
uses: actions/checkout@v4.1.1
|
||||
- name: Sentry Release
|
||||
uses: getsentry/action-release@v1.4.1
|
||||
uses: getsentry/action-release@v1.6.0
|
||||
env:
|
||||
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
|
||||
SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
|
||||
|
2
.github/workflows/stale.yml
vendored
2
.github/workflows/stale.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
stale:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v8.0.0
|
||||
- uses: actions/stale@v9.0.0
|
||||
with:
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
days-before-stale: 30
|
||||
|
@@ -3,4 +3,5 @@ ignored:
|
||||
- DL3006
|
||||
- DL3013
|
||||
- DL3018
|
||||
- DL3042
|
||||
- SC2155
|
||||
|
@@ -7,7 +7,7 @@ repos:
|
||||
- --safe
|
||||
- --quiet
|
||||
- --target-version
|
||||
- py310
|
||||
- py311
|
||||
files: ^((supervisor|tests)/.+)?[^/]+\.py$
|
||||
- repo: https://github.com/PyCQA/flake8
|
||||
rev: 6.0.0
|
||||
@@ -28,7 +28,7 @@ repos:
|
||||
hooks:
|
||||
- id: isort
|
||||
- repo: https://github.com/asottile/pyupgrade
|
||||
rev: v3.4.0
|
||||
rev: v3.15.0
|
||||
hooks:
|
||||
- id: pyupgrade
|
||||
args: [--py310-plus]
|
||||
args: [--py311-plus]
|
||||
|
@@ -15,6 +15,7 @@ WORKDIR /usr/src
|
||||
RUN \
|
||||
set -x \
|
||||
&& apk add --no-cache \
|
||||
findutils \
|
||||
eudev \
|
||||
eudev-libs \
|
||||
git \
|
||||
@@ -22,6 +23,7 @@ RUN \
|
||||
libpulse \
|
||||
musl \
|
||||
openssl \
|
||||
yaml \
|
||||
\
|
||||
&& curl -Lso /usr/bin/cosign "https://github.com/home-assistant/cosign/releases/download/${COSIGN_VERSION}/cosign_${BUILD_ARCH}" \
|
||||
&& chmod a+x /usr/bin/cosign
|
||||
@@ -30,15 +32,14 @@ RUN \
|
||||
COPY requirements.txt .
|
||||
RUN \
|
||||
export MAKEFLAGS="-j$(nproc)" \
|
||||
&& pip3 install --no-cache-dir --no-index --only-binary=:all: --find-links \
|
||||
"https://wheels.home-assistant.io/musllinux/" \
|
||||
&& pip3 install --only-binary=:all: \
|
||||
-r ./requirements.txt \
|
||||
&& rm -f requirements.txt
|
||||
|
||||
# Install Home Assistant Supervisor
|
||||
COPY . supervisor
|
||||
RUN \
|
||||
pip3 install --no-cache-dir -e ./supervisor \
|
||||
pip3 install -e ./supervisor \
|
||||
&& python3 -m compileall ./supervisor/supervisor
|
||||
|
||||
|
||||
|
10
build.yaml
10
build.yaml
@@ -1,10 +1,10 @@
|
||||
image: ghcr.io/home-assistant/{arch}-hassio-supervisor
|
||||
build_from:
|
||||
aarch64: ghcr.io/home-assistant/aarch64-base-python:3.11-alpine3.16
|
||||
armhf: ghcr.io/home-assistant/armhf-base-python:3.11-alpine3.16
|
||||
armv7: ghcr.io/home-assistant/armv7-base-python:3.11-alpine3.16
|
||||
amd64: ghcr.io/home-assistant/amd64-base-python:3.11-alpine3.16
|
||||
i386: ghcr.io/home-assistant/i386-base-python:3.11-alpine3.16
|
||||
aarch64: ghcr.io/home-assistant/aarch64-base-python:3.11-alpine3.18
|
||||
armhf: ghcr.io/home-assistant/armhf-base-python:3.11-alpine3.18
|
||||
armv7: ghcr.io/home-assistant/armv7-base-python:3.11-alpine3.18
|
||||
amd64: ghcr.io/home-assistant/amd64-base-python:3.11-alpine3.18
|
||||
i386: ghcr.io/home-assistant/i386-base-python:3.11-alpine3.18
|
||||
codenotary:
|
||||
signer: notary@home-assistant.io
|
||||
base_image: notary@home-assistant.io
|
||||
|
@@ -1,2 +1,6 @@
|
||||
[pytest]
|
||||
asyncio_mode = auto
|
||||
filterwarnings =
|
||||
error
|
||||
ignore:pkg_resources is deprecated as an API:DeprecationWarning:dirhash
|
||||
ignore::pytest.PytestUnraisableExceptionWarning
|
||||
|
@@ -1,26 +1,27 @@
|
||||
aiodns==3.0.0
|
||||
aiohttp==3.8.5
|
||||
aiodns==3.1.1
|
||||
aiohttp==3.9.1
|
||||
aiohttp-fast-url-dispatcher==0.3.0
|
||||
async_timeout==4.0.3
|
||||
atomicwrites-homeassistant==1.4.1
|
||||
attrs==23.1.0
|
||||
awesomeversion==23.8.0
|
||||
awesomeversion==23.11.0
|
||||
brotli==1.1.0
|
||||
ciso8601==2.3.0
|
||||
colorlog==6.7.0
|
||||
ciso8601==2.3.1
|
||||
colorlog==6.8.0
|
||||
cpe==1.2.1
|
||||
cryptography==41.0.3
|
||||
debugpy==1.7.0
|
||||
cryptography==41.0.7
|
||||
debugpy==1.8.0
|
||||
deepmerge==1.1.0
|
||||
dirhash==0.2.1
|
||||
docker==6.1.3
|
||||
docker==7.0.0
|
||||
faust-cchardet==2.1.19
|
||||
gitpython==3.1.35
|
||||
gitpython==3.1.40
|
||||
jinja2==3.1.2
|
||||
pulsectl==23.5.2
|
||||
pyudev==0.24.1
|
||||
ruamel.yaml==0.17.21
|
||||
securetar==2023.3.0
|
||||
sentry-sdk==1.30.0
|
||||
voluptuous==0.13.1
|
||||
dbus-fast==2.2.0
|
||||
typing_extensions==4.7.1
|
||||
PyYAML==6.0.1
|
||||
securetar==2023.12.0
|
||||
sentry-sdk==1.39.0
|
||||
voluptuous==0.14.1
|
||||
dbus-fast==2.21.0
|
||||
typing_extensions==4.9.0
|
||||
|
@@ -1,16 +1,16 @@
|
||||
black==23.9.1
|
||||
coverage==7.3.1
|
||||
black==23.12.0
|
||||
coverage==7.3.2
|
||||
flake8-docstrings==1.7.0
|
||||
flake8==6.1.0
|
||||
pre-commit==3.4.0
|
||||
pre-commit==3.6.0
|
||||
pydocstyle==6.3.0
|
||||
pylint==2.17.5
|
||||
pylint==3.0.3
|
||||
pytest-aiohttp==1.0.5
|
||||
pytest-asyncio==0.18.3
|
||||
pytest-cov==4.1.0
|
||||
pytest-timeout==2.1.0
|
||||
pytest==7.4.2
|
||||
pyupgrade==3.10.1
|
||||
time-machine==2.12.0
|
||||
typing_extensions==4.7.1
|
||||
urllib3==2.0.4
|
||||
pytest-timeout==2.2.0
|
||||
pytest==7.4.3
|
||||
pyupgrade==3.15.0
|
||||
time-machine==2.13.0
|
||||
typing_extensions==4.9.0
|
||||
urllib3==2.1.0
|
||||
|
@@ -15,7 +15,7 @@ do
|
||||
if [[ "${supervisor_state}" = "running" ]]; then
|
||||
|
||||
# Check API
|
||||
if bashio::supervisor.ping; then
|
||||
if bashio::supervisor.ping > /dev/null; then
|
||||
failed_count=0
|
||||
else
|
||||
bashio::log.warning "Maybe found an issue on API healthy"
|
||||
|
12
setup.py
12
setup.py
@@ -15,18 +15,6 @@ setup(
|
||||
"A maintainless private cloud operator system that"
|
||||
"setup a Home-Assistant instance. Based on HassOS"
|
||||
),
|
||||
classifiers=[
|
||||
"Intended Audience :: End Users/Desktop",
|
||||
"Intended Audience :: Developers",
|
||||
"License :: OSI Approved :: Apache Software License",
|
||||
"Operating System :: OS Independent",
|
||||
"Topic :: Home Automation",
|
||||
"Topic :: Software Development :: Libraries :: Python Modules",
|
||||
"Topic :: Scientific/Engineering :: Atmospheric Science",
|
||||
"Development Status :: 5 - Production/Stable",
|
||||
"Intended Audience :: Developers",
|
||||
"Programming Language :: Python :: 3.8",
|
||||
],
|
||||
keywords=["docker", "home-assistant", "api"],
|
||||
zip_safe=False,
|
||||
platforms="any",
|
||||
|
@@ -6,6 +6,7 @@ from pathlib import Path
|
||||
import sys
|
||||
|
||||
from supervisor import bootstrap
|
||||
from supervisor.utils.logging import activate_log_queue_handler
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,6 +39,8 @@ if __name__ == "__main__":
|
||||
executor = ThreadPoolExecutor(thread_name_prefix="SyncWorker")
|
||||
loop.set_default_executor(executor)
|
||||
|
||||
activate_log_queue_handler()
|
||||
|
||||
_LOGGER.info("Initializing Supervisor setup")
|
||||
coresys = loop.run_until_complete(bootstrap.initialize_coresys())
|
||||
loop.set_debug(coresys.config.debug)
|
||||
|
@@ -1,477 +1 @@
|
||||
"""Init file for Supervisor add-ons."""
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
from contextlib import suppress
|
||||
import logging
|
||||
import tarfile
|
||||
from typing import Union
|
||||
|
||||
from ..const import AddonBoot, AddonStartup, AddonState
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
from ..exceptions import (
|
||||
AddonConfigurationError,
|
||||
AddonsError,
|
||||
AddonsJobError,
|
||||
AddonsNotSupportedError,
|
||||
CoreDNSError,
|
||||
DockerAPIError,
|
||||
DockerError,
|
||||
DockerNotFound,
|
||||
HomeAssistantAPIError,
|
||||
HostAppArmorError,
|
||||
)
|
||||
from ..jobs.decorator import Job, JobCondition
|
||||
from ..resolution.const import ContextType, IssueType, SuggestionType
|
||||
from ..store.addon import AddonStore
|
||||
from ..utils import check_exception_chain
|
||||
from ..utils.sentry import capture_exception
|
||||
from .addon import Addon
|
||||
from .const import ADDON_UPDATE_CONDITIONS
|
||||
from .data import AddonsData
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
AnyAddon = Union[Addon, AddonStore]
|
||||
|
||||
|
||||
class AddonManager(CoreSysAttributes):
|
||||
"""Manage add-ons inside Supervisor."""
|
||||
|
||||
def __init__(self, coresys: CoreSys):
|
||||
"""Initialize Docker base wrapper."""
|
||||
self.coresys: CoreSys = coresys
|
||||
self.data: AddonsData = AddonsData(coresys)
|
||||
self.local: dict[str, Addon] = {}
|
||||
self.store: dict[str, AddonStore] = {}
|
||||
|
||||
@property
|
||||
def all(self) -> list[AnyAddon]:
|
||||
"""Return a list of all add-ons."""
|
||||
addons: dict[str, AnyAddon] = {**self.store, **self.local}
|
||||
return list(addons.values())
|
||||
|
||||
@property
|
||||
def installed(self) -> list[Addon]:
|
||||
"""Return a list of all installed add-ons."""
|
||||
return list(self.local.values())
|
||||
|
||||
def get(self, addon_slug: str, local_only: bool = False) -> AnyAddon | None:
|
||||
"""Return an add-on from slug.
|
||||
|
||||
Prio:
|
||||
1 - Local
|
||||
2 - Store
|
||||
"""
|
||||
if addon_slug in self.local:
|
||||
return self.local[addon_slug]
|
||||
if not local_only:
|
||||
return self.store.get(addon_slug)
|
||||
return None
|
||||
|
||||
def from_token(self, token: str) -> Addon | None:
|
||||
"""Return an add-on from Supervisor token."""
|
||||
for addon in self.installed:
|
||||
if token == addon.supervisor_token:
|
||||
return addon
|
||||
return None
|
||||
|
||||
async def load(self) -> None:
|
||||
"""Start up add-on management."""
|
||||
tasks = []
|
||||
for slug in self.data.system:
|
||||
addon = self.local[slug] = Addon(self.coresys, slug)
|
||||
tasks.append(self.sys_create_task(addon.load()))
|
||||
|
||||
# Run initial tasks
|
||||
_LOGGER.info("Found %d installed add-ons", len(tasks))
|
||||
if tasks:
|
||||
await asyncio.wait(tasks)
|
||||
|
||||
# Sync DNS
|
||||
await self.sync_dns()
|
||||
|
||||
async def boot(self, stage: AddonStartup) -> None:
|
||||
"""Boot add-ons with mode auto."""
|
||||
tasks: list[Addon] = []
|
||||
for addon in self.installed:
|
||||
if addon.boot != AddonBoot.AUTO or addon.startup != stage:
|
||||
continue
|
||||
tasks.append(addon)
|
||||
|
||||
# Evaluate add-ons which need to be started
|
||||
_LOGGER.info("Phase '%s' starting %d add-ons", stage, len(tasks))
|
||||
if not tasks:
|
||||
return
|
||||
|
||||
# Start Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
# Config.wait_boot is deprecated. Until addons update with healthchecks,
|
||||
# add a sleep task for it to keep the same minimum amount of wait time
|
||||
wait_boot: list[Awaitable[None]] = [asyncio.sleep(self.sys_config.wait_boot)]
|
||||
for addon in tasks:
|
||||
try:
|
||||
if start_task := await addon.start():
|
||||
wait_boot.append(start_task)
|
||||
except AddonsError as err:
|
||||
# Check if there is an system/user issue
|
||||
if check_exception_chain(
|
||||
err, (DockerAPIError, DockerNotFound, AddonConfigurationError)
|
||||
):
|
||||
addon.boot = AddonBoot.MANUAL
|
||||
addon.save_persist()
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
capture_exception(err)
|
||||
else:
|
||||
continue
|
||||
|
||||
_LOGGER.warning("Can't start Add-on %s", addon.slug)
|
||||
|
||||
# Ignore exceptions from waiting for addon startup, addon errors handled elsewhere
|
||||
await asyncio.gather(*wait_boot, return_exceptions=True)
|
||||
|
||||
async def shutdown(self, stage: AddonStartup) -> None:
|
||||
"""Shutdown addons."""
|
||||
tasks: list[Addon] = []
|
||||
for addon in self.installed:
|
||||
if addon.state != AddonState.STARTED or addon.startup != stage:
|
||||
continue
|
||||
tasks.append(addon)
|
||||
|
||||
# Evaluate add-ons which need to be stopped
|
||||
_LOGGER.info("Phase '%s' stopping %d add-ons", stage, len(tasks))
|
||||
if not tasks:
|
||||
return
|
||||
|
||||
# Stop Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
for addon in tasks:
|
||||
try:
|
||||
await addon.stop()
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.warning("Can't stop Add-on %s: %s", addon.slug, err)
|
||||
capture_exception(err)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_install",
|
||||
conditions=ADDON_UPDATE_CONDITIONS,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def install(self, slug: str) -> None:
|
||||
"""Install an add-on."""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is already installed", _LOGGER.warning)
|
||||
store = self.store.get(slug)
|
||||
|
||||
if not store:
|
||||
raise AddonsError(f"Add-on {slug} does not exist", _LOGGER.error)
|
||||
|
||||
store.validate_availability()
|
||||
|
||||
self.data.install(store)
|
||||
addon = Addon(self.coresys, slug)
|
||||
await addon.load()
|
||||
|
||||
if not addon.path_data.is_dir():
|
||||
_LOGGER.info(
|
||||
"Creating Home Assistant add-on data folder %s", addon.path_data
|
||||
)
|
||||
addon.path_data.mkdir()
|
||||
|
||||
# Setup/Fix AppArmor profile
|
||||
await addon.install_apparmor()
|
||||
|
||||
try:
|
||||
await addon.instance.install(store.version, store.image, arch=addon.arch)
|
||||
except DockerError as err:
|
||||
self.data.uninstall(addon)
|
||||
raise AddonsError() from err
|
||||
|
||||
self.local[slug] = addon
|
||||
|
||||
# Reload ingress tokens
|
||||
if addon.with_ingress:
|
||||
await self.sys_ingress.reload()
|
||||
|
||||
_LOGGER.info("Add-on '%s' successfully installed", slug)
|
||||
|
||||
async def uninstall(self, slug: str) -> None:
|
||||
"""Remove an add-on."""
|
||||
if slug not in self.local:
|
||||
_LOGGER.warning("Add-on %s is not installed", slug)
|
||||
return
|
||||
addon = self.local[slug]
|
||||
|
||||
try:
|
||||
await addon.instance.remove()
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
addon.state = AddonState.UNKNOWN
|
||||
|
||||
await addon.unload()
|
||||
|
||||
# Cleanup audio settings
|
||||
if addon.path_pulse.exists():
|
||||
with suppress(OSError):
|
||||
addon.path_pulse.unlink()
|
||||
|
||||
# Cleanup AppArmor profile
|
||||
with suppress(HostAppArmorError):
|
||||
await addon.uninstall_apparmor()
|
||||
|
||||
# Cleanup Ingress panel from sidebar
|
||||
if addon.ingress_panel:
|
||||
addon.ingress_panel = False
|
||||
with suppress(HomeAssistantAPIError):
|
||||
await self.sys_ingress.update_hass_panel(addon)
|
||||
|
||||
# Cleanup Ingress dynamic port assignment
|
||||
if addon.with_ingress:
|
||||
self.sys_create_task(self.sys_ingress.reload())
|
||||
self.sys_ingress.del_dynamic_port(slug)
|
||||
|
||||
# Cleanup discovery data
|
||||
for message in self.sys_discovery.list_messages:
|
||||
if message.addon != addon.slug:
|
||||
continue
|
||||
self.sys_discovery.remove(message)
|
||||
|
||||
# Cleanup services data
|
||||
for service in self.sys_services.list_services:
|
||||
if addon.slug not in service.active:
|
||||
continue
|
||||
service.del_service_data(addon)
|
||||
|
||||
self.data.uninstall(addon)
|
||||
self.local.pop(slug)
|
||||
|
||||
_LOGGER.info("Add-on '%s' successfully removed", slug)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_update",
|
||||
conditions=ADDON_UPDATE_CONDITIONS,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def update(
|
||||
self, slug: str, backup: bool | None = False
|
||||
) -> Awaitable[None] | None:
|
||||
"""Update add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after update. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is not installed", _LOGGER.error)
|
||||
addon = self.local[slug]
|
||||
|
||||
if addon.is_detached:
|
||||
raise AddonsError(
|
||||
f"Add-on {slug} is not available inside store", _LOGGER.error
|
||||
)
|
||||
store = self.store[slug]
|
||||
|
||||
if addon.version == store.version:
|
||||
raise AddonsError(f"No update available for add-on {slug}", _LOGGER.warning)
|
||||
|
||||
# Check if available, Maybe something have changed
|
||||
store.validate_availability()
|
||||
|
||||
if backup:
|
||||
await self.sys_backups.do_backup_partial(
|
||||
name=f"addon_{addon.slug}_{addon.version}",
|
||||
homeassistant=False,
|
||||
addons=[addon.slug],
|
||||
)
|
||||
|
||||
# Update instance
|
||||
last_state: AddonState = addon.state
|
||||
old_image = addon.image
|
||||
try:
|
||||
await addon.instance.update(store.version, store.image)
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
_LOGGER.info("Add-on '%s' successfully updated", slug)
|
||||
self.data.update(store)
|
||||
|
||||
# Cleanup
|
||||
with suppress(DockerError):
|
||||
await addon.instance.cleanup(old_image=old_image)
|
||||
|
||||
# Setup/Fix AppArmor profile
|
||||
await addon.install_apparmor()
|
||||
|
||||
# restore state
|
||||
return (
|
||||
await addon.start()
|
||||
if last_state in [AddonState.STARTED, AddonState.STARTUP]
|
||||
else None
|
||||
)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_rebuild",
|
||||
conditions=[
|
||||
JobCondition.FREE_SPACE,
|
||||
JobCondition.INTERNET_HOST,
|
||||
JobCondition.HEALTHY,
|
||||
],
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def rebuild(self, slug: str) -> Awaitable[None] | None:
|
||||
"""Perform a rebuild of local build add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after rebuild. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is not installed", _LOGGER.error)
|
||||
addon = self.local[slug]
|
||||
|
||||
if addon.is_detached:
|
||||
raise AddonsError(
|
||||
f"Add-on {slug} is not available inside store", _LOGGER.error
|
||||
)
|
||||
store = self.store[slug]
|
||||
|
||||
# Check if a rebuild is possible now
|
||||
if addon.version != store.version:
|
||||
raise AddonsError(
|
||||
"Version changed, use Update instead Rebuild", _LOGGER.error
|
||||
)
|
||||
if not addon.need_build:
|
||||
raise AddonsNotSupportedError(
|
||||
"Can't rebuild a image based add-on", _LOGGER.error
|
||||
)
|
||||
|
||||
# remove docker container but not addon config
|
||||
last_state: AddonState = addon.state
|
||||
try:
|
||||
await addon.instance.remove()
|
||||
await addon.instance.install(addon.version)
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
self.data.update(store)
|
||||
_LOGGER.info("Add-on '%s' successfully rebuilt", slug)
|
||||
|
||||
# restore state
|
||||
return (
|
||||
await addon.start()
|
||||
if last_state in [AddonState.STARTED, AddonState.STARTUP]
|
||||
else None
|
||||
)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_restore",
|
||||
conditions=[
|
||||
JobCondition.FREE_SPACE,
|
||||
JobCondition.INTERNET_HOST,
|
||||
JobCondition.HEALTHY,
|
||||
],
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def restore(
|
||||
self, slug: str, tar_file: tarfile.TarFile
|
||||
) -> Awaitable[None] | None:
|
||||
"""Restore state of an add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after restore. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
_LOGGER.debug("Add-on %s is not local available for restore", slug)
|
||||
addon = Addon(self.coresys, slug)
|
||||
else:
|
||||
_LOGGER.debug("Add-on %s is local available for restore", slug)
|
||||
addon = self.local[slug]
|
||||
|
||||
wait_for_start = await addon.restore(tar_file)
|
||||
|
||||
# Check if new
|
||||
if slug not in self.local:
|
||||
_LOGGER.info("Detect new Add-on after restore %s", slug)
|
||||
self.local[slug] = addon
|
||||
|
||||
# Update ingress
|
||||
if addon.with_ingress:
|
||||
await self.sys_ingress.reload()
|
||||
with suppress(HomeAssistantAPIError):
|
||||
await self.sys_ingress.update_hass_panel(addon)
|
||||
|
||||
return wait_for_start
|
||||
|
||||
@Job(
|
||||
name="addon_manager_repair",
|
||||
conditions=[JobCondition.FREE_SPACE, JobCondition.INTERNET_HOST],
|
||||
)
|
||||
async def repair(self) -> None:
|
||||
"""Repair local add-ons."""
|
||||
needs_repair: list[Addon] = []
|
||||
|
||||
# Evaluate Add-ons to repair
|
||||
for addon in self.installed:
|
||||
if await addon.instance.exists():
|
||||
continue
|
||||
needs_repair.append(addon)
|
||||
|
||||
_LOGGER.info("Found %d add-ons to repair", len(needs_repair))
|
||||
if not needs_repair:
|
||||
return
|
||||
|
||||
for addon in needs_repair:
|
||||
_LOGGER.info("Repairing for add-on: %s", addon.slug)
|
||||
with suppress(DockerError, KeyError):
|
||||
# Need pull a image again
|
||||
if not addon.need_build:
|
||||
await addon.instance.install(addon.version, addon.image)
|
||||
continue
|
||||
|
||||
# Need local lookup
|
||||
if addon.need_build and not addon.is_detached:
|
||||
store = self.store[addon.slug]
|
||||
# If this add-on is available for rebuild
|
||||
if addon.version == store.version:
|
||||
await addon.instance.install(addon.version, addon.image)
|
||||
continue
|
||||
|
||||
_LOGGER.error("Can't repair %s", addon.slug)
|
||||
with suppress(AddonsError):
|
||||
await self.uninstall(addon.slug)
|
||||
|
||||
async def sync_dns(self) -> None:
|
||||
"""Sync add-ons DNS names."""
|
||||
# Update hosts
|
||||
add_host_coros: list[Awaitable[None]] = []
|
||||
for addon in self.installed:
|
||||
try:
|
||||
if not await addon.instance.is_running():
|
||||
continue
|
||||
except DockerError as err:
|
||||
_LOGGER.warning("Add-on %s is corrupt: %s", addon.slug, err)
|
||||
self.sys_resolution.create_issue(
|
||||
IssueType.CORRUPT_DOCKER,
|
||||
ContextType.ADDON,
|
||||
reference=addon.slug,
|
||||
suggestions=[SuggestionType.EXECUTE_REPAIR],
|
||||
)
|
||||
capture_exception(err)
|
||||
else:
|
||||
add_host_coros.append(
|
||||
self.sys_plugins.dns.add_host(
|
||||
ipv4=addon.ip_address, names=[addon.hostname], write=False
|
||||
)
|
||||
)
|
||||
|
||||
await asyncio.gather(*add_host_coros)
|
||||
|
||||
# Write hosts files
|
||||
with suppress(CoreDNSError):
|
||||
await self.sys_plugins.dns.write_hosts()
|
||||
|
@@ -47,6 +47,7 @@ from ..const import (
|
||||
ATTR_VERSION,
|
||||
ATTR_WATCHDOG,
|
||||
DNS_SUFFIX,
|
||||
MAP_ADDON_CONFIG,
|
||||
AddonBoot,
|
||||
AddonStartup,
|
||||
AddonState,
|
||||
@@ -64,12 +65,14 @@ from ..exceptions import (
|
||||
AddonsNotSupportedError,
|
||||
ConfigurationFileError,
|
||||
DockerError,
|
||||
HomeAssistantAPIError,
|
||||
HostAppArmorError,
|
||||
)
|
||||
from ..hardware.data import Device
|
||||
from ..homeassistant.const import WSEvent, WSType
|
||||
from ..jobs.const import JobExecutionLimit
|
||||
from ..jobs.decorator import Job
|
||||
from ..store.addon import AddonStore
|
||||
from ..utils import check_port
|
||||
from ..utils.apparmor import adjust_profile
|
||||
from ..utils.json import read_json_file, write_json_file
|
||||
@@ -181,6 +184,7 @@ class Addon(AddonModel):
|
||||
)
|
||||
)
|
||||
|
||||
await self._check_ingress_port()
|
||||
with suppress(DockerError):
|
||||
await self.instance.attach(version=self.version)
|
||||
|
||||
@@ -199,6 +203,11 @@ class Addon(AddonModel):
|
||||
"""Return add-on data from store."""
|
||||
return self.sys_store.data.addons.get(self.slug, self.data)
|
||||
|
||||
@property
|
||||
def addon_store(self) -> AddonStore | None:
|
||||
"""Return store representation of addon."""
|
||||
return self.sys_addons.store.get(self.slug)
|
||||
|
||||
@property
|
||||
def persist(self) -> Data:
|
||||
"""Return add-on data/config."""
|
||||
@@ -387,7 +396,7 @@ class Addon(AddonModel):
|
||||
|
||||
port = self.data[ATTR_INGRESS_PORT]
|
||||
if port == 0:
|
||||
return self.sys_ingress.get_dynamic_port(self.slug)
|
||||
raise RuntimeError(f"No port set for add-on {self.slug}")
|
||||
return port
|
||||
|
||||
@property
|
||||
@@ -453,6 +462,21 @@ class Addon(AddonModel):
|
||||
"""Return add-on data path external for Docker."""
|
||||
return PurePath(self.sys_config.path_extern_addons_data, self.slug)
|
||||
|
||||
@property
|
||||
def addon_config_used(self) -> bool:
|
||||
"""Add-on is using its public config folder."""
|
||||
return MAP_ADDON_CONFIG in self.map_volumes
|
||||
|
||||
@property
|
||||
def path_config(self) -> Path:
|
||||
"""Return add-on config path inside Supervisor."""
|
||||
return Path(self.sys_config.path_addon_configs, self.slug)
|
||||
|
||||
@property
|
||||
def path_extern_config(self) -> PurePath:
|
||||
"""Return add-on config path external for Docker."""
|
||||
return PurePath(self.sys_config.path_extern_addon_configs, self.slug)
|
||||
|
||||
@property
|
||||
def path_options(self) -> Path:
|
||||
"""Return path to add-on options."""
|
||||
@@ -516,7 +540,7 @@ class Addon(AddonModel):
|
||||
|
||||
# TCP monitoring
|
||||
if s_prefix == "tcp":
|
||||
return await self.sys_run_in_executor(check_port, self.ip_address, port)
|
||||
return await check_port(self.ip_address, port)
|
||||
|
||||
# lookup the correct protocol from config
|
||||
if t_proto:
|
||||
@@ -532,7 +556,7 @@ class Addon(AddonModel):
|
||||
) as req:
|
||||
if req.status < 300:
|
||||
return True
|
||||
except (asyncio.TimeoutError, aiohttp.ClientError):
|
||||
except (TimeoutError, aiohttp.ClientError):
|
||||
pass
|
||||
|
||||
return False
|
||||
@@ -559,6 +583,11 @@ class Addon(AddonModel):
|
||||
|
||||
raise AddonConfigurationError()
|
||||
|
||||
@Job(
|
||||
name="addon_unload",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def unload(self) -> None:
|
||||
"""Unload add-on and remove data."""
|
||||
if self._startup_task:
|
||||
@@ -570,11 +599,185 @@ class Addon(AddonModel):
|
||||
for listener in self._listeners:
|
||||
self.sys_bus.remove_listener(listener)
|
||||
|
||||
if not self.path_data.is_dir():
|
||||
if self.path_data.is_dir():
|
||||
_LOGGER.info("Removing add-on data folder %s", self.path_data)
|
||||
await remove_data(self.path_data)
|
||||
|
||||
async def _check_ingress_port(self):
|
||||
"""Assign a ingress port if dynamic port selection is used."""
|
||||
if not self.with_ingress:
|
||||
return
|
||||
|
||||
_LOGGER.info("Removing add-on data folder %s", self.path_data)
|
||||
await remove_data(self.path_data)
|
||||
if self.data[ATTR_INGRESS_PORT] == 0:
|
||||
self.data[ATTR_INGRESS_PORT] = await self.sys_ingress.get_dynamic_port(
|
||||
self.slug
|
||||
)
|
||||
|
||||
@Job(
|
||||
name="addon_install",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def install(self) -> None:
|
||||
"""Install and setup this addon."""
|
||||
self.sys_addons.data.install(self.addon_store)
|
||||
await self.load()
|
||||
|
||||
if not self.path_data.is_dir():
|
||||
_LOGGER.info(
|
||||
"Creating Home Assistant add-on data folder %s", self.path_data
|
||||
)
|
||||
self.path_data.mkdir()
|
||||
|
||||
# Setup/Fix AppArmor profile
|
||||
await self.install_apparmor()
|
||||
|
||||
# Install image
|
||||
try:
|
||||
await self.instance.install(
|
||||
self.latest_version, self.addon_store.image, arch=self.arch
|
||||
)
|
||||
except DockerError as err:
|
||||
self.sys_addons.data.uninstall(self)
|
||||
raise AddonsError() from err
|
||||
|
||||
# Add to addon manager
|
||||
self.sys_addons.local[self.slug] = self
|
||||
|
||||
# Reload ingress tokens
|
||||
if self.with_ingress:
|
||||
await self.sys_ingress.reload()
|
||||
|
||||
@Job(
|
||||
name="addon_uninstall",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def uninstall(self) -> None:
|
||||
"""Uninstall and cleanup this addon."""
|
||||
try:
|
||||
await self.instance.remove()
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
self.state = AddonState.UNKNOWN
|
||||
|
||||
await self.unload()
|
||||
|
||||
# Cleanup audio settings
|
||||
if self.path_pulse.exists():
|
||||
with suppress(OSError):
|
||||
self.path_pulse.unlink()
|
||||
|
||||
# Cleanup AppArmor profile
|
||||
with suppress(HostAppArmorError):
|
||||
await self.uninstall_apparmor()
|
||||
|
||||
# Cleanup Ingress panel from sidebar
|
||||
if self.ingress_panel:
|
||||
self.ingress_panel = False
|
||||
with suppress(HomeAssistantAPIError):
|
||||
await self.sys_ingress.update_hass_panel(self)
|
||||
|
||||
# Cleanup Ingress dynamic port assignment
|
||||
if self.with_ingress:
|
||||
self.sys_create_task(self.sys_ingress.reload())
|
||||
self.sys_ingress.del_dynamic_port(self.slug)
|
||||
|
||||
# Cleanup discovery data
|
||||
for message in self.sys_discovery.list_messages:
|
||||
if message.addon != self.slug:
|
||||
continue
|
||||
self.sys_discovery.remove(message)
|
||||
|
||||
# Cleanup services data
|
||||
for service in self.sys_services.list_services:
|
||||
if self.slug not in service.active:
|
||||
continue
|
||||
service.del_service_data(self)
|
||||
|
||||
# Remove from addon manager
|
||||
self.sys_addons.data.uninstall(self)
|
||||
self.sys_addons.local.pop(self.slug)
|
||||
|
||||
@Job(
|
||||
name="addon_update",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def update(self) -> asyncio.Task | None:
|
||||
"""Update this addon to latest version.
|
||||
|
||||
Returns a Task that completes when addon has state 'started' (see start)
|
||||
if it was running. Else nothing is returned.
|
||||
"""
|
||||
old_image = self.image
|
||||
# Cache data to prevent races with other updates to global
|
||||
store = self.addon_store.clone()
|
||||
|
||||
try:
|
||||
await self.instance.update(store.version, store.image)
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
# Stop the addon if running
|
||||
if (last_state := self.state) in {AddonState.STARTED, AddonState.STARTUP}:
|
||||
await self.stop()
|
||||
|
||||
try:
|
||||
_LOGGER.info("Add-on '%s' successfully updated", self.slug)
|
||||
self.sys_addons.data.update(store)
|
||||
await self._check_ingress_port()
|
||||
|
||||
# Cleanup
|
||||
with suppress(DockerError):
|
||||
await self.instance.cleanup(
|
||||
old_image=old_image, image=store.image, version=store.version
|
||||
)
|
||||
|
||||
# Setup/Fix AppArmor profile
|
||||
await self.install_apparmor()
|
||||
|
||||
finally:
|
||||
# restore state. Return Task for caller if no exception
|
||||
out = (
|
||||
await self.start()
|
||||
if last_state in {AddonState.STARTED, AddonState.STARTUP}
|
||||
else None
|
||||
)
|
||||
return out
|
||||
|
||||
@Job(
|
||||
name="addon_rebuild",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def rebuild(self) -> asyncio.Task | None:
|
||||
"""Rebuild this addons container and image.
|
||||
|
||||
Returns a Task that completes when addon has state 'started' (see start)
|
||||
if it was running. Else nothing is returned.
|
||||
"""
|
||||
last_state: AddonState = self.state
|
||||
try:
|
||||
# remove docker container but not addon config
|
||||
try:
|
||||
await self.instance.remove()
|
||||
await self.instance.install(self.version)
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
self.sys_addons.data.update(self.addon_store)
|
||||
_LOGGER.info("Add-on '%s' successfully rebuilt", self.slug)
|
||||
|
||||
finally:
|
||||
# restore state
|
||||
out = (
|
||||
await self.start()
|
||||
if last_state in [AddonState.STARTED, AddonState.STARTUP]
|
||||
else None
|
||||
)
|
||||
return out
|
||||
|
||||
def write_pulse(self) -> None:
|
||||
"""Write asound config to file and return True on success."""
|
||||
@@ -660,9 +863,9 @@ class Addon(AddonModel):
|
||||
try:
|
||||
self._startup_task = self.sys_create_task(self._startup_event.wait())
|
||||
await asyncio.wait_for(self._startup_task, STARTUP_TIMEOUT)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.warning(
|
||||
"Timeout while waiting for addon %s to start, took more then %s seconds",
|
||||
"Timeout while waiting for addon %s to start, took more than %s seconds",
|
||||
self.name,
|
||||
STARTUP_TIMEOUT,
|
||||
)
|
||||
@@ -671,16 +874,21 @@ class Addon(AddonModel):
|
||||
finally:
|
||||
self._startup_task = None
|
||||
|
||||
async def start(self) -> Awaitable[None]:
|
||||
@Job(
|
||||
name="addon_start",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def start(self) -> asyncio.Task:
|
||||
"""Set options and start add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started'.
|
||||
Returns a Task that completes when addon has state 'started'.
|
||||
For addons with a healthcheck, that is when they become healthy or unhealthy.
|
||||
Addons without a healthcheck have state 'started' immediately.
|
||||
"""
|
||||
if await self.instance.is_running():
|
||||
_LOGGER.warning("%s is already running!", self.slug)
|
||||
return self._wait_for_startup()
|
||||
return self.sys_create_task(self._wait_for_startup())
|
||||
|
||||
# Access Token
|
||||
self.persist[ATTR_ACCESS_TOKEN] = secrets.token_hex(56)
|
||||
@@ -693,6 +901,18 @@ class Addon(AddonModel):
|
||||
if self.with_audio:
|
||||
self.write_pulse()
|
||||
|
||||
def _check_addon_config_dir():
|
||||
if self.path_config.is_dir():
|
||||
return
|
||||
|
||||
_LOGGER.info(
|
||||
"Creating Home Assistant add-on config folder %s", self.path_config
|
||||
)
|
||||
self.path_config.mkdir()
|
||||
|
||||
if self.addon_config_used:
|
||||
await self.sys_run_in_executor(_check_addon_config_dir)
|
||||
|
||||
# Start Add-on
|
||||
self._startup_event.clear()
|
||||
try:
|
||||
@@ -701,8 +921,13 @@ class Addon(AddonModel):
|
||||
self.state = AddonState.ERROR
|
||||
raise AddonsError() from err
|
||||
|
||||
return self._wait_for_startup()
|
||||
return self.sys_create_task(self._wait_for_startup())
|
||||
|
||||
@Job(
|
||||
name="addon_stop",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def stop(self) -> None:
|
||||
"""Stop add-on."""
|
||||
self._manual_stop = True
|
||||
@@ -712,10 +937,15 @@ class Addon(AddonModel):
|
||||
self.state = AddonState.ERROR
|
||||
raise AddonsError() from err
|
||||
|
||||
async def restart(self) -> Awaitable[None]:
|
||||
@Job(
|
||||
name="addon_restart",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def restart(self) -> asyncio.Task:
|
||||
"""Restart add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see start).
|
||||
Returns a Task that completes when addon has state 'started' (see start).
|
||||
"""
|
||||
with suppress(AddonsError):
|
||||
await self.stop()
|
||||
@@ -742,6 +972,11 @@ class Addon(AddonModel):
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
|
||||
@Job(
|
||||
name="addon_write_stdin",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def write_stdin(self, data) -> None:
|
||||
"""Write data to add-on stdin."""
|
||||
if not self.with_stdin:
|
||||
@@ -771,7 +1006,11 @@ class Addon(AddonModel):
|
||||
_LOGGER.error,
|
||||
) from err
|
||||
|
||||
@Job(name="addon_begin_backup")
|
||||
@Job(
|
||||
name="addon_begin_backup",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def begin_backup(self) -> bool:
|
||||
"""Execute pre commands or stop addon if necessary.
|
||||
|
||||
@@ -782,21 +1021,22 @@ class Addon(AddonModel):
|
||||
|
||||
if self.backup_mode == AddonBackupMode.COLD:
|
||||
_LOGGER.info("Shutdown add-on %s for cold backup", self.slug)
|
||||
try:
|
||||
await self.instance.stop()
|
||||
except DockerError as err:
|
||||
raise AddonsError() from err
|
||||
await self.stop()
|
||||
|
||||
elif self.backup_pre is not None:
|
||||
await self._backup_command(self.backup_pre)
|
||||
|
||||
return True
|
||||
|
||||
@Job(name="addon_end_backup")
|
||||
async def end_backup(self) -> Awaitable[None] | None:
|
||||
@Job(
|
||||
name="addon_end_backup",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def end_backup(self) -> asyncio.Task | None:
|
||||
"""Execute post commands or restart addon if necessary.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see start)
|
||||
Returns a Task that completes when addon has state 'started' (see start)
|
||||
for cold backup. Else nothing is returned.
|
||||
"""
|
||||
if self.backup_mode is AddonBackupMode.COLD:
|
||||
@@ -807,11 +1047,15 @@ class Addon(AddonModel):
|
||||
await self._backup_command(self.backup_post)
|
||||
return None
|
||||
|
||||
@Job(name="addon_backup")
|
||||
async def backup(self, tar_file: tarfile.TarFile) -> Awaitable[None] | None:
|
||||
@Job(
|
||||
name="addon_backup",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def backup(self, tar_file: tarfile.TarFile) -> asyncio.Task | None:
|
||||
"""Backup state of an add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see start)
|
||||
Returns a Task that completes when addon has state 'started' (see start)
|
||||
for cold backup. Else nothing is returned.
|
||||
"""
|
||||
wait_for_start: Awaitable[None] | None = None
|
||||
@@ -866,6 +1110,15 @@ class Addon(AddonModel):
|
||||
arcname="data",
|
||||
)
|
||||
|
||||
# Backup config
|
||||
if self.addon_config_used:
|
||||
atomic_contents_add(
|
||||
backup,
|
||||
self.path_config,
|
||||
excludes=self.backup_exclude,
|
||||
arcname="config",
|
||||
)
|
||||
|
||||
is_running = await self.begin_backup()
|
||||
try:
|
||||
_LOGGER.info("Building backup for add-on %s", self.slug)
|
||||
@@ -881,10 +1134,15 @@ class Addon(AddonModel):
|
||||
_LOGGER.info("Finish backup for addon %s", self.slug)
|
||||
return wait_for_start
|
||||
|
||||
async def restore(self, tar_file: tarfile.TarFile) -> Awaitable[None] | None:
|
||||
@Job(
|
||||
name="addon_restore",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def restore(self, tar_file: tarfile.TarFile) -> asyncio.Task | None:
|
||||
"""Restore state of an add-on.
|
||||
|
||||
Returns a coroutine that completes when addon has state 'started' (see start)
|
||||
Returns a Task that completes when addon has state 'started' (see start)
|
||||
if addon is started after restore. Else nothing is returned.
|
||||
"""
|
||||
wait_for_start: Awaitable[None] | None = None
|
||||
@@ -933,64 +1191,77 @@ class Addon(AddonModel):
|
||||
|
||||
# Stop it first if its running
|
||||
if await self.instance.is_running():
|
||||
with suppress(DockerError):
|
||||
await self.instance.stop()
|
||||
await self.stop()
|
||||
|
||||
# Check version / restore image
|
||||
version = data[ATTR_VERSION]
|
||||
if not await self.instance.exists():
|
||||
_LOGGER.info("Restore/Install of image for addon %s", self.slug)
|
||||
|
||||
image_file = Path(temp, "image.tar")
|
||||
if image_file.is_file():
|
||||
with suppress(DockerError):
|
||||
await self.instance.import_image(image_file)
|
||||
else:
|
||||
with suppress(DockerError):
|
||||
await self.instance.install(version, restore_image)
|
||||
await self.instance.cleanup()
|
||||
elif self.instance.version != version or self.legacy:
|
||||
_LOGGER.info("Restore/Update of image for addon %s", self.slug)
|
||||
with suppress(DockerError):
|
||||
await self.instance.update(version, restore_image)
|
||||
|
||||
# Restore data
|
||||
def _restore_data():
|
||||
"""Restore data."""
|
||||
temp_data = Path(temp, "data")
|
||||
if temp_data.is_dir():
|
||||
shutil.copytree(temp_data, self.path_data, symlinks=True)
|
||||
else:
|
||||
self.path_data.mkdir()
|
||||
|
||||
_LOGGER.info("Restoring data for addon %s", self.slug)
|
||||
if self.path_data.is_dir():
|
||||
await remove_data(self.path_data)
|
||||
try:
|
||||
await self.sys_run_in_executor(_restore_data)
|
||||
except shutil.Error as err:
|
||||
raise AddonsError(
|
||||
f"Can't restore origin data: {err}", _LOGGER.error
|
||||
) from err
|
||||
# Check version / restore image
|
||||
version = data[ATTR_VERSION]
|
||||
if not await self.instance.exists():
|
||||
_LOGGER.info("Restore/Install of image for addon %s", self.slug)
|
||||
|
||||
image_file = Path(temp, "image.tar")
|
||||
if image_file.is_file():
|
||||
with suppress(DockerError):
|
||||
await self.instance.import_image(image_file)
|
||||
else:
|
||||
with suppress(DockerError):
|
||||
await self.instance.install(version, restore_image)
|
||||
await self.instance.cleanup()
|
||||
elif self.instance.version != version or self.legacy:
|
||||
_LOGGER.info("Restore/Update of image for addon %s", self.slug)
|
||||
with suppress(DockerError):
|
||||
await self.instance.update(version, restore_image)
|
||||
self._check_ingress_port()
|
||||
|
||||
# Restore data and config
|
||||
def _restore_data():
|
||||
"""Restore data and config."""
|
||||
temp_data = Path(temp, "data")
|
||||
if temp_data.is_dir():
|
||||
shutil.copytree(temp_data, self.path_data, symlinks=True)
|
||||
else:
|
||||
self.path_data.mkdir()
|
||||
|
||||
temp_config = Path(temp, "config")
|
||||
if temp_config.is_dir():
|
||||
shutil.copytree(temp_config, self.path_config, symlinks=True)
|
||||
elif self.addon_config_used:
|
||||
self.path_config.mkdir()
|
||||
|
||||
_LOGGER.info("Restoring data and config for addon %s", self.slug)
|
||||
if self.path_data.is_dir():
|
||||
await remove_data(self.path_data)
|
||||
if self.path_config.is_dir():
|
||||
await remove_data(self.path_config)
|
||||
|
||||
# Restore AppArmor
|
||||
profile_file = Path(temp, "apparmor.txt")
|
||||
if profile_file.exists():
|
||||
try:
|
||||
await self.sys_host.apparmor.load_profile(self.slug, profile_file)
|
||||
except HostAppArmorError as err:
|
||||
_LOGGER.error(
|
||||
"Can't restore AppArmor profile for add-on %s", self.slug
|
||||
)
|
||||
raise AddonsError() from err
|
||||
await self.sys_run_in_executor(_restore_data)
|
||||
except shutil.Error as err:
|
||||
raise AddonsError(
|
||||
f"Can't restore origin data: {err}", _LOGGER.error
|
||||
) from err
|
||||
|
||||
# Is add-on loaded
|
||||
if not self.loaded:
|
||||
await self.load()
|
||||
# Restore AppArmor
|
||||
profile_file = Path(temp, "apparmor.txt")
|
||||
if profile_file.exists():
|
||||
try:
|
||||
await self.sys_host.apparmor.load_profile(
|
||||
self.slug, profile_file
|
||||
)
|
||||
except HostAppArmorError as err:
|
||||
_LOGGER.error(
|
||||
"Can't restore AppArmor profile for add-on %s", self.slug
|
||||
)
|
||||
raise AddonsError() from err
|
||||
|
||||
# Run add-on
|
||||
if data[ATTR_STATE] == AddonState.STARTED:
|
||||
wait_for_start = await self.start()
|
||||
# Is add-on loaded
|
||||
if not self.loaded:
|
||||
await self.load()
|
||||
|
||||
finally:
|
||||
# Run add-on
|
||||
if data[ATTR_STATE] == AddonState.STARTED:
|
||||
wait_for_start = await self.start()
|
||||
|
||||
_LOGGER.info("Finished restore for add-on %s", self.slug)
|
||||
return wait_for_start
|
||||
|
374
supervisor/addons/manager.py
Normal file
374
supervisor/addons/manager.py
Normal file
@@ -0,0 +1,374 @@
|
||||
"""Supervisor add-on manager."""
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
from contextlib import suppress
|
||||
import logging
|
||||
import tarfile
|
||||
from typing import Union
|
||||
|
||||
from ..const import AddonBoot, AddonStartup, AddonState
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
from ..exceptions import (
|
||||
AddonConfigurationError,
|
||||
AddonsError,
|
||||
AddonsJobError,
|
||||
AddonsNotSupportedError,
|
||||
CoreDNSError,
|
||||
DockerAPIError,
|
||||
DockerError,
|
||||
DockerNotFound,
|
||||
HassioError,
|
||||
HomeAssistantAPIError,
|
||||
)
|
||||
from ..jobs.decorator import Job, JobCondition
|
||||
from ..resolution.const import ContextType, IssueType, SuggestionType
|
||||
from ..store.addon import AddonStore
|
||||
from ..utils import check_exception_chain
|
||||
from ..utils.sentry import capture_exception
|
||||
from .addon import Addon
|
||||
from .const import ADDON_UPDATE_CONDITIONS
|
||||
from .data import AddonsData
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
AnyAddon = Union[Addon, AddonStore]
|
||||
|
||||
|
||||
class AddonManager(CoreSysAttributes):
|
||||
"""Manage add-ons inside Supervisor."""
|
||||
|
||||
def __init__(self, coresys: CoreSys):
|
||||
"""Initialize Docker base wrapper."""
|
||||
self.coresys: CoreSys = coresys
|
||||
self.data: AddonsData = AddonsData(coresys)
|
||||
self.local: dict[str, Addon] = {}
|
||||
self.store: dict[str, AddonStore] = {}
|
||||
|
||||
@property
|
||||
def all(self) -> list[AnyAddon]:
|
||||
"""Return a list of all add-ons."""
|
||||
addons: dict[str, AnyAddon] = {**self.store, **self.local}
|
||||
return list(addons.values())
|
||||
|
||||
@property
|
||||
def installed(self) -> list[Addon]:
|
||||
"""Return a list of all installed add-ons."""
|
||||
return list(self.local.values())
|
||||
|
||||
def get(self, addon_slug: str, local_only: bool = False) -> AnyAddon | None:
|
||||
"""Return an add-on from slug.
|
||||
|
||||
Prio:
|
||||
1 - Local
|
||||
2 - Store
|
||||
"""
|
||||
if addon_slug in self.local:
|
||||
return self.local[addon_slug]
|
||||
if not local_only:
|
||||
return self.store.get(addon_slug)
|
||||
return None
|
||||
|
||||
def from_token(self, token: str) -> Addon | None:
|
||||
"""Return an add-on from Supervisor token."""
|
||||
for addon in self.installed:
|
||||
if token == addon.supervisor_token:
|
||||
return addon
|
||||
return None
|
||||
|
||||
async def load(self) -> None:
|
||||
"""Start up add-on management."""
|
||||
tasks = []
|
||||
for slug in self.data.system:
|
||||
addon = self.local[slug] = Addon(self.coresys, slug)
|
||||
tasks.append(self.sys_create_task(addon.load()))
|
||||
|
||||
# Run initial tasks
|
||||
_LOGGER.info("Found %d installed add-ons", len(tasks))
|
||||
if tasks:
|
||||
await asyncio.wait(tasks)
|
||||
|
||||
# Sync DNS
|
||||
await self.sync_dns()
|
||||
|
||||
async def boot(self, stage: AddonStartup) -> None:
|
||||
"""Boot add-ons with mode auto."""
|
||||
tasks: list[Addon] = []
|
||||
for addon in self.installed:
|
||||
if addon.boot != AddonBoot.AUTO or addon.startup != stage:
|
||||
continue
|
||||
tasks.append(addon)
|
||||
|
||||
# Evaluate add-ons which need to be started
|
||||
_LOGGER.info("Phase '%s' starting %d add-ons", stage, len(tasks))
|
||||
if not tasks:
|
||||
return
|
||||
|
||||
# Start Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
# Config.wait_boot is deprecated. Until addons update with healthchecks,
|
||||
# add a sleep task for it to keep the same minimum amount of wait time
|
||||
wait_boot: list[Awaitable[None]] = [asyncio.sleep(self.sys_config.wait_boot)]
|
||||
for addon in tasks:
|
||||
try:
|
||||
if start_task := await addon.start():
|
||||
wait_boot.append(start_task)
|
||||
except AddonsError as err:
|
||||
# Check if there is an system/user issue
|
||||
if check_exception_chain(
|
||||
err, (DockerAPIError, DockerNotFound, AddonConfigurationError)
|
||||
):
|
||||
addon.boot = AddonBoot.MANUAL
|
||||
addon.save_persist()
|
||||
except HassioError:
|
||||
pass # These are already handled
|
||||
else:
|
||||
continue
|
||||
|
||||
_LOGGER.warning("Can't start Add-on %s", addon.slug)
|
||||
|
||||
# Ignore exceptions from waiting for addon startup, addon errors handled elsewhere
|
||||
await asyncio.gather(*wait_boot, return_exceptions=True)
|
||||
|
||||
async def shutdown(self, stage: AddonStartup) -> None:
|
||||
"""Shutdown addons."""
|
||||
tasks: list[Addon] = []
|
||||
for addon in self.installed:
|
||||
if addon.state != AddonState.STARTED or addon.startup != stage:
|
||||
continue
|
||||
tasks.append(addon)
|
||||
|
||||
# Evaluate add-ons which need to be stopped
|
||||
_LOGGER.info("Phase '%s' stopping %d add-ons", stage, len(tasks))
|
||||
if not tasks:
|
||||
return
|
||||
|
||||
# Stop Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
for addon in tasks:
|
||||
try:
|
||||
await addon.stop()
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.warning("Can't stop Add-on %s: %s", addon.slug, err)
|
||||
capture_exception(err)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_install",
|
||||
conditions=ADDON_UPDATE_CONDITIONS,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def install(self, slug: str) -> None:
|
||||
"""Install an add-on."""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is already installed", _LOGGER.warning)
|
||||
store = self.store.get(slug)
|
||||
|
||||
if not store:
|
||||
raise AddonsError(f"Add-on {slug} does not exist", _LOGGER.error)
|
||||
|
||||
store.validate_availability()
|
||||
|
||||
await Addon(self.coresys, slug).install()
|
||||
|
||||
_LOGGER.info("Add-on '%s' successfully installed", slug)
|
||||
|
||||
async def uninstall(self, slug: str) -> None:
|
||||
"""Remove an add-on."""
|
||||
if slug not in self.local:
|
||||
_LOGGER.warning("Add-on %s is not installed", slug)
|
||||
return
|
||||
|
||||
await self.local[slug].uninstall()
|
||||
|
||||
_LOGGER.info("Add-on '%s' successfully removed", slug)
|
||||
|
||||
@Job(
|
||||
name="addon_manager_update",
|
||||
conditions=ADDON_UPDATE_CONDITIONS,
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def update(
|
||||
self, slug: str, backup: bool | None = False
|
||||
) -> asyncio.Task | None:
|
||||
"""Update add-on.
|
||||
|
||||
Returns a Task that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after update. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is not installed", _LOGGER.error)
|
||||
addon = self.local[slug]
|
||||
|
||||
if addon.is_detached:
|
||||
raise AddonsError(
|
||||
f"Add-on {slug} is not available inside store", _LOGGER.error
|
||||
)
|
||||
store = self.store[slug]
|
||||
|
||||
if addon.version == store.version:
|
||||
raise AddonsError(f"No update available for add-on {slug}", _LOGGER.warning)
|
||||
|
||||
# Check if available, Maybe something have changed
|
||||
store.validate_availability()
|
||||
|
||||
if backup:
|
||||
await self.sys_backups.do_backup_partial(
|
||||
name=f"addon_{addon.slug}_{addon.version}",
|
||||
homeassistant=False,
|
||||
addons=[addon.slug],
|
||||
)
|
||||
|
||||
return await addon.update()
|
||||
|
||||
@Job(
|
||||
name="addon_manager_rebuild",
|
||||
conditions=[
|
||||
JobCondition.FREE_SPACE,
|
||||
JobCondition.INTERNET_HOST,
|
||||
JobCondition.HEALTHY,
|
||||
],
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def rebuild(self, slug: str) -> asyncio.Task | None:
|
||||
"""Perform a rebuild of local build add-on.
|
||||
|
||||
Returns a Task that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after rebuild. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
raise AddonsError(f"Add-on {slug} is not installed", _LOGGER.error)
|
||||
addon = self.local[slug]
|
||||
|
||||
if addon.is_detached:
|
||||
raise AddonsError(
|
||||
f"Add-on {slug} is not available inside store", _LOGGER.error
|
||||
)
|
||||
store = self.store[slug]
|
||||
|
||||
# Check if a rebuild is possible now
|
||||
if addon.version != store.version:
|
||||
raise AddonsError(
|
||||
"Version changed, use Update instead Rebuild", _LOGGER.error
|
||||
)
|
||||
if not addon.need_build:
|
||||
raise AddonsNotSupportedError(
|
||||
"Can't rebuild a image based add-on", _LOGGER.error
|
||||
)
|
||||
|
||||
return await addon.rebuild()
|
||||
|
||||
@Job(
|
||||
name="addon_manager_restore",
|
||||
conditions=[
|
||||
JobCondition.FREE_SPACE,
|
||||
JobCondition.INTERNET_HOST,
|
||||
JobCondition.HEALTHY,
|
||||
],
|
||||
on_condition=AddonsJobError,
|
||||
)
|
||||
async def restore(
|
||||
self, slug: str, tar_file: tarfile.TarFile
|
||||
) -> asyncio.Task | None:
|
||||
"""Restore state of an add-on.
|
||||
|
||||
Returns a Task that completes when addon has state 'started' (see addon.start)
|
||||
if addon is started after restore. Else nothing is returned.
|
||||
"""
|
||||
self.sys_jobs.current.reference = slug
|
||||
|
||||
if slug not in self.local:
|
||||
_LOGGER.debug("Add-on %s is not local available for restore", slug)
|
||||
addon = Addon(self.coresys, slug)
|
||||
had_ingress = False
|
||||
else:
|
||||
_LOGGER.debug("Add-on %s is local available for restore", slug)
|
||||
addon = self.local[slug]
|
||||
had_ingress = addon.ingress_panel
|
||||
|
||||
wait_for_start = await addon.restore(tar_file)
|
||||
|
||||
# Check if new
|
||||
if slug not in self.local:
|
||||
_LOGGER.info("Detect new Add-on after restore %s", slug)
|
||||
self.local[slug] = addon
|
||||
|
||||
# Update ingress
|
||||
if had_ingress != addon.ingress_panel:
|
||||
await self.sys_ingress.reload()
|
||||
with suppress(HomeAssistantAPIError):
|
||||
await self.sys_ingress.update_hass_panel(addon)
|
||||
|
||||
return wait_for_start
|
||||
|
||||
@Job(
|
||||
name="addon_manager_repair",
|
||||
conditions=[JobCondition.FREE_SPACE, JobCondition.INTERNET_HOST],
|
||||
)
|
||||
async def repair(self) -> None:
|
||||
"""Repair local add-ons."""
|
||||
needs_repair: list[Addon] = []
|
||||
|
||||
# Evaluate Add-ons to repair
|
||||
for addon in self.installed:
|
||||
if await addon.instance.exists():
|
||||
continue
|
||||
needs_repair.append(addon)
|
||||
|
||||
_LOGGER.info("Found %d add-ons to repair", len(needs_repair))
|
||||
if not needs_repair:
|
||||
return
|
||||
|
||||
for addon in needs_repair:
|
||||
_LOGGER.info("Repairing for add-on: %s", addon.slug)
|
||||
with suppress(DockerError, KeyError):
|
||||
# Need pull a image again
|
||||
if not addon.need_build:
|
||||
await addon.instance.install(addon.version, addon.image)
|
||||
continue
|
||||
|
||||
# Need local lookup
|
||||
if addon.need_build and not addon.is_detached:
|
||||
store = self.store[addon.slug]
|
||||
# If this add-on is available for rebuild
|
||||
if addon.version == store.version:
|
||||
await addon.instance.install(addon.version, addon.image)
|
||||
continue
|
||||
|
||||
_LOGGER.error("Can't repair %s", addon.slug)
|
||||
with suppress(AddonsError):
|
||||
await self.uninstall(addon.slug)
|
||||
|
||||
async def sync_dns(self) -> None:
|
||||
"""Sync add-ons DNS names."""
|
||||
# Update hosts
|
||||
add_host_coros: list[Awaitable[None]] = []
|
||||
for addon in self.installed:
|
||||
try:
|
||||
if not await addon.instance.is_running():
|
||||
continue
|
||||
except DockerError as err:
|
||||
_LOGGER.warning("Add-on %s is corrupt: %s", addon.slug, err)
|
||||
self.sys_resolution.create_issue(
|
||||
IssueType.CORRUPT_DOCKER,
|
||||
ContextType.ADDON,
|
||||
reference=addon.slug,
|
||||
suggestions=[SuggestionType.EXECUTE_REPAIR],
|
||||
)
|
||||
capture_exception(err)
|
||||
else:
|
||||
add_host_coros.append(
|
||||
self.sys_plugins.dns.add_host(
|
||||
ipv4=addon.ip_address, names=[addon.hostname], write=False
|
||||
)
|
||||
)
|
||||
|
||||
await asyncio.gather(*add_host_coros)
|
||||
|
||||
# Write hosts files
|
||||
with suppress(CoreDNSError):
|
||||
await self.sys_plugins.dns.write_hosts()
|
@@ -1,7 +1,7 @@
|
||||
"""Init file for Supervisor add-ons."""
|
||||
from abc import ABC, abstractmethod
|
||||
from collections import defaultdict
|
||||
from collections.abc import Awaitable, Callable
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
import logging
|
||||
from pathlib import Path
|
||||
@@ -85,6 +85,7 @@ from ..docker.const import Capabilities
|
||||
from ..exceptions import AddonsNotSupportedError
|
||||
from ..jobs.const import JOB_GROUP_ADDON
|
||||
from ..jobs.job_group import JobGroup
|
||||
from ..utils import version_is_new_enough
|
||||
from .const import ATTR_BACKUP, ATTR_CODENOTARY, AddonBackupMode
|
||||
from .options import AddonOptions, UiOptions
|
||||
from .validate import RE_SERVICE, RE_VOLUME
|
||||
@@ -645,7 +646,9 @@ class AddonModel(JobGroup, ABC):
|
||||
# Home Assistant
|
||||
version: AwesomeVersion | None = config.get(ATTR_HOMEASSISTANT)
|
||||
with suppress(AwesomeVersionException, TypeError):
|
||||
if self.sys_homeassistant.version < version:
|
||||
if version and not version_is_new_enough(
|
||||
self.sys_homeassistant.version, version
|
||||
):
|
||||
raise AddonsNotSupportedError(
|
||||
f"Add-on {self.slug} not supported on this system, requires Home Assistant version {version} or greater",
|
||||
logger,
|
||||
@@ -669,19 +672,3 @@ class AddonModel(JobGroup, ABC):
|
||||
|
||||
# local build
|
||||
return f"{config[ATTR_REPOSITORY]}/{self.sys_arch.default}-addon-{config[ATTR_SLUG]}"
|
||||
|
||||
def install(self) -> Awaitable[None]:
|
||||
"""Install this add-on."""
|
||||
return self.sys_addons.install(self.slug)
|
||||
|
||||
def uninstall(self) -> Awaitable[None]:
|
||||
"""Uninstall this add-on."""
|
||||
return self.sys_addons.uninstall(self.slug)
|
||||
|
||||
def update(self, backup: bool | None = False) -> Awaitable[Awaitable[None] | None]:
|
||||
"""Update this add-on."""
|
||||
return self.sys_addons.update(self.slug, backup=backup)
|
||||
|
||||
def rebuild(self) -> Awaitable[Awaitable[None] | None]:
|
||||
"""Rebuild this add-on."""
|
||||
return self.sys_addons.rebuild(self.slug)
|
||||
|
@@ -91,6 +91,9 @@ from ..const import (
|
||||
ATTR_VIDEO,
|
||||
ATTR_WATCHDOG,
|
||||
ATTR_WEBUI,
|
||||
MAP_ADDON_CONFIG,
|
||||
MAP_CONFIG,
|
||||
MAP_HOMEASSISTANT_CONFIG,
|
||||
ROLE_ALL,
|
||||
ROLE_DEFAULT,
|
||||
AddonBoot,
|
||||
@@ -114,7 +117,9 @@ from .options import RE_SCHEMA_ELEMENT
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
RE_VOLUME = re.compile(r"^(config|ssl|addons|backup|share|media)(?::(rw|ro))?$")
|
||||
RE_VOLUME = re.compile(
|
||||
r"^(config|ssl|addons|backup|share|media|homeassistant_config|all_addon_configs|addon_config)(?::(rw|ro))?$"
|
||||
)
|
||||
RE_SERVICE = re.compile(r"^(?P<service>mqtt|mysql):(?P<rights>provide|want|need)$")
|
||||
|
||||
|
||||
@@ -143,6 +148,7 @@ RE_MACHINE = re.compile(
|
||||
r"|raspberrypi3"
|
||||
r"|raspberrypi4-64"
|
||||
r"|raspberrypi4"
|
||||
r"|raspberrypi5-64"
|
||||
r"|yellow"
|
||||
r"|green"
|
||||
r"|tinker"
|
||||
@@ -260,6 +266,29 @@ def _migrate_addon_config(protocol=False):
|
||||
name,
|
||||
)
|
||||
|
||||
# 2023-10 "config" became "homeassistant" so /config can be used for addon's public config
|
||||
volumes = [RE_VOLUME.match(entry) for entry in config.get(ATTR_MAP, [])]
|
||||
if any(volume and volume.group(1) == MAP_CONFIG for volume in volumes):
|
||||
if any(
|
||||
volume
|
||||
and volume.group(1) in {MAP_ADDON_CONFIG, MAP_HOMEASSISTANT_CONFIG}
|
||||
for volume in volumes
|
||||
):
|
||||
_LOGGER.warning(
|
||||
"Add-on config using incompatible map options, '%s' and '%s' are ignored if '%s' is included. Please report this to the maintainer of %s",
|
||||
MAP_ADDON_CONFIG,
|
||||
MAP_HOMEASSISTANT_CONFIG,
|
||||
MAP_CONFIG,
|
||||
name,
|
||||
)
|
||||
else:
|
||||
_LOGGER.debug(
|
||||
"Add-on config using deprecated map option '%s' instead of '%s'. Please report this to the maintainer of %s",
|
||||
MAP_CONFIG,
|
||||
MAP_HOMEASSISTANT_CONFIG,
|
||||
name,
|
||||
)
|
||||
|
||||
return config
|
||||
|
||||
return _migrate
|
||||
|
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from aiohttp import web
|
||||
from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispatcher
|
||||
|
||||
from ..const import AddonState
|
||||
from ..coresys import CoreSys, CoreSysAttributes
|
||||
@@ -64,9 +65,10 @@ class RestAPI(CoreSysAttributes):
|
||||
"max_field_size": MAX_LINE_SIZE,
|
||||
},
|
||||
)
|
||||
attach_fast_url_dispatcher(self.webapp, FastUrlDispatcher())
|
||||
|
||||
# service stuff
|
||||
self._runner: web.AppRunner = web.AppRunner(self.webapp)
|
||||
self._runner: web.AppRunner = web.AppRunner(self.webapp, shutdown_timeout=5)
|
||||
self._site: web.TCPSite | None = None
|
||||
|
||||
async def load(self) -> None:
|
||||
@@ -186,6 +188,8 @@ class RestAPI(CoreSysAttributes):
|
||||
# Boards endpoints
|
||||
self.webapp.add_routes(
|
||||
[
|
||||
web.get("/os/boards/green", api_os.boards_green_info),
|
||||
web.post("/os/boards/green", api_os.boards_green_options),
|
||||
web.get("/os/boards/yellow", api_os.boards_yellow_info),
|
||||
web.post("/os/boards/yellow", api_os.boards_yellow_options),
|
||||
web.get("/os/boards/{board}", api_os.boards_other_info),
|
||||
@@ -669,9 +673,7 @@ class RestAPI(CoreSysAttributes):
|
||||
async def start(self) -> None:
|
||||
"""Run RESTful API webserver."""
|
||||
await self._runner.setup()
|
||||
self._site = web.TCPSite(
|
||||
self._runner, host="0.0.0.0", port=80, shutdown_timeout=5
|
||||
)
|
||||
self._site = web.TCPSite(self._runner, host="0.0.0.0", port=80)
|
||||
|
||||
try:
|
||||
await self._site.start()
|
||||
|
@@ -8,8 +8,8 @@ from aiohttp import web
|
||||
import voluptuous as vol
|
||||
from voluptuous.humanize import humanize_error
|
||||
|
||||
from ..addons import AnyAddon
|
||||
from ..addons.addon import Addon
|
||||
from ..addons.manager import AnyAddon
|
||||
from ..addons.utils import rating_security
|
||||
from ..const import (
|
||||
ATTR_ADDONS,
|
||||
@@ -388,7 +388,7 @@ class APIAddons(CoreSysAttributes):
|
||||
def uninstall(self, request: web.Request) -> Awaitable[None]:
|
||||
"""Uninstall add-on."""
|
||||
addon = self._extract_addon(request)
|
||||
return asyncio.shield(addon.uninstall())
|
||||
return asyncio.shield(self.sys_addons.uninstall(addon.slug))
|
||||
|
||||
@api_process
|
||||
async def start(self, request: web.Request) -> None:
|
||||
@@ -414,7 +414,7 @@ class APIAddons(CoreSysAttributes):
|
||||
async def rebuild(self, request: web.Request) -> None:
|
||||
"""Rebuild local build add-on."""
|
||||
addon = self._extract_addon(request)
|
||||
if start_task := await asyncio.shield(addon.rebuild()):
|
||||
if start_task := await asyncio.shield(self.sys_addons.rebuild(addon.slug)):
|
||||
await start_task
|
||||
|
||||
@api_process_raw(CONTENT_TYPE_BINARY)
|
||||
|
@@ -20,6 +20,7 @@ from ..const import (
|
||||
ATTR_DAYS_UNTIL_STALE,
|
||||
ATTR_FOLDERS,
|
||||
ATTR_HOMEASSISTANT,
|
||||
ATTR_HOMEASSISTANT_EXCLUDE_DATABASE,
|
||||
ATTR_LOCATON,
|
||||
ATTR_NAME,
|
||||
ATTR_PASSWORD,
|
||||
@@ -64,6 +65,7 @@ SCHEMA_BACKUP_FULL = vol.Schema(
|
||||
vol.Optional(ATTR_PASSWORD): vol.Maybe(str),
|
||||
vol.Optional(ATTR_COMPRESSED): vol.Maybe(vol.Boolean()),
|
||||
vol.Optional(ATTR_LOCATON): vol.Maybe(str),
|
||||
vol.Optional(ATTR_HOMEASSISTANT_EXCLUDE_DATABASE): vol.Boolean(),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -184,6 +186,7 @@ class APIBackups(CoreSysAttributes):
|
||||
ATTR_ADDONS: data_addons,
|
||||
ATTR_REPOSITORIES: backup.repositories,
|
||||
ATTR_FOLDERS: backup.folders,
|
||||
ATTR_HOMEASSISTANT_EXCLUDE_DATABASE: backup.homeassistant_exclude_database,
|
||||
}
|
||||
|
||||
def _location_to_mount(self, body: dict[str, Any]) -> dict[str, Any]:
|
||||
|
@@ -23,7 +23,6 @@ ATTR_CONNECTION_BUS = "connection_bus"
|
||||
ATTR_DATA_DISK = "data_disk"
|
||||
ATTR_DEVICE = "device"
|
||||
ATTR_DEV_PATH = "dev_path"
|
||||
ATTR_DISK_LED = "disk_led"
|
||||
ATTR_DISKS = "disks"
|
||||
ATTR_DRIVES = "drives"
|
||||
ATTR_DT_SYNCHRONIZED = "dt_synchronized"
|
||||
@@ -31,7 +30,6 @@ ATTR_DT_UTC = "dt_utc"
|
||||
ATTR_EJECTABLE = "ejectable"
|
||||
ATTR_FALLBACK = "fallback"
|
||||
ATTR_FILESYSTEMS = "filesystems"
|
||||
ATTR_HEARTBEAT_LED = "heartbeat_led"
|
||||
ATTR_IDENTIFIERS = "identifiers"
|
||||
ATTR_JOBS = "jobs"
|
||||
ATTR_LLMNR = "llmnr"
|
||||
@@ -41,7 +39,6 @@ ATTR_MODEL = "model"
|
||||
ATTR_MOUNTS = "mounts"
|
||||
ATTR_MOUNT_POINTS = "mount_points"
|
||||
ATTR_PANEL_PATH = "panel_path"
|
||||
ATTR_POWER_LED = "power_led"
|
||||
ATTR_REMOVABLE = "removable"
|
||||
ATTR_REVISION = "revision"
|
||||
ATTR_SEAT = "seat"
|
||||
@@ -49,6 +46,7 @@ ATTR_SIGNED = "signed"
|
||||
ATTR_STARTUP_TIME = "startup_time"
|
||||
ATTR_SUBSYSTEM = "subsystem"
|
||||
ATTR_SYSFS = "sysfs"
|
||||
ATTR_SYSTEM_HEALTH_LED = "system_health_led"
|
||||
ATTR_TIME_DETECTED = "time_detected"
|
||||
ATTR_UPDATE_TYPE = "update_type"
|
||||
ATTR_USE_NTP = "use_ntp"
|
||||
|
@@ -12,6 +12,7 @@ from ..const import (
|
||||
ATTR_AUDIO_INPUT,
|
||||
ATTR_AUDIO_OUTPUT,
|
||||
ATTR_BACKUP,
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE,
|
||||
ATTR_BLK_READ,
|
||||
ATTR_BLK_WRITE,
|
||||
ATTR_BOOT,
|
||||
@@ -51,6 +52,7 @@ SCHEMA_OPTIONS = vol.Schema(
|
||||
vol.Optional(ATTR_REFRESH_TOKEN): vol.Maybe(str),
|
||||
vol.Optional(ATTR_AUDIO_OUTPUT): vol.Maybe(str),
|
||||
vol.Optional(ATTR_AUDIO_INPUT): vol.Maybe(str),
|
||||
vol.Optional(ATTR_BACKUPS_EXCLUDE_DATABASE): vol.Boolean(),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -82,6 +84,7 @@ class APIHomeAssistant(CoreSysAttributes):
|
||||
ATTR_WATCHDOG: self.sys_homeassistant.watchdog,
|
||||
ATTR_AUDIO_INPUT: self.sys_homeassistant.audio_input,
|
||||
ATTR_AUDIO_OUTPUT: self.sys_homeassistant.audio_output,
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE: self.sys_homeassistant.backups_exclude_database,
|
||||
}
|
||||
|
||||
@api_process
|
||||
@@ -113,6 +116,11 @@ class APIHomeAssistant(CoreSysAttributes):
|
||||
if ATTR_AUDIO_OUTPUT in body:
|
||||
self.sys_homeassistant.audio_output = body[ATTR_AUDIO_OUTPUT]
|
||||
|
||||
if ATTR_BACKUPS_EXCLUDE_DATABASE in body:
|
||||
self.sys_homeassistant.backups_exclude_database = body[
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE
|
||||
]
|
||||
|
||||
self.sys_homeassistant.save_data()
|
||||
|
||||
@api_process
|
||||
|
@@ -48,6 +48,29 @@ SCHEMA_INGRESS_CREATE_SESSION_DATA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
# from https://github.com/aio-libs/aiohttp/blob/8ae650bee4add9f131d49b96a0a150311ea58cd1/aiohttp/helpers.py#L1059C1-L1079C1
|
||||
def must_be_empty_body(method: str, code: int) -> bool:
|
||||
"""Check if a request must return an empty body."""
|
||||
return (
|
||||
status_code_must_be_empty_body(code)
|
||||
or method_must_be_empty_body(method)
|
||||
or (200 <= code < 300 and method.upper() == hdrs.METH_CONNECT)
|
||||
)
|
||||
|
||||
|
||||
def method_must_be_empty_body(method: str) -> bool:
|
||||
"""Check if a method must return an empty body."""
|
||||
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.1
|
||||
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.2
|
||||
return method.upper() == hdrs.METH_HEAD
|
||||
|
||||
|
||||
def status_code_must_be_empty_body(code: int) -> bool:
|
||||
"""Check if a status code must return an empty body."""
|
||||
# https://datatracker.ietf.org/doc/html/rfc9112#section-6.3-2.1
|
||||
return code in {204, 304} or 100 <= code < 200
|
||||
|
||||
|
||||
class APIIngress(CoreSysAttributes):
|
||||
"""Ingress view to handle add-on webui routing."""
|
||||
|
||||
@@ -225,10 +248,18 @@ class APIIngress(CoreSysAttributes):
|
||||
skip_auto_headers={hdrs.CONTENT_TYPE},
|
||||
) as result:
|
||||
headers = _response_header(result)
|
||||
|
||||
# Avoid parsing content_type in simple cases for better performance
|
||||
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
|
||||
content_type = (maybe_content_type.partition(";"))[0].strip()
|
||||
else:
|
||||
content_type = result.content_type
|
||||
# Simple request
|
||||
if (
|
||||
hdrs.CONTENT_LENGTH in result.headers
|
||||
# empty body responses should not be streamed,
|
||||
# otherwise aiohttp < 3.9.0 may generate
|
||||
# an invalid "0\r\n\r\n" chunk instead of an empty response.
|
||||
must_be_empty_body(request.method, result.status)
|
||||
or hdrs.CONTENT_LENGTH in result.headers
|
||||
and int(result.headers.get(hdrs.CONTENT_LENGTH, 0)) < 4_194_000
|
||||
):
|
||||
# Return Response
|
||||
@@ -236,13 +267,13 @@ class APIIngress(CoreSysAttributes):
|
||||
return web.Response(
|
||||
headers=headers,
|
||||
status=result.status,
|
||||
content_type=result.content_type,
|
||||
content_type=content_type,
|
||||
body=body,
|
||||
)
|
||||
|
||||
# Stream response
|
||||
response = web.StreamResponse(status=result.status, headers=headers)
|
||||
response.content_type = result.content_type
|
||||
response.content_type = content_type
|
||||
|
||||
try:
|
||||
await response.prepare(request)
|
||||
|
@@ -19,6 +19,7 @@ from ...const import (
|
||||
CoreState,
|
||||
)
|
||||
from ...coresys import CoreSys, CoreSysAttributes
|
||||
from ...utils import version_is_new_enough
|
||||
from ..utils import api_return_error, excract_supervisor_token
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
@@ -273,9 +274,8 @@ class SecurityMiddleware(CoreSysAttributes):
|
||||
@middleware
|
||||
async def core_proxy(self, request: Request, handler: RequestHandler) -> Response:
|
||||
"""Validate user from Core API proxy."""
|
||||
if (
|
||||
request[REQUEST_FROM] != self.sys_homeassistant
|
||||
or self.sys_homeassistant.version >= _CORE_VERSION
|
||||
if request[REQUEST_FROM] != self.sys_homeassistant or version_is_new_enough(
|
||||
self.sys_homeassistant.version, _CORE_VERSION
|
||||
):
|
||||
return await handler(request)
|
||||
|
||||
|
@@ -8,11 +8,15 @@ from aiohttp import web
|
||||
import voluptuous as vol
|
||||
|
||||
from ..const import (
|
||||
ATTR_ACTIVITY_LED,
|
||||
ATTR_BOARD,
|
||||
ATTR_BOOT,
|
||||
ATTR_DEVICES,
|
||||
ATTR_DISK_LED,
|
||||
ATTR_HEARTBEAT_LED,
|
||||
ATTR_ID,
|
||||
ATTR_NAME,
|
||||
ATTR_POWER_LED,
|
||||
ATTR_SERIAL,
|
||||
ATTR_SIZE,
|
||||
ATTR_UPDATE_AVAILABLE,
|
||||
@@ -27,21 +31,19 @@ from .const import (
|
||||
ATTR_DATA_DISK,
|
||||
ATTR_DEV_PATH,
|
||||
ATTR_DEVICE,
|
||||
ATTR_DISK_LED,
|
||||
ATTR_DISKS,
|
||||
ATTR_HEARTBEAT_LED,
|
||||
ATTR_MODEL,
|
||||
ATTR_POWER_LED,
|
||||
ATTR_SYSTEM_HEALTH_LED,
|
||||
ATTR_VENDOR,
|
||||
)
|
||||
from .utils import api_process, api_validate
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
# pylint: disable=no-value-for-parameter
|
||||
SCHEMA_VERSION = vol.Schema({vol.Optional(ATTR_VERSION): version_tag})
|
||||
SCHEMA_DISK = vol.Schema({vol.Required(ATTR_DEVICE): str})
|
||||
|
||||
# pylint: disable=no-value-for-parameter
|
||||
SCHEMA_YELLOW_OPTIONS = vol.Schema(
|
||||
{
|
||||
vol.Optional(ATTR_DISK_LED): vol.Boolean(),
|
||||
@@ -49,6 +51,14 @@ SCHEMA_YELLOW_OPTIONS = vol.Schema(
|
||||
vol.Optional(ATTR_POWER_LED): vol.Boolean(),
|
||||
}
|
||||
)
|
||||
SCHEMA_GREEN_OPTIONS = vol.Schema(
|
||||
{
|
||||
vol.Optional(ATTR_ACTIVITY_LED): vol.Boolean(),
|
||||
vol.Optional(ATTR_POWER_LED): vol.Boolean(),
|
||||
vol.Optional(ATTR_SYSTEM_HEALTH_LED): vol.Boolean(),
|
||||
}
|
||||
)
|
||||
# pylint: enable=no-value-for-parameter
|
||||
|
||||
|
||||
class APIOS(CoreSysAttributes):
|
||||
@@ -105,6 +115,31 @@ class APIOS(CoreSysAttributes):
|
||||
],
|
||||
}
|
||||
|
||||
@api_process
|
||||
async def boards_green_info(self, request: web.Request) -> dict[str, Any]:
|
||||
"""Get green board settings."""
|
||||
return {
|
||||
ATTR_ACTIVITY_LED: self.sys_dbus.agent.board.green.activity_led,
|
||||
ATTR_POWER_LED: self.sys_dbus.agent.board.green.power_led,
|
||||
ATTR_SYSTEM_HEALTH_LED: self.sys_dbus.agent.board.green.user_led,
|
||||
}
|
||||
|
||||
@api_process
|
||||
async def boards_green_options(self, request: web.Request) -> None:
|
||||
"""Update green board settings."""
|
||||
body = await api_validate(SCHEMA_GREEN_OPTIONS, request)
|
||||
|
||||
if ATTR_ACTIVITY_LED in body:
|
||||
self.sys_dbus.agent.board.green.activity_led = body[ATTR_ACTIVITY_LED]
|
||||
|
||||
if ATTR_POWER_LED in body:
|
||||
self.sys_dbus.agent.board.green.power_led = body[ATTR_POWER_LED]
|
||||
|
||||
if ATTR_SYSTEM_HEALTH_LED in body:
|
||||
self.sys_dbus.agent.board.green.user_led = body[ATTR_SYSTEM_HEALTH_LED]
|
||||
|
||||
self.sys_dbus.agent.board.green.save_data()
|
||||
|
||||
@api_process
|
||||
async def boards_yellow_info(self, request: web.Request) -> dict[str, Any]:
|
||||
"""Get yellow board settings."""
|
||||
@@ -128,6 +163,7 @@ class APIOS(CoreSysAttributes):
|
||||
if ATTR_POWER_LED in body:
|
||||
self.sys_dbus.agent.board.yellow.power_led = body[ATTR_POWER_LED]
|
||||
|
||||
self.sys_dbus.agent.board.yellow.save_data()
|
||||
self.sys_resolution.create_issue(
|
||||
IssueType.REBOOT_REQUIRED,
|
||||
ContextType.SYSTEM,
|
||||
|
@@ -6,7 +6,10 @@ import logging
|
||||
import aiohttp
|
||||
from aiohttp import web
|
||||
from aiohttp.client_exceptions import ClientConnectorError
|
||||
from aiohttp.client_ws import ClientWebSocketResponse
|
||||
from aiohttp.hdrs import AUTHORIZATION, CONTENT_TYPE
|
||||
from aiohttp.http import WSMessage
|
||||
from aiohttp.http_websocket import WSMsgType
|
||||
from aiohttp.web_exceptions import HTTPBadGateway, HTTPUnauthorized
|
||||
|
||||
from ..coresys import CoreSysAttributes
|
||||
@@ -74,7 +77,7 @@ class APIProxy(CoreSysAttributes):
|
||||
_LOGGER.error("Error on API for request %s", path)
|
||||
except aiohttp.ClientError as err:
|
||||
_LOGGER.error("Client error on API %s request %s", path, err)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.error("Client timeout error on API request %s", path)
|
||||
|
||||
raise HTTPBadGateway()
|
||||
@@ -114,7 +117,7 @@ class APIProxy(CoreSysAttributes):
|
||||
body=data, status=client.status, content_type=client.content_type
|
||||
)
|
||||
|
||||
async def _websocket_client(self):
|
||||
async def _websocket_client(self) -> ClientWebSocketResponse:
|
||||
"""Initialize a WebSocket API connection."""
|
||||
url = f"{self.sys_homeassistant.api_url}/api/websocket"
|
||||
|
||||
@@ -167,6 +170,25 @@ class APIProxy(CoreSysAttributes):
|
||||
|
||||
raise APIError()
|
||||
|
||||
async def _proxy_message(
|
||||
self,
|
||||
read_task: asyncio.Task,
|
||||
target: web.WebSocketResponse | ClientWebSocketResponse,
|
||||
) -> None:
|
||||
"""Proxy a message from client to server or vice versa."""
|
||||
if read_task.exception():
|
||||
raise read_task.exception()
|
||||
|
||||
msg: WSMessage = read_task.result()
|
||||
if msg.type == WSMsgType.TEXT:
|
||||
return await target.send_str(msg.data)
|
||||
if msg.type == WSMsgType.BINARY:
|
||||
return await target.send_bytes(msg.data)
|
||||
|
||||
raise TypeError(
|
||||
f"Cannot proxy websocket message of unsupported type: {msg.type}"
|
||||
)
|
||||
|
||||
async def websocket(self, request: web.Request):
|
||||
"""Initialize a WebSocket API connection."""
|
||||
if not await self.sys_homeassistant.api.check_api_state():
|
||||
@@ -214,13 +236,13 @@ class APIProxy(CoreSysAttributes):
|
||||
|
||||
_LOGGER.info("Home Assistant WebSocket API request running")
|
||||
try:
|
||||
client_read = None
|
||||
server_read = None
|
||||
client_read: asyncio.Task | None = None
|
||||
server_read: asyncio.Task | None = None
|
||||
while not server.closed and not client.closed:
|
||||
if not client_read:
|
||||
client_read = self.sys_create_task(client.receive_str())
|
||||
client_read = self.sys_create_task(client.receive())
|
||||
if not server_read:
|
||||
server_read = self.sys_create_task(server.receive_str())
|
||||
server_read = self.sys_create_task(server.receive())
|
||||
|
||||
# wait until data need to be processed
|
||||
await asyncio.wait(
|
||||
@@ -229,14 +251,12 @@ class APIProxy(CoreSysAttributes):
|
||||
|
||||
# server
|
||||
if server_read.done() and not client.closed:
|
||||
server_read.exception()
|
||||
await client.send_str(server_read.result())
|
||||
await self._proxy_message(server_read, client)
|
||||
server_read = None
|
||||
|
||||
# client
|
||||
if client_read.done() and not server.closed:
|
||||
client_read.exception()
|
||||
await server.send_str(client_read.result())
|
||||
await self._proxy_message(client_read, server)
|
||||
client_read = None
|
||||
|
||||
except asyncio.CancelledError:
|
||||
@@ -246,9 +266,9 @@ class APIProxy(CoreSysAttributes):
|
||||
_LOGGER.info("Home Assistant WebSocket API error: %s", err)
|
||||
|
||||
finally:
|
||||
if client_read:
|
||||
if client_read and not client_read.done():
|
||||
client_read.cancel()
|
||||
if server_read:
|
||||
if server_read and not server_read.done():
|
||||
server_read.cancel()
|
||||
|
||||
# close connections
|
||||
|
@@ -6,7 +6,7 @@ from typing import Any
|
||||
from aiohttp import web
|
||||
import voluptuous as vol
|
||||
|
||||
from ..addons import AnyAddon
|
||||
from ..addons.manager import AnyAddon
|
||||
from ..addons.utils import rating_security
|
||||
from ..api.const import ATTR_SIGNED
|
||||
from ..api.utils import api_process, api_process_raw, api_validate
|
||||
@@ -186,18 +186,20 @@ class APIStore(CoreSysAttributes):
|
||||
}
|
||||
|
||||
@api_process
|
||||
async def addons_list(self, request: web.Request) -> list[dict[str, Any]]:
|
||||
async def addons_list(self, request: web.Request) -> dict[str, Any]:
|
||||
"""Return all store add-ons."""
|
||||
return [
|
||||
self._generate_addon_information(self.sys_addons.store[addon])
|
||||
for addon in self.sys_addons.store
|
||||
]
|
||||
return {
|
||||
ATTR_ADDONS: [
|
||||
self._generate_addon_information(self.sys_addons.store[addon])
|
||||
for addon in self.sys_addons.store
|
||||
]
|
||||
}
|
||||
|
||||
@api_process
|
||||
def addons_addon_install(self, request: web.Request) -> Awaitable[None]:
|
||||
"""Install add-on."""
|
||||
addon = self._extract_addon(request)
|
||||
return asyncio.shield(addon.install())
|
||||
return asyncio.shield(self.sys_addons.install(addon.slug))
|
||||
|
||||
@api_process
|
||||
async def addons_addon_update(self, request: web.Request) -> None:
|
||||
@@ -209,7 +211,7 @@ class APIStore(CoreSysAttributes):
|
||||
body = await api_validate(SCHEMA_UPDATE, request)
|
||||
|
||||
if start_task := await asyncio.shield(
|
||||
addon.update(backup=body.get(ATTR_BACKUP))
|
||||
self.sys_addons.update(addon.slug, backup=body.get(ATTR_BACKUP))
|
||||
):
|
||||
await start_task
|
||||
|
||||
|
@@ -28,6 +28,7 @@ class CpuArch(CoreSysAttributes):
|
||||
"""Initialize CPU Architecture handler."""
|
||||
self.coresys = coresys
|
||||
self._supported_arch: list[str] = []
|
||||
self._supported_set: set[str] = set()
|
||||
self._default_arch: str
|
||||
|
||||
@property
|
||||
@@ -70,9 +71,11 @@ class CpuArch(CoreSysAttributes):
|
||||
if native_support not in self._supported_arch:
|
||||
self._supported_arch.append(native_support)
|
||||
|
||||
self._supported_set = set(self._supported_arch)
|
||||
|
||||
def is_supported(self, arch_list: list[str]) -> bool:
|
||||
"""Return True if there is a supported arch by this platform."""
|
||||
return not set(self.supported).isdisjoint(set(arch_list))
|
||||
return not self._supported_set.isdisjoint(arch_list)
|
||||
|
||||
def match(self, arch_list: list[str]) -> str:
|
||||
"""Return best match for this CPU/Platform."""
|
||||
|
@@ -1,4 +1,5 @@
|
||||
"""Representation of a backup file."""
|
||||
import asyncio
|
||||
from base64 import b64decode, b64encode
|
||||
from collections.abc import Awaitable
|
||||
from datetime import timedelta
|
||||
@@ -18,13 +19,14 @@ from securetar import SecureTarFile, atomic_contents_add, secure_path
|
||||
import voluptuous as vol
|
||||
from voluptuous.humanize import humanize_error
|
||||
|
||||
from ..addons import Addon
|
||||
from ..addons.manager import Addon
|
||||
from ..const import (
|
||||
ATTR_ADDONS,
|
||||
ATTR_COMPRESSED,
|
||||
ATTR_CRYPTO,
|
||||
ATTR_DATE,
|
||||
ATTR_DOCKER,
|
||||
ATTR_EXCLUDE_DATABASE,
|
||||
ATTR_FOLDERS,
|
||||
ATTR_HOMEASSISTANT,
|
||||
ATTR_NAME,
|
||||
@@ -129,7 +131,14 @@ class Backup(CoreSysAttributes):
|
||||
"""Return backup Home Assistant version."""
|
||||
if self.homeassistant is None:
|
||||
return None
|
||||
return self._data[ATTR_HOMEASSISTANT][ATTR_VERSION]
|
||||
return self.homeassistant[ATTR_VERSION]
|
||||
|
||||
@property
|
||||
def homeassistant_exclude_database(self) -> bool:
|
||||
"""Return whether database was excluded from Home Assistant backup."""
|
||||
if self.homeassistant is None:
|
||||
return None
|
||||
return self.homeassistant[ATTR_EXCLUDE_DATABASE]
|
||||
|
||||
@property
|
||||
def homeassistant(self):
|
||||
@@ -340,14 +349,14 @@ class Backup(CoreSysAttributes):
|
||||
finally:
|
||||
self._tmp.cleanup()
|
||||
|
||||
async def store_addons(self, addon_list: list[str]) -> list[Awaitable[None]]:
|
||||
async def store_addons(self, addon_list: list[str]) -> list[asyncio.Task]:
|
||||
"""Add a list of add-ons into backup.
|
||||
|
||||
For each addon that needs to be started after backup, returns a task which
|
||||
For each addon that needs to be started after backup, returns a Task which
|
||||
completes when that addon has state 'started' (see addon.start).
|
||||
"""
|
||||
|
||||
async def _addon_save(addon: Addon) -> Awaitable[None] | None:
|
||||
async def _addon_save(addon: Addon) -> asyncio.Task | None:
|
||||
"""Task to store an add-on into backup."""
|
||||
tar_name = f"{addon.slug}.tar{'.gz' if self.compressed else ''}"
|
||||
addon_file = SecureTarFile(
|
||||
@@ -379,7 +388,7 @@ class Backup(CoreSysAttributes):
|
||||
|
||||
# Save Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
start_tasks: list[Awaitable[None]] = []
|
||||
start_tasks: list[asyncio.Task] = []
|
||||
for addon in addon_list:
|
||||
try:
|
||||
if start_task := await _addon_save(addon):
|
||||
@@ -389,10 +398,10 @@ class Backup(CoreSysAttributes):
|
||||
|
||||
return start_tasks
|
||||
|
||||
async def restore_addons(self, addon_list: list[str]) -> list[Awaitable[None]]:
|
||||
async def restore_addons(self, addon_list: list[str]) -> list[asyncio.Task]:
|
||||
"""Restore a list add-on from backup."""
|
||||
|
||||
async def _addon_restore(addon_slug: str) -> Awaitable[None] | None:
|
||||
async def _addon_restore(addon_slug: str) -> asyncio.Task | None:
|
||||
"""Task to restore an add-on into backup."""
|
||||
tar_name = f"{addon_slug}.tar{'.gz' if self.compressed else ''}"
|
||||
addon_file = SecureTarFile(
|
||||
@@ -416,7 +425,7 @@ class Backup(CoreSysAttributes):
|
||||
|
||||
# Save Add-ons sequential
|
||||
# avoid issue on slow IO
|
||||
start_tasks: list[Awaitable[None]] = []
|
||||
start_tasks: list[asyncio.Task] = []
|
||||
for slug in addon_list:
|
||||
try:
|
||||
if start_task := await _addon_restore(slug):
|
||||
@@ -490,6 +499,18 @@ class Backup(CoreSysAttributes):
|
||||
_LOGGER.warning("Can't find restore folder %s", name)
|
||||
return
|
||||
|
||||
# Unmount any mounts within folder
|
||||
bind_mounts = [
|
||||
bound.bind_mount
|
||||
for bound in self.sys_mounts.bound_mounts
|
||||
if bound.bind_mount.local_where
|
||||
and bound.bind_mount.local_where.is_relative_to(origin_dir)
|
||||
]
|
||||
if bind_mounts:
|
||||
await asyncio.gather(
|
||||
*[bind_mount.unmount() for bind_mount in bind_mounts]
|
||||
)
|
||||
|
||||
# Clean old stuff
|
||||
if origin_dir.is_dir():
|
||||
await remove_folder(origin_dir, content_only=True)
|
||||
@@ -510,7 +531,13 @@ class Backup(CoreSysAttributes):
|
||||
except (tarfile.TarError, OSError) as err:
|
||||
_LOGGER.warning("Can't restore folder %s: %s", name, err)
|
||||
|
||||
await self.sys_run_in_executor(_restore)
|
||||
try:
|
||||
await self.sys_run_in_executor(_restore)
|
||||
finally:
|
||||
if bind_mounts:
|
||||
await asyncio.gather(
|
||||
*[bind_mount.mount() for bind_mount in bind_mounts]
|
||||
)
|
||||
|
||||
# Restore folder sequential
|
||||
# avoid issue on slow IO
|
||||
@@ -520,9 +547,12 @@ class Backup(CoreSysAttributes):
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.warning("Can't restore folder %s: %s", folder, err)
|
||||
|
||||
async def store_homeassistant(self):
|
||||
"""Backup Home Assitant Core configuration folder."""
|
||||
self._data[ATTR_HOMEASSISTANT] = {ATTR_VERSION: self.sys_homeassistant.version}
|
||||
async def store_homeassistant(self, exclude_database: bool = False):
|
||||
"""Backup Home Assistant Core configuration folder."""
|
||||
self._data[ATTR_HOMEASSISTANT] = {
|
||||
ATTR_VERSION: self.sys_homeassistant.version,
|
||||
ATTR_EXCLUDE_DATABASE: exclude_database,
|
||||
}
|
||||
|
||||
# Backup Home Assistant Core config directory
|
||||
tar_name = Path(
|
||||
@@ -532,13 +562,13 @@ class Backup(CoreSysAttributes):
|
||||
tar_name, "w", key=self._key, gzip=self.compressed, bufsize=BUF_SIZE
|
||||
)
|
||||
|
||||
await self.sys_homeassistant.backup(homeassistant_file)
|
||||
await self.sys_homeassistant.backup(homeassistant_file, exclude_database)
|
||||
|
||||
# Store size
|
||||
self.homeassistant[ATTR_SIZE] = homeassistant_file.size
|
||||
|
||||
async def restore_homeassistant(self) -> Awaitable[None]:
|
||||
"""Restore Home Assitant Core configuration folder."""
|
||||
"""Restore Home Assistant Core configuration folder."""
|
||||
await self.sys_homeassistant.core.stop()
|
||||
|
||||
# Restore Home Assistant Core config directory
|
||||
@@ -549,7 +579,9 @@ class Backup(CoreSysAttributes):
|
||||
tar_name, "r", key=self._key, gzip=self.compressed, bufsize=BUF_SIZE
|
||||
)
|
||||
|
||||
await self.sys_homeassistant.restore(homeassistant_file)
|
||||
await self.sys_homeassistant.restore(
|
||||
homeassistant_file, self.homeassistant_exclude_database
|
||||
)
|
||||
|
||||
# Generate restore task
|
||||
async def _core_update():
|
||||
|
@@ -226,6 +226,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
addon_list: list[Addon],
|
||||
folder_list: list[str],
|
||||
homeassistant: bool,
|
||||
homeassistant_exclude_database: bool | None,
|
||||
) -> Backup | None:
|
||||
"""Create a backup.
|
||||
|
||||
@@ -245,7 +246,11 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
# HomeAssistant Folder is for v1
|
||||
if homeassistant:
|
||||
self._change_stage(BackupJobStage.HOME_ASSISTANT, backup)
|
||||
await backup.store_homeassistant()
|
||||
await backup.store_homeassistant(
|
||||
self.sys_homeassistant.backups_exclude_database
|
||||
if homeassistant_exclude_database is None
|
||||
else homeassistant_exclude_database
|
||||
)
|
||||
|
||||
# Backup folders
|
||||
if folder_list:
|
||||
@@ -272,7 +277,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
|
||||
@Job(
|
||||
name="backup_manager_full_backup",
|
||||
conditions=[JobCondition.FREE_SPACE, JobCondition.RUNNING],
|
||||
conditions=[JobCondition.RUNNING],
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=BackupJobError,
|
||||
)
|
||||
@@ -282,15 +287,25 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
password: str | None = None,
|
||||
compressed: bool = True,
|
||||
location: Mount | type[DEFAULT] | None = DEFAULT,
|
||||
homeassistant_exclude_database: bool | None = None,
|
||||
) -> Backup | None:
|
||||
"""Create a full backup."""
|
||||
if self._get_base_path(location) == self.sys_config.path_backup:
|
||||
await Job.check_conditions(
|
||||
self, {JobCondition.FREE_SPACE}, "BackupManager.do_backup_full"
|
||||
)
|
||||
|
||||
backup = self._create_backup(
|
||||
name, BackupType.FULL, password, compressed, location
|
||||
)
|
||||
|
||||
_LOGGER.info("Creating new full backup with slug %s", backup.slug)
|
||||
backup = await self._do_backup(
|
||||
backup, self.sys_addons.installed, ALL_FOLDERS, True
|
||||
backup,
|
||||
self.sys_addons.installed,
|
||||
ALL_FOLDERS,
|
||||
True,
|
||||
homeassistant_exclude_database,
|
||||
)
|
||||
if backup:
|
||||
_LOGGER.info("Creating full backup with slug %s completed", backup.slug)
|
||||
@@ -298,7 +313,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
|
||||
@Job(
|
||||
name="backup_manager_partial_backup",
|
||||
conditions=[JobCondition.FREE_SPACE, JobCondition.RUNNING],
|
||||
conditions=[JobCondition.RUNNING],
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
on_condition=BackupJobError,
|
||||
)
|
||||
@@ -311,8 +326,14 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
homeassistant: bool = False,
|
||||
compressed: bool = True,
|
||||
location: Mount | type[DEFAULT] | None = DEFAULT,
|
||||
homeassistant_exclude_database: bool | None = None,
|
||||
) -> Backup | None:
|
||||
"""Create a partial backup."""
|
||||
if self._get_base_path(location) == self.sys_config.path_backup:
|
||||
await Job.check_conditions(
|
||||
self, {JobCondition.FREE_SPACE}, "BackupManager.do_backup_partial"
|
||||
)
|
||||
|
||||
addons = addons or []
|
||||
folders = folders or []
|
||||
|
||||
@@ -337,7 +358,9 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
continue
|
||||
_LOGGER.warning("Add-on %s not found/installed", addon_slug)
|
||||
|
||||
backup = await self._do_backup(backup, addon_list, folders, homeassistant)
|
||||
backup = await self._do_backup(
|
||||
backup, addon_list, folders, homeassistant, homeassistant_exclude_database
|
||||
)
|
||||
if backup:
|
||||
_LOGGER.info("Creating partial backup with slug %s completed", backup.slug)
|
||||
return backup
|
||||
@@ -383,7 +406,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
# Remove Add-on because it's not a part of the new env
|
||||
# Do it sequential avoid issue on slow IO
|
||||
try:
|
||||
await addon.uninstall()
|
||||
await self.sys_addons.uninstall(addon.slug)
|
||||
except AddonsError:
|
||||
_LOGGER.warning("Can't uninstall Add-on %s", addon.slug)
|
||||
|
||||
@@ -582,7 +605,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
try:
|
||||
try:
|
||||
await asyncio.wait_for(self._thaw_event.wait(), timeout)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.warning(
|
||||
"Timeout waiting for signal to thaw after manual freeze, beginning thaw now"
|
||||
)
|
||||
@@ -591,7 +614,7 @@ class BackupManager(FileConfiguration, JobGroup):
|
||||
await self.sys_homeassistant.end_backup()
|
||||
|
||||
self._change_stage(BackupJobStage.ADDONS)
|
||||
addon_start_tasks: list[Awaitable[None]] = [
|
||||
addon_start_tasks: list[asyncio.Task] = [
|
||||
task
|
||||
for task in await asyncio.gather(
|
||||
*[addon.end_backup() for addon in running_addons]
|
||||
|
@@ -14,6 +14,7 @@ from ..const import (
|
||||
ATTR_DATE,
|
||||
ATTR_DAYS_UNTIL_STALE,
|
||||
ATTR_DOCKER,
|
||||
ATTR_EXCLUDE_DATABASE,
|
||||
ATTR_FOLDERS,
|
||||
ATTR_HOMEASSISTANT,
|
||||
ATTR_NAME,
|
||||
@@ -103,6 +104,9 @@ SCHEMA_BACKUP = vol.Schema(
|
||||
{
|
||||
vol.Required(ATTR_VERSION): version_tag,
|
||||
vol.Optional(ATTR_SIZE, default=0): vol.Coerce(float),
|
||||
vol.Optional(
|
||||
ATTR_EXCLUDE_DATABASE, default=False
|
||||
): vol.Boolean(),
|
||||
},
|
||||
extra=vol.REMOVE_EXTRA,
|
||||
)
|
||||
|
@@ -6,7 +6,7 @@ import signal
|
||||
|
||||
from colorlog import ColoredFormatter
|
||||
|
||||
from .addons import AddonManager
|
||||
from .addons.manager import AddonManager
|
||||
from .api import RestAPI
|
||||
from .arch import CpuArch
|
||||
from .auth import Auth
|
||||
@@ -221,6 +221,14 @@ def initialize_system(coresys: CoreSys) -> None:
|
||||
)
|
||||
config.path_emergency.mkdir()
|
||||
|
||||
# Addon Configs folder
|
||||
if not config.path_addon_configs.is_dir():
|
||||
_LOGGER.debug(
|
||||
"Creating Supervisor add-on configs folder at '%s'",
|
||||
config.path_addon_configs,
|
||||
)
|
||||
config.path_addon_configs.mkdir()
|
||||
|
||||
|
||||
def migrate_system_env(coresys: CoreSys) -> None:
|
||||
"""Cleanup some stuff after update."""
|
||||
|
@@ -48,6 +48,7 @@ MEDIA_DATA = PurePath("media")
|
||||
MOUNTS_FOLDER = PurePath("mounts")
|
||||
MOUNTS_CREDENTIALS = PurePath(".mounts_credentials")
|
||||
EMERGENCY_DATA = PurePath("emergency")
|
||||
ADDON_CONFIGS = PurePath("addon_configs")
|
||||
|
||||
DEFAULT_BOOT_TIME = datetime.utcfromtimestamp(0).isoformat()
|
||||
|
||||
@@ -231,6 +232,16 @@ class CoreConfig(FileConfiguration):
|
||||
"""Return root add-on data folder external for Docker."""
|
||||
return PurePath(self.path_extern_supervisor, ADDONS_DATA)
|
||||
|
||||
@property
|
||||
def path_addon_configs(self) -> Path:
|
||||
"""Return root Add-on configs folder."""
|
||||
return self.path_supervisor / ADDON_CONFIGS
|
||||
|
||||
@property
|
||||
def path_extern_addon_configs(self) -> PurePath:
|
||||
"""Return root Add-on configs folder external for Docker."""
|
||||
return PurePath(self.path_extern_supervisor, ADDON_CONFIGS)
|
||||
|
||||
@property
|
||||
def path_audio(self) -> Path:
|
||||
"""Return root audio data folder."""
|
||||
|
@@ -4,6 +4,7 @@ from enum import StrEnum
|
||||
from ipaddress import ip_network
|
||||
from pathlib import Path
|
||||
from sys import version_info as systemversion
|
||||
from typing import Self
|
||||
|
||||
from aiohttp import __version__ as aiohttpversion
|
||||
|
||||
@@ -19,6 +20,7 @@ SUPERVISOR_DATA = Path("/data")
|
||||
FILE_HASSIO_ADDONS = Path(SUPERVISOR_DATA, "addons.json")
|
||||
FILE_HASSIO_AUTH = Path(SUPERVISOR_DATA, "auth.json")
|
||||
FILE_HASSIO_BACKUPS = Path(SUPERVISOR_DATA, "backups.json")
|
||||
FILE_HASSIO_BOARD = Path(SUPERVISOR_DATA, "board.json")
|
||||
FILE_HASSIO_CONFIG = Path(SUPERVISOR_DATA, "config.json")
|
||||
FILE_HASSIO_DISCOVERY = Path(SUPERVISOR_DATA, "discovery.json")
|
||||
FILE_HASSIO_DOCKER = Path(SUPERVISOR_DATA, "docker.json")
|
||||
@@ -88,6 +90,7 @@ REQUEST_FROM = "HASSIO_FROM"
|
||||
ATTR_ACCESS_TOKEN = "access_token"
|
||||
ATTR_ACCESSPOINTS = "accesspoints"
|
||||
ATTR_ACTIVE = "active"
|
||||
ATTR_ACTIVITY_LED = "activity_led"
|
||||
ATTR_ADDON = "addon"
|
||||
ATTR_ADDONS = "addons"
|
||||
ATTR_ADDONS_CUSTOM_LIST = "addons_custom_list"
|
||||
@@ -113,6 +116,7 @@ ATTR_BACKUP_EXCLUDE = "backup_exclude"
|
||||
ATTR_BACKUP_POST = "backup_post"
|
||||
ATTR_BACKUP_PRE = "backup_pre"
|
||||
ATTR_BACKUPS = "backups"
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE = "backups_exclude_database"
|
||||
ATTR_BLK_READ = "blk_read"
|
||||
ATTR_BLK_WRITE = "blk_write"
|
||||
ATTR_BOARD = "board"
|
||||
@@ -152,9 +156,11 @@ ATTR_DIAGNOSTICS = "diagnostics"
|
||||
ATTR_DISCOVERY = "discovery"
|
||||
ATTR_DISK = "disk"
|
||||
ATTR_DISK_FREE = "disk_free"
|
||||
ATTR_DISK_LED = "disk_led"
|
||||
ATTR_DISK_LIFE_TIME = "disk_life_time"
|
||||
ATTR_DISK_TOTAL = "disk_total"
|
||||
ATTR_DISK_USED = "disk_used"
|
||||
ATTR_DISPLAYNAME = "displayname"
|
||||
ATTR_DNS = "dns"
|
||||
ATTR_DOCKER = "docker"
|
||||
ATTR_DOCKER_API = "docker_api"
|
||||
@@ -164,6 +170,7 @@ ATTR_ENABLE = "enable"
|
||||
ATTR_ENABLED = "enabled"
|
||||
ATTR_ENVIRONMENT = "environment"
|
||||
ATTR_EVENT = "event"
|
||||
ATTR_EXCLUDE_DATABASE = "exclude_database"
|
||||
ATTR_FEATURES = "features"
|
||||
ATTR_FILENAME = "filename"
|
||||
ATTR_FLAGS = "flags"
|
||||
@@ -177,7 +184,9 @@ ATTR_HASSIO_API = "hassio_api"
|
||||
ATTR_HASSIO_ROLE = "hassio_role"
|
||||
ATTR_HASSOS = "hassos"
|
||||
ATTR_HEALTHY = "healthy"
|
||||
ATTR_HEARTBEAT_LED = "heartbeat_led"
|
||||
ATTR_HOMEASSISTANT = "homeassistant"
|
||||
ATTR_HOMEASSISTANT_EXCLUDE_DATABASE = "homeassistant_exclude_database"
|
||||
ATTR_HOMEASSISTANT_API = "homeassistant_api"
|
||||
ATTR_HOST = "host"
|
||||
ATTR_HOST_DBUS = "host_dbus"
|
||||
@@ -252,6 +261,7 @@ ATTR_PLUGINS = "plugins"
|
||||
ATTR_PORT = "port"
|
||||
ATTR_PORTS = "ports"
|
||||
ATTR_PORTS_DESCRIPTION = "ports_description"
|
||||
ATTR_POWER_LED = "power_led"
|
||||
ATTR_PREFIX = "prefix"
|
||||
ATTR_PRIMARY = "primary"
|
||||
ATTR_PRIORITY = "priority"
|
||||
@@ -315,6 +325,7 @@ ATTR_UPDATE_KEY = "update_key"
|
||||
ATTR_URL = "url"
|
||||
ATTR_USB = "usb"
|
||||
ATTR_USER = "user"
|
||||
ATTR_USER_LED = "user_led"
|
||||
ATTR_USERNAME = "username"
|
||||
ATTR_UUID = "uuid"
|
||||
ATTR_VALID = "valid"
|
||||
@@ -341,6 +352,9 @@ MAP_ADDONS = "addons"
|
||||
MAP_BACKUP = "backup"
|
||||
MAP_SHARE = "share"
|
||||
MAP_MEDIA = "media"
|
||||
MAP_HOMEASSISTANT_CONFIG = "homeassistant_config"
|
||||
MAP_ALL_ADDON_CONFIGS = "all_addon_configs"
|
||||
MAP_ADDON_CONFIG = "addon_config"
|
||||
|
||||
ARCH_ARMHF = "armhf"
|
||||
ARCH_ARMV7 = "armv7"
|
||||
@@ -479,6 +493,23 @@ class IngressSessionDataUser:
|
||||
display_name: str | None = None
|
||||
username: str | None = None
|
||||
|
||||
def to_dict(self) -> dict[str, str | None]:
|
||||
"""Get dictionary representation."""
|
||||
return {
|
||||
ATTR_ID: self.id,
|
||||
ATTR_DISPLAYNAME: self.display_name,
|
||||
ATTR_USERNAME: self.username,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, str | None]) -> Self:
|
||||
"""Return object from dictionary representation."""
|
||||
return cls(
|
||||
id=data[ATTR_ID],
|
||||
display_name=data.get(ATTR_DISPLAYNAME),
|
||||
username=data.get(ATTR_USERNAME),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class IngressSessionData:
|
||||
@@ -486,6 +517,15 @@ class IngressSessionData:
|
||||
|
||||
user: IngressSessionDataUser
|
||||
|
||||
def to_dict(self) -> dict[str, dict[str, str | None]]:
|
||||
"""Get dictionary representation."""
|
||||
return {ATTR_USER: self.user.to_dict()}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: dict[str, dict[str, str | None]]) -> Self:
|
||||
"""Return object from dictionary representation."""
|
||||
return cls(user=IngressSessionDataUser.from_dict(data[ATTR_USER]))
|
||||
|
||||
|
||||
STARTING_STATES = [
|
||||
CoreState.INITIALIZE,
|
||||
|
@@ -28,7 +28,7 @@ from .homeassistant.core import LANDINGPAGE
|
||||
from .resolution.const import ContextType, IssueType, SuggestionType, UnhealthyReason
|
||||
from .utils.dt import utcnow
|
||||
from .utils.sentry import capture_exception
|
||||
from .utils.whoami import retrieve_whoami
|
||||
from .utils.whoami import WhoamiData, retrieve_whoami
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -250,7 +250,7 @@ class Core(CoreSysAttributes):
|
||||
except HomeAssistantError as err:
|
||||
capture_exception(err)
|
||||
else:
|
||||
_LOGGER.info("Skiping start of Home Assistant")
|
||||
_LOGGER.info("Skipping start of Home Assistant")
|
||||
|
||||
# Core is not running
|
||||
if self.sys_homeassistant.core.error_state:
|
||||
@@ -309,7 +309,7 @@ class Core(CoreSysAttributes):
|
||||
)
|
||||
]
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.warning("Stage 1: Force Shutdown!")
|
||||
|
||||
# Stage 2
|
||||
@@ -326,7 +326,7 @@ class Core(CoreSysAttributes):
|
||||
)
|
||||
]
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.warning("Stage 2: Force Shutdown!")
|
||||
|
||||
self.state = CoreState.CLOSE
|
||||
@@ -363,6 +363,13 @@ class Core(CoreSysAttributes):
|
||||
self.sys_config.last_boot = self.sys_hardware.helper.last_boot
|
||||
self.sys_config.save_data()
|
||||
|
||||
async def _retrieve_whoami(self, with_ssl: bool) -> WhoamiData | None:
|
||||
try:
|
||||
return await retrieve_whoami(self.sys_websession, with_ssl)
|
||||
except WhoamiSSLError:
|
||||
_LOGGER.info("Whoami service SSL error")
|
||||
return None
|
||||
|
||||
async def _adjust_system_datetime(self):
|
||||
"""Adjust system time/date on startup."""
|
||||
# If no timezone is detect or set
|
||||
@@ -375,21 +382,15 @@ class Core(CoreSysAttributes):
|
||||
|
||||
# Get Timezone data
|
||||
try:
|
||||
data = await retrieve_whoami(self.sys_websession)
|
||||
except WhoamiSSLError:
|
||||
pass
|
||||
data = await self._retrieve_whoami(True)
|
||||
|
||||
# SSL Date Issue & possible time drift
|
||||
if not data:
|
||||
data = await self._retrieve_whoami(False)
|
||||
except WhoamiError as err:
|
||||
_LOGGER.warning("Can't adjust Time/Date settings: %s", err)
|
||||
return
|
||||
|
||||
# SSL Date Issue & possible time drift
|
||||
if not data:
|
||||
try:
|
||||
data = await retrieve_whoami(self.sys_websession, with_ssl=False)
|
||||
except WhoamiError as err:
|
||||
_LOGGER.error("Can't adjust Time/Date settings: %s", err)
|
||||
return
|
||||
|
||||
self.sys_config.timezone = self.sys_config.timezone or data.timezone
|
||||
|
||||
# Calculate if system time is out of sync
|
||||
|
@@ -18,7 +18,7 @@ from .const import ENV_SUPERVISOR_DEV, SERVER_SOFTWARE
|
||||
from .utils.dt import UTC, get_time_zone
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .addons import AddonManager
|
||||
from .addons.manager import AddonManager
|
||||
from .api import RestAPI
|
||||
from .arch import CpuArch
|
||||
from .auth import Auth
|
||||
@@ -725,7 +725,7 @@ class CoreSysAttributes:
|
||||
def sys_run_in_executor(
|
||||
self, funct: Callable[..., T], *args: tuple[Any], **kwargs: dict[str, Any]
|
||||
) -> Coroutine[Any, Any, T]:
|
||||
"""Add an job to the executor pool."""
|
||||
"""Add a job to the executor pool."""
|
||||
return self.coresys.run_in_executor(funct, *args, **kwargs)
|
||||
|
||||
def sys_create_task(self, coroutine: Coroutine) -> asyncio.Task:
|
||||
|
@@ -5,6 +5,7 @@
|
||||
"raspberrypi3-64": ["aarch64", "armv7", "armhf"],
|
||||
"raspberrypi4": ["armv7", "armhf"],
|
||||
"raspberrypi4-64": ["aarch64", "armv7", "armhf"],
|
||||
"raspberrypi5-64": ["aarch64", "armv7", "armhf"],
|
||||
"yellow": ["aarch64", "armv7", "armhf"],
|
||||
"green": ["aarch64", "armv7", "armhf"],
|
||||
"tinker": ["armv7", "armhf"],
|
||||
|
@@ -6,7 +6,7 @@ from typing import Any
|
||||
from awesomeversion import AwesomeVersion
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ...exceptions import DBusError, DBusInterfaceError
|
||||
from ...exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from ..const import (
|
||||
DBUS_ATTR_DIAGNOSTICS,
|
||||
DBUS_ATTR_VERSION,
|
||||
@@ -99,7 +99,7 @@ class OSAgent(DBusInterfaceProxy):
|
||||
await asyncio.gather(*[dbus.connect(bus) for dbus in self.all])
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to OS-Agent")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No OS-Agent support on the host. Some Host functions have been disabled."
|
||||
)
|
||||
|
@@ -11,7 +11,8 @@ from ...const import (
|
||||
DBUS_OBJECT_HAOS_BOARDS,
|
||||
)
|
||||
from ...interface import DBusInterfaceProxy, dbus_property
|
||||
from .const import BOARD_NAME_SUPERVISED, BOARD_NAME_YELLOW
|
||||
from .const import BOARD_NAME_GREEN, BOARD_NAME_SUPERVISED, BOARD_NAME_YELLOW
|
||||
from .green import Green
|
||||
from .interface import BoardProxy
|
||||
from .supervised import Supervised
|
||||
from .yellow import Yellow
|
||||
@@ -39,6 +40,14 @@ class BoardManager(DBusInterfaceProxy):
|
||||
"""Get board name."""
|
||||
return self.properties[DBUS_ATTR_BOARD]
|
||||
|
||||
@property
|
||||
def green(self) -> Green:
|
||||
"""Get Green board."""
|
||||
if self.board != BOARD_NAME_GREEN:
|
||||
raise BoardInvalidError("Green board is not in use", _LOGGER.error)
|
||||
|
||||
return self._board_proxy
|
||||
|
||||
@property
|
||||
def supervised(self) -> Supervised:
|
||||
"""Get Supervised board."""
|
||||
@@ -61,6 +70,8 @@ class BoardManager(DBusInterfaceProxy):
|
||||
|
||||
if self.board == BOARD_NAME_YELLOW:
|
||||
self._board_proxy = Yellow()
|
||||
elif self.board == BOARD_NAME_GREEN:
|
||||
self._board_proxy = Green()
|
||||
elif self.board == BOARD_NAME_SUPERVISED:
|
||||
self._board_proxy = Supervised()
|
||||
|
||||
|
@@ -1,4 +1,5 @@
|
||||
"""Constants for boards."""
|
||||
|
||||
BOARD_NAME_GREEN = "Green"
|
||||
BOARD_NAME_SUPERVISED = "Supervised"
|
||||
BOARD_NAME_YELLOW = "Yellow"
|
||||
|
65
supervisor/dbus/agent/boards/green.py
Normal file
65
supervisor/dbus/agent/boards/green.py
Normal file
@@ -0,0 +1,65 @@
|
||||
"""Green board management."""
|
||||
|
||||
import asyncio
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ....const import ATTR_ACTIVITY_LED, ATTR_POWER_LED, ATTR_USER_LED
|
||||
from ...const import DBUS_ATTR_ACTIVITY_LED, DBUS_ATTR_POWER_LED, DBUS_ATTR_USER_LED
|
||||
from ...interface import dbus_property
|
||||
from .const import BOARD_NAME_GREEN
|
||||
from .interface import BoardProxy
|
||||
from .validate import SCHEMA_GREEN_BOARD
|
||||
|
||||
|
||||
class Green(BoardProxy):
|
||||
"""Green board manager object."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize properties."""
|
||||
super().__init__(BOARD_NAME_GREEN, SCHEMA_GREEN_BOARD)
|
||||
|
||||
@property
|
||||
@dbus_property
|
||||
def activity_led(self) -> bool:
|
||||
"""Get activity LED enabled."""
|
||||
return self.properties[DBUS_ATTR_ACTIVITY_LED]
|
||||
|
||||
@activity_led.setter
|
||||
def activity_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable activity LED."""
|
||||
self._data[ATTR_ACTIVITY_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Green.set_activity_led(enabled))
|
||||
|
||||
@property
|
||||
@dbus_property
|
||||
def power_led(self) -> bool:
|
||||
"""Get power LED enabled."""
|
||||
return self.properties[DBUS_ATTR_POWER_LED]
|
||||
|
||||
@power_led.setter
|
||||
def power_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable power LED."""
|
||||
self._data[ATTR_POWER_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Green.set_power_led(enabled))
|
||||
|
||||
@property
|
||||
@dbus_property
|
||||
def user_led(self) -> bool:
|
||||
"""Get user LED enabled."""
|
||||
return self.properties[DBUS_ATTR_USER_LED]
|
||||
|
||||
@user_led.setter
|
||||
def user_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable disk LED."""
|
||||
self._data[ATTR_USER_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Green.set_user_led(enabled))
|
||||
|
||||
async def connect(self, bus: MessageBus) -> None:
|
||||
"""Connect to D-Bus."""
|
||||
await super().connect(bus)
|
||||
|
||||
# Set LEDs based on settings on connect
|
||||
self.activity_led = self._data[ATTR_ACTIVITY_LED]
|
||||
self.power_led = self._data[ATTR_POWER_LED]
|
||||
self.user_led = self._data[ATTR_USER_LED]
|
@@ -1,17 +1,23 @@
|
||||
"""Board dbus proxy interface."""
|
||||
|
||||
from voluptuous import Schema
|
||||
|
||||
from ....const import FILE_HASSIO_BOARD
|
||||
from ....utils.common import FileConfiguration
|
||||
from ...const import DBUS_IFACE_HAOS_BOARDS, DBUS_NAME_HAOS, DBUS_OBJECT_HAOS_BOARDS
|
||||
from ...interface import DBusInterfaceProxy
|
||||
from .validate import SCHEMA_BASE_BOARD
|
||||
|
||||
|
||||
class BoardProxy(DBusInterfaceProxy):
|
||||
class BoardProxy(FileConfiguration, DBusInterfaceProxy):
|
||||
"""DBus interface proxy for os board."""
|
||||
|
||||
bus_name: str = DBUS_NAME_HAOS
|
||||
|
||||
def __init__(self, name: str) -> None:
|
||||
def __init__(self, name: str, file_schema: Schema | None = None) -> None:
|
||||
"""Initialize properties."""
|
||||
super().__init__()
|
||||
super().__init__(FILE_HASSIO_BOARD, file_schema or SCHEMA_BASE_BOARD)
|
||||
super(FileConfiguration, self).__init__()
|
||||
|
||||
self._name: str = name
|
||||
self.object_path: str = f"{DBUS_OBJECT_HAOS_BOARDS}/{name}"
|
||||
|
32
supervisor/dbus/agent/boards/validate.py
Normal file
32
supervisor/dbus/agent/boards/validate.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""Validation for board config."""
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from ....const import (
|
||||
ATTR_ACTIVITY_LED,
|
||||
ATTR_DISK_LED,
|
||||
ATTR_HEARTBEAT_LED,
|
||||
ATTR_POWER_LED,
|
||||
ATTR_USER_LED,
|
||||
)
|
||||
|
||||
# pylint: disable=no-value-for-parameter
|
||||
SCHEMA_BASE_BOARD = vol.Schema({}, extra=vol.REMOVE_EXTRA)
|
||||
|
||||
SCHEMA_GREEN_BOARD = vol.Schema(
|
||||
{
|
||||
vol.Optional(ATTR_ACTIVITY_LED, default=True): vol.Boolean(),
|
||||
vol.Optional(ATTR_POWER_LED, default=True): vol.Boolean(),
|
||||
vol.Optional(ATTR_USER_LED, default=True): vol.Boolean(),
|
||||
},
|
||||
extra=vol.REMOVE_EXTRA,
|
||||
)
|
||||
|
||||
SCHEMA_YELLOW_BOARD = vol.Schema(
|
||||
{
|
||||
vol.Optional(ATTR_DISK_LED, default=True): vol.Boolean(),
|
||||
vol.Optional(ATTR_HEARTBEAT_LED, default=True): vol.Boolean(),
|
||||
vol.Optional(ATTR_POWER_LED, default=True): vol.Boolean(),
|
||||
},
|
||||
extra=vol.REMOVE_EXTRA,
|
||||
)
|
@@ -2,10 +2,14 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ....const import ATTR_DISK_LED, ATTR_HEARTBEAT_LED, ATTR_POWER_LED
|
||||
from ...const import DBUS_ATTR_DISK_LED, DBUS_ATTR_HEARTBEAT_LED, DBUS_ATTR_POWER_LED
|
||||
from ...interface import dbus_property
|
||||
from .const import BOARD_NAME_YELLOW
|
||||
from .interface import BoardProxy
|
||||
from .validate import SCHEMA_YELLOW_BOARD
|
||||
|
||||
|
||||
class Yellow(BoardProxy):
|
||||
@@ -13,7 +17,7 @@ class Yellow(BoardProxy):
|
||||
|
||||
def __init__(self) -> None:
|
||||
"""Initialize properties."""
|
||||
super().__init__(BOARD_NAME_YELLOW)
|
||||
super().__init__(BOARD_NAME_YELLOW, SCHEMA_YELLOW_BOARD)
|
||||
|
||||
@property
|
||||
@dbus_property
|
||||
@@ -24,6 +28,7 @@ class Yellow(BoardProxy):
|
||||
@heartbeat_led.setter
|
||||
def heartbeat_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable heartbeat LED."""
|
||||
self._data[ATTR_HEARTBEAT_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Yellow.set_heartbeat_led(enabled))
|
||||
|
||||
@property
|
||||
@@ -35,6 +40,7 @@ class Yellow(BoardProxy):
|
||||
@power_led.setter
|
||||
def power_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable power LED."""
|
||||
self._data[ATTR_POWER_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Yellow.set_power_led(enabled))
|
||||
|
||||
@property
|
||||
@@ -46,4 +52,14 @@ class Yellow(BoardProxy):
|
||||
@disk_led.setter
|
||||
def disk_led(self, enabled: bool) -> None:
|
||||
"""Enable/disable disk LED."""
|
||||
self._data[ATTR_DISK_LED] = enabled
|
||||
asyncio.create_task(self.dbus.Boards.Yellow.set_disk_led(enabled))
|
||||
|
||||
async def connect(self, bus: MessageBus) -> None:
|
||||
"""Connect to D-Bus."""
|
||||
await super().connect(bus)
|
||||
|
||||
# Set LEDs based on settings on connect
|
||||
self.disk_led = self._data[ATTR_DISK_LED]
|
||||
self.heartbeat_led = self._data[ATTR_HEARTBEAT_LED]
|
||||
self.power_led = self._data[ATTR_POWER_LED]
|
||||
|
@@ -64,6 +64,7 @@ DBUS_OBJECT_UDISKS2 = "/org/freedesktop/UDisks2/Manager"
|
||||
DBUS_ATTR_ACTIVE_ACCESSPOINT = "ActiveAccessPoint"
|
||||
DBUS_ATTR_ACTIVE_CONNECTION = "ActiveConnection"
|
||||
DBUS_ATTR_ACTIVE_CONNECTIONS = "ActiveConnections"
|
||||
DBUS_ATTR_ACTIVITY_LED = "ActivityLED"
|
||||
DBUS_ATTR_ADDRESS_DATA = "AddressData"
|
||||
DBUS_ATTR_BITRATE = "Bitrate"
|
||||
DBUS_ATTR_BOARD = "Board"
|
||||
@@ -169,6 +170,7 @@ DBUS_ATTR_TIMEUSEC = "TimeUSec"
|
||||
DBUS_ATTR_TIMEZONE = "Timezone"
|
||||
DBUS_ATTR_TRANSACTION_STATISTICS = "TransactionStatistics"
|
||||
DBUS_ATTR_TYPE = "Type"
|
||||
DBUS_ATTR_USER_LED = "UserLED"
|
||||
DBUS_ATTR_USERSPACE_TIMESTAMP_MONOTONIC = "UserspaceTimestampMonotonic"
|
||||
DBUS_ATTR_UUID_UPPERCASE = "UUID"
|
||||
DBUS_ATTR_UUID = "Uuid"
|
||||
|
@@ -3,7 +3,7 @@ import logging
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ..exceptions import DBusError, DBusInterfaceError
|
||||
from ..exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from .const import (
|
||||
DBUS_ATTR_CHASSIS,
|
||||
DBUS_ATTR_DEPLOYMENT,
|
||||
@@ -39,7 +39,7 @@ class Hostname(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to systemd-hostname")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No hostname support on the host. Hostname functions have been disabled."
|
||||
)
|
||||
|
@@ -3,7 +3,7 @@ import logging
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ..exceptions import DBusError, DBusInterfaceError
|
||||
from ..exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from .const import DBUS_NAME_LOGIND, DBUS_OBJECT_LOGIND
|
||||
from .interface import DBusInterface
|
||||
from .utils import dbus_connected
|
||||
@@ -28,8 +28,8 @@ class Logind(DBusInterface):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to systemd-logind")
|
||||
except DBusInterfaceError:
|
||||
_LOGGER.info("No systemd-logind support on the host.")
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning("No systemd-logind support on the host.")
|
||||
|
||||
@dbus_connected
|
||||
async def reboot(self) -> None:
|
||||
|
@@ -9,6 +9,8 @@ from ...exceptions import (
|
||||
DBusError,
|
||||
DBusFatalError,
|
||||
DBusInterfaceError,
|
||||
DBusNoReplyError,
|
||||
DBusServiceUnkownError,
|
||||
HostNotSupportedError,
|
||||
NetworkInterfaceNotFound,
|
||||
)
|
||||
@@ -143,7 +145,7 @@ class NetworkManager(DBusInterfaceProxy):
|
||||
await self.settings.connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to Network Manager")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No Network Manager support on the host. Local network functions have been disabled."
|
||||
)
|
||||
@@ -210,8 +212,22 @@ class NetworkManager(DBusInterfaceProxy):
|
||||
# try to query it. Ignore those cases.
|
||||
_LOGGER.debug("Can't process %s: %s", device, err)
|
||||
continue
|
||||
except (
|
||||
DBusNoReplyError,
|
||||
DBusServiceUnkownError,
|
||||
) as err:
|
||||
# This typically means that NetworkManager disappeared. Give up immeaditly.
|
||||
_LOGGER.error(
|
||||
"NetworkManager not responding while processing %s: %s. Giving up.",
|
||||
device,
|
||||
err,
|
||||
)
|
||||
capture_exception(err)
|
||||
return
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Error while processing %s: %s", device, err)
|
||||
_LOGGER.exception(
|
||||
"Unkown error while processing %s: %s", device, err
|
||||
)
|
||||
capture_exception(err)
|
||||
continue
|
||||
|
||||
|
@@ -12,7 +12,7 @@ from ...const import (
|
||||
ATTR_PRIORITY,
|
||||
ATTR_VPN,
|
||||
)
|
||||
from ...exceptions import DBusError, DBusInterfaceError
|
||||
from ...exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from ..const import (
|
||||
DBUS_ATTR_CONFIGURATION,
|
||||
DBUS_ATTR_MODE,
|
||||
@@ -67,7 +67,7 @@ class NetworkManagerDNS(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to DnsManager")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No DnsManager support on the host. Local DNS functions have been disabled."
|
||||
)
|
||||
|
@@ -2,7 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import socket
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING
|
||||
from uuid import uuid4
|
||||
|
||||
from dbus_fast import Variant
|
||||
@@ -19,6 +19,7 @@ from . import (
|
||||
CONF_ATTR_PATH,
|
||||
CONF_ATTR_VLAN,
|
||||
)
|
||||
from .. import NetworkManager
|
||||
from ....host.const import InterfaceMethod, InterfaceType
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -26,8 +27,11 @@ if TYPE_CHECKING:
|
||||
|
||||
|
||||
def get_connection_from_interface(
|
||||
interface: Interface, name: str | None = None, uuid: str | None = None
|
||||
) -> Any:
|
||||
interface: Interface,
|
||||
network_manager: NetworkManager,
|
||||
name: str | None = None,
|
||||
uuid: str | None = None,
|
||||
) -> dict[str, dict[str, Variant]]:
|
||||
"""Generate message argument for network interface update."""
|
||||
|
||||
# Generate/Update ID/name
|
||||
@@ -121,9 +125,15 @@ def get_connection_from_interface(
|
||||
if interface.type == InterfaceType.ETHERNET:
|
||||
conn[CONF_ATTR_802_ETHERNET] = {ATTR_ASSIGNED_MAC: Variant("s", "preserve")}
|
||||
elif interface.type == "vlan":
|
||||
parent = interface.vlan.interface
|
||||
if parent in network_manager and (
|
||||
parent_connection := network_manager.get(parent).connection
|
||||
):
|
||||
parent = parent_connection.uuid
|
||||
|
||||
conn[CONF_ATTR_VLAN] = {
|
||||
"id": Variant("u", interface.vlan.id),
|
||||
"parent": Variant("s", interface.vlan.interface),
|
||||
"parent": Variant("s", parent),
|
||||
}
|
||||
elif interface.type == InterfaceType.WIRELESS:
|
||||
wireless = {
|
||||
|
@@ -4,7 +4,7 @@ from typing import Any
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ...exceptions import DBusError, DBusInterfaceError
|
||||
from ...exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from ..const import DBUS_NAME_NM, DBUS_OBJECT_SETTINGS
|
||||
from ..interface import DBusInterface
|
||||
from ..network.setting import NetworkSetting
|
||||
@@ -28,7 +28,7 @@ class NetworkManagerSettings(DBusInterface):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to Network Manager Settings")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No Network Manager Settings support on the host. Local network functions have been disabled."
|
||||
)
|
||||
|
@@ -4,7 +4,7 @@ from typing import Any
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ..exceptions import DBusError, DBusInterfaceError
|
||||
from ..exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from ..utils.dbus import DBusSignalWrapper
|
||||
from .const import (
|
||||
DBUS_ATTR_BOOT_SLOT,
|
||||
@@ -49,7 +49,7 @@ class Rauc(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to rauc")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning("Host has no rauc support. OTA updates have been disabled.")
|
||||
|
||||
@property
|
||||
|
@@ -5,7 +5,7 @@ import logging
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ..exceptions import DBusError, DBusInterfaceError
|
||||
from ..exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from .const import (
|
||||
DBUS_ATTR_CACHE_STATISTICS,
|
||||
DBUS_ATTR_CURRENT_DNS_SERVER,
|
||||
@@ -59,7 +59,7 @@ class Resolved(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to systemd-resolved.")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"Host has no systemd-resolved support. DNS will not work correctly."
|
||||
)
|
||||
|
@@ -10,6 +10,7 @@ from ..exceptions import (
|
||||
DBusError,
|
||||
DBusFatalError,
|
||||
DBusInterfaceError,
|
||||
DBusServiceUnkownError,
|
||||
DBusSystemdNoSuchUnit,
|
||||
)
|
||||
from .const import (
|
||||
@@ -86,7 +87,7 @@ class Systemd(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to systemd")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No systemd support on the host. Host control has been disabled."
|
||||
)
|
||||
|
@@ -4,7 +4,7 @@ import logging
|
||||
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
|
||||
from ..exceptions import DBusError, DBusInterfaceError
|
||||
from ..exceptions import DBusError, DBusInterfaceError, DBusServiceUnkownError
|
||||
from ..utils.dt import utc_from_timestamp
|
||||
from .const import (
|
||||
DBUS_ATTR_NTP,
|
||||
@@ -63,7 +63,7 @@ class TimeDate(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to systemd-timedate")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No timedate support on the host. Time/Date functions have been disabled."
|
||||
)
|
||||
|
@@ -6,7 +6,12 @@ from typing import Any
|
||||
from awesomeversion import AwesomeVersion
|
||||
from dbus_fast.aio import MessageBus
|
||||
|
||||
from ...exceptions import DBusError, DBusInterfaceError, DBusObjectError
|
||||
from ...exceptions import (
|
||||
DBusError,
|
||||
DBusInterfaceError,
|
||||
DBusObjectError,
|
||||
DBusServiceUnkownError,
|
||||
)
|
||||
from ..const import (
|
||||
DBUS_ATTR_SUPPORTED_FILESYSTEMS,
|
||||
DBUS_ATTR_VERSION,
|
||||
@@ -45,7 +50,7 @@ class UDisks2(DBusInterfaceProxy):
|
||||
await super().connect(bus)
|
||||
except DBusError:
|
||||
_LOGGER.warning("Can't connect to udisks2")
|
||||
except DBusInterfaceError:
|
||||
except (DBusServiceUnkownError, DBusInterfaceError):
|
||||
_LOGGER.warning(
|
||||
"No udisks2 support on the host. Host control has been disabled."
|
||||
)
|
||||
|
@@ -3,10 +3,9 @@
|
||||
from dataclasses import dataclass
|
||||
from inspect import get_annotations
|
||||
from pathlib import Path
|
||||
from typing import Any, TypedDict
|
||||
from typing import Any, NotRequired, TypedDict
|
||||
|
||||
from dbus_fast import Variant
|
||||
from typing_extensions import NotRequired
|
||||
|
||||
from .const import EncryptType, EraseMode
|
||||
|
||||
|
@@ -18,9 +18,12 @@ from ..addons.build import AddonBuild
|
||||
from ..bus import EventListener
|
||||
from ..const import (
|
||||
DOCKER_CPU_RUNTIME_ALLOCATION,
|
||||
MAP_ADDON_CONFIG,
|
||||
MAP_ADDONS,
|
||||
MAP_ALL_ADDON_CONFIGS,
|
||||
MAP_BACKUP,
|
||||
MAP_CONFIG,
|
||||
MAP_HOMEASSISTANT_CONFIG,
|
||||
MAP_MEDIA,
|
||||
MAP_SHARE,
|
||||
MAP_SSL,
|
||||
@@ -350,6 +353,39 @@ class DockerAddon(DockerInterface):
|
||||
)
|
||||
)
|
||||
|
||||
else:
|
||||
# Map addon's public config folder if not using deprecated config option
|
||||
if self.addon.addon_config_used:
|
||||
mounts.append(
|
||||
Mount(
|
||||
type=MountType.BIND,
|
||||
source=self.addon.path_extern_config.as_posix(),
|
||||
target="/config",
|
||||
read_only=addon_mapping[MAP_ADDON_CONFIG],
|
||||
)
|
||||
)
|
||||
|
||||
# Map Home Assistant config in new way
|
||||
if MAP_HOMEASSISTANT_CONFIG in addon_mapping:
|
||||
mounts.append(
|
||||
Mount(
|
||||
type=MountType.BIND,
|
||||
source=self.sys_config.path_extern_homeassistant.as_posix(),
|
||||
target="/homeassistant",
|
||||
read_only=addon_mapping[MAP_HOMEASSISTANT_CONFIG],
|
||||
)
|
||||
)
|
||||
|
||||
if MAP_ALL_ADDON_CONFIGS in addon_mapping:
|
||||
mounts.append(
|
||||
Mount(
|
||||
type=MountType.BIND,
|
||||
source=self.sys_config.path_extern_addon_configs.as_posix(),
|
||||
target="/addon_configs",
|
||||
read_only=addon_mapping[MAP_ALL_ADDON_CONFIGS],
|
||||
)
|
||||
)
|
||||
|
||||
if MAP_SSL in addon_mapping:
|
||||
mounts.append(
|
||||
Mount(
|
||||
@@ -501,24 +537,16 @@ class DockerAddon(DockerInterface):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Security check
|
||||
if not self.addon.protected:
|
||||
_LOGGER.warning("%s running with disabled protected mode!", self.addon.name)
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Don't set a hostname if no separate UTS namespace is used
|
||||
hostname = None if self.uts_mode else self.addon.hostname
|
||||
|
||||
# Create & Run container
|
||||
try:
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=str(self.addon.version),
|
||||
name=self.name,
|
||||
hostname=hostname,
|
||||
@@ -549,7 +577,6 @@ class DockerAddon(DockerInterface):
|
||||
)
|
||||
raise
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting Docker add-on %s with version %s", self.image, self.version
|
||||
)
|
||||
@@ -589,10 +616,6 @@ class DockerAddon(DockerInterface):
|
||||
version, image=image, latest=latest, need_build=self.addon.latest_need_build
|
||||
)
|
||||
|
||||
# Stop container & cleanup
|
||||
with suppress(DockerError):
|
||||
await self.stop()
|
||||
|
||||
@Job(
|
||||
name="docker_addon_install",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
|
@@ -92,16 +92,7 @@ class DockerAudio(DockerInterface, CoreSysAttributes):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=str(self.sys_plugins.audio.version),
|
||||
init=False,
|
||||
ipv4=self.sys_docker.network.audio,
|
||||
@@ -118,8 +109,6 @@ class DockerAudio(DockerInterface, CoreSysAttributes):
|
||||
},
|
||||
mounts=self.mounts,
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting Audio %s with version %s - %s",
|
||||
self.image,
|
||||
|
@@ -33,16 +33,7 @@ class DockerCli(DockerInterface, CoreSysAttributes):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
entrypoint=["/init"],
|
||||
tag=str(self.sys_plugins.cli.version),
|
||||
init=False,
|
||||
@@ -60,8 +51,6 @@ class DockerCli(DockerInterface, CoreSysAttributes):
|
||||
ENV_TOKEN: self.sys_plugins.cli.supervisor_token,
|
||||
},
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting CLI %s with version %s - %s",
|
||||
self.image,
|
||||
|
@@ -35,16 +35,7 @@ class DockerDNS(DockerInterface, CoreSysAttributes):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=str(self.sys_plugins.dns.version),
|
||||
init=False,
|
||||
dns=False,
|
||||
@@ -65,8 +56,6 @@ class DockerDNS(DockerInterface, CoreSysAttributes):
|
||||
],
|
||||
oom_score_adj=-300,
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting DNS %s with version %s - %s",
|
||||
self.image,
|
||||
|
@@ -53,9 +53,10 @@ class DockerHomeAssistant(DockerInterface):
|
||||
@property
|
||||
def timeout(self) -> int:
|
||||
"""Return timeout for Docker actions."""
|
||||
# Synchronized homeassistant's S6_SERVICES_GRACETIME
|
||||
# to avoid killing Home Assistant Core
|
||||
return 220 + 20
|
||||
# Synchronized with the homeassistant core container's S6_SERVICES_GRACETIME
|
||||
# to avoid killing Home Assistant Core, see
|
||||
# https://github.com/home-assistant/core/tree/dev/Dockerfile
|
||||
return 240 + 20
|
||||
|
||||
@property
|
||||
def ip_address(self) -> IPv4Address:
|
||||
@@ -152,16 +153,7 @@ class DockerHomeAssistant(DockerInterface):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=(self.sys_homeassistant.version),
|
||||
name=self.name,
|
||||
hostname=self.name,
|
||||
@@ -186,8 +178,6 @@ class DockerHomeAssistant(DockerInterface):
|
||||
tmpfs={"/tmp": ""},
|
||||
oom_score_adj=-300,
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting Home Assistant %s with version %s", self.image, self.version
|
||||
)
|
||||
|
@@ -1,7 +1,6 @@
|
||||
"""Interface class for Supervisor Docker object."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections import defaultdict
|
||||
from collections.abc import Awaitable
|
||||
from contextlib import suppress
|
||||
@@ -92,7 +91,6 @@ class DockerInterface(JobGroup):
|
||||
)
|
||||
self.coresys: CoreSys = coresys
|
||||
self._meta: dict[str, Any] | None = None
|
||||
self.lock: asyncio.Lock = asyncio.Lock()
|
||||
|
||||
@property
|
||||
def timeout(self) -> int:
|
||||
@@ -153,7 +151,7 @@ class DockerInterface(JobGroup):
|
||||
@property
|
||||
def in_progress(self) -> bool:
|
||||
"""Return True if a task is in progress."""
|
||||
return self.lock.locked()
|
||||
return self.active_job
|
||||
|
||||
@property
|
||||
def restart_policy(self) -> RestartPolicy | None:
|
||||
@@ -379,6 +377,27 @@ class DockerInterface(JobGroup):
|
||||
"""Run Docker image."""
|
||||
raise NotImplementedError()
|
||||
|
||||
async def _run(self, **kwargs) -> None:
|
||||
"""Run Docker image with retry inf necessary."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
try:
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run, self.image, **kwargs
|
||||
)
|
||||
except DockerNotFound as err:
|
||||
# If image is missing, capture the exception as this shouldn't happen
|
||||
capture_exception(err)
|
||||
raise
|
||||
|
||||
# Store metadata
|
||||
self._meta = docker_container.attrs
|
||||
|
||||
@Job(
|
||||
name="docker_interface_stop",
|
||||
limit=JobExecutionLimit.GROUP_ONCE,
|
||||
@@ -451,12 +470,17 @@ class DockerInterface(JobGroup):
|
||||
return b""
|
||||
|
||||
@Job(name="docker_interface_cleanup", limit=JobExecutionLimit.GROUP_WAIT)
|
||||
def cleanup(self, old_image: str | None = None) -> Awaitable[None]:
|
||||
def cleanup(
|
||||
self,
|
||||
old_image: str | None = None,
|
||||
image: str | None = None,
|
||||
version: AwesomeVersion | None = None,
|
||||
) -> Awaitable[None]:
|
||||
"""Check if old version exists and cleanup."""
|
||||
return self.sys_run_in_executor(
|
||||
self.sys_docker.cleanup_old_images,
|
||||
self.image,
|
||||
self.version,
|
||||
image or self.image,
|
||||
version or self.version,
|
||||
{old_image} if old_image else None,
|
||||
)
|
||||
|
||||
|
@@ -38,16 +38,7 @@ class DockerMulticast(DockerInterface, CoreSysAttributes):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=str(self.sys_plugins.multicast.version),
|
||||
init=False,
|
||||
name=self.name,
|
||||
@@ -59,8 +50,6 @@ class DockerMulticast(DockerInterface, CoreSysAttributes):
|
||||
extra_hosts={"supervisor": self.sys_docker.network.supervisor},
|
||||
environment={ENV_TIME: self.sys_timezone},
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting Multicast %s with version %s - Host", self.image, self.version
|
||||
)
|
||||
|
@@ -35,16 +35,7 @@ class DockerObserver(DockerInterface, CoreSysAttributes):
|
||||
)
|
||||
async def run(self) -> None:
|
||||
"""Run Docker image."""
|
||||
if await self.is_running():
|
||||
return
|
||||
|
||||
# Cleanup
|
||||
await self.stop()
|
||||
|
||||
# Create & Run container
|
||||
docker_container = await self.sys_run_in_executor(
|
||||
self.sys_docker.run,
|
||||
self.image,
|
||||
await self._run(
|
||||
tag=str(self.sys_plugins.observer.version),
|
||||
init=False,
|
||||
ipv4=self.sys_docker.network.observer,
|
||||
@@ -63,8 +54,6 @@ class DockerObserver(DockerInterface, CoreSysAttributes):
|
||||
ports={"80/tcp": 4357},
|
||||
oom_score_adj=-300,
|
||||
)
|
||||
|
||||
self._meta = docker_container.attrs
|
||||
_LOGGER.info(
|
||||
"Starting Observer %s with version %s - %s",
|
||||
self.image,
|
||||
|
@@ -67,6 +67,10 @@ class HomeAssistantCrashError(HomeAssistantError):
|
||||
"""Error on crash of a Home Assistant startup."""
|
||||
|
||||
|
||||
class HomeAssistantStartupTimeout(HomeAssistantCrashError):
|
||||
"""Timeout waiting for Home Assistant successful startup."""
|
||||
|
||||
|
||||
class HomeAssistantAPIError(HomeAssistantError):
|
||||
"""Home Assistant API exception."""
|
||||
|
||||
@@ -331,6 +335,10 @@ class DBusNotConnectedError(HostNotSupportedError):
|
||||
"""D-Bus is not connected and call a method."""
|
||||
|
||||
|
||||
class DBusServiceUnkownError(HassioNotSupportedError):
|
||||
"""D-Bus service was not available."""
|
||||
|
||||
|
||||
class DBusInterfaceError(HassioNotSupportedError):
|
||||
"""D-Bus interface not connected."""
|
||||
|
||||
@@ -359,6 +367,10 @@ class DBusTimeoutError(DBusError):
|
||||
"""D-Bus call timed out."""
|
||||
|
||||
|
||||
class DBusNoReplyError(DBusError):
|
||||
"""D-Bus remote didn't reply/disconnected."""
|
||||
|
||||
|
||||
class DBusFatalError(DBusError):
|
||||
"""D-Bus call going wrong.
|
||||
|
||||
|
@@ -13,7 +13,7 @@ from ..coresys import CoreSys, CoreSysAttributes
|
||||
from ..exceptions import HomeAssistantAPIError, HomeAssistantAuthError
|
||||
from ..jobs.const import JobExecutionLimit
|
||||
from ..jobs.decorator import Job
|
||||
from ..utils import check_port
|
||||
from ..utils import check_port, version_is_new_enough
|
||||
from .const import LANDINGPAGE
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
@@ -107,7 +107,7 @@ class HomeAssistantAPI(CoreSysAttributes):
|
||||
continue
|
||||
yield resp
|
||||
return
|
||||
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
|
||||
except (TimeoutError, aiohttp.ClientError) as err:
|
||||
_LOGGER.error("Error on call %s: %s", url, err)
|
||||
break
|
||||
|
||||
@@ -130,34 +130,41 @@ class HomeAssistantAPI(CoreSysAttributes):
|
||||
"""Return Home Assistant core state."""
|
||||
return await self._get_json("api/core/state")
|
||||
|
||||
async def check_api_state(self) -> bool:
|
||||
"""Return True if Home Assistant up and running."""
|
||||
async def get_api_state(self) -> str | None:
|
||||
"""Return state of Home Assistant Core or None."""
|
||||
# Skip check on landingpage
|
||||
if (
|
||||
self.sys_homeassistant.version is None
|
||||
or self.sys_homeassistant.version == LANDINGPAGE
|
||||
):
|
||||
return False
|
||||
return None
|
||||
|
||||
# Check if port is up
|
||||
if not await self.sys_run_in_executor(
|
||||
check_port,
|
||||
if not await check_port(
|
||||
self.sys_homeassistant.ip_address,
|
||||
self.sys_homeassistant.api_port,
|
||||
):
|
||||
return False
|
||||
return None
|
||||
|
||||
# Check if API is up
|
||||
with suppress(HomeAssistantAPIError):
|
||||
# get_core_state is available since 2023.8.0 and preferred
|
||||
# since it is significantly faster than get_config because
|
||||
# it does not require serializing the entire config
|
||||
if self.sys_homeassistant.version >= GET_CORE_STATE_MIN_VERSION:
|
||||
if version_is_new_enough(
|
||||
self.sys_homeassistant.version, GET_CORE_STATE_MIN_VERSION
|
||||
):
|
||||
data = await self.get_core_state()
|
||||
else:
|
||||
data = await self.get_config()
|
||||
# Older versions of home assistant does not expose the state
|
||||
if data and data.get("state", "RUNNING") == "RUNNING":
|
||||
return True
|
||||
if data:
|
||||
return data.get("state", "RUNNING")
|
||||
|
||||
return None
|
||||
|
||||
async def check_api_state(self) -> bool:
|
||||
"""Return Home Assistant Core state if up."""
|
||||
if state := await self.get_api_state():
|
||||
return state == "RUNNING"
|
||||
return False
|
||||
|
@@ -2,12 +2,14 @@
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
from contextlib import suppress
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
import re
|
||||
import secrets
|
||||
import shutil
|
||||
from typing import Final
|
||||
|
||||
import attr
|
||||
from awesomeversion import AwesomeVersion
|
||||
|
||||
from ..const import ATTR_HOMEASSISTANT, BusEvent
|
||||
@@ -21,6 +23,7 @@ from ..exceptions import (
|
||||
HomeAssistantCrashError,
|
||||
HomeAssistantError,
|
||||
HomeAssistantJobError,
|
||||
HomeAssistantStartupTimeout,
|
||||
HomeAssistantUpdateError,
|
||||
JobException,
|
||||
)
|
||||
@@ -40,15 +43,20 @@ from .const import (
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
SECONDS_BETWEEN_API_CHECKS: Final[int] = 5
|
||||
# Core Stage 1 and some wiggle room
|
||||
STARTUP_API_RESPONSE_TIMEOUT: Final[timedelta] = timedelta(minutes=3)
|
||||
# All stages plus event start timeout and some wiggle rooom
|
||||
STARTUP_API_CHECK_RUNNING_TIMEOUT: Final[timedelta] = timedelta(minutes=15)
|
||||
RE_YAML_ERROR = re.compile(r"homeassistant\.util\.yaml")
|
||||
|
||||
|
||||
@attr.s(frozen=True)
|
||||
@dataclass
|
||||
class ConfigResult:
|
||||
"""Return object from config check."""
|
||||
|
||||
valid = attr.ib()
|
||||
log = attr.ib()
|
||||
valid: bool
|
||||
log: str
|
||||
|
||||
|
||||
class HomeAssistantCore(JobGroup):
|
||||
@@ -58,7 +66,6 @@ class HomeAssistantCore(JobGroup):
|
||||
"""Initialize Home Assistant object."""
|
||||
super().__init__(coresys, JOB_GROUP_HOME_ASSISTANT_CORE)
|
||||
self.instance: DockerHomeAssistant = DockerHomeAssistant(coresys)
|
||||
self.lock: asyncio.Lock = asyncio.Lock()
|
||||
self._error_state: bool = False
|
||||
|
||||
@property
|
||||
@@ -402,7 +409,7 @@ class HomeAssistantCore(JobGroup):
|
||||
@property
|
||||
def in_progress(self) -> bool:
|
||||
"""Return True if a task is in progress."""
|
||||
return self.instance.in_progress or self.lock.locked()
|
||||
return self.instance.in_progress or self.active_job
|
||||
|
||||
async def check_config(self) -> ConfigResult:
|
||||
"""Run Home Assistant config check."""
|
||||
@@ -436,21 +443,38 @@ class HomeAssistantCore(JobGroup):
|
||||
return
|
||||
_LOGGER.info("Wait until Home Assistant is ready")
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(5)
|
||||
deadline = datetime.now() + STARTUP_API_RESPONSE_TIMEOUT
|
||||
last_state = None
|
||||
while not (timeout := datetime.now() >= deadline):
|
||||
await asyncio.sleep(SECONDS_BETWEEN_API_CHECKS)
|
||||
|
||||
# 1: Check if Container is is_running
|
||||
if not await self.instance.is_running():
|
||||
_LOGGER.error("Home Assistant has crashed!")
|
||||
break
|
||||
|
||||
# 2: Check if API response
|
||||
if await self.sys_homeassistant.api.check_api_state():
|
||||
_LOGGER.info("Detect a running Home Assistant instance")
|
||||
self._error_state = False
|
||||
return
|
||||
# 2: Check API response
|
||||
if state := await self.sys_homeassistant.api.get_api_state():
|
||||
if last_state is None:
|
||||
# API initially available, move deadline up and check API
|
||||
# state to be running now
|
||||
deadline = datetime.now() + STARTUP_API_CHECK_RUNNING_TIMEOUT
|
||||
|
||||
if last_state != state:
|
||||
_LOGGER.info("Home Assistant Core state changed to %s", state)
|
||||
last_state = state
|
||||
|
||||
if state == "RUNNING":
|
||||
_LOGGER.info("Detect a running Home Assistant instance")
|
||||
self._error_state = False
|
||||
return
|
||||
|
||||
self._error_state = True
|
||||
if timeout:
|
||||
raise HomeAssistantStartupTimeout(
|
||||
"No Home Assistant Core response, assuming a fatal startup error",
|
||||
_LOGGER.error,
|
||||
)
|
||||
raise HomeAssistantCrashError()
|
||||
|
||||
@Job(
|
||||
|
@@ -18,6 +18,7 @@ from ..const import (
|
||||
ATTR_ACCESS_TOKEN,
|
||||
ATTR_AUDIO_INPUT,
|
||||
ATTR_AUDIO_OUTPUT,
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE,
|
||||
ATTR_BOOT,
|
||||
ATTR_IMAGE,
|
||||
ATTR_PORT,
|
||||
@@ -62,6 +63,10 @@ HOMEASSISTANT_BACKUP_EXCLUDE = [
|
||||
"*.log.*",
|
||||
"OZW_Log.txt",
|
||||
]
|
||||
HOMEASSISTANT_BACKUP_EXCLUDE_DATABASE = [
|
||||
"home-assistant_v?.db",
|
||||
"home-assistant_v?.db-wal",
|
||||
]
|
||||
|
||||
|
||||
class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
@@ -258,6 +263,16 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
except (AwesomeVersionException, TypeError):
|
||||
return False
|
||||
|
||||
@property
|
||||
def backups_exclude_database(self) -> bool:
|
||||
"""Exclude database from core backups by default."""
|
||||
return self._data[ATTR_BACKUPS_EXCLUDE_DATABASE]
|
||||
|
||||
@backups_exclude_database.setter
|
||||
def backups_exclude_database(self, value: bool) -> None:
|
||||
"""Set whether backups should exclude database by default."""
|
||||
self._data[ATTR_BACKUPS_EXCLUDE_DATABASE] = value
|
||||
|
||||
async def load(self) -> None:
|
||||
"""Prepare Home Assistant object."""
|
||||
await asyncio.wait(
|
||||
@@ -327,7 +342,9 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
)
|
||||
|
||||
@Job(name="home_assistant_module_backup")
|
||||
async def backup(self, tar_file: tarfile.TarFile) -> None:
|
||||
async def backup(
|
||||
self, tar_file: tarfile.TarFile, exclude_database: bool = False
|
||||
) -> None:
|
||||
"""Backup Home Assistant Core config/ directory."""
|
||||
await self.begin_backup()
|
||||
try:
|
||||
@@ -351,11 +368,16 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
# Backup metadata
|
||||
backup.add(temp, arcname=".")
|
||||
|
||||
# Set excludes
|
||||
excludes = HOMEASSISTANT_BACKUP_EXCLUDE.copy()
|
||||
if exclude_database:
|
||||
excludes += HOMEASSISTANT_BACKUP_EXCLUDE_DATABASE
|
||||
|
||||
# Backup data
|
||||
atomic_contents_add(
|
||||
backup,
|
||||
self.sys_config.path_homeassistant,
|
||||
excludes=HOMEASSISTANT_BACKUP_EXCLUDE,
|
||||
excludes=excludes,
|
||||
arcname="data",
|
||||
)
|
||||
|
||||
@@ -371,7 +393,10 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
finally:
|
||||
await self.end_backup()
|
||||
|
||||
async def restore(self, tar_file: tarfile.TarFile) -> None:
|
||||
@Job(name="home_assistant_module_restore")
|
||||
async def restore(
|
||||
self, tar_file: tarfile.TarFile, exclude_database: bool = False
|
||||
) -> None:
|
||||
"""Restore Home Assistant Core config/ directory."""
|
||||
with TemporaryDirectory(dir=self.sys_config.path_tmp) as temp:
|
||||
temp_path = Path(temp)
|
||||
@@ -399,11 +424,22 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
def _restore_data():
|
||||
"""Restore data."""
|
||||
shutil.copytree(
|
||||
temp_data, self.sys_config.path_homeassistant, symlinks=True
|
||||
temp_data,
|
||||
self.sys_config.path_homeassistant,
|
||||
symlinks=True,
|
||||
dirs_exist_ok=True,
|
||||
)
|
||||
|
||||
_LOGGER.info("Restore Home Assistant Core config folder")
|
||||
await remove_folder(self.sys_config.path_homeassistant)
|
||||
excludes = (
|
||||
HOMEASSISTANT_BACKUP_EXCLUDE_DATABASE if exclude_database else None
|
||||
)
|
||||
await remove_folder(
|
||||
self.sys_config.path_homeassistant,
|
||||
content_only=True,
|
||||
excludes=excludes,
|
||||
tmp_dir=self.sys_config.path_tmp,
|
||||
)
|
||||
try:
|
||||
await self.sys_run_in_executor(_restore_data)
|
||||
except shutil.Error as err:
|
||||
@@ -455,11 +491,13 @@ class HomeAssistant(FileConfiguration, CoreSysAttributes):
|
||||
{ATTR_TYPE: "config/auth/list"}
|
||||
)
|
||||
|
||||
return [
|
||||
IngressSessionDataUser(
|
||||
id=data["id"],
|
||||
username=data.get("username"),
|
||||
display_name=data.get("name"),
|
||||
)
|
||||
for data in list_of_users
|
||||
]
|
||||
if list_of_users:
|
||||
return [
|
||||
IngressSessionDataUser(
|
||||
id=data["id"],
|
||||
username=data.get("username"),
|
||||
display_name=data.get("name"),
|
||||
)
|
||||
for data in list_of_users
|
||||
]
|
||||
return []
|
||||
|
@@ -7,6 +7,7 @@ from ..const import (
|
||||
ATTR_ACCESS_TOKEN,
|
||||
ATTR_AUDIO_INPUT,
|
||||
ATTR_AUDIO_OUTPUT,
|
||||
ATTR_BACKUPS_EXCLUDE_DATABASE,
|
||||
ATTR_BOOT,
|
||||
ATTR_IMAGE,
|
||||
ATTR_PORT,
|
||||
@@ -32,6 +33,7 @@ SCHEMA_HASS_CONFIG = vol.Schema(
|
||||
vol.Optional(ATTR_WATCHDOG, default=True): vol.Boolean(),
|
||||
vol.Optional(ATTR_AUDIO_OUTPUT, default=None): vol.Maybe(str),
|
||||
vol.Optional(ATTR_AUDIO_INPUT, default=None): vol.Maybe(str),
|
||||
vol.Optional(ATTR_BACKUPS_EXCLUDE_DATABASE, default=False): vol.Boolean(),
|
||||
},
|
||||
extra=vol.REMOVE_EXTRA,
|
||||
)
|
||||
|
@@ -154,7 +154,7 @@ class WSClient:
|
||||
@classmethod
|
||||
async def connect_with_auth(
|
||||
cls, session: aiohttp.ClientSession, loop, url: str, token: str
|
||||
) -> "WSClient":
|
||||
) -> WSClient:
|
||||
"""Create an authenticated websocket client."""
|
||||
try:
|
||||
client = await session.ws_connect(url, ssl=False)
|
||||
|
@@ -175,4 +175,4 @@ class HostManager(CoreSysAttributes):
|
||||
async def _hardware_events(self, device: Device) -> None:
|
||||
"""Process hardware requests."""
|
||||
if self.sys_hardware.policy.is_match_cgroup(PolicyGroup.AUDIO, device):
|
||||
await self.sound.update()
|
||||
await self.sound.update(reload_pulse=True)
|
||||
|
@@ -189,6 +189,7 @@ class NetworkManager(CoreSysAttributes):
|
||||
_LOGGER.debug("Updating existing configuration for %s", interface.name)
|
||||
settings = get_connection_from_interface(
|
||||
interface,
|
||||
self.sys_dbus.network,
|
||||
name=inet.settings.connection.id,
|
||||
uuid=inet.settings.connection.uuid,
|
||||
)
|
||||
@@ -217,7 +218,7 @@ class NetworkManager(CoreSysAttributes):
|
||||
# Create new configuration and activate interface
|
||||
elif inet and interface.enabled:
|
||||
_LOGGER.debug("Create new configuration for %s", interface.name)
|
||||
settings = get_connection_from_interface(interface)
|
||||
settings = get_connection_from_interface(interface, self.sys_dbus.network)
|
||||
|
||||
try:
|
||||
settings, con = await self.sys_dbus.network.add_and_activate_connection(
|
||||
@@ -244,7 +245,7 @@ class NetworkManager(CoreSysAttributes):
|
||||
|
||||
# Create new interface (like vlan)
|
||||
elif not inet:
|
||||
settings = get_connection_from_interface(interface)
|
||||
settings = get_connection_from_interface(interface, self.sys_dbus.network)
|
||||
|
||||
try:
|
||||
await self.sys_dbus.network.settings.add_connection(settings)
|
||||
|
@@ -15,6 +15,9 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
PULSE_NAME = "supervisor"
|
||||
|
||||
PULSE_ALSA_MODULE = "module-alsa-card"
|
||||
PULSE_UDEV_MODULE = "module-udev-detect"
|
||||
|
||||
|
||||
class StreamType(StrEnum):
|
||||
"""INPUT/OUTPUT type of source."""
|
||||
@@ -235,9 +238,9 @@ class SoundControl(CoreSysAttributes):
|
||||
@Job(
|
||||
name="sound_control_update",
|
||||
limit=JobExecutionLimit.THROTTLE_WAIT,
|
||||
throttle_period=timedelta(seconds=10),
|
||||
throttle_period=timedelta(seconds=2),
|
||||
)
|
||||
async def update(self):
|
||||
async def update(self, reload_pulse: bool = False):
|
||||
"""Update properties over dbus."""
|
||||
_LOGGER.info("Updating PulseAudio information")
|
||||
|
||||
@@ -348,11 +351,32 @@ class SoundControl(CoreSysAttributes):
|
||||
f"Error while processing pulse update: {err}", _LOGGER.error
|
||||
) from err
|
||||
except PulseError as err:
|
||||
_LOGGER.debug("Can't update PulseAudio data: %s", err)
|
||||
_LOGGER.warning("Can't update PulseAudio data: %s", err)
|
||||
|
||||
return data
|
||||
|
||||
def _reload_pulse_modules():
|
||||
try:
|
||||
with Pulse(PULSE_NAME) as pulse:
|
||||
modules = pulse.module_list()
|
||||
for alsa_module in filter(
|
||||
lambda x: x.name == PULSE_ALSA_MODULE, modules
|
||||
):
|
||||
pulse.module_unload(alsa_module.index)
|
||||
udev_module = next(
|
||||
filter(lambda x: x.name == PULSE_UDEV_MODULE, modules)
|
||||
)
|
||||
pulse.module_unload(udev_module.index)
|
||||
# And now reload
|
||||
pulse.module_load(PULSE_UDEV_MODULE)
|
||||
except StopIteration:
|
||||
_LOGGER.warning("Can't reload PulseAudio modules.")
|
||||
except PulseError as err:
|
||||
_LOGGER.warning("Can't reload PulseAudio modules: %s", err)
|
||||
|
||||
# Update data from pulse server
|
||||
if reload_pulse:
|
||||
await self.sys_run_in_executor(_reload_pulse_modules)
|
||||
data: PulseData = await self.sys_run_in_executor(_get_pulse_data)
|
||||
self._applications = data.applications
|
||||
self._cards = data.cards
|
||||
|
@@ -38,7 +38,9 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
|
||||
def get_session_data(self, session_id: str) -> IngressSessionData | None:
|
||||
"""Return complementary data of current session or None."""
|
||||
return self.sessions_data.get(session_id)
|
||||
if data := self.sessions_data.get(session_id):
|
||||
return IngressSessionData.from_dict(data)
|
||||
return None
|
||||
|
||||
@property
|
||||
def sessions(self) -> dict[str, float]:
|
||||
@@ -46,7 +48,7 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
return self._data[ATTR_SESSION]
|
||||
|
||||
@property
|
||||
def sessions_data(self) -> dict[str, IngressSessionData]:
|
||||
def sessions_data(self) -> dict[str, dict[str, str | None]]:
|
||||
"""Return sessions_data."""
|
||||
return self._data[ATTR_SESSION_DATA]
|
||||
|
||||
@@ -86,7 +88,7 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
now = utcnow()
|
||||
|
||||
sessions = {}
|
||||
sessions_data: dict[str, IngressSessionData] = {}
|
||||
sessions_data: dict[str, dict[str, str | None]] = {}
|
||||
for session, valid in self.sessions.items():
|
||||
# check if timestamp valid, to avoid crash on malformed timestamp
|
||||
try:
|
||||
@@ -100,7 +102,8 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
|
||||
# Is valid
|
||||
sessions[session] = valid
|
||||
sessions_data[session] = self.get_session_data(session)
|
||||
if session_data := self.sessions_data.get(session):
|
||||
sessions_data[session] = session_data
|
||||
|
||||
# Write back
|
||||
self.sessions.clear()
|
||||
@@ -123,7 +126,7 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
|
||||
self.sessions[session] = valid.timestamp()
|
||||
if data is not None:
|
||||
self.sessions_data[session] = data
|
||||
self.sessions_data[session] = data.to_dict()
|
||||
|
||||
return session
|
||||
|
||||
@@ -151,7 +154,7 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
|
||||
return True
|
||||
|
||||
def get_dynamic_port(self, addon_slug: str) -> int:
|
||||
async def get_dynamic_port(self, addon_slug: str) -> int:
|
||||
"""Get/Create a dynamic port from range."""
|
||||
if addon_slug in self.ports:
|
||||
return self.ports[addon_slug]
|
||||
@@ -160,7 +163,7 @@ class Ingress(FileConfiguration, CoreSysAttributes):
|
||||
while (
|
||||
port is None
|
||||
or port in self.ports.values()
|
||||
or check_port(self.sys_docker.network.gateway, port)
|
||||
or await check_port(self.sys_docker.network.gateway, port)
|
||||
):
|
||||
port = random.randint(62000, 65500)
|
||||
|
||||
|
@@ -86,7 +86,7 @@ class SupervisorJob:
|
||||
}
|
||||
|
||||
@contextmanager
|
||||
def start(self, *, on_done: Callable[["SupervisorJob"], None] | None = None):
|
||||
def start(self):
|
||||
"""Start the job in the current task.
|
||||
|
||||
This can only be called if the parent ID matches the job running in the current task.
|
||||
@@ -107,8 +107,6 @@ class SupervisorJob:
|
||||
self.done = True
|
||||
if token:
|
||||
_CURRENT_JOB.reset(token)
|
||||
if on_done:
|
||||
on_done(self)
|
||||
|
||||
|
||||
class JobManager(FileConfiguration, CoreSysAttributes):
|
||||
@@ -192,7 +190,7 @@ class JobManager(FileConfiguration, CoreSysAttributes):
|
||||
if job.uuid not in self._jobs:
|
||||
raise JobNotFound(f"Could not find job {job.name}", _LOGGER.error)
|
||||
|
||||
if not job.done:
|
||||
if job.done is False:
|
||||
_LOGGER.warning("Removing incomplete job %s from job manager", job.name)
|
||||
|
||||
del self._jobs[job.uuid]
|
||||
|
@@ -174,6 +174,14 @@ class Job(CoreSysAttributes):
|
||||
return obj
|
||||
return None
|
||||
|
||||
def _handle_job_condition_exception(self, err: JobConditionException) -> None:
|
||||
"""Handle a job condition failure."""
|
||||
error_msg = str(err)
|
||||
if self.on_condition is None:
|
||||
_LOGGER.info(error_msg)
|
||||
return
|
||||
raise self.on_condition(error_msg, _LOGGER.warning) from None
|
||||
|
||||
def __call__(self, method):
|
||||
"""Call the wrapper logic."""
|
||||
self._method = method
|
||||
@@ -193,99 +201,125 @@ class Job(CoreSysAttributes):
|
||||
internal=self._internal,
|
||||
)
|
||||
|
||||
# Handle condition
|
||||
if self.conditions:
|
||||
try:
|
||||
await self._check_conditions()
|
||||
except JobConditionException as err:
|
||||
error_msg = str(err)
|
||||
if self.on_condition is None:
|
||||
_LOGGER.info(error_msg)
|
||||
try:
|
||||
# Handle condition
|
||||
if self.conditions:
|
||||
try:
|
||||
await Job.check_conditions(
|
||||
self, set(self.conditions), self._method.__qualname__
|
||||
)
|
||||
except JobConditionException as err:
|
||||
return self._handle_job_condition_exception(err)
|
||||
|
||||
# Handle exection limits
|
||||
if self.limit in (
|
||||
JobExecutionLimit.SINGLE_WAIT,
|
||||
JobExecutionLimit.ONCE,
|
||||
):
|
||||
await self._acquire_exection_limit()
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.GROUP_ONCE,
|
||||
JobExecutionLimit.GROUP_WAIT,
|
||||
):
|
||||
try:
|
||||
await obj.acquire(
|
||||
job, self.limit == JobExecutionLimit.GROUP_WAIT
|
||||
)
|
||||
except JobGroupExecutionLimitExceeded as err:
|
||||
if self.on_condition:
|
||||
raise self.on_condition(str(err)) from err
|
||||
raise err
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE,
|
||||
JobExecutionLimit.GROUP_THROTTLE,
|
||||
):
|
||||
time_since_last_call = datetime.now() - self.last_call(group_name)
|
||||
if time_since_last_call < self.throttle_period(group_name):
|
||||
return
|
||||
raise self.on_condition(error_msg, _LOGGER.warning) from None
|
||||
|
||||
# Handle exection limits
|
||||
if self.limit in (JobExecutionLimit.SINGLE_WAIT, JobExecutionLimit.ONCE):
|
||||
await self._acquire_exection_limit()
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.GROUP_ONCE,
|
||||
JobExecutionLimit.GROUP_WAIT,
|
||||
):
|
||||
try:
|
||||
await obj.acquire(job, self.limit == JobExecutionLimit.GROUP_WAIT)
|
||||
except JobGroupExecutionLimitExceeded as err:
|
||||
if self.on_condition:
|
||||
raise self.on_condition(str(err)) from err
|
||||
raise err
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE,
|
||||
JobExecutionLimit.GROUP_THROTTLE,
|
||||
):
|
||||
time_since_last_call = datetime.now() - self.last_call(group_name)
|
||||
if time_since_last_call < self.throttle_period(group_name):
|
||||
return
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE_WAIT,
|
||||
JobExecutionLimit.GROUP_THROTTLE_WAIT,
|
||||
):
|
||||
await self._acquire_exection_limit()
|
||||
time_since_last_call = datetime.now() - self.last_call(group_name)
|
||||
if time_since_last_call < self.throttle_period(group_name):
|
||||
self._release_exception_limits()
|
||||
return
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE_RATE_LIMIT,
|
||||
JobExecutionLimit.GROUP_THROTTLE_RATE_LIMIT,
|
||||
):
|
||||
# Only reprocess array when necessary (at limit)
|
||||
if len(self.rate_limited_calls(group_name)) >= self.throttle_max_calls:
|
||||
self.set_rate_limited_calls(
|
||||
[
|
||||
call
|
||||
for call in self.rate_limited_calls(group_name)
|
||||
if call > datetime.now() - self.throttle_period(group_name)
|
||||
],
|
||||
group_name,
|
||||
)
|
||||
|
||||
if len(self.rate_limited_calls(group_name)) >= self.throttle_max_calls:
|
||||
on_condition = (
|
||||
JobException if self.on_condition is None else self.on_condition
|
||||
)
|
||||
raise on_condition(
|
||||
f"Rate limit exceeded, more then {self.throttle_max_calls} calls in {self.throttle_period(group_name)}",
|
||||
)
|
||||
|
||||
# Execute Job
|
||||
with job.start(on_done=self.sys_jobs.remove_job if self.cleanup else None):
|
||||
try:
|
||||
self.set_last_call(datetime.now(), group_name)
|
||||
if self.rate_limited_calls(group_name) is not None:
|
||||
self.add_rate_limited_call(
|
||||
self.last_call(group_name), group_name
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE_WAIT,
|
||||
JobExecutionLimit.GROUP_THROTTLE_WAIT,
|
||||
):
|
||||
await self._acquire_exection_limit()
|
||||
time_since_last_call = datetime.now() - self.last_call(group_name)
|
||||
if time_since_last_call < self.throttle_period(group_name):
|
||||
self._release_exception_limits()
|
||||
return
|
||||
elif self.limit in (
|
||||
JobExecutionLimit.THROTTLE_RATE_LIMIT,
|
||||
JobExecutionLimit.GROUP_THROTTLE_RATE_LIMIT,
|
||||
):
|
||||
# Only reprocess array when necessary (at limit)
|
||||
if (
|
||||
len(self.rate_limited_calls(group_name))
|
||||
>= self.throttle_max_calls
|
||||
):
|
||||
self.set_rate_limited_calls(
|
||||
[
|
||||
call
|
||||
for call in self.rate_limited_calls(group_name)
|
||||
if call
|
||||
> datetime.now() - self.throttle_period(group_name)
|
||||
],
|
||||
group_name,
|
||||
)
|
||||
|
||||
return await self._method(obj, *args, **kwargs)
|
||||
except HassioError as err:
|
||||
raise err
|
||||
except Exception as err:
|
||||
_LOGGER.exception("Unhandled exception: %s", err)
|
||||
capture_exception(err)
|
||||
raise JobException() from err
|
||||
finally:
|
||||
self._release_exception_limits()
|
||||
if self.limit in (
|
||||
JobExecutionLimit.GROUP_ONCE,
|
||||
JobExecutionLimit.GROUP_WAIT,
|
||||
if (
|
||||
len(self.rate_limited_calls(group_name))
|
||||
>= self.throttle_max_calls
|
||||
):
|
||||
obj.release()
|
||||
on_condition = (
|
||||
JobException
|
||||
if self.on_condition is None
|
||||
else self.on_condition
|
||||
)
|
||||
raise on_condition(
|
||||
f"Rate limit exceeded, more than {self.throttle_max_calls} calls in {self.throttle_period(group_name)}",
|
||||
)
|
||||
|
||||
# Execute Job
|
||||
with job.start():
|
||||
try:
|
||||
self.set_last_call(datetime.now(), group_name)
|
||||
if self.rate_limited_calls(group_name) is not None:
|
||||
self.add_rate_limited_call(
|
||||
self.last_call(group_name), group_name
|
||||
)
|
||||
|
||||
return await self._method(obj, *args, **kwargs)
|
||||
|
||||
# If a method has a conditional JobCondition, they must check it in the method
|
||||
# These should be handled like normal JobConditions as much as possible
|
||||
except JobConditionException as err:
|
||||
return self._handle_job_condition_exception(err)
|
||||
except HassioError as err:
|
||||
raise err
|
||||
except Exception as err:
|
||||
_LOGGER.exception("Unhandled exception: %s", err)
|
||||
capture_exception(err)
|
||||
raise JobException() from err
|
||||
finally:
|
||||
self._release_exception_limits()
|
||||
if self.limit in (
|
||||
JobExecutionLimit.GROUP_ONCE,
|
||||
JobExecutionLimit.GROUP_WAIT,
|
||||
):
|
||||
obj.release()
|
||||
|
||||
# Jobs that weren't started are always cleaned up. Also clean up done jobs if required
|
||||
finally:
|
||||
if job.done is None or self.cleanup:
|
||||
self.sys_jobs.remove_job(job)
|
||||
|
||||
return wrapper
|
||||
|
||||
async def _check_conditions(self):
|
||||
@staticmethod
|
||||
async def check_conditions(
|
||||
coresys: CoreSysAttributes, conditions: set[JobCondition], method_name: str
|
||||
):
|
||||
"""Check conditions."""
|
||||
used_conditions = set(self.conditions) - set(self.sys_jobs.ignore_conditions)
|
||||
ignored_conditions = set(self.conditions) & set(self.sys_jobs.ignore_conditions)
|
||||
used_conditions = set(conditions) - set(coresys.sys_jobs.ignore_conditions)
|
||||
ignored_conditions = set(conditions) & set(coresys.sys_jobs.ignore_conditions)
|
||||
|
||||
# Check if somethings is ignored
|
||||
if ignored_conditions:
|
||||
@@ -294,93 +328,97 @@ class Job(CoreSysAttributes):
|
||||
ignored_conditions,
|
||||
)
|
||||
|
||||
if JobCondition.HEALTHY in used_conditions and not self.sys_core.healthy:
|
||||
if JobCondition.HEALTHY in used_conditions and not coresys.sys_core.healthy:
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, system is not healthy - {', '.join(self.sys_resolution.unhealthy)}"
|
||||
f"'{method_name}' blocked from execution, system is not healthy - {', '.join(coresys.sys_resolution.unhealthy)}"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.RUNNING in used_conditions
|
||||
and self.sys_core.state != CoreState.RUNNING
|
||||
and coresys.sys_core.state != CoreState.RUNNING
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, system is not running - {self.sys_core.state!s}"
|
||||
f"'{method_name}' blocked from execution, system is not running - {coresys.sys_core.state!s}"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.FROZEN in used_conditions
|
||||
and self.sys_core.state != CoreState.FREEZE
|
||||
and coresys.sys_core.state != CoreState.FREEZE
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, system is not frozen - {self.sys_core.state!s}"
|
||||
f"'{method_name}' blocked from execution, system is not frozen - {coresys.sys_core.state!s}"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.FREE_SPACE in used_conditions
|
||||
and self.sys_host.info.free_space < MINIMUM_FREE_SPACE_THRESHOLD
|
||||
and coresys.sys_host.info.free_space < MINIMUM_FREE_SPACE_THRESHOLD
|
||||
):
|
||||
self.sys_resolution.create_issue(IssueType.FREE_SPACE, ContextType.SYSTEM)
|
||||
coresys.sys_resolution.create_issue(
|
||||
IssueType.FREE_SPACE, ContextType.SYSTEM
|
||||
)
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, not enough free space ({self.sys_host.info.free_space}GB) left on the device"
|
||||
f"'{method_name}' blocked from execution, not enough free space ({coresys.sys_host.info.free_space}GB) left on the device"
|
||||
)
|
||||
|
||||
if JobCondition.INTERNET_SYSTEM in used_conditions:
|
||||
await self.sys_supervisor.check_connectivity()
|
||||
if not self.sys_supervisor.connectivity:
|
||||
await coresys.sys_supervisor.check_connectivity()
|
||||
if not coresys.sys_supervisor.connectivity:
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, no supervisor internet connection"
|
||||
f"'{method_name}' blocked from execution, no supervisor internet connection"
|
||||
)
|
||||
|
||||
if JobCondition.INTERNET_HOST in used_conditions:
|
||||
await self.sys_host.network.check_connectivity()
|
||||
await coresys.sys_host.network.check_connectivity()
|
||||
if (
|
||||
self.sys_host.network.connectivity is not None
|
||||
and not self.sys_host.network.connectivity
|
||||
coresys.sys_host.network.connectivity is not None
|
||||
and not coresys.sys_host.network.connectivity
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, no host internet connection"
|
||||
f"'{method_name}' blocked from execution, no host internet connection"
|
||||
)
|
||||
|
||||
if JobCondition.HAOS in used_conditions and not self.sys_os.available:
|
||||
if JobCondition.HAOS in used_conditions and not coresys.sys_os.available:
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, no Home Assistant OS available"
|
||||
f"'{method_name}' blocked from execution, no Home Assistant OS available"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.OS_AGENT in used_conditions
|
||||
and HostFeature.OS_AGENT not in self.sys_host.features
|
||||
and HostFeature.OS_AGENT not in coresys.sys_host.features
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, no Home Assistant OS-Agent available"
|
||||
f"'{method_name}' blocked from execution, no Home Assistant OS-Agent available"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.HOST_NETWORK in used_conditions
|
||||
and not self.sys_dbus.network.is_connected
|
||||
and not coresys.sys_dbus.network.is_connected
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, host Network Manager not available"
|
||||
f"'{method_name}' blocked from execution, host Network Manager not available"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.AUTO_UPDATE in used_conditions
|
||||
and not self.sys_updater.auto_update
|
||||
and not coresys.sys_updater.auto_update
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, supervisor auto updates disabled"
|
||||
f"'{method_name}' blocked from execution, supervisor auto updates disabled"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.SUPERVISOR_UPDATED in used_conditions
|
||||
and self.sys_supervisor.need_update
|
||||
and coresys.sys_supervisor.need_update
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, supervisor needs to be updated first"
|
||||
f"'{method_name}' blocked from execution, supervisor needs to be updated first"
|
||||
)
|
||||
|
||||
if JobCondition.PLUGINS_UPDATED in used_conditions and (
|
||||
out_of_date := [
|
||||
plugin for plugin in self.sys_plugins.all_plugins if plugin.need_update
|
||||
plugin
|
||||
for plugin in coresys.sys_plugins.all_plugins
|
||||
if plugin.need_update
|
||||
]
|
||||
):
|
||||
errors = await asyncio.gather(
|
||||
@@ -391,15 +429,15 @@ class Job(CoreSysAttributes):
|
||||
out_of_date[i].slug for i in range(len(errors)) if errors[i] is not None
|
||||
]:
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, was unable to update plugin(s) {', '.join(update_failures)} and all plugins must be up to date first"
|
||||
f"'{method_name}' blocked from execution, was unable to update plugin(s) {', '.join(update_failures)} and all plugins must be up to date first"
|
||||
)
|
||||
|
||||
if (
|
||||
JobCondition.MOUNT_AVAILABLE in used_conditions
|
||||
and HostFeature.MOUNT not in self.sys_host.features
|
||||
and HostFeature.MOUNT not in coresys.sys_host.features
|
||||
):
|
||||
raise JobConditionException(
|
||||
f"'{self._method.__qualname__}' blocked from execution, mounting not supported on system"
|
||||
f"'{method_name}' blocked from execution, mounting not supported on system"
|
||||
)
|
||||
|
||||
async def _acquire_exection_limit(self) -> None:
|
||||
|
@@ -115,5 +115,5 @@ class Scheduler(CoreSysAttributes):
|
||||
try:
|
||||
async with async_timeout.timeout(timeout):
|
||||
await asyncio.wait(running)
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
_LOGGER.error("Timeout while waiting for jobs shutdown")
|
||||
|
@@ -7,6 +7,7 @@ from ..addons.const import ADDON_UPDATE_CONDITIONS
|
||||
from ..const import AddonState
|
||||
from ..coresys import CoreSysAttributes
|
||||
from ..exceptions import AddonsError, HomeAssistantError, ObserverError
|
||||
from ..homeassistant.const import LANDINGPAGE
|
||||
from ..jobs.decorator import Job, JobCondition
|
||||
from ..plugins.const import PLUGIN_UPDATE_CONDITIONS
|
||||
from ..utils.sentry import capture_exception
|
||||
@@ -102,7 +103,7 @@ class Tasks(CoreSysAttributes):
|
||||
# avoid issue on slow IO
|
||||
_LOGGER.info("Add-on auto update process %s", addon.slug)
|
||||
try:
|
||||
if start_task := await addon.update(backup=True):
|
||||
if start_task := await self.sys_addons.update(addon.slug, backup=True):
|
||||
start_tasks.append(start_task)
|
||||
except AddonsError:
|
||||
_LOGGER.error("Can't auto update Add-on %s", addon.slug)
|
||||
@@ -142,6 +143,9 @@ class Tasks(CoreSysAttributes):
|
||||
if self.sys_homeassistant.error_state:
|
||||
# Home Assistant is in an error state, this is handled by the rollback feature
|
||||
return
|
||||
if self.sys_homeassistant.version == LANDINGPAGE:
|
||||
# Skip watchdog for landingpage
|
||||
return
|
||||
if not await self.sys_homeassistant.core.is_running():
|
||||
# The home assistant container is not running
|
||||
return
|
||||
|
@@ -170,6 +170,16 @@ class Mount(CoreSysAttributes, ABC):
|
||||
elif self.state != UnitActiveState.ACTIVE:
|
||||
await self.reload()
|
||||
|
||||
async def update_state(self) -> None:
|
||||
"""Update mount unit state."""
|
||||
try:
|
||||
self._state = await self.unit.get_active_state()
|
||||
except DBusError as err:
|
||||
capture_exception(err)
|
||||
raise MountError(
|
||||
f"Could not get active state of mount due to: {err!s}"
|
||||
) from err
|
||||
|
||||
async def update(self) -> None:
|
||||
"""Update info about mount from dbus."""
|
||||
try:
|
||||
@@ -182,13 +192,7 @@ class Mount(CoreSysAttributes, ABC):
|
||||
capture_exception(err)
|
||||
raise MountError(f"Could not get mount unit due to: {err!s}") from err
|
||||
|
||||
try:
|
||||
self._state = await self.unit.get_active_state()
|
||||
except DBusError as err:
|
||||
capture_exception(err)
|
||||
raise MountError(
|
||||
f"Could not get active state of mount due to: {err!s}"
|
||||
) from err
|
||||
await self.update_state()
|
||||
|
||||
# If active, dismiss corresponding failed mount issue if found
|
||||
if (
|
||||
@@ -197,6 +201,20 @@ class Mount(CoreSysAttributes, ABC):
|
||||
):
|
||||
self.sys_resolution.dismiss_issue(self.failed_issue)
|
||||
|
||||
async def _update_state_await(self, expected_states: list[UnitActiveState]) -> None:
|
||||
"""Update state info about mount from dbus. Wait up to 30 seconds for the state to appear."""
|
||||
for i in range(5):
|
||||
await self.update_state()
|
||||
if self.state in expected_states:
|
||||
return
|
||||
await asyncio.sleep(i**2)
|
||||
|
||||
_LOGGER.warning(
|
||||
"Mount %s still in state %s after waiting for 30 seconods to complete",
|
||||
self.name,
|
||||
str(self.state).lower(),
|
||||
)
|
||||
|
||||
async def _update_await_activating(self):
|
||||
"""Update info about mount from dbus. If 'activating' wait up to 30 seconds."""
|
||||
await self.update()
|
||||
@@ -269,10 +287,15 @@ class Mount(CoreSysAttributes, ABC):
|
||||
await self.update()
|
||||
|
||||
try:
|
||||
if self.state != UnitActiveState.FAILED:
|
||||
await self.sys_dbus.systemd.stop_unit(self.unit_name, StopUnitMode.FAIL)
|
||||
|
||||
await self._update_state_await(
|
||||
[UnitActiveState.INACTIVE, UnitActiveState.FAILED]
|
||||
)
|
||||
|
||||
if self.state == UnitActiveState.FAILED:
|
||||
await self.sys_dbus.systemd.reset_failed_unit(self.unit_name)
|
||||
else:
|
||||
await self.sys_dbus.systemd.stop_unit(self.unit_name, StopUnitMode.FAIL)
|
||||
except DBusSystemdNoSuchUnit:
|
||||
_LOGGER.info("Mount %s is not mounted, skipping unmount", self.name)
|
||||
except DBusError as err:
|
||||
|
@@ -1,9 +1,8 @@
|
||||
"""Validation for mount manager."""
|
||||
|
||||
import re
|
||||
from typing import TypedDict
|
||||
from typing import NotRequired, TypedDict
|
||||
|
||||
from typing_extensions import NotRequired
|
||||
import voluptuous as vol
|
||||
|
||||
from ..const import (
|
||||
|
@@ -1,5 +1,4 @@
|
||||
"""OS support on supervisor."""
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
import logging
|
||||
from pathlib import Path
|
||||
@@ -114,7 +113,7 @@ class OSManager(CoreSysAttributes):
|
||||
|
||||
_LOGGER.info("Completed download of OTA update file %s", raucb)
|
||||
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||
except (aiohttp.ClientError, TimeoutError) as err:
|
||||
self.sys_supervisor.connectivity = False
|
||||
raise HassOSUpdateError(
|
||||
f"Can't fetch OTA update from {url}: {err!s}", _LOGGER.error
|
||||
|
@@ -105,11 +105,7 @@ class PluginBase(ABC, FileConfiguration, CoreSysAttributes):
|
||||
if not (event.name == self.instance.name):
|
||||
return
|
||||
|
||||
if event.state in [
|
||||
ContainerState.FAILED,
|
||||
ContainerState.STOPPED,
|
||||
ContainerState.UNHEALTHY,
|
||||
]:
|
||||
if event.state in {ContainerState.FAILED, ContainerState.UNHEALTHY}:
|
||||
await self._restart_after_problem(event.state)
|
||||
|
||||
async def _restart_after_problem(self, state: ContainerState):
|
||||
@@ -123,10 +119,7 @@ class PluginBase(ABC, FileConfiguration, CoreSysAttributes):
|
||||
state,
|
||||
)
|
||||
try:
|
||||
if state == ContainerState.STOPPED and attempts == 0:
|
||||
await self.start()
|
||||
else:
|
||||
await self.rebuild()
|
||||
await self.rebuild()
|
||||
except PluginError as err:
|
||||
attempts = attempts + 1
|
||||
_LOGGER.error("Watchdog restart of %s plugin failed!", self.slug)
|
||||
|
@@ -139,7 +139,7 @@ class PluginObserver(PluginBase):
|
||||
) as request:
|
||||
if request.status == 200:
|
||||
return True
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError):
|
||||
except (aiohttp.ClientError, TimeoutError):
|
||||
pass
|
||||
|
||||
return False
|
||||
|
57
supervisor/resolution/fixups/addon_execute_repair.py
Normal file
57
supervisor/resolution/fixups/addon_execute_repair.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""Helper to fix missing image for addon."""
|
||||
|
||||
import logging
|
||||
|
||||
from ...coresys import CoreSys
|
||||
from ..const import ContextType, IssueType, SuggestionType
|
||||
from .base import FixupBase
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup(coresys: CoreSys) -> FixupBase:
|
||||
"""Check setup function."""
|
||||
return FixupAddonExecuteRepair(coresys)
|
||||
|
||||
|
||||
class FixupAddonExecuteRepair(FixupBase):
|
||||
"""Storage class for fixup."""
|
||||
|
||||
async def process_fixup(self, reference: str | None = None) -> None:
|
||||
"""Pull the addons image."""
|
||||
addon = self.sys_addons.get(reference, local_only=True)
|
||||
if not addon:
|
||||
_LOGGER.info(
|
||||
"Cannot repair addon %s as it is not installed, dismissing suggestion",
|
||||
reference,
|
||||
)
|
||||
return
|
||||
|
||||
if await addon.instance.exists():
|
||||
_LOGGER.info(
|
||||
"Addon %s does not need repair, dismissing suggestion", reference
|
||||
)
|
||||
return
|
||||
|
||||
_LOGGER.info("Installing image for addon %s")
|
||||
await addon.instance.install(addon.version)
|
||||
|
||||
@property
|
||||
def suggestion(self) -> SuggestionType:
|
||||
"""Return a SuggestionType enum."""
|
||||
return SuggestionType.EXECUTE_REPAIR
|
||||
|
||||
@property
|
||||
def context(self) -> ContextType:
|
||||
"""Return a ContextType enum."""
|
||||
return ContextType.ADDON
|
||||
|
||||
@property
|
||||
def issues(self) -> list[IssueType]:
|
||||
"""Return a IssueType enum list."""
|
||||
return [IssueType.MISSING_IMAGE]
|
||||
|
||||
@property
|
||||
def auto(self) -> bool:
|
||||
"""Return if a fixup can be apply as auto fix."""
|
||||
return True
|
@@ -24,6 +24,8 @@ class FixupMountExecuteRemove(FixupBase):
|
||||
await self.sys_mounts.remove_mount(reference)
|
||||
except MountNotFound:
|
||||
_LOGGER.warning("Can't find mount %s for fixup", reference)
|
||||
else:
|
||||
self.sys_mounts.save_data()
|
||||
|
||||
@property
|
||||
def suggestion(self) -> SuggestionType:
|
||||
|
@@ -1,7 +1,11 @@
|
||||
"""Init file for Supervisor add-ons."""
|
||||
|
||||
from copy import deepcopy
|
||||
import logging
|
||||
from typing import Self
|
||||
|
||||
from ..addons.model import AddonModel, Data
|
||||
from ..coresys import CoreSys
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -9,6 +13,11 @@ _LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
class AddonStore(AddonModel):
|
||||
"""Hold data for add-on inside Supervisor."""
|
||||
|
||||
def __init__(self, coresys: CoreSys, slug: str, data: Data | None = None):
|
||||
"""Initialize object."""
|
||||
super().__init__(coresys, slug)
|
||||
self._data: Data | None = data
|
||||
|
||||
def __repr__(self) -> str:
|
||||
"""Return internal representation."""
|
||||
return f"<Store: {self.slug}>"
|
||||
@@ -16,7 +25,7 @@ class AddonStore(AddonModel):
|
||||
@property
|
||||
def data(self) -> Data:
|
||||
"""Return add-on data/config."""
|
||||
return self.sys_store.data.addons[self.slug]
|
||||
return self._data or self.sys_store.data.addons[self.slug]
|
||||
|
||||
@property
|
||||
def is_installed(self) -> bool:
|
||||
@@ -27,3 +36,7 @@ class AddonStore(AddonModel):
|
||||
def is_detached(self) -> bool:
|
||||
"""Return True if add-on is detached."""
|
||||
return False
|
||||
|
||||
def clone(self) -> Self:
|
||||
"""Return a copy that includes data and does not use global store data."""
|
||||
return type(self)(self.coresys, self.slug, deepcopy(self.data))
|
||||
|
@@ -1,5 +1,4 @@
|
||||
"""Home Assistant control object."""
|
||||
import asyncio
|
||||
from collections.abc import Awaitable
|
||||
from contextlib import suppress
|
||||
from datetime import timedelta
|
||||
@@ -129,7 +128,7 @@ class Supervisor(CoreSysAttributes):
|
||||
)
|
||||
data = await request.text()
|
||||
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||
except (aiohttp.ClientError, TimeoutError) as err:
|
||||
self.sys_supervisor.connectivity = False
|
||||
raise SupervisorAppArmorError(
|
||||
f"Can't fetch AppArmor profile {url}: {str(err) or 'Timeout'}",
|
||||
@@ -270,7 +269,7 @@ class Supervisor(CoreSysAttributes):
|
||||
await self.sys_websession.head(
|
||||
"https://checkonline.home-assistant.io/online.txt", timeout=timeout
|
||||
)
|
||||
except (ClientError, asyncio.TimeoutError):
|
||||
except (ClientError, TimeoutError):
|
||||
self.connectivity = False
|
||||
else:
|
||||
self.connectivity = True
|
||||
|
@@ -1,5 +1,4 @@
|
||||
"""Fetch last versions from webserver."""
|
||||
import asyncio
|
||||
from contextlib import suppress
|
||||
from datetime import timedelta
|
||||
import json
|
||||
@@ -207,7 +206,7 @@ class Updater(FileConfiguration, CoreSysAttributes):
|
||||
)
|
||||
data = await request.read()
|
||||
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||
except (aiohttp.ClientError, TimeoutError) as err:
|
||||
self.sys_supervisor.connectivity = False
|
||||
raise UpdaterError(
|
||||
f"Can't fetch versions from {url}: {str(err) or 'Timeout'}",
|
||||
|
@@ -1,13 +1,17 @@
|
||||
"""Tools file for Supervisor."""
|
||||
import asyncio
|
||||
from functools import lru_cache
|
||||
from ipaddress import IPv4Address
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import socket
|
||||
from tempfile import TemporaryDirectory
|
||||
from typing import Any
|
||||
|
||||
from awesomeversion import AwesomeVersion
|
||||
|
||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||
|
||||
RE_STRING: re.Pattern = re.compile(r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))")
|
||||
@@ -35,20 +39,19 @@ def process_lock(method):
|
||||
return wrap_api
|
||||
|
||||
|
||||
def check_port(address: IPv4Address, port: int) -> bool:
|
||||
async def check_port(address: IPv4Address, port: int) -> bool:
|
||||
"""Check if port is mapped."""
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(0.5)
|
||||
sock.setblocking(False)
|
||||
try:
|
||||
result = sock.connect_ex((str(address), port))
|
||||
sock.close()
|
||||
|
||||
# Check if the port is available
|
||||
if result == 0:
|
||||
return True
|
||||
except OSError:
|
||||
pass
|
||||
return False
|
||||
async with asyncio.timeout(0.5):
|
||||
await asyncio.get_running_loop().sock_connect(sock, (str(address), port))
|
||||
except (OSError, TimeoutError):
|
||||
return False
|
||||
finally:
|
||||
if sock is not None:
|
||||
sock.close()
|
||||
return True
|
||||
|
||||
|
||||
def check_exception_chain(err: Exception, object_type: Any) -> bool:
|
||||
@@ -76,31 +79,60 @@ def get_message_from_exception_chain(err: Exception) -> str:
|
||||
return get_message_from_exception_chain(err.__context__)
|
||||
|
||||
|
||||
async def remove_folder(folder: Path, content_only: bool = False) -> None:
|
||||
async def remove_folder(
|
||||
folder: Path,
|
||||
content_only: bool = False,
|
||||
excludes: list[str] | None = None,
|
||||
tmp_dir: Path | None = None,
|
||||
) -> None:
|
||||
"""Remove folder and reset privileged.
|
||||
|
||||
Is needed to avoid issue with:
|
||||
- CAP_DAC_OVERRIDE
|
||||
- CAP_DAC_READ_SEARCH
|
||||
"""
|
||||
del_folder = f"{folder}" + "/{,.[!.],..?}*" if content_only else f"{folder}"
|
||||
if excludes:
|
||||
if not tmp_dir:
|
||||
raise ValueError("tmp_dir is required if excludes are provided")
|
||||
if not content_only:
|
||||
raise ValueError("Cannot delete the folder if excludes are provided")
|
||||
|
||||
temp = TemporaryDirectory(dir=tmp_dir)
|
||||
temp_path = Path(temp.name)
|
||||
moved_files: list[Path] = []
|
||||
for item in folder.iterdir():
|
||||
if any(item.match(exclude) for exclude in excludes):
|
||||
moved_files.append(item.rename(temp_path / item.name))
|
||||
|
||||
find_args = []
|
||||
if content_only:
|
||||
find_args.extend(["-mindepth", "1"])
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"bash",
|
||||
"-c",
|
||||
f"rm -rf {del_folder}",
|
||||
"/usr/bin/find",
|
||||
folder,
|
||||
"-xdev",
|
||||
*find_args,
|
||||
"-delete",
|
||||
stdout=asyncio.subprocess.DEVNULL,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
env=clean_env(),
|
||||
)
|
||||
|
||||
_, error_msg = await proc.communicate()
|
||||
except OSError as err:
|
||||
error_msg = str(err)
|
||||
_LOGGER.exception("Can't remove folder %s: %s", folder, err)
|
||||
else:
|
||||
if proc.returncode == 0:
|
||||
return
|
||||
|
||||
_LOGGER.error("Can't remove folder %s: %s", folder, error_msg)
|
||||
_LOGGER.error(
|
||||
"Can't remove folder %s: %s", folder, error_msg.decode("utf-8").strip()
|
||||
)
|
||||
finally:
|
||||
if excludes:
|
||||
for item in moved_files:
|
||||
item.rename(folder / item.name)
|
||||
temp.cleanup()
|
||||
|
||||
|
||||
def clean_env() -> dict[str, str]:
|
||||
@@ -110,3 +142,11 @@ def clean_env() -> dict[str, str]:
|
||||
if value := os.environ.get(key):
|
||||
new_env[key] = value
|
||||
return new_env
|
||||
|
||||
|
||||
@lru_cache
|
||||
def version_is_new_enough(
|
||||
version: AwesomeVersion, want_version: AwesomeVersion
|
||||
) -> bool:
|
||||
"""Return True if the given version is new enough."""
|
||||
return version >= want_version
|
||||
|
@@ -69,7 +69,7 @@ async def cas_validate(
|
||||
|
||||
async with async_timeout.timeout(15):
|
||||
data, error = await proc.communicate()
|
||||
except asyncio.TimeoutError:
|
||||
except TimeoutError:
|
||||
raise CodeNotaryBackendError(
|
||||
"Timeout while processing CodeNotary", _LOGGER.warning
|
||||
) from None
|
||||
|
@@ -15,18 +15,21 @@ from dbus_fast import (
|
||||
)
|
||||
from dbus_fast.aio.message_bus import MessageBus
|
||||
from dbus_fast.aio.proxy_object import ProxyInterface, ProxyObject
|
||||
from dbus_fast.errors import DBusError
|
||||
from dbus_fast.errors import DBusError as DBusFastDBusError
|
||||
from dbus_fast.introspection import Node
|
||||
|
||||
from ..exceptions import (
|
||||
DBusError,
|
||||
DBusFatalError,
|
||||
DBusInterfaceError,
|
||||
DBusInterfaceMethodError,
|
||||
DBusInterfacePropertyError,
|
||||
DBusInterfaceSignalError,
|
||||
DBusNoReplyError,
|
||||
DBusNotConnectedError,
|
||||
DBusObjectError,
|
||||
DBusParseError,
|
||||
DBusServiceUnkownError,
|
||||
DBusTimeoutError,
|
||||
HassioNotSupportedError,
|
||||
)
|
||||
@@ -62,9 +65,11 @@ class DBus:
|
||||
return self
|
||||
|
||||
@staticmethod
|
||||
def from_dbus_error(err: DBusError) -> HassioNotSupportedError | DBusError:
|
||||
def from_dbus_error(err: DBusFastDBusError) -> HassioNotSupportedError | DBusError:
|
||||
"""Return correct dbus error based on type."""
|
||||
if err.type in {ErrorType.SERVICE_UNKNOWN, ErrorType.UNKNOWN_INTERFACE}:
|
||||
if err.type == ErrorType.SERVICE_UNKNOWN:
|
||||
return DBusServiceUnkownError(err.text)
|
||||
if err.type == ErrorType.UNKNOWN_INTERFACE:
|
||||
return DBusInterfaceError(err.text)
|
||||
if err.type in {
|
||||
ErrorType.UNKNOWN_METHOD,
|
||||
@@ -80,6 +85,8 @@ class DBus:
|
||||
return DBusNotConnectedError(err.text)
|
||||
if err.type == ErrorType.TIMEOUT:
|
||||
return DBusTimeoutError(err.text)
|
||||
if err.type == ErrorType.NO_REPLY:
|
||||
return DBusNoReplyError(err.text)
|
||||
return DBusFatalError(err.text, type_=err.type)
|
||||
|
||||
@staticmethod
|
||||
@@ -102,7 +109,7 @@ class DBus:
|
||||
*args, unpack_variants=True
|
||||
)
|
||||
return await getattr(proxy_interface, method)(*args)
|
||||
except DBusError as err:
|
||||
except DBusFastDBusError as err:
|
||||
raise DBus.from_dbus_error(err)
|
||||
except Exception as err: # pylint: disable=broad-except
|
||||
capture_exception(err)
|
||||
@@ -126,7 +133,9 @@ class DBus:
|
||||
raise DBusParseError(
|
||||
f"Can't parse introspect data: {err}", _LOGGER.error
|
||||
) from err
|
||||
except (EOFError, asyncio.TimeoutError):
|
||||
except DBusFastDBusError as err:
|
||||
raise DBus.from_dbus_error(err)
|
||||
except (EOFError, TimeoutError):
|
||||
_LOGGER.warning(
|
||||
"Busy system at %s - %s", self.bus_name, self.object_path
|
||||
)
|
||||
|
73
supervisor/utils/logging.py
Normal file
73
supervisor/utils/logging.py
Normal file
@@ -0,0 +1,73 @@
|
||||
"""Logging utilities."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
import queue
|
||||
from typing import Any
|
||||
|
||||
|
||||
class SupervisorQueueHandler(logging.handlers.QueueHandler):
|
||||
"""Process the log in another thread."""
|
||||
|
||||
listener: logging.handlers.QueueListener | None = None
|
||||
|
||||
def prepare(self, record: logging.LogRecord) -> logging.LogRecord:
|
||||
"""Prepare a record for queuing.
|
||||
|
||||
This is added as a workaround for https://bugs.python.org/issue46755
|
||||
"""
|
||||
record = super().prepare(record)
|
||||
record.stack_info = None
|
||||
return record
|
||||
|
||||
def handle(self, record: logging.LogRecord) -> Any:
|
||||
"""Conditionally emit the specified logging record.
|
||||
|
||||
Depending on which filters have been added to the handler, push the new
|
||||
records onto the backing Queue.
|
||||
|
||||
The default python logger Handler acquires a lock
|
||||
in the parent class which we do not need as
|
||||
SimpleQueue is already thread safe.
|
||||
|
||||
See https://bugs.python.org/issue24645
|
||||
"""
|
||||
return_value = self.filter(record)
|
||||
if return_value:
|
||||
self.emit(record)
|
||||
return return_value
|
||||
|
||||
def close(self) -> None:
|
||||
"""Tidy up any resources used by the handler.
|
||||
|
||||
This adds shutdown of the QueueListener
|
||||
"""
|
||||
super().close()
|
||||
if not self.listener:
|
||||
return
|
||||
self.listener.stop()
|
||||
self.listener = None
|
||||
|
||||
|
||||
def activate_log_queue_handler() -> None:
|
||||
"""Migrate the existing log handlers to use the queue.
|
||||
|
||||
This allows us to avoid blocking I/O and formatting messages
|
||||
in the event loop as log messages are written in another thread.
|
||||
"""
|
||||
simple_queue: queue.SimpleQueue[logging.Handler] = queue.SimpleQueue()
|
||||
queue_handler = SupervisorQueueHandler(simple_queue)
|
||||
logging.root.addHandler(queue_handler)
|
||||
|
||||
migrated_handlers: list[logging.Handler] = []
|
||||
for handler in logging.root.handlers[:]:
|
||||
if handler is queue_handler:
|
||||
continue
|
||||
logging.root.removeHandler(handler)
|
||||
migrated_handlers.append(handler)
|
||||
|
||||
listener = logging.handlers.QueueListener(simple_queue, *migrated_handlers)
|
||||
queue_handler.listener = listener
|
||||
|
||||
listener.start()
|
@@ -1,5 +1,4 @@
|
||||
"""Small wrapper for haveibeenpwned.com API."""
|
||||
import asyncio
|
||||
import io
|
||||
import logging
|
||||
|
||||
@@ -40,7 +39,7 @@ async def check_pwned_password(websession: aiohttp.ClientSession, sha1_pw: str)
|
||||
_CACHE.add(sha1_short)
|
||||
raise PwnedSecret()
|
||||
|
||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||
except (aiohttp.ClientError, TimeoutError) as err:
|
||||
raise PwnedConnectivityError(
|
||||
f"Can't fetch HIBP data: {str(err) or 'Timeout'}", _LOGGER.warning
|
||||
) from err
|
||||
|
@@ -40,7 +40,7 @@ def init_sentry(coresys: CoreSys) -> None:
|
||||
DedupeIntegration(),
|
||||
AtexitIntegration(),
|
||||
ThreadingIntegration(),
|
||||
LoggingIntegration(level=logging.WARNING, event_level=logging.CRITICAL),
|
||||
LoggingIntegration(level=logging.INFO, event_level=logging.CRITICAL),
|
||||
],
|
||||
release=SUPERVISOR_VERSION,
|
||||
max_breadcrumbs=30,
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user