Skip to content

Instantly share code, notes, and snippets.

@dale3h
Last active September 24, 2018 18:39
Show Gist options
  • Save dale3h/8cf94b896f30a30b86d17b49554ff66d to your computer and use it in GitHub Desktop.
Save dale3h/8cf94b896f30a30b86d17b49554ff66d to your computer and use it in GitHub Desktop.

Revisions

  1. dale3h revised this gist Sep 18, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions speck
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    readonly BACKUP_DIR=/backup/speck

    usage() {
    echo "usage: $(basename $0) addon_name" 2>&1
    exit 1
    echo "usage: $(basename "$0") addon_name" 2>&1
    exit 1
    }

    addon_install() {
  2. dale3h revised this gist Sep 18, 2018. 1 changed file with 88 additions and 14 deletions.
    102 changes: 88 additions & 14 deletions speck
    Original file line number Diff line number Diff line change
    @@ -1,37 +1,111 @@
    #!/bin/bash

    function usage() {
    readonly BACKUP_DIR=/backup/speck

    usage() {
    echo "usage: $(basename $0) addon_name" 2>&1
    exit 1
    }

    function addon_install() {
    hassio addons install --name "$1"
    addon_install() {
    local addon="${1}"
    hassio addons install --name "${addon}"
    }

    function addon_uninstall() {
    hassio addons uninstall --name "$1"
    addon_uninstall() {
    local addon="${1}"
    hassio addons uninstall --name "${addon}"
    }

    function addon_reload() {
    addon_reload() {
    hassio addons reload
    }

    function main() {
    readonly ADDON_NAME="$1"
    addon_config_backup() {
    local addon="${1}"
    local backup_file
    local result
    local config
    local ex_code

    # Ensure backup directory exists
    if [[ ! -d "${BACKUP_DIR}" ]]; then
    mkdir -p "${BACKUP_DIR}"
    fi

    # Create backup file name
    backup_file="${BACKUP_DIR}/${addon}.json"

    # Get addon config from Hass.io
    result=$(hassio addons info --name "${addon}")
    ex_code="$?"

    # Check for exit code
    if [[ "${ex_code}" -ne 0 ]]; then
    echo "fail (${result})"
    return "${ex_code}"
    fi

    # Pull config from results
    config=$(jq --raw-output ".data.options // empty" <<< "${result}")

    # Test for empty config
    if [[ -z "${config}" ]]; then
    echo "fail (Empty config)"
    return 1
    fi

    # Save config to file
    echo "${config}" > "${backup_file}"
    echo "ok"
    }

    addon_config_restore() {
    local addon="${1}"
    local backup_file="${BACKUP_DIR}/${addon}.json"
    local config
    local options
    local ex_code

    # Check if backup file exists
    if [[ ! -f "${backup_file}" ]]; then
    echo "fail (Missing backup file)"
    return 1
    fi

    # Read backup file
    config=$(jq --compact-output "." "${backup_file}")
    options="{\"options\":${config}}"

    # Restore config
    curl -sS -X "POST" -H "X-HASSIO-KEY: ${HASSIO_TOKEN}" --data-binary "${options}" "http://hassio/addons/${addon}/options"
    ex_code="$?"

    if [[ -z "$ADDON_NAME" ]]; then
    echo
    return "${ex_code}"
    }

    main() {
    local addon="${1}"

    if [[ -z "${addon}" ]]; then
    usage
    fi

    echo -n "Uninstalling $ADDON_NAME: "
    addon_uninstall "$ADDON_NAME" || echo
    echo -n "Backup config: "
    addon_config_backup "${addon}"

    echo -n "Reloading addons: "
    echo -n "Uninstall ${addon}: "
    addon_uninstall "${addon}" || echo

    echo -n "Reload addons: "
    addon_reload || exit 1

    echo -n "Installing $ADDON_NAME: "
    addon_install "$ADDON_NAME" || exit 1
    echo -n "Install ${addon}: "
    addon_install "${addon}" || exit 1

    echo -n "Restore config: "
    addon_config_restore "${addon}"
    }

    if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
  3. dale3h created this gist Sep 7, 2018.
    39 changes: 39 additions & 0 deletions speck
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/bin/bash

    function usage() {
    echo "usage: $(basename $0) addon_name" 2>&1
    exit 1
    }

    function addon_install() {
    hassio addons install --name "$1"
    }

    function addon_uninstall() {
    hassio addons uninstall --name "$1"
    }

    function addon_reload() {
    hassio addons reload
    }

    function main() {
    readonly ADDON_NAME="$1"

    if [[ -z "$ADDON_NAME" ]]; then
    usage
    fi

    echo -n "Uninstalling $ADDON_NAME: "
    addon_uninstall "$ADDON_NAME" || echo

    echo -n "Reloading addons: "
    addon_reload || exit 1

    echo -n "Installing $ADDON_NAME: "
    addon_install "$ADDON_NAME" || exit 1
    }

    if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "${@}"
    fi