Skip to content

Commit

Permalink
Adding support to the deployex version 0.3.0-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoesteves committed May 29, 2024
1 parent 5d97c0d commit 4f99881
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 77 deletions.
75 changes: 60 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,62 @@ In the [github actions](.github/workflows/release.yaml) files, you can check tha

Tracking the `mix.exs` version is essential to allow hot-upgrades.

### 7. HTTPS certificates
### 7. Setting Up HTTPS Certificates with Let's Encrypt

*__ATTENTION: For this step to work, be sure that the DNS is pointing to the EC2 instance.__*
*__Before proceeding, ensure that the DNS is correctly pointing to the EC2 instance__*

For HTTPS, the project can set Free certificates from [Let's encrypt](https://letsencrypt.org/getting-started/). In this deployment, we are going to use the [cert bot for ubuntu](https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal):

```bash
sudo apt update
sudo apt install snapd
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx
sudo su
apt update
apt install snapd
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot
certbot --nginx
```

This will install Certbot and automatically configure Nginx to use the obtained certificates. After Nginx finishes setup, it will create paths for the certificates. They will typically look like this:

```bash
vi /etc/nginx/sites-available/default
...
ssl_certificate /etc/letsencrypt/live/calori.com.br/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/calori.com.br/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
```

It's possible that Nginx has modified the configuration file `/etc/nginx/sites-available/default` in a way that it won't work as expected. You'll need to retrieve the original file [nginx file](devops/terraform/modules/standard-account/cloud-config.tpl) and update it with the Let's Encrypt certificate paths. Find the section where it mentions:

and where it mentions:

```bash
# Add here the letsencrypt paths
```
Replace this comment with the certificate paths obtained in the previous step.

```bash
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://deployex;
}
ssl_certificate /etc/letsencrypt/live/calori.com.br/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/calori.com.br/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
```

Also, for both servers, re-enable port 443, e. g:

```bash
server {
listen 443 ssl; # managed by Certbot
```
Nginx will automatically generate certificates and modify your configuration files during installation. After installation, verify if the contents of the nginx configuration file match those specified in the original [nginx file ](devops/terraform/modules/standard-account/cloud-config.tpl). If any discrepancies are found, edit the file accordingly and restart Nginx to apply the changes.
After modifying the configuration file, save the changes and restart Nginx:
```bash
sudo su
Expand All @@ -179,6 +220,8 @@ vi /etc/nginx/sites-available/default
systemctl reload nginx
```
__PS: After the changes, It may require a reboot__
The comands above will modify nginx file for the correct routing. Once it is all set, you need to check if the [runtime.exs](apps/calori/config/runtime.exs) is pointing to the correct SCHEME/HOST/PORT, e. g.:
```elixir
Expand All @@ -202,10 +245,11 @@ Avoid the execute a hotupgrade in the following situations:
#### 1. IEX shell Access to Deployex App
Connecting the iex shell:
To connect to the iex shell, you may need to export the cookie if AWS is configured with a value different from the default 'cookie', which is highly recommended to change.
```bash
ubuntu@ip-10-0-1-56:~$ sudo su
root@ip-10-0-1-56:/home/ubuntu$ export RELEASE_COOKIE=COOKIE12345678912345789
root@ip-10-0-1-56:/home/ubuntu$ /opt/deployex/bin/deployex remote
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]
Expand All @@ -215,15 +259,16 @@ iex(deployex@ip-10-0-1-56)1>
##### 2. IEX shell Access to Calori App
Connecting the iex shell:
To connect to the iex shell, you may need to export the cookie if AWS is configured with a value different from the default 'cookie', which is highly recommended to change.
```bash
root@ip-10-0-1-56:/home/ubuntu$ sudo -su deployex
deployex@ip-10-0-1-56:$ /var/lib/deployex/service/calori/current/bin/calori remote
Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]
ubuntu@ip-10-0-1-56:~$ sudo su
root@ip-10-0-1-56:/home/ubuntu$ export RELEASE_COOKIE=COOKIE12345678912345789
root@ip-10-0-1-56:/home/ubuntu$ /var/lib/deployex/service/calori/current/bin/calori remote
Erlang/OTP 26 [erts-14.1.1] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:1] [jit:ns]
Interactive Elixir (1.16.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(calori@ip-10-0-1-56)1>
iex(calori@ip-10-0-1-174)1>
```
##### 3. Logs
Expand Down Expand Up @@ -262,7 +307,7 @@ root@ip-10-0-1-56:/home/ubuntu$ tail -f /var/log/calori-stdout.log
##### 4. Updating CALORI_PHX_HOST
In case you need to update the *__CALORI_PHX_HOST__*, there are 2 files that need to be updated: `/etc/systemd/system/deployex.service` and `/etc/nginx/sites-available/default` (you need to be `root`` user to update them).
In case you need to update the *__CALORI_PHX_HOST__*, there are 2 files that need to be updated: `/etc/systemd/system/deployex.service` and `/etc/nginx/sites-available/default` (you need to be `root` user to update them).
```bash
ubuntu@ip-10-0-1-56:~$ sudo su
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Config
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
phx_port = String.to_integer(System.get_env("CALORI_PHX_PORT") || "4000")
phx_port = String.to_integer(System.get_env("PORT") || "4000")

config :calori, CaloriWeb.Endpoint,
# Binding to loopback ipv4 address prevents access from other machines.
Expand Down
2 changes: 1 addition & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if config_env() == :prod do
# to check this value into version control, so we use an environment
# variable instead.
host = System.get_env("CALORI_PHX_HOST") || "example.com"
port = String.to_integer(System.fetch_env!("CALORI_PHX_PORT"))
port = String.to_integer(System.fetch_env!("PORT"))

config :calori, :dns_cluster_query, System.get_env("DNS_CLUSTER_QUERY")

Expand Down
1 change: 1 addition & 0 deletions devops/terraform/environments/prod/main_example.tf_
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module "standard_account" {
account_name = "stage"
server_dns = "example.com"
deployex_dns = "deployex.example.com"
replicas = "3"
}
40 changes: 16 additions & 24 deletions devops/terraform/modules/standard-account/cloud-config.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ write_files:
# Check if the version was passed as an argument
if [ -z "$1" ]; then
# If not passed, use the default value
VERSION="0.2.0"
VERSION="0.3.0-rc1"
else
# If passed, use the passed value
VERSION="$1"
Expand Down Expand Up @@ -86,14 +86,14 @@ write_files:
"timestamp_format": "%H: %M: %S%Y%b%-d"
},
{
"file_path": "/var/log/calori-stdout.log",
"file_path": "/var/log/calori/calori-*-stdout.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}-calori-stdout-log",
"timezone": "UTC",
"timestamp_format": "%H: %M: %S%Y%b%-d"
},
{
"file_path": "/var/log/calori-stderr.log",
"file_path": "/var/log/calori/calori-*-stderr.log",
"log_group_name": "${log_group_name}",
"log_stream_name": "{instance_id}-calori-stderr-log",
"timezone": "UTC",
Expand All @@ -110,6 +110,8 @@ write_files:
content: |
upstream phoenix {
server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
server 127.0.0.1:4001 max_fails=5 fail_timeout=60s;
server 127.0.0.1:4002 max_fails=5 fail_timeout=60s;
}

upstream deployex {
Expand All @@ -132,10 +134,11 @@ write_files:
return 404; # managed by Certbot
}

server {
server {
#listen 443 ssl; # managed by Certbot
server_name deployex.calori.com.br;
client_max_body_size 30M;
location / {
allow all;
Expand All @@ -151,17 +154,15 @@ write_files:
proxy_pass http://deployex;
}

ssl_certificate /etc/letsencrypt/live/calori.com.br/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/calori.com.br/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

# Add here the letsencrypt paths
}

server {
#listen 443 ssl; # managed by Certbot
server_name calori.com.br;
client_max_body_size 30M;
location / {
allow all;
Expand All @@ -177,11 +178,7 @@ write_files:
proxy_pass http://phoenix;
}

ssl_certificate /etc/letsencrypt/live/calori.com.br/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/calori.com.br/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
# Add here the letsencrypt paths
}
- path: /etc/systemd/system/deployex.service
owner: root:root
Expand All @@ -196,16 +193,13 @@ write_files:
Environment=AWS_REGION=${aws_region}
Environment=CALORI_PHX_HOST=${hostname}
Environment=CALORI_PHX_SERVER=true
Environment=CALORI_PHX_PORT=4000
Environment=CALORI_CLOUD_ENVIRONMENT=${account_name}
Environment=CALORI_OTP_TLS_CERT_PATH=/usr/local/share/ca-certificates
Environment=DEPLOYEX_CLOUD_ENVIRONMENT=${account_name}
Environment=DEPLOYEX_OTP_TLS_CERT_PATH=/usr/local/share/ca-certificates
Environment=DEPLOYEX_STORAGE_ADAPTER=s3
Environment=DEPLOYEX_MONITORED_APP_NAME=calori
Environment=DEPLOYEX_PHX_SERVER=true
Environment=DEPLOYEX_PHX_HOST=${deployex_hostname}
Environment=DEPLOYEX_PHX_PORT=5001
Environment=DEPLOYEX_MONITORED_REPLICAS=${replicas}
ExecStart=/opt/deployex/bin/deployex start
StandardOutput=append:/var/log/deployex.log
KillMode=process
Expand All @@ -232,10 +226,8 @@ runcmd:
- mkdir /var/lib/deployex
- chown deployex:deployex /var/lib/deployex
- touch /var/log/deployex.log
- touch /var/log/calori-stdout.log
- touch /var/log/calori-stderr.log
- chown deployex:deployex /var/log/calori-stdout.log
- chown deployex:deployex /var/log/calori-stderr.log
- mkdir /var/log/calori/
- chown deployex:deployex /var/log/calori/
- wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
- dpkg -i -E ./amazon-cloudwatch-agent.deb
- /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/home/ubuntu/config.json -s
Expand Down
1 change: 1 addition & 0 deletions devops/terraform/modules/standard-account/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ data "cloudinit_config" "server_config" {
log_group_name = aws_cloudwatch_log_group.ec2_instance_logs.name
account_name = "${var.account_name}"
aws_region = "${var.aws_region}"
replicas = "${var.replicas}"
})
}
}
Expand Down
10 changes: 10 additions & 0 deletions devops/terraform/modules/standard-account/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ variable "server_dns" {
nullable = false
}

variable "deployex_dns" {
type = string
nullable = false
}

variable "replicas" {
type = string
nullable = false
}

# ec2 key pair name
variable "aws_key_name" {
default = "calori-web-ec2"
Expand Down
15 changes: 11 additions & 4 deletions lib/calori_web/components/core_components.ex
Original file line number Diff line number Diff line change
Expand Up @@ -678,21 +678,27 @@ defmodule CaloriWeb.CoreComponents do
@doc """
Copied/Modified from https://fullstackphoenix.com/tutorials/tailwind-navbar-new-liveview-0-18-components
"""
attr :name, :string, default: "Calori App"
attr :name, :string, default: "Calori Software"

def logo(assigns) do
~H"""
<svg
class="w-10 h-10 p-2 mr-3 text-white rounded-full bg-primary"
class="w-10 h-10 p-1 mr-3 text-black rounded-full bg-white"
xmlns="http://www.w3.org/2000/svg"
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
viewBox="0 0 24 24"
viewBox="-8 -4 40 40"
>
<path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path>
<g transform="translate(-291.484 -256.287)">
<path d="M304.462,289.851a12.976,12.976,0,0,1-5.163-24.882l.789,1.838a11,11,0,1,0,7.782-.364l.613-1.9a12.971,12.971,0,0,1-4.021,25.312Z" />
<path d="M299.7,266.884a1,1,0,0,1-1-1v-9.6h10.486v9.2a1,1,0,0,1-2,0v-7.2H300.7v7.6A1,1,0,0,1,299.7,266.884Z" />
<path d="M293.753,280.571l-1.689-1.072c.159-.251,3.989-6.123,13.419-4.129,8.031,1.7,10.357.191,10.379.177l1.225,1.58c-.276.222-2.959,2.12-12.018.2C297.054,275.623,293.883,280.368,293.753,280.571Z" />
</g>
</svg>
<span class="self-center text-xl font-semibold whitespace-nowrap dark:text-white">
Expand All @@ -702,6 +708,7 @@ defmodule CaloriWeb.CoreComponents do
end

slot :logo

slot(:link, required: true) do
attr :to, :string
attr :label, :string
Expand Down
12 changes: 10 additions & 2 deletions lib/calori_web/components/layouts/app.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,25 @@
</.link>
</:logo>
<:link label="Home" to={~p"/home"} />
<:link label="About" to={~p"/home"} />
<:link label="Supervisor" to={~p"/supervisor"} />
</.navbar>
</header>

<main class="relative">
<div class="flex items-center justify-center">
<div class="w-2/12"></div>
<div class="w-8/12 mx-auto">
<div class="w-8/12 mx-auto ">
<.flash_group flash={@flash} />
<%= @inner_content %>
</div>
<div class="w-2/12"></div>
</div>

<footer class="bg-white">
<div class="w-full max-w-screen-xl mx-auto p-2 md:py-6">
<span class="block text-sm text-black sm:text-center">
© 2024 <a href="https://calori.com.br/" class="hover:underline">Calori Software</a>. All Rights Reserved.
</span>
</div>
</footer>
</main>
4 changes: 2 additions & 2 deletions lib/calori_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="csrf-token" content={get_csrf_token()} />
<.live_title suffix=" · Beam">
<%= assigns[:page_title] || "Calori" %>
<.live_title suffix="">
<%= assigns[:page_title] || "Calori Software" %>
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script defer phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
Expand Down
22 changes: 0 additions & 22 deletions lib/calori_web/live/about.ex

This file was deleted.

Loading

0 comments on commit 4f99881

Please sign in to comment.