6 Commits

7 changed files with 165 additions and 19 deletions

39
.github/workflows/vagrant.yml vendored Normal file
View File

@@ -0,0 +1,39 @@
name: homelab-ci
on:
push:
branches:
- main
- testing
jobs:
homelab-ci:
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Cache Vagrant boxes
uses: actions/cache@v3
with:
path: ~/.vagrant.d/boxes
key: ${{ runner.os }}-vagrant-${{ hashFiles('Vagrantfile') }}
restore-keys: |
${{ runner.os }}-vagrant-
- name: Install Ansible
run: brew install ansible@7
- name: Software Versions
run: |
printf "VirtualBox "
vboxmanage --version
vagrant --version
export PATH="/usr/local/opt/ansible@7/bin:$PATH"
ansible --version
- name: Vagrant Up with Dockerbox Playbook
run: |
export PATH="/usr/local/opt/ansible@7/bin:$PATH"
PLAYBOOK=dockerbox vagrant up
vagrant ssh -c "docker ps"

View File

@@ -1,21 +1,3 @@
- name: 'Install Ansible dependency: python3-apt'
ansible.builtin.shell: 'apt-get update && apt-get install python3-apt -y'
args:
creates: /usr/lib/python3/dist-packages/apt
warn: false
- name: Install additional Ansible dependencies
ansible.builtin.apt:
name: "{{ item }}"
state: present
force_apt_get: true
update_cache: true
loop:
- aptitude
- python3-docker
- python3-pymysql
- python3-psycopg2
- name: Create Ansible's temporary remote directory
ansible.builtin.file:
path: "~/.ansible/tmp"

View File

@@ -1,3 +1,6 @@
docker_compose_root: /var/lib/compose
docker_compose: /usr/bin/docker-compose
docker_compose_service: compose
docker_compose: /usr/bin/docker-compose
docker_repos_keys: "{{ docker_repos_path }}/.keys"
docker_repos_keytype: rsa
docker_repos_path: /srv/compose_repos

View File

@@ -17,6 +17,58 @@
mode: 0400
notify: compose_systemd
- name: Create directories to clone docker-compose repositories
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0400
loop:
- "{{ docker_repos_path }}"
- "{{ docker_repos_keys }}"
when: docker_compose_deploy is defined
- name: Generate OpenSSH deploy keys for docker-compose clones
community.crypto.openssh_keypair:
path: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
type: "{{ docker_repos_keytype }}"
mode: 0400
state: present
when: docker_compose_deploy is defined
- name: Clone external docker-compose projects
ansible.builtin.git:
repo: "{{ item.url }}"
dest: "{{ docker_repos_path }}/{{ item.name }}"
version: "{{ item.version | default('main') }}"
force: true
key_file: "{{ docker_repos_keys }}/id_{{ docker_repos_keytype }}"
when: docker_compose_deploy is defined
loop: "{{ docker_compose_deploy }}"
- name: Create directories for docker-compose projects using the systemd service
ansible.builtin.file:
path: "{{ docker_compose_root }}/{{ item.name }}"
state: directory
mode: 0400
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined
- name: Copy docker-compose.yml files to their service directories
ansible.builtin.copy:
src: "{{ docker_repos_path }}/{{ item.name }}/{{ item.path | default('docker-compose.yml') }}"
dest: "{{ docker_compose_root }}/{{ item.name }}/docker-compose.yml"
remote_src: yes
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined
- name: Set environment variables for docker-compose projects
ansible.builtin.template:
src: docker-compose-env.j2
dest: "{{ docker_compose_root }}/{{ item.name }}/.env"
mode: 0400
loop: "{{ docker_compose_deploy }}"
when: docker_compose_deploy is defined and item.env is defined
- name: Add users to docker group
ansible.builtin.user:
name: "{{ item }}"
@@ -30,3 +82,11 @@
name: docker
state: started
enabled: true
- name: Start docker-compose services and enable on boot
ansible.builtin.service:
name: "{{ docker_compose_service }}@{{ item.name }}"
state: started
enabled: true
loop: "{{ docker_compose_deploy }}"
when: item.enabled is defined and item.enabled is true

View File

@@ -0,0 +1,7 @@
# {{ ansible_managed }}
{% if item.env is defined %}
{% for kvpair in item.env.items() %}
{{ kvpair.0 }}={{ kvpair.1 }}
{% endfor %}
{% endif %}

View File

@@ -29,6 +29,8 @@
networks:
- name: "{{ nextcloud_container }}"
- name: traefik
env:
PHP_MEMORY_LIMIT: 1024M
labels:
traefik.http.routers.nextcloud.rule: "Host(`{{ nextcloud_domain }}`)"
traefik.http.routers.nextcloud.entrypoints: websecure
@@ -103,6 +105,13 @@
- "php occ maintenance:mode --off"
when: nextcloud_install.changed
- name: Install Nextcloud background jobs cron
ansible.builtin.cron:
name: Nextcloud background job
minute: "*/5"
job: "/usr/bin/docker exec -u www-data nextcloud /usr/local/bin/php -f /var/www/html/cron.php"
user: root
- name: Remove Nextcloud's CAN_INSTALL file
ansible.builtin.file:
path: "{{ nextcloud_root }}/config/CAN_INSTALL"

46
run-proxy.sh Executable file
View File

@@ -0,0 +1,46 @@
#!/bin/bash
# Find private key file
PRIVATE_KEY="$(find .vagrant -name "private_key")"
# Does the private_key file exist?
if [ ! -f "$PRIVATE_KEY" ]; then
echo "[ERROR] File not found at \"$PRIVATE_KEY\""
exit 1
fi
# Is the private_key a valid SSH key?
echo "Checking validity of private key at $(pwd)/$PRIVATE_KEY"
if ! ssh-keygen -l -f "$PRIVATE_KEY"; then
echo "[Error] The private key at \"$PRIVATE_KEY\" is invalid (CODE: $?)"
exit 1
fi
# Find an IP on the VM for the SSH tunnel
HOST_IP="$(vagrant ssh -c "hostname -I | cut -d' ' -f${HOSTNAME_FIELD:-1}" 2>/dev/null | sed 's/.$//')"
# SSH command to match in processes table
CMD="ssh -fNT -i $PRIVATE_KEY -L 8443:localhost:8443 -L 80:localhost:80 -L 443:localhost:443.*vagrant@$HOST_IP"
# Not just after PIDs
# shellcheck disable=SC2009
PS_TUNNELS="$(ps aux | grep -e "$CMD" | grep -v grep)"
PS_COUNTER="$(echo "$PS_TUNNELS" | wc -l)"
if [ "$PS_COUNTER" -gt 0 ]; then
echo "[ERROR] Tunnel(s) already seems to exist (counted $PS_COUNTER)"
echo \""$PS_TUNNELS"\"
exit 1
fi
# Create an SSH tunnel
echo "Starting background SSH connection for localhost port forwarding"
set -x
ssh -fNT -i "$PRIVATE_KEY" \
-L 8443:localhost:8443 \
-L 80:localhost:80 \
-L 443:localhost:443 \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
vagrant@"${HOST_IP}" 2>/dev/null