Merge pull request #2 from armbian/test

Add Hetzner cloud action script
This commit is contained in:
Igor Pečovnik
2023-04-20 14:46:42 +02:00
committed by GitHub
4 changed files with 152 additions and 163 deletions

20
hetzner/README.md Normal file
View File

@@ -0,0 +1,20 @@
<p align="left"><img src="https://www.versio.io/img/supported-technology/hetzner-cloud.svg"></p
<h1>Hetzner runner deployments</h1>
## Usage
```yaml
- name: Enable Hetzner Virtual Machines
uses: armbian/actions/hetzner@main
with:
action-type: enable # enable or disable VM cluster
machine-type: cax21|cax31|... # machine type
machine-id: 0,1,2 ... # selected machine
runners-count: 1,2,3 # runners per machine
machine-count: 1,2,3 # numbers of machines
key: ${{ secrets.KEY_TORRENTS }} # key which we have in the Hezner API
known_hosts: ${{ secrets.KNOWN_HOSTS_TORRENTS }} # key_knowns_hosts which we have in the Hezner API
hetzner_id: ${{ secrets.HETZNER_ONE }} # hetzner API token
github_token: ${{ secrets.ACCESS_TOKEN }} # github action token
```

132
hetzner/action.yml Normal file
View File

@@ -0,0 +1,132 @@
name: "Enable Hetzner VMs"
author: "Igor Pecovnik"
inputs:
action-type:
required: true
machine-type:
required: true
machine-id:
required: false
machine-count:
required: true
runners-count:
required: true
key:
required: true
known_hosts:
required: true
hetzner_id:
required: true
github_token:
required: true
concurrency:
cancel-in-progress: false
runs:
using: "composite"
steps:
- name: Install SSH key for storage
if: ${{ github.repository_owner == 'Armbian' }}
uses: shimataro/ssh-key-action@v2
with:
key: ${{ inputs.key }}
known_hosts: ${{ inputs.known_hosts }}
if_key_exists: replace
- name: Install Homebrew
if: ${{ github.repository_owner == 'Armbian' }}
shell: bash
run: |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> ${HOME}/.bash_profile
- name: Install CLI for Hetzner Cloud
if: ${{ github.repository_owner == 'Armbian' }}
shell: bash
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install hcloud
hcloud server list
- name: "Kill runners"
if: ${{ github.repository_owner == 'Armbian' && inputs.action-type == 'enable' }}
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS: 3650
shell: bash
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
machinenames=("Faerie" "November" "Raven" "Hammer" "Foxtrot" "Papa" "Chimera" "Panther")
machinename="${machinenames[${{ inputs.machine-id }}]}"
machinenameid=$(hcloud server list --output columns=id,name | grep ${machinename} | cut -d" " -f1 || true)
[[ -n "${machinenameid}" ]] && hcloud server delete "${machinenameid}" || true
- name: "Kill runners"
if: ${{ github.repository_owner == 'Armbian' && inputs.action-type == 'disable' }}
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS: 3650
shell: bash
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# we will set fixed number and fixed machine names
machinenames=("Faerie" "November" "Raven" "Hammer" "Foxtrot" "Papa" "Chimera" "Panther")
for machinename in "${machinenames[@]}"
do
machinenameid=$(hcloud server list --output columns=id,name | grep ${machinename} | cut -d" " -f1 || true)
[[ -n "${machinenameid}" ]] && hcloud server delete "${machinenameid}" || true
done
- name: "Deploy runners"
if: ${{ inputs.action-type == 'enable' }}
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
GH_TOKEN: ${{ inputs.github_token }}
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS: 3650
shell: bash
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
machinenames=("Faerie" "November" "Raven" "Hammer" "Foxtrot" "Papa" "Chimera" "Panther")
machinename="${machinenames[${{ inputs.machine-id }}]}"
hcloud server create --name ${machinename} --image ubuntu-22.04 --type ${{ inputs.machine-type }} --ssh-key TORRENT
# make sure all are up
sleep 30
IP=$(hcloud server list --output columns=ipv4,name | grep ${machinename} | cut -d" " -f1)
ssh -o StrictHostKeyChecking=no root@${IP} "\
export STOP=${{ inputs.runners-count}} NAME=${machinename} GH_TOKEN=${{ env.GH_TOKEN }}; \
git clone https://github.com/armbian/scripts; cd scripts/generate-runners; ./deploy.sh"
- name: Remove runners
if: ${{ inputs.action-type == 'disable' }}
env:
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
RUNNERS=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/armbian/os/actions/runners?per_page=1000" | \
jq -r '.runners[] | select(.labels[].name=="temp")' | jq '.id')
while IFS= read -r id; do
gh api --silent \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/armbian/os/actions/runners/${id}
done <<< $RUNNERS

View File

@@ -1,92 +0,0 @@
name: "Enable Hetzner VMs"
author: "Igor Pecovnik"
inputs:
machine-type:
required: true
machine-count:
required: true
runners-count:
required: true
key:
required: true
known_hosts:
required: true
hetzner_id:
required: true
github_token:
required: true
runs:
using: "composite"
steps:
- name: Install SSH key for storage
uses: shimataro/ssh-key-action@v2
with:
key: ${{ inputs.key }}
known_hosts: ${{ inputs.known_hosts }}
if_key_exists: replace
- name: Install Homebrew
shell: bash
run: |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> ${HOME}/.bash_profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install hcloud
- name: Install Homebrew
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
# we will use fixed machine names.
machinenames=("Faerie" "November" "Raven" "Hammer" "Foxtrot" "Papa" "Chimera" "Panther")
for machinename in "${machinenames[@]}"
do
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
hcloud server delete $(hcloud server list --output columns=id,name | grep ${machinename} | cut -d" " -f1)
i=$(( i + 1 ))
[[ $i == ${{ inputs.machine-count }} ]] && break
done
- name: Remove runners
env:
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
# for some reason last added doesn't show up
#RUNNERS=$(gh api \
#-H "Accept: application/vnd.github+json" \
#-H "X-GitHub-Api-Version: 2022-11-28" \
#"/orgs/${{ github.repository_owner }}/actions/runners?per_page=1000" | \
#jq -r '.runners[] | select(.labels[].name=="temp")' | jq '.id')
# get all runners that have label "temp"
RUNNERS=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${{ github.repository_owner }}/os/actions/runners?per_page=1000" | \
jq -r '.runners[] | select(.labels[].name=="temp")' | jq '.id')
# remove them
# while IFS= read -r id; do
#gh api \
#--method DELETE \
#-H "Accept: application/vnd.github+json" \
#-H "X-GitHub-Api-Version: 2022-11-28" \
#/orgs/${{ github.repository_owner }}/actions/runners/${id}
#done <<< $RUNNERS
# remove them
while IFS= read -r id; do
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository_owner }}/os/actions/runners/${id}
done <<< $RUNNERS

View File

@@ -1,71 +0,0 @@
name: "Enable Hetzner VMs"
author: "Igor Pecovnik"
inputs:
machine-type:
required: true
machine-count:
required: true
runners-count:
required: true
key:
required: true
known_hosts:
required: true
hetzner_id:
required: true
github_token:
required: true
runs:
using: "composite"
steps:
- name: Install SSH key for storage
uses: shimataro/ssh-key-action@v2
with:
key: ${{ inputs.key }}
known_hosts: ${{ inputs.known_hosts }}
if_key_exists: replace
- name: Install Homebrew
shell: bash
run: |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
(echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> ${HOME}/.bash_profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
brew install hcloud
- name: Install Homebrew
env:
HCLOUD_TOKEN: ${{ inputs.hetzner_id }}
GH_TOKEN: ${{ inputs.github_token }}
shell: bash
run: |
COMMAND="git clone https://github.com/armbian/scripts; cd scripts/generate-runners; ./deploy.sh"
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# we will use fixed machine names.
machinenames=("Faerie" "November" "Raven" "Hammer" "Foxtrot" "Papa" "Chimera" "Panther")
for machinename in "${machinenames[@]}"
do
hcloud server delete $(hcloud server list --output columns=id,name | grep ${machinename} | cut -d" " -f1) || true
hcloud server create --name ${machinename} --image ubuntu-22.04 --type ${{ inputs.machine-type }} --ssh-key TORRENT
i=$(( i + 1 ))
[[ $i == ${{ inputs.machine-count }} ]] && break
done
# make sure all are up
sleep 20
i=0
for machinename in "${machinenames[@]}"
do
IP=$(hcloud server list --output columns=ipv4,name | grep ${machinename} | cut -d" " -f1)
ssh-keygen -f "$HOME/.ssh/known_hosts" -R "${IP}" || true
ssh -o StrictHostKeyChecking=no root@${IP} "export STOP=${{ inputs.runners-count}} NAME=${machinename} GH_TOKEN=${{ env.GH_TOKEN }}; $COMMAND"
i=$(( i + 1 ))
[[ $i == ${{ inputs.machine-count }} ]] && break
done