From 62665a67b793c6f7a4fd4fec6a994c4fe71a3ca8 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Fri, 18 Nov 2022 02:44:05 -0500 Subject: [PATCH] Deploy a simple webserver docker-compose stack The beginning of a revamp of FRITA infrastructure into containers --- .gitignore | 10 ++-- README.md | 30 ++++++------ Vagrantfile | 42 ++++++++--------- ansible.cfg | 3 +- dev/vars/webserver.yml | 5 ++ dev/webserver.yml | 8 ++++ group_vars/all | 46 ------------------- roles/ansible/tasks/main.yml | 22 --------- roles/docker/defaults/main.yml | 3 ++ roles/docker/tasks/main.yml | 25 ++++++++++ roles/webserver/defaults/main.yml | 1 + roles/webserver/files/docker-compose.yml | 30 ++++++++++++ roles/webserver/handlers/main.yml | 23 ++-------- roles/webserver/tasks/main.yml | 58 +++++++++--------------- roles/webserver/templates/compose-env.j2 | 4 ++ webserver.yml | 22 +-------- 16 files changed, 144 insertions(+), 188 deletions(-) create mode 100644 dev/vars/webserver.yml create mode 100644 dev/webserver.yml delete mode 100644 group_vars/all delete mode 100644 roles/ansible/tasks/main.yml create mode 100644 roles/docker/defaults/main.yml create mode 100644 roles/docker/tasks/main.yml create mode 100644 roles/webserver/defaults/main.yml create mode 100644 roles/webserver/files/docker-compose.yml create mode 100644 roles/webserver/templates/compose-env.j2 diff --git a/.gitignore b/.gitignore index 0654ec8..2213c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,4 @@ -# Vagrant files +environments +.playbook .vagrant - -# Unneeded ansible file -*.retry - -# Custom environments -/environments/ +.vscode diff --git a/README.md b/README.md index eab6390..16718bf 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,26 @@ -# Free I.T. Athen’s Infrastructure -Ansible code used to deploy and maintain websites and services used by Free I.T. Athens. +# Free I.T. Athen's Infrastructure +This project is used to develop Ansible for deploying and maintaining websites +and services operated by Free I.T. Athens. -## Getting Started -frita-infra is developed in Ansible 2.7.5 using Vagrant 2.2.2 + vagrant-libvirt as a test environment. +- Requires Ansible and Vagrant on the host -Check it out by simply typing: `vagrant up` - -## Versioning -We use [SemVer](http://semver.org/) for versioning. For the versions available, see the tags on this repository. +## Quick Start +1. Clone this project +2. Run `vagrant up` to provision a Debian 11 base box ## Authors -* **Kris Lamoureux** - *Project Founder* - [krislamo](https://github.com/krislamo) +* **Kris Lamoureux** - *Project Founder* - @[krislamo](https://github.com/krislamo) ## Copyrights and Licenses -Copyright (C) 2019 Free I.T. Athens +Copyright (C) 2019, 2020, 2022 Free I.T. Athens -This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, version 3 of the License. -This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. -You should have received a copy of the GNU General Public License along with this program. If not, see . +You should have received a copy of the GNU General Public License along with +this program. If not, see . diff --git a/Vagrantfile b/Vagrantfile index 9221854..a4265f5 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,28 +1,24 @@ -# Copyright (C) 2019 Free I.T. Athens -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - # vi: set ft=ruby : +# Set PLAYBOOK shell var for ./dev/playbook.yml +PLAYBOOK=ENV["PLAYBOOK"] +if !PLAYBOOK + if File.exist?('.playbook') + PLAYBOOK = IO.read('.playbook').split("\n")[0] + end + + if !PLAYBOOK || PLAYBOOK.empty? + PLAYBOOK = "webserver" + end +else + File.write(".playbook", PLAYBOOK) +end + +# Debian 11 Vagrant.configure("2") do |config| - - # Debian Stable box - config.vm.box = "debian/stretch64" + config.vm.box = "debian/bullseye64" config.vm.synced_folder ".", "/vagrant", disabled: true - - # Set static IP - config.vm.network "private_network", ip: "192.168.121.2" + config.vm.network "private_network", type: "dhcp" # Machine Name config.vm.define :frita do |frita| # @@ -35,9 +31,9 @@ Vagrant.configure("2") do |config| # Provision with Ansible config.vm.provision "ansible" do |ansible| + ENV['ANSIBLE_ROLES_PATH'] = File.dirname(__FILE__) + "/roles" ansible.compatibility_mode = "2.0" - ansible.playbook = "site.yml" + ansible.playbook = "dev/" + PLAYBOOK + ".yml" end end - diff --git a/ansible.cfg b/ansible.cfg index 1a88ca1..ddb62ee 100644 --- a/ansible.cfg +++ b/ansible.cfg @@ -1,7 +1,6 @@ [defaults] inventory = ./environments/development -interpreter_python = /usr/bin/python +interpreter_python = /usr/bin/python3 [ssh_connection] pipelining=True - diff --git a/dev/vars/webserver.yml b/dev/vars/webserver.yml new file mode 100644 index 0000000..67b1fc7 --- /dev/null +++ b/dev/vars/webserver.yml @@ -0,0 +1,5 @@ +docker_users: + - vagrant + +webserver_env: + TRAEFIK_DOMAIN: traefik.example.org diff --git a/dev/webserver.yml b/dev/webserver.yml new file mode 100644 index 0000000..06bba64 --- /dev/null +++ b/dev/webserver.yml @@ -0,0 +1,8 @@ +- name: Install FRITA Web Server + hosts: all + become: true + vars_files: + - vars/webserver.yml + roles: + - docker + - webserver diff --git a/group_vars/all b/group_vars/all deleted file mode 100644 index 699a75f..0000000 --- a/group_vars/all +++ /dev/null @@ -1,46 +0,0 @@ -### WordPress Configuration ### - -# Domain -wp_domain: www.freeitathens.org -wp_admin_email: contact@freeitathens.org - -# Version of WordPress to deploy -wp_version: 5.1.1 -wp_sha1_hash: f1bff89cc360bf5ef7086594e8a9b68b4cbf2192 - -# WordPress Home Directory -# Note: value is a directory without trailing '/' -wp_dir: /var/www/wordpress - -# WordPress Database Settings -wp_db_host: localhost -wp_db_name: wordpress -wp_db_user: wordpress_user -wp_db_pass: Password1 -wp_db_table_prefix: wp_ - - -### Nextcloud Configuration ### - -# Domain -nc_domain: cloud.freeitathens.org -nc_admin_email: contact@freeitathens.org - -# Version of Nextcloud to deploy -nc_version: 15.0.2 -nc_sha256_hash: c1f4cc33e39994ddbe6777370b62c30b7ae52136a0530c0b9922770803ca0fea - -# Nextcloud Home Directory -# Note: value is a directory without trailing '/' -nc_dir: /var/www/nextcloud - -# Nextcloud Database Settings -nc_db_host: localhost -nc_db_name: nextcloud -nc_db_user: nextcloud_user -nc_db_pass: Password1 - -# Nextcloud Admin -nc_admin: admin -nc_admin_pass: Password1 - diff --git a/roles/ansible/tasks/main.yml b/roles/ansible/tasks/main.yml deleted file mode 100644 index 74cf37a..0000000 --- a/roles/ansible/tasks/main.yml +++ /dev/null @@ -1,22 +0,0 @@ -- name: 'Install Ansible dependency: python-apt' - shell: 'apt-get update && apt-get install python-apt -y' - args: - creates: /usr/lib/python2.7/dist-packages/apt - warn: false - -- name: 'Install Ansible dependency: aptitude' - apt: - name: 'aptitude' - state: present - force_apt_get: true - -- name: 'Install Ansible dependency: python-docker' - apt: - name: python-docker - state: present - -- name: Create Ansible's temporary directory - file: - path: /root/.ansible/tmp - state: directory - mode: '0700' diff --git a/roles/docker/defaults/main.yml b/roles/docker/defaults/main.yml new file mode 100644 index 0000000..44d7474 --- /dev/null +++ b/roles/docker/defaults/main.yml @@ -0,0 +1,3 @@ +docker_compose_root: /var/lib/compose +docker_compose: /usr/bin/docker-compose +docker_compose_service: compose diff --git a/roles/docker/tasks/main.yml b/roles/docker/tasks/main.yml new file mode 100644 index 0000000..56dd697 --- /dev/null +++ b/roles/docker/tasks/main.yml @@ -0,0 +1,25 @@ +- name: Install Docker + ansible.builtin.apt: + name: ['docker.io', 'docker-compose'] + state: present + update_cache: true + +- name: Create docker-compose root + ansible.builtin.file: + path: "{{ docker_compose_root }}" + state: directory + mode: 0600 + +- name: Add users to docker group + ansible.builtin.user: + name: "{{ item }}" + groups: docker + append: true + loop: "{{ docker_users }}" + when: docker_users is defined + +- name: Start Docker and enable on boot + ansible.builtin.service: + name: docker + state: started + enabled: true diff --git a/roles/webserver/defaults/main.yml b/roles/webserver/defaults/main.yml new file mode 100644 index 0000000..363632e --- /dev/null +++ b/roles/webserver/defaults/main.yml @@ -0,0 +1 @@ +webserver_root: "{{ docker_compose_root }}/webserver" diff --git a/roles/webserver/files/docker-compose.yml b/roles/webserver/files/docker-compose.yml new file mode 100644 index 0000000..852d0c2 --- /dev/null +++ b/roles/webserver/files/docker-compose.yml @@ -0,0 +1,30 @@ +version: '3.5' + +networks: + traefik: + name: traefik + +services: + traefik: + image: traefik:2.2 + command: + - --api.dashboard=true + - --api.debug=true + - --providers.docker=true + - --entrypoints.web.address=:80 + - --entrypoints.web.http.redirections.entrypoint.to=websecure + - --entrypoints.web.http.redirections.entrypoint.scheme=https + - --entrypoints.web.http.redirections.entrypoint.permanent=true + - --entrypoints.websecure.address=:443 + ports: + - 80:80 + - 443:443 + volumes: + - /var/run/docker.sock:/var/run/docker.sock + labels: + traefik.http.routers.api.rule: Host(`${TRAEFIK_DOMAIN}`) + traefik.http.routers.api.entrypoints: local + traefik.http.routers.api.service: api@internal + traefik.http.routers.api.tls: true + networks: + - traefik diff --git a/roles/webserver/handlers/main.yml b/roles/webserver/handlers/main.yml index 402d740..5a1b951 100644 --- a/roles/webserver/handlers/main.yml +++ b/roles/webserver/handlers/main.yml @@ -1,18 +1,5 @@ -# Copyright (C) 2019 Free I.T. Athens -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -- name: Reload Apache2 - service: name=apache2 state=reloaded - +- name: Compose up on webserver stack + ansible.builtin.command: "docker-compose up -d" + args: + chdir: "{{ webserver_root }}" + listen: composeup_webserver diff --git a/roles/webserver/tasks/main.yml b/roles/webserver/tasks/main.yml index a65049e..d8263e4 100644 --- a/roles/webserver/tasks/main.yml +++ b/roles/webserver/tasks/main.yml @@ -1,40 +1,24 @@ -# Copyright (C) 2019 Free I.T. Athens -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - -- name: Install Apache2 Web Server - apt: - name: apache2 - state: present - -- name: Start Apache2 Web Server - service: - name: apache2 - state: started - -- name: Install PHP - apt: - name: php - state: present - -- name: Install PHP MySQL Extension - apt: - name: php-mysql - state: present - notify: Reload Apache2 - - name: Install MariaDB Server - apt: + ansible.builtin.apt: name: mariadb-server state: present + +- name: Create webserver docker-compose directory + ansible.builtin.file: + path: "{{ webserver_root }}" + state: directory + mode: 0600 + +- name: Install webserver docker-compose.yml + ansible.builtin.copy: + src: docker-compose.yml + dest: "{{ webserver_root }}/docker-compose.yml" + mode: 0600 + notify: composeup_webserver + +- name: Install docker-compose .env + ansible.builtin.template: + src: compose-env.j2 + dest: "{{ webserver_root }}/.env" + mode: 0600 + notify: composeup_webserver diff --git a/roles/webserver/templates/compose-env.j2 b/roles/webserver/templates/compose-env.j2 new file mode 100644 index 0000000..f536af2 --- /dev/null +++ b/roles/webserver/templates/compose-env.j2 @@ -0,0 +1,4 @@ +# {{ ansible_managed }} +{% for key, value in webserver_env.items() %} +{{ key }}={{ value }} +{% endfor %} diff --git a/webserver.yml b/webserver.yml index a371951..f981b76 100644 --- a/webserver.yml +++ b/webserver.yml @@ -1,24 +1,6 @@ -# Copyright (C) 2019 Free I.T. Athens -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, version 3 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - - - name: Install FRITA Web Server hosts: all - become: yes + become: true roles: - - ansible + - docker - webserver - - wordpress - - nextcloud - - timetrex