Skip to content

Commit 6d91055

Browse files
authored
Merge branch 'master' into capacities
2 parents 8c94e97 + cfbfda4 commit 6d91055

File tree

11 files changed

+248
-51
lines changed

11 files changed

+248
-51
lines changed

.github/workflows/alpha.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Deploy Alpha
2+
3+
on:
4+
push:
5+
branches: [ alpha ]
6+
7+
jobs:
8+
build-frontend:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
14+
- name: Write SSH keys
15+
run: |
16+
install -m 600 -D /dev/null ~/.ssh/id_rsa
17+
echo "${{ secrets.ALPHA_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
18+
echo "${{ vars.ALPHA_SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
19+
20+
- name: Use Node.js 16
21+
uses: actions/setup-node@v2
22+
with:
23+
node-version: 16
24+
cache-dependency-path: |
25+
frontend/yarn.lock
26+
backend/yarn.lock
27+
cache: 'yarn'
28+
29+
- name: Install Frontend Dependencies
30+
working-directory: frontend
31+
run: yarn install --frozen-lockfile
32+
33+
- name: Build Frontend
34+
working-directory: frontend
35+
run: CI=false yarn build; cp build/index.html build/404.html
36+
37+
- name: Install Docs Dependencies
38+
working-directory: doc
39+
run: yarn install
40+
41+
- name: Build Docs
42+
working-directory: doc
43+
run: yarn build; mv ./dist ../frontend/build/docs
44+
45+
- name: Make sure remote folder exists
46+
run: ssh ${{vars.ALPHA_SSH_USER}}@${{vars.ALPHA_SSH_HOST}} -p ${{vars.ALPHA_SSH_PORT}} "mkdir -p SNM-I"
47+
48+
- name: Rsync built frontend
49+
run: rsync -rvz -e "ssh -p ${{vars.ALPHA_SSH_PORT}}" --progress --delete ./frontend/build/* ${{vars.ALPHA_SSH_USER}}@${{vars.ALPHA_SSH_HOST}}:/home/ubuntu/SNM-I/frontend-build
50+
51+
- name: Rsync backend
52+
run: rsync -rvz -e "ssh -p ${{vars.ALPHA_SSH_PORT}}" --progress --delete ./backend ${{vars.ALPHA_SSH_USER}}@${{vars.ALPHA_SSH_HOST}}:/home/ubuntu/SNM-I/
53+
54+
- name: Rsync scripts
55+
run: rsync -rvz -e "ssh -p ${{vars.ALPHA_SSH_PORT}}" --progress --delete ./scripts ${{vars.ALPHA_SSH_USER}}@${{vars.ALPHA_SSH_HOST}}:/home/ubuntu/SNM-I/
56+
57+
- name: Update & Restart Backend
58+
run: ssh ${{vars.ALPHA_SSH_USER}}@${{vars.ALPHA_SSH_HOST}} -p ${{vars.ALPHA_SSH_PORT}} "cd SNM-I && ./scripts/redeploy.sh"

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@
1111
- URL: https://socialneedsmarketplace.ca
1212

1313
- Docs: https://www.socialneedsmarketplace.ca/docs/
14+
15+
#### `alpha` Branch
16+
- URL: https://alpha.socialneedsmarketplace.ca
17+
18+
- Docs: https://alpha.socialneedsmarketplace.ca/docs/

backend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ MAIL_PASSWORD="password"
99
GRAPHDB_ADDRESS="http://127.0.0.1:7200"
1010
GRAPHDB_USERNAME="username"
1111
GRAPHDB_PASSWORD="password"
12+
GRAPHDB_REPO="snmi"
1213

1314
# Custom MongoDB connection string
1415
MONGODB_ADDRESS="mongodb://127.0.0.1:27017/snmi"

backend/config/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ if (isProduction)
77
const config = {
88
graphdb: {
99
addr: isProduction ? 'http://127.0.0.1:7200' : `http://${isDocker ? 'host.docker.internal' : '127.0.0.1'}:7200`,
10+
repositoryName: process.env.GRAPHDB_REPO || (process.env.test ? "snmiTest" : "snmi")
1011
},
1112
mongodb: {
1213
addr: isProduction ? 'mongodb://localhost:27017/snmi' : `mongodb://localhost:27017/${process.env.test ? "snmiTest" : "snmi"}`
@@ -18,6 +19,7 @@ const config = {
1819
'http://localhost:3002',
1920
'https://www.socialneedsmarketplace.ca',
2021
'https://beta.socialneedsmarketplace.ca',
22+
'https://alpha.socialneedsmarketplace.ca',
2123
'https://www.snmi.ca'],
2224

2325
frontend: {

backend/loaders/graphDB.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ INSERT DATA {
436436

437437
let url;
438438
for (let query of query_list){
439-
url = `${graphdb.addr}/repositories/snmi/statements?update=${encodeURIComponent(query)}`;
439+
url = `${graphdb.addr}/repositories/${graphdb.repositoryName}/statements?update=${encodeURIComponent(query)}`;
440440

441441
const response = await fetch(url, {
442442
method: 'POST',
@@ -464,7 +464,8 @@ async function load() {
464464
password: graphdb.password,
465465
namespaces,
466466
// The repository name, a new repository will be created if it does not exist.
467-
repositoryName: process.env.test ? "snmiTest" : "snmi",
467+
repositoryName: graphdb.repositoryName,
468+
// Set to true if you want to see SPARQL queries
468469
debug: false
469470
});
470471

backend/routes/serviceProviders.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const express = require('express');
22
const {createSingleServiceProvider, fetchMultipleServiceProviders, fetchSingleServiceProvider,
3-
deleteSingleServiceProvider, updateServiceProvider, searchMultipleServiceProviders
3+
deleteSingleServiceProvider, updateServiceProvider, fetchHomeServiceProvider, searchMultipleServiceProviders
44
} = require("../services/genericData/serviceProvider");
55
const router = express.Router();
66

77
router.post('/providers', createSingleServiceProvider);
88
router.get('/providers', fetchMultipleServiceProviders);
9+
router.get('/providers/home', fetchHomeServiceProvider);
910
router.get('/providers/search', searchMultipleServiceProviders);
1011
router.get('/providers/:id', fetchSingleServiceProvider);
1112
router.delete('/providers/:id', deleteSingleServiceProvider);

backend/services/genericData/serviceProvider.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,21 @@ const deleteSingleServiceProvider = async (req, res, next) => {
294294

295295
};
296296

297+
const fetchHomeServiceProvider = async (req, res, next) => {
298+
try {
299+
const homeOrganization = await GDBOrganizationModel.findOne({status: 'Home'}, {populates: []});
300+
if (!homeOrganization) {
301+
return res.status(400).json({message: 'This deployment has no home organization'})
302+
}
303+
const provider = await GDBServiceProviderModel.findOne({organization: homeOrganization});
304+
provider.organization = homeOrganization;
305+
return res.status(200).json({provider, success: true});
306+
} catch (e) {
307+
next(e);
308+
}
309+
};
310+
297311
module.exports = {
298-
createSingleServiceProvider, fetchMultipleServiceProviders, fetchSingleServiceProvider, deleteSingleServiceProvider,
299-
updateServiceProvider, searchMultipleServiceProviders, getProviderById
312+
createSingleServiceProvider, fetchMultipleServiceProviders, fetchSingleServiceProvider, deleteSingleServiceProvider,
313+
updateServiceProvider, getProviderById, fetchHomeServiceProvider, searchMultipleServiceProviders
300314
};

doc/src/content/docs/guides/deployment.md

Lines changed: 138 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,99 @@
22
title: Deployment Guide
33
---
44

5-
## Deploy to any server
6-
7-
### Remote Server Preparations
8-
> We will initialize a server and set up an automatic deployment from GitHub Actions.
9-
>
10-
> This setup assumes the SNM-I deployment server is behind a reverse proxy,
11-
> we also need to set up ssh port forwarding on the reverse proxy to make the ssh server accessible from the public.
12-
>
13-
### GitHub Secrets and Variable to Set up
14-
15-
| KEY | Example Value | Variable or Secret? |
16-
|-------------------------|----------------------------------------------------|---------------------|
17-
| RELEASE_SSH_HOST | 206.12.97.46 | Variable |
18-
| RELEASE_SSH_KNOWN_HOSTS | [206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2... | Variable |
19-
| RELEASE_SSH_PORT | 14572 | Variable |
20-
| RELEASE_SSH_USER | ubuntu | Variable |
21-
| RELEASE_SSH_PRIVATE_KEY | -----BEGIN OPENSSH PRIVATE KEY-----\nb3Blbn.... | **Secret** |
22-
23-
#### Remote Server Requirement
5+
# Deploy to any server
6+
A push to the relative branch triggers the GitHub Actions to compile the frontend, and run tests if existed.
7+
Then it ssh into the remote server to executive a sequence of commands to update the code & compiled frontend.
8+
9+
## Things you might what to know
10+
- Docker and how to install Docker Engine
11+
- Understand Docker Compose
12+
- GitHub Actions and GitHub Workflow file
13+
- GitHub Secrets/variables to store credentials/configurations
14+
- How to set up SSH with the private key
15+
- How to preconfigure the SSH `know_hosts` in an CI environment
16+
- How to ssh and execute commands on a remote server in an CI environment
17+
- Use rsync to synchronize files to remote servers
18+
- How to host static files (compiled react frontend) in Caddy
19+
- How to use iptables to do a port forward (In the server that hosts a reverse proxy)
20+
21+
## GitHub Secrets and Variable to Set up
22+
23+
We will go through each of the variables/secrets.
24+
25+
| KEY | Example Value | Variable or Secret? |
26+
|-----------------------|----------------------------------------------------|---------------------|
27+
| BETA_SSH_HOST | 206.12.97.46 | Variable |
28+
| BETA_SSH_KNOWN_HOSTS | [206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2... | Variable |
29+
| BETA_SSH_PORT | 14572 | Variable |
30+
| BETA_SSH_USER | ubuntu | Variable |
31+
| BETA_SSH_PRIVATE_KEY | -----BEGIN OPENSSH PRIVATE KEY-----\nb3Blbn.... | **Secret** |
32+
33+
34+
## Remote Servers Overview
35+
We will initialize a server for SNM-I and set up an automatic deployment from GitHub Actions.
36+
37+
This setup assumes the SNM-I deployment server is behind a reverse proxy,
38+
we also need to set up ssh port forwarding on the reverse proxy to make the ssh server accessible from the public.
39+
40+
The following example assumes:
41+
- The SNM-I is hosted on the subdomain `beta.socialneedsmarketplace.ca`.
42+
- SNM-I server has an internal IP **192.168.41.202**.
43+
- Reverse proxy server has an internal IP **192.168.41.156** and external IP **206.12.97.46**.
44+
45+
46+
### SSH port forward
47+
ssh to `206.12.97.46:14572` goes directly into the SNM-I server.
48+
```text
49+
--------------------- ------------------------
50+
| SNM-I Server | | Reverse Proxy Server |
51+
| 192.168.41.202:22 | <-> | 192.168.41.156:14572 |
52+
| | | 206.12.97.46:14572 | <-> Internet
53+
--------------------- ------------------------
54+
```
55+
56+
### Webserver reverse proxy
57+
58+
#### Frontend includes docs
59+
The compiled frontend and docs are hosted on port 80.
60+
```text
61+
beta.socialneedsmarketplace.ca -> 192.168.41.202:80
62+
```
63+
64+
#### Backend API
65+
The Node.js Backend opens a port 5000 for api calls.
66+
```text
67+
beta.socialneedsmarketplace.ca/api/* -> 192.168.41.202:5000
68+
```
69+
70+
71+
## SNM-I Server Preparations
2472
Recommended OS: Ubuntu 22.04 or above
2573

26-
Follow [Docker Engine Installation](https://docs.docker.com/engine/install/ubuntu/).
74+
### Setup Docker
75+
> Follow the [official guide](https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)
76+
```shell
77+
# Add Docker's official GPG key:
78+
sudo apt-get update
79+
sudo apt-get install ca-certificates curl
80+
sudo install -m 0755 -d /etc/apt/keyrings
81+
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
82+
sudo chmod a+r /etc/apt/keyrings/docker.asc
83+
84+
# Add the repository to Apt sources:
85+
echo \
86+
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
87+
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
88+
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
89+
sudo apt-get update
90+
91+
# Install the latest version
92+
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
93+
```
2794

2895
Create an `.env` in the root directory `~/` that specifies the correct email server and graphdb server location & credentials.
2996

30-
#### Generate SSH KEY on Remote Server
97+
### Generate SSH KEY on Remote Server
3198
```shell
3299
ssh-keygen
33100
```
@@ -41,11 +108,12 @@ Your identification has been saved in /home/ubuntu/.ssh/id_rsa
41108
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub
42109
...
43110
```
44-
#### Add to `authorized_keys`
111+
### Add to `authorized_keys`
112+
This authorizes the generated key.
45113
```shell
46114
cat ./.ssh/id_rsa.pub >> ./.ssh/authorized_keys
47115
```
48-
#### Copy the private key `id_rsa` to GitHub Secret
116+
### Copy the private key `id_rsa` to GitHub Secret
49117
```shell
50118
cat ./.ssh/id_rsa
51119
```
@@ -58,17 +126,13 @@ b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
58126
```
59127
1. Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> ["Secrets"](https://github.com/csse-uoft/SNM-I/settings/secrets/actions).
60128
1. Click "New repository secret"
61-
1. Set the name to `RELEASE_SSH_PRIVATE_KEY` and copy the whole private key to the secret field.
62-
#### Set `RELEASE_SSH_KNOWN_HOSTS`
63-
On Windows or Ubuntu, ssh to the server and disconnect. Open `~/.ssh/known_hosts`. Copy the line for the remote server.
64-
```text
65-
[206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBxpcp7MHTdVfkaFHitfrRdvmXBXDLC+s4FcFb75oaAmxPAj2FeEEwRRaVv0/jXhiaPqsHl92OdY2xiRVHkZsMM=
66-
```
67-
1. Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> ["Variables"](https://github.com/csse-uoft/SNM-I/settings/variables/actions).
68-
1. Click "New repository variable"
69-
1. Set the name to `RELEASE_SSH_KNOWN_HOSTS` and copy the whole line to the secret field.
129+
1. Set the name to `BETA_SSH_PRIVATE_KEY` and copy the whole private key to the secret field.
70130

71-
#### Set up ssh port forwarding on **Reverse Proxy Server**
131+
132+
133+
## Reverse Proxy Server Setup
134+
135+
### Set up ssh port forwarding on **Reverse Proxy Server**
72136
> The following command should be executed on the reverse proxy server.
73137
Consider the following setup:
74138
```text
@@ -93,30 +157,61 @@ Add `net.ipv4.ip_forward = 1` to the bottom. Apply the change.
93157
sysctl -p
94158
```
95159

96-
97160
Add `PREROUTING` and `POSTROUTING` rules to `iptables`:
98161
```shell
99162
# ens3 is the network interface that has the public ip
100163
# Forward all requests of 206.12.97.46:14572 to 192.168.41.202:22
101164
sudo iptables -t nat -A PREROUTING -i ens3 -p tcp --dport 14572 -j DNAT --to-destination 192.168.41.202:22
102-
sudo iptables -t nat -A POSTROUTING -p tcp --dport 22 -j SNAT --to-source 192.168.41.156
165+
sudo iptables -t nat -A POSTROUTING -d 192.168.41.202 -p tcp --dport 22 -j SNAT --to-source 192.168.41.156
103166
```
104167
Make the iptables config persistent.
105168
```shell
106169
sudo apt update && sudo apt install iptables-persistent
170+
sudo sh -c '/sbin/iptables-save > /etc/iptables/rules.v4'
171+
```
172+
### Setup Caddy Reverse Proxy
173+
```text
174+
beta.socialneedsmarketplace.ca {
175+
handle /api* {
176+
reverse_proxy 192.168.41.202:5000
177+
}
178+
reverse_proxy 192.168.41.202:80
179+
}
107180
```
108181

109-
#### Set Other Variables
182+
### Add DNS Record
183+
```text
184+
Host name Type TTL Data
185+
beta.socialneedsmarketplace.ca A 1 hour 206.12.97.46
186+
```
187+
188+
## Set Other Variables
110189
Add the following Variables as well according to how you ssh to the remote server by using port forwarding:
111-
- `RELEASE_SSH_HOST`: 206.12.97.46
112-
- `RELEASE_SSH_PORT`: 14572
113-
- `RELEASE_SSH_USER`: ubuntu
190+
- `BETA_SSH_HOST`: 206.12.97.46
191+
- `BETA_SSH_PORT`: 14572
192+
- `BETA_SSH_USER`: ubuntu
114193

115-
### Add GitHub Workflow
116-
https://github.com/csse-uoft/SNM-I/blob/master/.github/workflows/release.yml
194+
### Set `BETA_SSH_KNOWN_HOSTS`
195+
If the ssh port forward is correctly configured, you can generate the `known_hosts` by:
196+
```shell
197+
ssh-keyscan -p 14572 -t ecdsa-sha2-nistp256 206.12.97.46
198+
```
199+
Output (The second line is `BETA_SSH_KNOWN_HOSTS`)
200+
```text
201+
# 206.12.97.46:14572 SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
202+
[206.12.97.46]:14572 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBBxpcp7MHTdVfkaFHitfrRdvmXBXDLC+s4FcFb75oaAmxPAj2FeEEwRRaVv0/jXhiaPqsHl92OdY2xiRVHkZsMM=
203+
```
204+
1. Navigate to "SNM-I Repo Setting" -> "Security" -> "Secrets and variables" -> "Actions" -> ["Variables"](https://github.com/csse-uoft/SNM-I/settings/variables/actions).
205+
1. Click "New repository variable"
206+
1. Set the name to `BETA_SSH_KNOWN_HOSTS` and copy the whole line to the secret field.
207+
208+
209+
## Add GitHub Workflow
210+
https://github.com/csse-uoft/SNM-I/blob/master/.github/workflows/beta.yml
117211
Remember to change the branch name:
118212
```yml
119213
on:
120214
push:
121-
branches: [ master ]
122-
```
215+
branches: [ beta ]
216+
...
217+
```

frontend/src/api/providersApi.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ export async function searchMultipleProviders(searchitem) {
2626
export async function deleteSingleProvider(id) {
2727
return deleteJson(`/api/providers/${id}`);
2828
}
29+
30+
export async function fetchHomeServiceProvider() {
31+
return getJson(`/api/providers/home`);
32+
}

0 commit comments

Comments
 (0)