-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip Fix #14 automatically rebuild vpn servers
- Loading branch information
1 parent
533ea93
commit e9b43e4
Showing
8 changed files
with
128 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,9 @@ name: Add VPN User | |
on: | ||
workflow_dispatch: | ||
inputs: | ||
wireguard_vpn_ip_address: | ||
description: 'wireguard_vpn_ip_address' | ||
ansible_vault_password: | ||
description: 'ansible_vault_password' | ||
required: true | ||
default: '192.0.2.1' | ||
|
||
ssh_username: | ||
description: 'ssh_username' | ||
required: true | ||
default: 'root' | ||
|
||
PSONO_SECRET_ID: | ||
description: 'PSONO_SECRET_ID' | ||
|
@@ -25,19 +19,31 @@ jobs: | |
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setting up SSH key | ||
uses: webfactory/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.VPN_SSH_PRIVATE_KEY }} | ||
|
||
- name: SSH into remote host | ||
- name: Setup ansible | ||
run: | | ||
ssh -o StrictHostKeyChecking=no ${{ inputs.ssh_username }}@${{ inputs.wireguard_vpn_ip_address }} "date" | ||
pip3 install ansible | ||
- name: Generate new vpn peer config & Add save client config to password manager | ||
run: | | ||
ssh -o StrictHostKeyChecking=no ${{ inputs.ssh_username }}@${{ inputs.wireguard_vpn_ip_address }} -C "cd /etc/wireguard && PSONO_CI_API_KEY_ID=${{ secrets.PSONO_CI_API_KEY_ID }} PSONO_CI_API_SECRET_KEY_HEX=${{ secrets.PSONO_CI_API_SECRET_KEY_HEX }} PSONO_CI_SERVER_URL=${{ secrets.PSONO_CI_SERVER_URL }} PSONO_SECRET_ID=${{ inputs.PSONO_SECRET_ID }} ./add-vpn-user.sh" | ||
- name: Run ansible | ||
run: | | ||
cd src/vpn | ||
echo Creating ANSIBLE_VAULT_PASSWORD_FILE | ||
TMPFILE=$(mktemp) | ||
trap "rm -f ${TMPFILE@Q}" EXIT | ||
echo -n "${{ inputs.ansible_vault_password }}" > $TMPFILE | ||
export ANSIBLE_VAULT_PASSWORD_FILE=$TMPFILE | ||
echo About to run playbook add-vpn-user.yml | ||
# Bend over backwards to keep storing secrets in ansible vault rather than a specific CI/CD runner. | ||
# note this uses dynamic inventory. Since you can't set/get group_vars from a dynamic inventory, | ||
# this is an 'inventive' way using 'localhost' group_vars to the vpn hosts (in this case hetzer VM(s)) | ||
# api token out of ansible vault. The api token is stord in group_vars/localhost.yml (where in fact) | ||
# the api token is used on other host groups. | ||
# The dynamic vpn hosts inventory is using the dynamic inventory file inventory-vpn-servers-hcloud.yml | ||
PSONO_SECRET_ID=${{ inputs.PSONO_SECRET_ID }} ansible-playbook --extra-vars _vault_hetzner_cloud_token=$(ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ANSIBLE_STDOUT_CALLBACK=ansible.posix.json ansible localhost -i inventory.ini -m debug -a "msg={{ hostvars[inventory_hostname].hetzner_hcloud_token }}" | jq '.plays[0]["tasks"][0]["hosts"]["localhost"]["msg"]') -i inventory-vpn-servers-hcloud.yml playbooks/add-vpn-user.yml | ||
rm $TMPFILE | ||
# Enable tmate debugging of manually-triggered workflows if the input option was provided | ||
- name: Setup tmate session | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
plugin: hetzner.hcloud.hcloud | ||
api_token: "{{ _vault_hetzner_cloud_token }}" | ||
groups: | ||
# Get only instances named 'vpn-server' | ||
vpn_servers: name == 'vpn-server' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,69 @@ | ||
--- | ||
- name: | ||
hosts: localhost | ||
ignore_unreachable: true | ||
|
||
tasks: | ||
- name: Attempt to destroy vpn-server regardless if exists or not | ||
hetzner.hcloud.server: | ||
api_token: "{{ hetzner_hcloud_token }}" | ||
name: vpn-server | ||
location: hel1 | ||
state: absent | ||
ignore_errors: true | ||
|
||
- name: Ensure public ssh_key published to Hetzner | ||
hetzner.hcloud.ssh_key: | ||
api_token: "{{ hetzner_hcloud_token }}" | ||
name: server_bootstrap_public_ssh_key | ||
public_key: "{{ server_bootstrap_public_ssh_key }}" | ||
state: present | ||
ignore_errors: true | ||
|
||
- name: Create/Rebuild vpn-server | ||
hetzner.hcloud.server: | ||
api_token: "{{ hetzner_hcloud_token }}" | ||
name: vpn-server | ||
location: eu-central | ||
server_type: cx11 | ||
location: hel1 | ||
server_type: cx22 | ||
image: ubuntu-20.04 | ||
state: rebuild | ||
state: present | ||
ssh_keys: | ||
- server_bootstrap_public_ssh_key | ||
register: hcloud_server | ||
|
||
- name: Store vpn_server_ipv4_address and vpn_server_ipv6_address | ||
ansible.builtin.set_fact: | ||
vpn_server_ipv4_address: "{{ hcloud_server.hcloud_server['ipv4_address'] }}" | ||
vpn_server_ipv6_address: "{{ hcloud_server.hcloud_server['ipv6'] }}" | ||
|
||
- name: Attempt to start vpn-server | ||
hetzner.hcloud.server: | ||
api_token: "{{ hetzner_hcloud_token }}" | ||
name: vpn-server | ||
state: started | ||
ignore_errors: true | ||
retries: 3 | ||
delay: 5 | ||
|
||
- name: Display vpn_server_ipv4_address & vpn_server_ipv6_address | ||
debug: | ||
msg: | ||
- "{{ vpn_server_ipv4_address }}" | ||
- "{{ vpn_server_ipv6_address }}" | ||
|
||
- name: Replace first changeme with vpn_server_ipv4_address and second changeme with root | ||
ansible.builtin.replace: | ||
path: ../inventory.ini | ||
regexp: '^(changeme)(.*ansible_user=)(changeme)$' | ||
replace: '{{ vpn_server_ipv4_address }}\2root' | ||
|
||
- name: Wait for port 22 on vpn_server | ||
ansible.builtin.wait_for: | ||
host: "{{ vpn_server_ipv4_address }}" | ||
port: 22 | ||
state: started | ||
|
||
- name: Include deploy-vpn.yml play | ||
ansible.builtin.import_playbook: deploy-vpn-client.yml | ||
vars: | ||
vpn_servers: "{{ vpn_server_ipv4_address }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters