From 57046f7ee9a2359bf12d60067b4b4925d254eb81 Mon Sep 17 00:00:00 2001 From: Ahmad Wilson Date: Thu, 26 Jan 2023 12:07:20 -0500 Subject: [PATCH] initial commit --- README.md | 69 ++++++++++++++++++++++++++++++++++ defaults/main.yml | 38 +++++++++++++++++++ handlers/main.yml | 22 +++++++++++ meta/main.yml | 24 ++++++++++++ tasks/common/storage-setup.yml | 9 +++++ tasks/container/setup.yml | 49 ++++++++++++++++++++++++ tasks/main.yml | 6 +++ tests/inventory | 2 + tests/test.yml | 6 +++ vars/main.yml | 2 + 10 files changed, 227 insertions(+) create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 handlers/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/common/storage-setup.yml create mode 100644 tasks/container/setup.yml create mode 100644 tasks/main.yml create mode 100644 tests/inventory create mode 100644 tests/test.yml create mode 100644 vars/main.yml diff --git a/README.md b/README.md new file mode 100644 index 0000000..d61c9c9 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ + + +

ansible logo

+ +# Ansible Role 🔗 Base-Service + +Configure and operate a base service: running anything from cypto blockchain clients to the vast list of cloud-native services + +## Requirements + +[Docker SDK](https://docker-py.readthedocs.io/en/stable/) for Python (for Python 2.6 support, use the deprecated `docker-py` library instead) or installation of the `docker` engine. + +## Role Variables + +| var | description | default | +| :-------------: | :--------------------------------------------------------: | :--------------: | +| _setupMode_ | infrastructure provisioning setup mode | `container` | +| _name_ | name of service to deploy | `required` | +| _image_ | service container image to deploy | `required` | +| _config_ | configuration files associated with the service to mount | `{}` | +| _configEnv_ | environment variables to set within the service runtime | `{}` | +| _ports_ | listening port information for a service | `{}` | +| _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` | +| _cpus_ | available CPU resources each deployed component can use | `1.0` | +| _memory_ | available memory resources each deployed component can use | `4g` | +| _uninstall_ | whether to remove installed components and artifacts | `false` | + +## Dependencies + +``` +collections: +- name: community.docker +``` + +## Example Playbook + +``` +- hosts: servers + roles: +``` + +- Launch a container that sleeps for infinity: + +``` + - role: o1labs.cloud.base-service + vars: + name: sleepy-service + image: busybox:latest + command: ["sleep", "infinity"] +``` + +## License + +MIT + +## Author Information + +This Ansible role was created in 2023 by O1.IO. + +🏆 **always happy to help & donations are always welcome** 💸 + +- **ETH (Ethereum):** 0x652eD9d222eeA1Ad843efec01E60C29bF2CF6E4c + +- **BTC (Bitcoin):** 3E8gMxwEnfAAWbvjoPVqSz6DvPfwQ1q8Jn + +- **ATOM (Cosmos):** cosmos19vmcf5t68w6ug45mrwjyauh4ey99u9htrgqv09 diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..c11ef38 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,38 @@ +--- +setup_mode: container +name: default-example +image: busbox:latest +command: "" +ports: + example-port1: + ingressPort: 80 + servicePort: 2411 + example-port2: + ingressPort: 443 + servicePort: 2412 + +config: + example-config1: + destinationPath: /config/example-1.yaml + data: | + a: 1 + b: 2 + c: + d: 3 + example-config2: + destintionPath: /config/example-2.toml + data: | + [a] + b=1 + [c] + d=2 + +configEnv: {} + +resources: + cpuLimit: 1 + memRequest: 1Gi + memLimit: 1Gi + +restart_policy: unless-stopped +uninstall: false diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..82a3c0c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,22 @@ +--- +- name: Uninstall service container + community.docker.docker_container: + name: "{{ name }}" + state: absent + purge_networks: yes + listen: Container uninstall + +- name: Remove generated service config files + become: true + ansible.builtin.file: + path: "{{ workDir }}/{{ item.destinationPath | basename }}" + state: absent + with_dict: "{{ config }}" + listen: Container uninstall + +- name: Remove service data directory + become: true + ansible.builtin.file: + path: "{{ hostDataDir }}" + state: absent + listen: Container uninstall diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..d6b1071 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,24 @@ +# @format +--- +galaxy_info: + author: O1 Labs + role_name: base-service + description: Ansible role for running any software process/service + company: "O1.IO" + license: MIT + min_ansible_version: 2.5 + platforms: + - name: Debian + versions: + - jessie + - stretch + - name: Ubuntu + versions: + - xenial + - bionic + - cosmic + galaxy_tags: + - cloud + - base + - service + - template diff --git a/tasks/common/storage-setup.yml b/tasks/common/storage-setup.yml new file mode 100644 index 0000000..0adca45 --- /dev/null +++ b/tasks/common/storage-setup.yml @@ -0,0 +1,9 @@ +--- +- name: Ensure existence of host data dir + become: true + file: + path: "{{ hostDataDir }}" + state: directory + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + mode: 0777 diff --git a/tasks/container/setup.yml b/tasks/container/setup.yml new file mode 100644 index 0000000..d2a9ce5 --- /dev/null +++ b/tasks/container/setup.yml @@ -0,0 +1,49 @@ +--- +- name: Uninstall provisioned container components + when: uninstall|bool == true + block: + - name: Broadcast uninstall signal + command: /bin/true + notify: Container uninstall + always: + - name: End play following uninstallation + meta: end_play + +- name: Ensure existence of operations/work dir + become: true + file: + path: "{{ workDir }}" + state: directory + mode: 0777 + +- name: Generate local rendering of service config files + copy: + content: "{{ item.value.data }}" + dest: "{{ item.value.destinationPath }}" + with_dict: "{{ config }}" + +- set_fact: + volumeList: |- + [ + {% for conf in config %} + "{{ workDir }}/{{ conf[destinationPath] | basename}}:{{ conf[destinationPath] }}", + {% endfor %} + ] + portList: |- + [ + {% for port in ports %} + "{{ port[ingressPort] }}:{{ port[servicePort] }}", + {% endfor %} + ] + +- name: Start service container + community.docker.docker_container: + name: "{{ name }}" + image: "{{ image }}" + command: "{{ command }}" + env: "{{ configEnv }}" + published_ports: "{{ portList }}" + volumes: "{{ volumeList }}" + cpus: "{{ resources.cpuLimit }}" + memory: "{{ resources.memLimit }}" + restart_policy: "{{ restartPolicy }}" diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..efabc63 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,6 @@ +--- +- name: Manage storage/data setup + include_tasks: "common/storage-setup.yml" + +- name: Setup service infrastructure topology + include_tasks: "{{ setupMode }}/setup.yml" diff --git a/tests/inventory b/tests/inventory new file mode 100644 index 0000000..878877b --- /dev/null +++ b/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/tests/test.yml b/tests/test.yml new file mode 100644 index 0000000..5c91a6b --- /dev/null +++ b/tests/test.yml @@ -0,0 +1,6 @@ +# @format +--- +- hosts: localhost + remote_user: root + roles: + - base-service diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..83ddb1c --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for base-service