diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..e8bba33 --- /dev/null +++ b/Jenkinsfile @@ -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() + } + } +} diff --git a/install.yaml b/install.yaml new file mode 100644 index 0000000..b7296fc --- /dev/null +++ b/install.yaml @@ -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} diff --git a/Dockerfile b/rebooter/Dockerfile similarity index 52% rename from Dockerfile rename to rebooter/Dockerfile index 3b116a4..25af983 100644 --- a/Dockerfile +++ b/rebooter/Dockerfile @@ -11,4 +11,8 @@ RUN pip3 install --upgrade pip COPY 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 . diff --git a/requirements.txt b/rebooter/requirements.txt similarity index 100% rename from requirements.txt rename to rebooter/requirements.txt diff --git a/src/rebooter.py b/rebooter/src/rebooter.py similarity index 98% rename from src/rebooter.py rename to rebooter/src/rebooter.py index f22b3f0..93ce24c 100644 --- a/src/rebooter.py +++ b/rebooter/src/rebooter.py @@ -19,7 +19,7 @@ class Router: return exit_code == 0 def check_lan_connectivity(self): - return self._ping(ip) + return self._ping(self.ip) def check_internet_connectivity(self): return self._ping('8.8.8.8') diff --git a/start.yaml b/start.yaml new file mode 100644 index 0000000..b0d9914 --- /dev/null +++ b/start.yaml @@ -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 diff --git a/stop.yaml b/stop.yaml new file mode 100644 index 0000000..c8372ce --- /dev/null +++ b/stop.yaml @@ -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 diff --git a/uninstall.yaml b/uninstall.yaml new file mode 100644 index 0000000..15b1536 --- /dev/null +++ b/uninstall.yaml @@ -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