From 601a4f8724973f3aa713b3792093721e8195cd30 Mon Sep 17 00:00:00 2001 From: nolim1t Date: Tue, 7 Jul 2020 11:23:07 +0700 Subject: [PATCH] Initialize this repository --- .github/workflows/on-push.yml | 60 ++++++++++++++++++++++++++++++++ .github/workflows/on-tag.yml | 65 +++++++++++++++++++++++++++++++++++ Dockerfile | 12 +++++++ switch.sh | 63 +++++++++++++++++++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 .github/workflows/on-push.yml create mode 100644 .github/workflows/on-tag.yml create mode 100644 Dockerfile create mode 100755 switch.sh diff --git a/.github/workflows/on-push.yml b/.github/workflows/on-push.yml new file mode 100644 index 0000000..aa995c1 --- /dev/null +++ b/.github/workflows/on-push.yml @@ -0,0 +1,60 @@ +name: Docker build on push +env: + DOCKER_CLI_EXPERIMENTAL: enabled + +on: push + +jobs: + build: + runs-on: ubuntu-18.04 + name: Build and push all the stuff defined in this repo + steps: + - name: Set env variables + run: echo "::set-env name=BRANCH::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//-/g')" + + - name: Show set env variables + run: | + printf " BRANCH: %s\n" "$BRANCH" + + - name: Login to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + + - name: Checkout project + uses: actions/checkout@v2 + + - name: Setup Docker buildx action + uses: crazy-max/ghaction-docker-buildx@v1 + id: buildx + with: + buildx-version: latest + qemu-version: latest + + - name: Show available Docker buildx platforms + run: echo ${{ steps.buildx.outputs.platforms }} + + - name: Cache Docker layers + uses: actions/cache@v2 + id: cache + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Run Docker buildx against the current branch (neutrino-switcher) + run: | + docker buildx build \ + --cache-from "type=local,src=/tmp/.buildx-cache" \ + --cache-to "type=local,dest=/tmp/.buildx-cache" \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag ${{ secrets.DOCKER_HUB_USER }}/neutrino-switcher:$BRANCH \ + --output "type=registry" . + + - name: build neutrino-switcher against latest (neutrino-switcher) + run: | + docker buildx build \ + --cache-from "type=local,src=/tmp/.buildx-cache" \ + --cache-to "type=local,dest=/tmp/.buildx-cache" \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag ${{ secrets.DOCKER_HUB_USER }}/lnd-unlock:latest \ + --output "type=registry" . diff --git a/.github/workflows/on-tag.yml b/.github/workflows/on-tag.yml new file mode 100644 index 0000000..82d3285 --- /dev/null +++ b/.github/workflows/on-tag.yml @@ -0,0 +1,65 @@ +name: Docker build on tag +env: + DOCKER_CLI_EXPERIMENTAL: enabled + +on: + push: + tags: + - v[0-9]+.[0-9]+.[0-9]+ + - v[0-9]+.[0-9]+.[0-9]+-* + +jobs: + build: + runs-on: ubuntu-18.04 + name: Build Neutrino switcher + steps: + - name: Setup Environment + run: echo ::set-env name=TAG::${GITHUB_REF/refs\/tags\//} + + - name: Show set environment variables + run: | + printf " TAG: %s\n" "$TAG" + + - name: Login to Docker Hub + run: echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + + - name: Checkout project + uses: actions/checkout@v2 + + - name: Setup Docker buildx action + uses: crazy-max/ghaction-docker-buildx@v1 + id: buildx + with: + buildx-version: latest + qemu-version: latest + + - name: Show available Docker buildx platforms + run: echo ${{ steps.buildx.outputs.platforms }} + + - name: Cache Docker layers + uses: actions/cache@v2 + id: cache + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + + - name: Run Docker buildx against the tag (neutrino-switcher) + run: | + docker buildx build \ + --cache-from "type=local,src=/tmp/.buildx-cache" \ + --cache-to "type=local,dest=/tmp/.buildx-cache" \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag ${{ secrets.DOCKER_HUB_USER }}/neutrino-switcher:$TAG \ + --output "type=registry" . + + - name: build neutrino-switcher against latest (neutrino-switcher) + run: | + docker buildx build \ + --cache-from "type=local,src=/tmp/.buildx-cache" \ + --cache-to "type=local,dest=/tmp/.buildx-cache" \ + --platform linux/amd64,linux/arm64,linux/arm/v7 \ + --tag ${{ secrets.DOCKER_HUB_USER }}/neutrino-switcher:latest \ + --output "type=registry" . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ec5dbee --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM alpine:3.10 + +RUN apk add --no-cache curl jq bash + +RUN mkdir /lnd/ +RUN mkdir /secrets/ +RUN mkdir /statuses/ + +COPY switch.sh /bin/switch +RUN chmod +x /bin/switch + +ENTRYPOINT ["switch"] diff --git a/switch.sh b/switch.sh new file mode 100755 index 0000000..613d4a9 --- /dev/null +++ b/switch.sh @@ -0,0 +1,63 @@ +#!/bin/bash -e + +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. + +# Directories +# Allow access to /secrets/rpcpass.txt +# Allow access to LND directory (use /lnd/lnd.conf) +# Allow access to 'statuses'. /statuses/ + +# Output: /statuses/node-status-bitcoind-ready (when ready, where a service can pick it up) +# Then +PASSWORD=`cat /secrets/rpcpass.txt` +JSONRPCURL="http://10.254.2.2:8332" + +while true; do + IS_NEUTRINO=`grep -c 'bitcoin.node=neutrino' /lnd/lnd.conf` + if [ $IS_NEUTRINO -eq 1 ]; then + echo "If set to neutrino then lets check" + + INFO=`curl --user lncm:$PASSWORD --data-binary '{"jsonrpc": "1.0", "id":"switchme", "method": "getblockchaininfo", "params": [] }' $JSONRPCURL 2>/dev/null` + # check for errors + ERROR=`echo $INFO | jq .error` + if [ ! -z $ERROR ]; then + # if no errors + # Check prune mode + PRUNE_MODE=`echo $INFO | jq .result.pruned` + # check headers + HEADERS=`echo $INFO | jq .result.headers` + # check blocks + BLOCKS=`echo $INFO | jq .result.blocks` + + if [ $PRUNE_MODE != "true" ]; then + echo "Not pruned" + # Node pruned so lets do the switching + echo "Checking if synced...." + if [ ! -f /statuses/node-status-bitcoind-ready ]; then + if [ $HEADERS -eq $BLOCKS ]; then + echo "Bitcoind has been switched across to neutrino" + touch /statuses/node-status-bitcoind-ready + sed -i 's/bitcoin.node\=neutrino/bitcoin.node\=bitcoind/g; ' /lnd/lnd.conf + fi + fi + else + echo "No need to switch from neutrino in pruned mode" + fi + else + # if bitcoind error + echo "Error from bitcoind" + echo $ERROR + fi + else + echo "Neutrino mode has been disabled" + #TODO: Lets maybe try to switch back + fi + # Run every every 1 hour + sleep 3600 +done