Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install Consul Democracy 2.1.0 #221

Merged
merged 21 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f1ca8a4
Update README to use Consul Democracy 2.1.0
javierm Nov 20, 2023
7456871
[TMP] Make the installer install the branch with a specific version o…
Senen Sep 20, 2023
49b5eca
Install a specific version of Node.js
Senen Sep 20, 2023
5c4b72b
Fix deprecation warning when using `include`
Senen Sep 20, 2023
c44a6ab
Simplify commands by using the newly added variable
Senen Oct 16, 2023
59b8bbb
Retry nodejs installation until succeed or reaches 10 failed attempts
Senen Oct 24, 2023
ad38999
Merge pull request #223 from consuldemocracy/install_specific_nodejs_…
javierm Nov 23, 2023
659732c
Use systemd to start Puma
javierm Sep 15, 2023
480c7b1
Modify the Puma command so it works with Ansible
javierm Oct 16, 2023
5af53dd
Don't test systemd on Debian in Github Actions
javierm Oct 17, 2023
29bc5ba
Merge pull request #232 from consuldemocracy/puma_systemd
javierm Nov 23, 2023
cb9d2f7
Add support for Ubuntu 22.04 Jammy
Senen Oct 23, 2023
249428f
Do not install Errbit in Ubuntu 22.04
Senen Oct 23, 2023
f289b71
Ensure deploy_user home has the correct permissions
Senen Oct 24, 2023
7c646ed
Merge pull request #228 from consuldemocracy/add_ubuntu_22.04_support
javierm Nov 23, 2023
20cd557
Add support for Debian Bookworm
javierm Oct 17, 2023
e26afe6
Merge pull request #229 from consuldemocracy/debian_bookworm
javierm Nov 24, 2023
4076398
Remove FNM dependency for puma service
Senen Feb 1, 2024
5ce1152
Enable puma socket activation
Senen Feb 1, 2024
ea4358a
Merge pull request #235 from consuldemocracy/puma_socket_activation
Senen Feb 13, 2024
73cfce0
Install Consul Democracy 2.1.0
javierm Feb 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
image: ["debian:bullseye"]
image: ["debian:bullseye", "debian:bookworm"]
rails_env: [staging, production]
container:
image: ${{ matrix.image }}
Expand All @@ -19,9 +19,7 @@ jobs:
- name: Update system packages
run: apt-get update -y
- name: Install needed packages
run: apt-get install -y lsb-release sudo python3-pip openssh-server
- name: Install Ansible
run: pip3 install ansible
run: apt-get install -y lsb-release sudo python3-pip openssh-server ansible
- name: Create hosts file
run: echo "localhost ansible_connection=local ansible_user=root" > hosts
- name: Generate dummy SSH key
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ on:

jobs:
ubuntu:
runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.platforms.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-20.04]
platforms: [
{ os: "ubuntu-20.04", errbit: "True" },
{ os: "ubuntu-22.04", errbit: "False"}
]
rails_env: [staging, production]
steps:
- uses: actions/checkout@v2
Expand All @@ -29,4 +32,4 @@ jobs:
- name: Generate dummy SSH key
run: ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
- name: Run CONSUL DEMOCRACY installer
run: ansible-playbook consul.yml -i hosts --extra-vars "env=${{ matrix.rails_env }} domain=localhost errbit=True"
run: ansible-playbook consul.yml -i hosts --extra-vars "env=${{ matrix.rails_env }} domain=localhost errbit=${{ matrix.platforms.errbit }}"
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ It will also create a `deploy` user to install these libraries
A remote server with one of the supported distributions:

- Ubuntu 20.04 x64
- Ubuntu 22.04 x64
- Debian Bullseye x64
- Debian Bookworm x64

Access to a remote server via public ssh key without password.
The default user is `deploy` but you can [use any user](#using-a-different-user-than-deploy) with sudo privileges.
Expand Down Expand Up @@ -102,7 +104,7 @@ Setup locally for your [development environment](https://docs.consuldemocracy.or
Checkout the latest stable version:

```
git checkout origin/2.0.1 -b stable
git checkout origin/2.1.0 -b stable
```

Create your `deploy-secrets.yml`
Expand Down Expand Up @@ -205,13 +207,13 @@ Using https instead of http is an important security configuration. Before you b

Once you have that setup we need to configure the Installer to use your domain in the application.

First, uncomment the `domain` variable in the [configuration file](https://github.com/consuldemocracy/installer/blob/2.0.1/group_vars/all) and update it with your domain name:
First, uncomment the `domain` variable in the [configuration file](https://github.com/consuldemocracy/installer/blob/2.1.0/group_vars/all) and update it with your domain name:

```
#domain: "your_domain.com"
```

Next, uncomment the `letsencrypt_email` variable in the [configuration file](https://github.com/consuldemocracy/installer/blob/2.0.1/group_vars/all) and update it with a valid email address:
Next, uncomment the `letsencrypt_email` variable in the [configuration file](https://github.com/consuldemocracy/installer/blob/2.1.0/group_vars/all) and update it with a valid email address:

```
#letsencrypt_email: "your_email@example.com"
Expand Down Expand Up @@ -258,7 +260,7 @@ If you are on Ubuntu and would like to use its default `sudo` group instead of `
deploy_group: sudo
```

There are many more variables available check them out [here]((https://github.com/consuldemocracy/installer/blob/2.0.1/group_vars/all))
There are many more variables available check them out [here]((https://github.com/consuldemocracy/installer/blob/2.1.0/group_vars/all))

## Other deployment options

Expand Down Expand Up @@ -288,7 +290,7 @@ If you do not have `root` access, you will need your system administrator to gra

## Using a different user than deploy

Change the variable [deploy_user](https://github.com/consuldemocracy/installer/blob/2.0.1/group_vars/all#L12) to the username you would like to use.
Change the variable [deploy_user](https://github.com/consuldemocracy/installer/blob/2.1.0/group_vars/all#L12) to the username you would like to use.

## Ansible Documentation

Expand Down
1 change: 1 addition & 0 deletions app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
roles:
- folder_structure
- ruby
- nodejs
- rails
- email
- queue
Expand Down
2 changes: 1 addition & 1 deletion galaxy/yatesr.timezone/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
- include: timezone.yml
- include_tasks: timezone.yml
15 changes: 15 additions & 0 deletions group_vars/all
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ database_user: "{{ deploy_user }}"
database_password: "{{ deploy_user }}"
database_hostname: "localhost"

# Puma
# If you use Capistrano to deploy, make sure the puma_service_unit_name
# variable is the same as `:puma_service_unit_name` in Capistrano
puma_service_unit_name: "puma_{{ app_name }}_{{ env }}"
puma_config_file: "{{ release_dir }}/config/puma/{{ env }}.rb"
puma_access_log: "{{ shared_dir }}/log/puma_access.log"
puma_error_log: "{{ shared_dir }}/log/puma_error.log"

#SMTP
smtp_address: "smtp.example.com"
smtp_port: 25
Expand All @@ -45,6 +53,13 @@ smtp_authentication: "plain"
#LetsEncrypt
#letsencrypt_email: "your_email@example.com"

# Node.js
fnm_dir: "{{ home_dir }}/.fnm"
fnm_command: "export PATH=\"{{ fnm_dir }}/:$PATH\" && eval \"$(fnm env)\""

# RVM
rvm_command: "source {{ home_dir }}/.rvm/scripts/rvm"

# Errbit
errbit: False
errbit_dir: "{{ home_dir }}/errbit"
Expand Down
16 changes: 8 additions & 8 deletions roles/errbit/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
executable: /bin/bash

- name: Install libv8-node for the right platform
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && gem install libv8-node --version '{{ libv8_version.stdout }}' --platform x86_64-linux-libc"
shell: "{{ rvm_command }} && gem install libv8-node --version '{{ libv8_version.stdout }}' --platform x86_64-linux-libc"
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash
Expand All @@ -48,13 +48,13 @@
executable: /bin/bash

- name: Install the mini_racer gem
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && gem install mini_racer --version '{{ mini_racer_version.stdout }}'"
shell: "{{ rvm_command }} && gem install mini_racer --version '{{ mini_racer_version.stdout }}'"
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash

- name: Install Errbit dependencies
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && bundle install"
shell: "{{ rvm_command }} && bundle install"
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash
Expand All @@ -81,7 +81,7 @@
- when: not existing_secret_key_base.found
block:
- name: Generate secret key
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && bin/rake secret"
shell: "{{ rvm_command }} && bin/rake secret"
register: secret_key_base
args:
chdir: "{{ errbit_dir }}"
Expand All @@ -93,13 +93,13 @@
line: "SECRET_KEY_BASE={{ secret_key_base.stdout }}"

- name: Setup Errbit
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && RAILS_ENV={{ env }} bin/rake errbit:bootstrap"
shell: "{{ rvm_command }} && RAILS_ENV={{ env }} bin/rake errbit:bootstrap"
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash

- name: Precompile Errbit assets
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && RAILS_ENV={{ env }} bin/rake assets:precompile"
shell: "{{ rvm_command }} && RAILS_ENV={{ env }} bin/rake assets:precompile"
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash
Expand All @@ -121,7 +121,7 @@
enabled: true

- name: Create app if it does not exist
shell: 'source /home/{{ deploy_user }}/.rvm/scripts/rvm && bin/rails runner -e {{ env }} "App.create(name: \"{{ domain }}\")"'
shell: '{{ rvm_command }} && bin/rails runner -e {{ env }} "App.create(name: \"{{ domain }}\")"'
args:
chdir: "{{ errbit_dir }}"
executable: /bin/bash
Expand All @@ -146,7 +146,7 @@
replace: ' errbit_host: "https://{{ errbit_domain }}"'

- name: Restart CONSUL DEMOCRACY
shell: "source /home/{{ deploy_user }}/.rvm/scripts/rvm && RAILS_ENV={{ env }} bin/rails restart"
shell: "{{ fnm_command }} && {{ rvm_command }} && fnm exec bin/rails restart RAILS_ENV={{ env }}"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
Expand Down
2 changes: 1 addition & 1 deletion roles/errbit/templates/errbit.service
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ After=mongodb.service network.target
Type=simple
WorkingDirectory={{ errbit_dir }}
Environment=RAILS_ENV={{ env }}
ExecStart=/bin/bash -lc 'source {{ home_dir }}/.rvm/scripts/rvm && bundle exec puma -C {{ errbit_dir }}/config/puma.default.rb -e {{ env }}'
ExecStart=/bin/bash -lc '{{ rvm_command }} && bundle exec puma -C {{ errbit_dir }}/config/puma.default.rb -e {{ env }}'
Restart=always
User={{ errbit_user }}
Group={{ errbit_group }}
Expand Down
2 changes: 1 addition & 1 deletion roles/folder_structure/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
state: directory

- name: Create first release
shell: "git archive 2.0.1 | /usr/bin/env tar -x -f - -C {{ first_release_dir }}"
shell: "git archive 2.1.0 | /usr/bin/env tar -x -f - -C {{ first_release_dir }}"
args:
chdir: "{{ consul_dir }}/repo"

Expand Down
30 changes: 30 additions & 0 deletions roles/nodejs/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
- name: Install fnm
shell: |
curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir "{{ fnm_dir }}"
args:
chdir: "{{ home_dir }}"
executable: /bin/bash
creates: "{{ fnm_dir }}/fnm"

- name: Read Node.js version
shell: "cat .node-version"
args:
chdir: "{{ release_dir }}"
register: node_version

- name: Install nodejs via fnm
shell: "{{ fnm_command }} && {{ rvm_command }} && fnm install {{ node_version.stdout }}"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
register: fnm_install_result
until: "fnm_install_result is not failed"
retries: 10
delay: 10

- name: Install Node packages
shell: "{{ fnm_command }} && {{ rvm_command }} && fnm exec npm install --production"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
13 changes: 7 additions & 6 deletions roles/postgresql/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
- become: true
become_user: postgres
block:
- name: Create PostgreSQL database
postgresql_db:
name: "{{ database_name }}"

- name: Create PostgreSQL users
postgresql_user:
state: present
name: "{{ database_user }}"
password: "{{ database_password }}"
db: "{{ database_name }}"
encrypted: yes
priv: ALL

- name: Create PostgreSQL database
postgresql_db:
state: present
name: "{{ database_name }}"
owner: "{{ database_user }}"

- name: Create the shared extensions schema
postgresql_schema:
Expand Down
91 changes: 65 additions & 26 deletions roles/puma/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,68 @@
- "pids"
- "sockets"

- name: Check that puma is running
stat:
path: "{{ shared_dir }}/tmp/pids/puma.pid"
register: puma_process

- name: Get running puma process
shell: "cat {{ shared_dir }}/tmp/pids/puma.pid"
register: running_process
when: puma_process.stat.exists == True

- name: Kill running process
shell: "kill -QUIT {{ item }}"
with_items: "{{ running_process.stdout_lines }}"
when: puma_process.stat.exists == True

- name: Start puma
shell: "source {{ home_dir }}/.rvm/scripts/rvm && bundle exec puma -C {{ release_dir }}/config/puma/{{ env }}.rb -e {{ env }} -d"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash

- name: Make sure Nginx has write access to the puma socket
shell: "chmod o+w tmp/sockets/*"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
- name: Create systemd folder
file:
path: "{{ home_dir }}/.config/systemd/user"
state: directory

- name: Copy Puma service file to the systemd folder
template:
src: "{{ playbook_dir }}/roles/puma/templates/puma.service"
dest: "{{ home_dir }}/.config/systemd/user/{{ puma_service_unit_name }}.service"

- name: Copy Puma socket file to the systemd folder
template:
src: "{{ playbook_dir }}/roles/puma/templates/puma.socket"
dest: "{{ home_dir }}/.config/systemd/user/{{ puma_service_unit_name }}.socket"

- name: Get distribution codename
shell: lsb_release -c --short
register: distro_codename

- when: distro_codename.stdout == "focal" or distro_codename.stdout == "jammy" or not lookup("env", "CI")
block:
- name: Check if user has access to systemd while running ansible tasks
stat:
path: "/var/lib/systemd/linger/{{ deploy_user }}"
register: linger_enabled

- name: Enable systemd access if needed
command: "loginctl enable-linger {{ deploy_user }}"
Senen marked this conversation as resolved.
Show resolved Hide resolved
when: not linger_enabled.stat.exists

- name: Get user UID
shell: "id -u"
register: current_uid

- name: Enable puma socket activation
systemd:
name: "{{ puma_service_unit_name }}.socket"
daemon_reload: true
enabled: true
state: started
scope: user
environment:
XDG_RUNTIME_DIR: "/run/user/{{ current_uid.stdout }}"

- name: Start puma
systemd:
name: "{{ puma_service_unit_name }}.service"
daemon_reload: true
enabled: true
state: started
scope: user
environment:
XDG_RUNTIME_DIR: "/run/user/{{ current_uid.stdout }}"
Senen marked this conversation as resolved.
Show resolved Hide resolved

- name: Wait until Puma has created the socket
wait_for:
path: "{{ release_dir }}/tmp/sockets/puma.sock"
state: present
msg: Puma socket is not available

- name: Make sure Nginx has write access to the puma socket
shell: "chmod o+w tmp/sockets/*"
args:
chdir: "{{ release_dir }}"
executable: /bin/bash
19 changes: 19 additions & 0 deletions roles/puma/templates/puma.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[Unit]
Description=Puma HTTP Server for {{ app_name }} ({{ env }})
After=network.target
Requires={{ puma_service_unit_name }}.socket

[Service]
Type=simple
WorkingDirectory={{ release_dir }}
ExecStart=/bin/bash -lc '{{ rvm_command }} && bundle exec puma -C {{ puma_config_file }} -e {{ env }}'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need to add the fnm_command too.

Suggested change
ExecStart=/bin/bash -lc '{{ rvm_command }} && bundle exec puma -C {{ puma_config_file }} -e {{ env }}'
ExecStart=/bin/bash -lc '{{ rvm_command }} && {{ fnm_command }} && bundle exec puma -C {{ puma_config_file }} -e {{ env }}'

Otherwise, the application crashes in Ubuntu distributions with the ExecJS error we know.

^[[A^[[A/home/deploy/consul/shared/bundle/ruby/3.1.0/gems/execjs-2.8.1/lib/execjs/runtimes.rb:58:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)
	from /home/deploy/consul/shared/bundle/ruby/3.1.0/gems/execjs-2.8.1/lib/execjs.rb:5:in `<module:ExecJS>'
	from /home/deploy/consul/shared/bundle/ruby/3.1.0/gems/execjs-2.8.1/lib/execjs.rb:4:in `<top (required)>'

Copy link
Member

@Senen Senen Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that the puma socket was never created, or maybe created and destroyed immediately as the puma task was failing. Maybe now we can wait for the socket to exist in all distributions and not just in Ubuntu 22.04. 🤔

Copy link
Member Author

@javierm javierm Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe now we can wait for the socket to exist in all distributions and not just in Ubuntu 22.04. 🤔

When running the installer, we wait on all distributions thanks to the or not lookup("env", "CI") condition. We weren't running this step on Ubuntu 22.04 when using the CI, though; I've just fixed that 👍.

ExecReload=/bin/kill -USR1 $MAINPID
StandardOutput=append:{{ puma_access_log }}
StandardError=append:{{ puma_error_log }}
Environment=EXECJS_RUNTIME=Disabled
Restart=always
RestartSec=1
SyslogIdentifier=puma

[Install]
WantedBy=default.target
Loading
Loading