diff --git a/README.md b/README.md index e9e9068..04dabee 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Static Site Host - Ansible playbook -This is an [Ansible](https://www.ansible.com/) playbook which allows you to set up a server to serve static sites that will be deployed via CI. +This is an [Ansible](https://www.ansible.com/) playbook which allows you to set up a server to serve static sites via [Static Web Server](https://github.com/static-web-server/static-web-server). Everything will be ready for your site to be deployed using your CI. ## Getting started -Create an file listing the repository host in `inventory/hosts`. You snouldn't put every domain that you intend to use as static site in there, only one domain per server. +Create an file listing the repository host in `inventory/hosts`. You shouldn't put every domain that you intend to use as static site in there, only one domain per server. ``` -[stitic_site_servers] +[static_site_servers] static.example.org ``` diff --git a/roles/static-server/defaults/main.yml b/roles/static-server/defaults/main.yml index 4782195..81598ec 100644 --- a/roles/static-server/defaults/main.yml +++ b/roles/static-server/defaults/main.yml @@ -2,18 +2,15 @@ # static_site_server sets up static_site_server_enabled: true -static_site_server_identifier: static_site_server +static_container_network: "static" -static_site_user: 'static_site' -static_site_group: 'static_site' +sws_version: 2 -# The hostname at which static_site_server is served. -static_site_server_hostname: '' +sws_container_image: "{{ sws_container_image_registry_prefix }}joseluisq/static-web-server:{{ sws_container_image_tag }}" +sws_container_image_registry_prefix: docker.io/ +sws_container_image_tag: "{{ sws_version }}" -static_site_server_auth_users: [] +traefik_docker_network: traefik -# The path at which static_site_server is served. -# This value must either be `/` or not end with a slash (e.g. `/static_site_server`). -static_site_server_path_prefix: / static_site_server_base_path: "/static_sites" diff --git a/roles/static-server/tasks/install.yml b/roles/static-server/tasks/install.yml index dba41d1..df1d679 100644 --- a/roles/static-server/tasks/install.yml +++ b/roles/static-server/tasks/install.yml @@ -1,8 +1,37 @@ --- -- name: Ensure Static Sits are setup +- name: Ensure static network is created in Docker + community.docker.docker_network: + name: "{{ static_container_network }}" + driver: bridge + +- name: Ensure SWS path exists + ansible.builtin.file: + path: "{{ item.path }}" + state: directory + mode: 0700 + with_items: + - {path: "{{ static_site_server_base_path }}", when: true} + +- name: Ensure docker-compose file is installed + ansible.builtin.template: + src: "{{ role_path }}/templates/docer-compose.yml.j2" + dest: "{{ static_site_server_base_path }}/docker-compose.yml" + mode: 0640 + +- name: Ensure SWS container image is pulled + community.docker.docker_image: + name: "{{ gotosocial_container_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" + +- name: Ensure Static Sites are setup ansible.builtin.include_tasks: "{{ role_path }}/tasks/setup_site.yml" with_items: "{{ static_sites }}" loop_control: loop_var: site - no_log: true \ No newline at end of file + no_log: true + +- name: Ensure SSW is pulled + community.docker.docker_image: + name: "{{ sws_container_image }}" + source: "{{ 'pull' if ansible_version.major > 2 or ansible_version.minor > 7 else omit }}" diff --git a/roles/static-server/templates/docker-compose.yml.j2 b/roles/static-server/templates/docker-compose.yml.j2 new file mode 100644 index 0000000..c27f1e0 --- /dev/null +++ b/roles/static-server/templates/docker-compose.yml.j2 @@ -0,0 +1,31 @@ +version: "3.3" + +services: +{% for site in static_sites %} + {{ site.domain }}: + image: {{ sws_container_image }} + container_name: "{{ site.domain }}" + environment: + # Note: those envs are customizable but also optional + - SERVER_PORT=8080 + - SERVER_ROOT=/public + - SERVER_LOG_LEVEL=info + volumes: + - {{ static_site_server_base_path }}/{{ site.user }}/public:/public + labels: + - "traefik.enable=true" + - "traefik.docker.network=traefik" + - "traefik.http.routers.{{ site.user }}.rule=Host(`{{ site.host }}`)" + - "traefik.http.routers.{{ site.user }}.service=present" + - "traefik.http.routers.{{ site.user }}.entrypoints=web-secure" + - "traefik.http.routers.{{ site.user }}.tls=true" + - "traefik.http.routers.{{ site.user }}.tls.certResolver=default" + - "traefik.http.services.{{ site.user }}.loadbalancer.server.port=8080" + networks: + - traefik +{% endfor %} + +networks: + traefik: + name: "{{ traefik_docker_network }}" + external: true