Finished Archer A9 Router Rebooter

This commit is contained in:
Ashish D'Souza 2023-06-07 05:52:17 -04:00
parent 805365259c
commit 32f5d9147a
8 changed files with 205 additions and 1 deletions

29
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,29 @@
pipeline {
agent any
stages {
stage('Deploy') {
when {
branch pattern: '^(master|main)$|stable|release', comparator: 'REGEXP'
}
steps {
httpRequest outputFile: 'get_service_config.yaml', url: 'https://gist.githubusercontent.com/computer-geek64/3ed7241e7a74ad6ddd00409900b590e4/raw/39d371d4040b319663bfd339ea1aa56bd37cdf77/get_service_config.yaml'
ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, extras: "--extra-vars 'service=\"archer_a9_router_rebooter\"'", playbook: 'get_service_config.yaml'
script {
archer_a9_router_rebooter_config = readJSON file: 'archer_a9_router_rebooter_config.json'
}
ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, extras: "--extra-vars 'router_password=\"${archer_a9_router_rebooter_config.router.password}\"'", playbook: 'install.yaml'
ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, playbook: 'stop.yaml'
ansiblePlaybook credentialsId: 'rivendell-ssh-key', disableHostKeyChecking: true, playbook: 'start.yaml'
}
}
}
post {
cleanup {
cleanWs()
}
}
}

83
install.yaml Normal file
View File

@ -0,0 +1,83 @@
- name: Install Archer A9 Router Rebooter
hosts: Rivendell
vars_prompt:
- name: router_password
prompt: Enter password for Archer A9 router
tasks:
- name: Create Archer A9 Router Rebooter logs Docker volume directory
become: true
ansible.builtin.file:
path: /data/archer-a9-router-rebooter-logs
state: directory
- name: Create Archer A9 Router Rebooter logs Docker volume
community.docker.docker_volume:
volume_name: archer-a9-router-rebooter-logs
driver: local
driver_options:
type: none
o: bind
device: /data/archer-a9-router-rebooter-logs
state: present
- name: Create temporary Docker build directory
ansible.builtin.tempfile:
state: directory
register: docker_build_dir
- name: Copy Docker build directory
ansible.builtin.copy:
src: rebooter/
dest: '{{docker_build_dir.path}}'
mode: preserve
- name: Build archer-a9-router-rebooter Docker image
community.docker.docker_image:
build:
path: '{{docker_build_dir.path}}'
name: archer-a9-router-rebooter
tag: latest
source: build
force_source: true
state: present
- name: Remove temporary Docker build directory
ansible.builtin.file:
path: '{{docker_build_dir.path}}'
state: absent
- name: Read homelab config
ansible.builtin.slurp:
src: '{{ansible_user_dir}}/.homelab.json'
register: homelab_config_file
- name: Set homelab_config variable
ansible.builtin.set_fact:
homelab_config: '{{homelab_config_file.content|b64decode|from_json}}'
- name: Insert into Postgres table service
vars:
archer_a9_router_rebooter_config:
router:
password: '{{router_password}}'
community.postgresql.postgresql_query:
login_host: '{{homelab_config.database.host}}'
login_user: '{{homelab_config.database.user}}'
login_password: '{{homelab_config.database.password}}'
db: '{{homelab_config.database.name}}'
query: 'INSERT INTO service (name, host, config) VALUES (%s, %s, %s) ON CONFLICT DO NOTHING;'
positional_args:
- '{{item.name}}'
- '{{item.host}}'
- '{{item.config}}'
with_items:
- {name: archer_a9_router_rebooter, host: '{{ansible_hostname}}', config: '{{archer_a9_router_rebooter_config|to_json}}'}
- name: Insert into Postgres table service_data
community.postgresql.postgresql_query:
login_host: '{{homelab_config.database.host}}'
login_user: '{{homelab_config.database.user}}'
login_password: '{{homelab_config.database.password}}'
db: '{{homelab_config.database.name}}'
query: 'INSERT INTO service_data (service, data_name, storage_type, source) VALUES (%s, %s, %s, %s) ON CONFLICT DO NOTHING;'
positional_args:
- '{{item.service}}'
- '{{item.data_name}}'
- '{{item.storage_type}}'
- '{{item.source}}'
with_items:
- {service: archer_a9_router_rebooter, data_name: logs, storage_type: docker, source: archer-a9-router-rebooter-logs}

View File

@ -11,4 +11,8 @@ RUN pip3 install --upgrade pip
COPY requirements.txt . COPY requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
RUN wget -q https://github.com/mozilla/geckodriver/releases/download/v0.33.0/geckodriver-v0.33.0-linux64.tar.gz
RUN tar -xzf geckodriver-v0.33.0-linux64.tar.gz -C /usr/local/bin
RUN rm geckodriver-v0.33.0-linux64.tar.gz
COPY src . COPY src .

View File

@ -19,7 +19,7 @@ class Router:
return exit_code == 0 return exit_code == 0
def check_lan_connectivity(self): def check_lan_connectivity(self):
return self._ping(ip) return self._ping(self.ip)
def check_internet_connectivity(self): def check_internet_connectivity(self):
return self._ping('8.8.8.8') return self._ping('8.8.8.8')

39
start.yaml Normal file
View File

@ -0,0 +1,39 @@
- name: Start Archer A9 Router Rebooter
hosts: Rivendell
tasks:
- name: Read homelab config
ansible.builtin.slurp:
src: '{{ansible_user_dir}}/.homelab.json'
register: homelab_config_file
- name: Set homelab_config variable
ansible.builtin.set_fact:
homelab_config: '{{homelab_config_file.content|b64decode|from_json}}'
- name: Get Archer A9 Router Rebooter config
community.postgresql.postgresql_query:
login_host: '{{homelab_config.database.host}}'
login_user: '{{homelab_config.database.user}}'
login_password: '{{homelab_config.database.password}}'
db: '{{homelab_config.database.name}}'
query: 'SELECT config FROM service WHERE name = %s;'
positional_args:
- archer_a9_router_rebooter
register: archer_a9_router_rebooter_config_query
- name: Set archer_a9_router_rebooter_config variable
ansible.builtin.set_fact:
archer_a9_router_rebooter_config: '{{archer_a9_router_rebooter_config_query.query_result[0].config}}'
- name: Start Archer A9 Router Rebooter Docker container
community.docker.docker_container:
name: archer-a9-router-rebooter
image: archer-a9-router-rebooter:latest
restart_policy: unless-stopped
env:
ROUTER_IP: 192.168.0.1
ROUTER_PASSWORD: '{{archer_a9_router_rebooter_config.router.password}}'
volumes:
- archer-a9-router-rebooter-logs:/logs
- /etc/localtime:/etc/localtime:ro
network_mode: host
keep_volumes: false
state: started

7
stop.yaml Normal file
View File

@ -0,0 +1,7 @@
- name: Stop Archer A9 Router Rebooter
hosts: Rivendell
tasks:
- name: Stop Archer A9 Router Rebooter Docker container
community.docker.docker_container:
name: archer-a9-router-rebooter
state: absent

42
uninstall.yaml Normal file
View File

@ -0,0 +1,42 @@
- name: Stop Archer A9 Router Rebooter
ansible.builtin.import_playbook: stop.yaml
- name: Uninstall Archer A9 Router Rebooter
hosts: Rivendell
tasks:
- name: Remove Archer A9 Router Rebooter logs Docker volume
community.docker.docker_volume:
volume_name: archer-a9-router-rebooter-logs
state: absent
- name: Remove Archer A9 Router Rebooter logs Docker volume directory
become: true
ansible.builtin.file:
path: /data/archer-a9-router-rebooter-logs
state: absent
- name: Read homelab config
ansible.builtin.slurp:
src: '{{ansible_user_dir}}/.homelab.json'
register: homelab_config_file
- name: Set homelab_config variable
ansible.builtin.set_fact:
homelab_config: '{{homelab_config_file.content|b64decode|from_json}}'
- name: Delete from Postgres table service_data
community.postgresql.postgresql_query:
login_host: '{{homelab_config.database.host}}'
login_user: '{{homelab_config.database.user}}'
login_password: '{{homelab_config.database.password}}'
db: '{{homelab_config.database.name}}'
query: 'DELETE FROM service_data WHERE service = %s;'
positional_args:
- archer_a9_router_rebooter
- name: Delete from Postgres table service
community.postgresql.postgresql_query:
login_host: '{{homelab_config.database.host}}'
login_user: '{{homelab_config.database.user}}'
login_password: '{{homelab_config.database.password}}'
db: '{{homelab_config.database.name}}'
query: 'DELETE FROM service WHERE name = %s;'
positional_args:
- archer_a9_router_rebooter