Skip to content

Commit

Permalink
initial python version
Browse files Browse the repository at this point in the history
  • Loading branch information
bmanojlovic committed Jan 2, 2025
1 parent cf1a253 commit 0d5fabe
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 246 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
addons: ${{ steps.addons.outputs.addons_list }}
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v3.5.0
uses: actions/checkout@v4

- name: 🔍 Find add-on directories
id: addons
Expand All @@ -33,9 +33,9 @@ jobs:
path: ${{ fromJson(needs.find.outputs.addons) }}
steps:
- name: ⤵️ Check out code from GitHub
uses: actions/checkout@v3.5.0
uses: actions/checkout@v4

- name: 🚀 Run Home Assistant Add-on Lint
uses: frenck/action-addon-linter@v2.11
uses: frenck/action-addon-linter@v2.14
with:
path: "./${{ matrix.path }}"
37 changes: 21 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ on:
push:
branches:
- "master"
- "python"
- "shell"
pull_request:
branches:
- "master"

env:
BUILD_ARGS: "--test"
MONITORED_FILES: "build.yaml config.yaml Dockerfile rootfs"
MONITORED_FILES: "build.yaml config.yaml Dockerfile run.py"

jobs:
init:
Expand All @@ -21,11 +23,14 @@ jobs:
changed: ${{ steps.changed_addons.outputs.changed }}
steps:
- name: Check out the repository
uses: actions/checkout@v3.5.0
uses: actions/checkout@v4

- name: Get changed files
id: changed_files
uses: jitterbit/get-changed-files@v1
uses: tj-actions/changed-files@v40
with:
files: |
**/{build.yaml,config.yaml,Dockerfile,rootfs}/**
- name: Find add-on directories
id: addons
Expand All @@ -36,9 +41,9 @@ jobs:
run: |
declare -a changed_addons
for addon in ${{ steps.addons.outputs.addons }}; do
if [[ "${{ steps.changed_files.outputs.all }}" =~ $addon ]]; then
if [[ "${{ steps.changed_files.outputs.all_changed_files }}" =~ $addon ]]; then
for file in ${{ env.MONITORED_FILES }}; do
if [[ "${{ steps.changed_files.outputs.all }}" =~ $addon/$file ]]; then
if [[ "${{ steps.changed_files.outputs.all_changed_files }}" =~ $addon/$file ]]; then
if [[ ! "${changed_addons[@]}" =~ $addon ]]; then
changed_addons+=("\"${addon}\",");
fi
Expand All @@ -50,11 +55,11 @@ jobs:
changed=$(echo ${changed_addons[@]} | rev | cut -c 2- | rev)
if [[ -n ${changed} ]]; then
echo "Changed add-ons: $changed";
echo "changed=true" >> $GITHUB_OUTPUT;
echo "addons=[$changed]" >> $GITHUB_OUTPUT;
echo "Changed add-ons: $changed"
echo "changed=true" >> $GITHUB_OUTPUT
echo "addons=[$changed]" >> $GITHUB_OUTPUT
else
echo "No add-on had any monitored files changed (${{ env.MONITORED_FILES }})";
echo "No add-on had any monitored files changed (${{ env.MONITORED_FILES }})"
fi
build:
needs: init
Expand All @@ -68,7 +73,7 @@ jobs:

steps:
- name: Check out repository
uses: actions/checkout@v3.5.0
uses: actions/checkout@v4

- name: Get information
id: info
Expand All @@ -80,8 +85,8 @@ jobs:
id: check
run: |
if [[ "${{ steps.info.outputs.architectures }}" =~ ${{ matrix.arch }} ]]; then
echo "build_arch=true" >> $GITHUB_OUTPUT;
echo "image=$(echo ${{ steps.info.outputs.image }} | cut -d'/' -f3)" >> $GITHUB_OUTPUT;
echo "build_arch=true" >> "$GITHUB_OUTPUT"
echo "image=$(echo ${{ steps.info.outputs.image }} | cut -d'/' -f3)" >> "$GITHUB_OUTPUT"
if [[ -z "${{ github.head_ref }}" ]] && [[ "${{ github.event_name }}" == "push" ]]; then
echo "BUILD_ARGS=" >> $GITHUB_ENV;
fi
Expand All @@ -90,22 +95,22 @@ jobs:
echo "build_arch=false" >> $GITHUB_OUTPUT;
fi
- name: Login to Docker Hub
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Login to GitHub Container Registry
if: env.BUILD_ARGS != '--test'
uses: docker/login-action@v2.1.0
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build ${{ matrix.addon }} add-on ghcr
if: steps.check.outputs.build_arch == 'true'
uses: home-assistant/builder@2023.03.0
uses: home-assistant/builder@2024.01.0
with:
args: |
${{ env.BUILD_ARGS }} \
Expand All @@ -117,7 +122,7 @@ jobs:
- name: Build ${{ matrix.addon }} add-on docker hub
if: steps.check.outputs.build_arch == 'true'
uses: home-assistant/builder@2023.03.0
uses: home-assistant/builder@2024.01.0
with:
args: |
${{ env.BUILD_ARGS }} \
Expand Down
21 changes: 16 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
ARG BUILD_FROM
FROM $BUILD_FROM

ENV LANG C.UTF-8
# Set shell
SHELL ["/bin/bash", "-o", "pipefail", "-c"]

# Setup base
ENV LANG=C.UTF-8

RUN apk add --no-cache \
borgbackup \
openssh-keygen \
openssh-client
openssh-client \
jq \
pigz \
python3 \
py3-pip \
py3-psutil \
&& pip3 install --no-cache-dir \
psutil

# Home Assistant CLI
ARG BUILD_ARCH
Expand All @@ -16,7 +27,7 @@ RUN curl -Lso /usr/bin/ha \
&& chmod a+x /usr/bin/ha

# Copy required data for add-on
COPY run.sh /
RUN chmod a+x /run.sh
COPY run.py /
RUN chmod a+x /run.py

CMD [ "/run.sh" ]
CMD [ "python3", "/run.py" ]
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ borg_repo_url: user@host:path/to/repo
Please be aware that you are supposed to use only one way of doing it, as if both are used addon will exit with error.
### SSH Parameters
You can customize the SSH connection using the `borg_ssh_params` option. This allows you to pass additional SSH parameters like:

```yaml
borg_ssh_params: "-p 2222 -o ConnectTimeout=10"
```

Common use cases include:
- Changing the SSH port: `-p PORT`
- Setting connection timeouts: `-o ConnectTimeout=SECONDS`
- Using different key types: `-o IdentityAgent=none`
- Setting compression: `-C`

The SSH parameters will be added to the base SSH command that includes the identity file and known hosts configuration.

When first run addon will provide in its logs information of ssh key that you should set on borg backup server. Example key how it should look like is shown bellow.
```
[00:01:07] INFO: Your ssh key to use for borg backup host
Expand Down Expand Up @@ -64,4 +80,4 @@ addon: xxxx_borg-backup
# Contact and issues
Use [issue tracker](https://github.com/bmanojlovic/home-assistant-borg-backup/issues) on github for any issue with this addon.
Use [issue tracker](https://github.com/bmanojlovic/home-assistant-borg-backup/issues) on github for any issue with this addon.
18 changes: 11 additions & 7 deletions build.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
build_from:
aarch64: homeassistant/aarch64-base:latest
amd64: homeassistant/amd64-base:latest
armhf: homeassistant/armhf-base:latest
armv7: homeassistant/armv7-base:latest
i386: homeassistant/i386-base:latest

aarch64: "ghcr.io/home-assistant/aarch64-base-python:"
armv7: "ghcr.io/home-assistant/armv7-base-python:"
armhf: "ghcr.io/home-assistant/armhf-base-python:"
amd64: "ghcr.io/home-assistant/amd64-base-python:"
i386: "ghcr.io/home-assistant/i386-base-python:"
labels:
org.opencontainers.image.title: "Home Assistant Add-on: Borg Backup"
org.opencontainers.image.description: "Makes backup to remote or local borg repository"
org.opencontainers.image.source: "https://github.com/bmanojlovic/home-assistant-borg-backup"
org.opencontainers.image.licenses: "Apache License 2.0"
args:
CLI_VERSION: "4.23.0"
CLI_VERSION: "4.36.0"
9 changes: 5 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: "Borgbackup for homeassistant"
version: "1.2"
name: "Borg Backup for Home Assistant"
version: "1.3.0"
slug: "borg_backup"
description: "Borgbackup!"
description: "Secure and efficient backup solution using Borg Backup"
url: "https://github.com/bmanojlovic/home-assistant-borg-backup/"
image: "bmanojlovic/{arch}-borg-backup"
image: "ghcr.io/bmanojlovic/{arch}-borg-backup"
init: false
arch:
- aarch64
- amd64
Expand Down
3 changes: 2 additions & 1 deletion docker_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export DOCKER_USER="bmanojlovic"
docker run --rm --privileged \
-v ~/.docker:/root/.docker \
homeassistant/amd64-builder --all -t borg_backup \
-r https://github.com/bmanojlovic/home-assistant-addons -b master
-r https://github.com/bmanojlovic/home-assistant-addons -b master

88 changes: 44 additions & 44 deletions local_deploy_sshfs.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
#!/bin/bash
function _log {
D=$(date +"%Y%m%dT%H:%M:%S")
echo -e $D "$@"
}
function log_error {
_log "\e[0;31mERROR\e[0m : $@"
exit -1
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

# Logging functions
function log_error() {
printf '\e[31mERROR:\e[0m %s\n' "$*" >&2
exit 1
}

function log_warn {
ISSUE=$(($ISSUE + 1 ))
_log "\e[0;33mWARN\e[0m : $@"
}
function log_info {
_log "\e[1;32mINFO\e[0m : $@"
function log_warn() {
printf '\e[33mWARN:\e[0m %s\n' "$*" >&2
((ISSUE++))
}

function remote_exec {
ssh -t $REMOTE_HOST "sh -lc \"set -e;$@\""
function log_info() {
printf '\e[32mINFO:\e[0m %s\n' "$*" >&2
}

####### CONFIGURATION #######
export MOUNT_POINT=/home/steki/addons
export REMOTE_HOST=hassio
#### END CONFIGURATION ######

function remote_exec() {
ssh -t "${REMOTE_HOST}" "sh -lc \"set -e;$*\""
}

L=$(LANG=C df -h ${MOUNT_POINT}|grep -c hassio)
# Configuration
readonly MOUNT_POINT="/home/steki/addons"
readonly REMOTE_HOST="hassio"

if [ $L -eq 1 ]; then
#replace...
log_info "already mounted"
# Check if already mounted
if LANG=C df -h "${MOUNT_POINT}" | grep -q hassio; then
log_info "Already mounted"
else
log_info "mounting sshfs"
sshfs ${REMOTE_HOST}:/addons ${MOUNT_POINT} || ( log_error "failed sshfs mount" )
log_info "Mounting sshfs"
if ! sshfs "${REMOTE_HOST}:/addons" "${MOUNT_POINT}"; then
log_error "Failed sshfs mount"
fi
fi

rm -rf ${MOUNT_POINT}/borg-backup ||:
mkdir -p ${MOUNT_POINT}/borg-backup
cp -a * ${MOUNT_POINT}/borg-backup/
log_info "deployed source"

CMD="ha addons reload"
remote_exec "$CMD"

CMD="ha addons rebuild local_borg-backup"
remote_exec "$CMD"


CMD="ha addons restart local_borg-backup"
remote_exec "$CMD"

# Clean and deploy
rm -rf "${MOUNT_POINT}/borg-backup" || true
mkdir -p "${MOUNT_POINT}/borg-backup"
cp -a ./* "${MOUNT_POINT}/borg-backup/"
log_info "Deployed source"

# Execute Home Assistant commands
for cmd in \
"ha addons reload" \
"ha addons rebuild local_borg-backup" \
"ha addons restart local_borg-backup"; do
log_info "Executing: ${cmd}"
remote_exec "${cmd}"
done

# Wait for restart and show logs
sleep 2
CMD="ha addons logs local_borg-backup"
remote_exec "$CMD"
log_info "Showing logs"
remote_exec "ha addons logs local_borg-backup"
Loading

0 comments on commit 0d5fabe

Please sign in to comment.