Skip to content

Commit

Permalink
Merge pull request #10 from O1ahmad/ahmad/add_binary_download_feat
Browse files Browse the repository at this point in the history
feat: add functionality for automatically downloading and extracting binaries with systemd setups
  • Loading branch information
O1ahmad authored Jun 20, 2024
2 parents 661ed57 + e4ba769 commit 0162dd3
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .ansible-lint-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file contains ignores rule violations for ansible-lint
.ansible-lint schema[ansible-lint-config]
meta/main.yml schema[meta]
tasks/common/download-binary.yml jinja[spacing]
tasks/systemd/setup.yml var-naming[no-role-prefix]
3 changes: 2 additions & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
name: Basic-Service Continuous-Integration
run-name: CI ${{ github.repository }} - ${{ github.ref }} job 🚀
on:
Expand All @@ -14,6 +15,6 @@ jobs:
- name: Lint yaml and Ansible configs
run: |
python -m pip install --upgrade pip
pip install ansible-lint==6.12.1 yamllint==1.29.0
pip install ansible-lint==24.6.0 yamllint==1.35.1
yamllint --config-file ./tests/yaml-lint.yml .
ansible-lint --config-file ./tests/ansible-lint.yml .
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ Systemd, [Docker SDK](https://docker-py.readthedocs.io/en/stable/) for Python (f

| var | description | default |
| :-------------: | :--------------------------------------------------------: | :--------------: |
| _setupMode_ | infrastructure provisioning setup mode (`container, k8s, systemd`) | `container` |
| _setup_mode_ | infrastructure provisioning setup mode (`container, k8s, systemd`) | `container` |
| _name_ | name of service to deploy | **required** |
| _image_ | service container image to deploy | "" |
| _binary_url_ | URL of the binary file to download | "" |
| _binary_file_name_override_ | Override the binary file name after moving it to the destination directory | "" |
| _destination_directory_ | directory where the binary file will be placed after downloading/extracting | `/usr/local/bin` |
| _command_ | Command and arguments to execute on startup | **required** |
| _user_ | service container image to deploy | `root` |
| _config_ | configuration files associated with the service to mount | `{}` |
| _configEnv_ | environment variables to set within the service runtime | `{}` |
| _config_env_ | environment variables to set within the service runtime | `{}` |
| _ports_ | listening port information for a service | `{}` |
| _setup_iptables_ | configure IP tables to allow ingress paths | `false` |
| _hostDataDir_ | host directory to store node runtime/operational data | `/var/tmp` |
| _dataDir_ | container directory to store node runtime/operational data | `/tmp` |
| _workDir_ | operational directory to store runtime artifacts | `/var/tmp` |
| _restartPolicy_ | container restart policy | `unless-stopped` |
| _work_dir_ | operational directory to store runtime artifacts | `/var/tmp` |
| _restart_policy_ | container restart policy | `unless-stopped` |
| _systemd_ | Systemd deployment custom unit, service and install properties | `{}` |
| _cpus_ | available CPU resources each deployed service can use | `1.0` |
| _memory_ | available memory resources each deployed service can use | `4g` |
Expand Down
22 changes: 13 additions & 9 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
# name: basic-service (for #systemd setup)
# image: <insert-image-repo-path> (for #container setup)
image: ""
binary_url: ""

setupMode: container
workDir: /var/tmp
# (Optional) Override the binary file name after moving it to the destination directory
binary_file_name_override:
destination_directory: /usr/local/bin
setup_mode: container
work_dir: /var/tmp

command: ""
ports: {}
Expand Down Expand Up @@ -37,10 +41,10 @@ config: {}
#
# echo "Hello World!

configEnv: {}
config_env: {}
# RUN_ARGS: --help

dataDirs: {}
data_dirs: {}
# chain-data:
# hostPath: /mnt/data
# appPath: /app/data
Expand All @@ -50,7 +54,7 @@ resources:
memRequest: 1G
memLimit: 1G

restartPolicy: unless-stopped
restart_policy: unless-stopped

systemd: {}
# unit_properties: {}
Expand All @@ -60,6 +64,6 @@ systemd: {}
uninstall: false

# default helper vars
volumeList: []
portList: []
systemdEnvironmentDirective: ""
volume_list: []
port_list: []
systemd_environment_directive: ""
2 changes: 1 addition & 1 deletion handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- name: Remove generated service config files
become: true
ansible.builtin.file:
path: "{{ workDir }}/{{ item.value.destinationPath }}"
path: "{{ work_dir }}/{{ item.value.destinationPath }}"
state: absent
with_dict: "{{ config }}"
listen: Container uninstall
Expand Down
2 changes: 1 addition & 1 deletion helm/templates/tests/test-connect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ spec:
- --request
- GET
- '{{ include "basic-service.fullname" . }}:{{ .Values.servicePort }}/'
restartPolicy: Never
restart_policy: Never
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ galaxy_info:
role_name: basic_service
namespace: 0x0i
description: Ansible role for running any software process/service
company: "O1.IO"
company: O1.IO
license: MIT
min_ansible_version: "2.5"
platforms:
Expand Down
48 changes: 48 additions & 0 deletions tasks/common/download-binary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
- name: Ensure destination directory exists
ansible.builtin.file:
path: "{{ destination_directory }}"
state: directory
mode: "0755"

- name: Download the binary file
ansible.builtin.get_url:
url: "{{ binary_url }}"
dest: /tmp/{{ binary_file_name }}
mode: "0755"
vars:
binary_file_name: "{{ binary_url | basename }}"

- name: Unarchive the file if it's a tar archive
ansible.builtin.unarchive:
src: /tmp/{{ binary_file_name }}
dest: "{{ destination_directory }}"
remote_src: true
when: binary_file_name.endswith('.tar') or binary_file_name.endswith('.tar.gz') or binary_file_name.endswith('.tgz')

- name: Unzip the file if it's a zip archive
unzip:
src: /tmp/{{ binary_file_name }}
dest: "{{ destination_directory }}"
when: binary_file_name.endswith('.zip')

- name: Move the file if it's not an archive

Check warning on line 29 in tasks/common/download-binary.yml

View workflow job for this annotation

GitHub Actions / Build-Test-Service-Setup

jinja[spacing]

Jinja2 spacing could be improved: mv /tmp/{{ binary_file_name }} {{ destination_directory }}/{{ binary_file_name_override|default(binary_file_name) }}
ansible.builtin.command: >
mv /tmp/{{ binary_file_name }} {{ destination_directory }}/{{ binary_file_name_override|default(binary_file_name) }}
when: >
not (binary_file_name.endswith('.tar') or
binary_file_name.endswith('.tar.gz') or
binary_file_name.endswith('.tgz') or
binary_file_name.endswith('.zip')
register: mv_output
changed_when: mv_output.rc != 0

- name: Ensure the file has executable permissions
ansible.builtin.file:
path: "{{ destination_directory }}/{{ binary_file_name_override | default(binary_file_name) }}"
mode: "0755"
when: >
not (binary_file_name.endswith('.tar') or
binary_file_name.endswith('.tar.gz') or
binary_file_name.endswith('.tgz') or
binary_file_name.endswith('.zip')
4 changes: 2 additions & 2 deletions tasks/common/network-setup.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
- name: Determine service ingress port list for iptables config
ansible.builtin.set_fact:
ingressList: "{{ ingressList + [item.value.ingressPort | string] }}"
ingress_list: "{{ ingress_list + [item.value.ingressPort | string] }}"
with_dict: "{{ ports }}"

- name: Allow service ingress ports in iptables setup
ansible.builtin.iptables:
chain: INPUT
protocol: tcp
destination_ports: "{{ ingressList }}"
destination_ports: "{{ ingress_list }}"
jump: ACCEPT
8 changes: 4 additions & 4 deletions tasks/common/storage-setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
state: directory
owner: "{{ ansible_user }}"
group: "{{ ansible_user }}"
mode: 0777
mode: "0777"

- name: Ensure existence of operations/work dir
become: true
ansible.builtin.file:
path: "{{ workDir }}{{ item.value.destinationPath | dirname }}"
path: "{{ work_dir }}{{ item.value.destinationPath | dirname }}"
state: directory
mode: 0777
mode: "0777"
with_dict: "{{ config }}"

- name: Generate local rendering of service config files
ansible.builtin.copy:
content: "{{ item.value.data | default(omit) }}"
dest: "{{ workDir }}/{{ item.value.destinationPath }}"
dest: "{{ work_dir }}/{{ item.value.destinationPath }}"
mode: "{{ item.value.mode | default(644) }}"
src: "{{ item.value.sourcePath | default(omit) }}"
with_dict: "{{ config }}"
16 changes: 8 additions & 8 deletions tasks/container/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

- name: Determine service container volume list
ansible.builtin.set_fact:
volumeList: "{{ volumeList + [workDir + item.value.destinationPath + ':' + item.value.destinationPath] }}"
volume_list: "{{ volume_list + [work_dir + item.value.destinationPath + ':' + item.value.destinationPath] }}"
with_dict: "{{ config }}"

- name: Add data DIRs to container volume list
ansible.builtin.set_fact:
volumeList: "{{ volumeList + [item.value.hostPath + ':' + item.value.appPath] }}"
with_dict: "{{ dataDirs }}"
volume_list: "{{ volume_list + [item.value.hostPath + ':' + item.value.appPath] }}"
with_dict: "{{ data_dirs }}"

- name: Determine service container port list
ansible.builtin.set_fact:
portList: "{{ portList + [item.value.ingressPort | string + ':' + item.value.servicePort | string] }}"
port_list: "{{ port_list + [item.value.ingressPort | string + ':' + item.value.servicePort | string] }}"
with_dict: "{{ ports }}"

# ---
Expand All @@ -35,9 +35,9 @@
image: "{{ image }}"
user: "{{ user | default(omit) }}:{{ group | default(omit) }}"
command: "{{ command | default(omit) }}"
env: "{{ configEnv }}"
published_ports: "{{ portList }}"
volumes: "{{ volumeList }}"
env: "{{ config_env }}"
published_ports: "{{ port_list }}"
volumes: "{{ volume_list }}"
cpus: "{{ resources.cpuLimit }}"
memory: "{{ resources.memLimit }}"
restart_policy: "{{ restartPolicy }}"
restart_policy: "{{ restart_policy }}"
6 changes: 3 additions & 3 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
- name: Manage storage/data setup
ansible.builtin.include_tasks: "common/storage-setup.yml"
ansible.builtin.include_tasks: common/storage-setup.yml

- name: Setup service infrastructure topology
ansible.builtin.include_tasks: "{{ setupMode }}/setup.yml"
ansible.builtin.include_tasks: "{{ setup_mode }}/setup.yml"

- name: Manage networking and IP tables setup
when: setup_iptables|bool
ansible.builtin.include_tasks: "common/network-setup.yml"
ansible.builtin.include_tasks: common/network-setup.yml
9 changes: 5 additions & 4 deletions tasks/systemd/setup.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
# Compile systemd unit settings
- name: Download service binary
ansible.builtin.include_tasks: ../common/download-binary.yml

- name: Determine service Environment directives
ansible.builtin.set_fact:
systemdEnvironmentDirective: "{{ systemdEnvironmentDirective + ' ' + item.key + '=' + item.value }}"
with_dict: "{{ configEnv }}"
systemd_environment_directive: "{{ systemd_environment_directive + ' ' + item.key + '=' + item.value }}"
with_dict: "{{ config_env }}"

- name: Set default Sysetmd unit configurations
ansible.builtin.set_fact:
Expand All @@ -15,7 +16,7 @@
ExecStart: "{{ command | default(name) }}"
User: "{{ user | default(omit) }}"
Group: "{{ group | default(user) | default(omit) }}"
Environment: "{{ systemdEnvironmentDirective }}"
Environment: "{{ systemd_environment_directive }}"
CPUQuota: "{{ cpus }}"
MemoryHigh: "{{ memory }}"
default_unit_install:
Expand Down

0 comments on commit 0162dd3

Please sign in to comment.