Skip to content

Commit a71775e

Browse files
Sing-LiDnouv
authored andcommitted
Backport substantially more functional build-deploy script from RC4Conferences.
Part of RocketChat#182
1 parent 2f01e62 commit a71775e

12 files changed

+237
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,4 @@ dist
130130
.pnp.*
131131

132132
package-lock.json
133+
cms/package-lock.json

app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
"eslint-config-next": "12.0.7"
5151
},
5252
"volta": {
53-
"node": "16.4.0"
53+
"node": "16.13.0"
5454
}
5555
}

cms/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"node": ">=12.x.x <=16.x.x",
2828
"npm": ">=6.0.0"
2929
},
30+
"volta": {
31+
"node": "16.13.0"
32+
},
3033
"license": "MIT"
3134
}

startBackend.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/sh
2+
FAUNA_CONTAINER_ID=$( docker ps -q -f name=faunadb )
3+
4+
5+
echo "--Starting Superprofile Backend--"
6+
cd ../superprofile
7+
sh initFaunaOnce.sh
8+
cd ..
9+
10+
if [ -z $FAUNA_CONTAINER_ID ]; then
11+
echo $FAUNA_CONTAINER_ID
12+
echo "\033[31m***FaunaDB container was unable to install and start, please rerun the script***\e[0m"
13+
fi

startdevenv.sh

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/sh
2+
3+
STRAPI_PORT=1337
4+
NEXTJS_PORT=3000
5+
counter=0
6+
watchdog=5
7+
8+
check_and_set_strapi_port() {
9+
10+
if lsof -Pi :$STRAPI_PORT -sTCP:LISTEN -t >/dev/null && [ "$counter" -lt $watchdog ]; then
11+
echo "Strapi port $STRAPI_PORT already occupied, changing to the next consecutive port"
12+
STRAPI_PORT=$((STRAPI_PORT+1))
13+
counter=$((counter+1))
14+
check_and_set_strapi_port
15+
elif [ "$counter" -ge $watchdog ]; then
16+
echo "\033[31m Unable to allocate an empty port for Strapi, the last tried port was $STRAPI_PORT\e[0m"
17+
echo "Please either change the $STRAPI_PORT to an other random number or to an unused port number"
18+
exit 1
19+
else
20+
echo "🚀 An empty port found for Strapi🚀"
21+
fi
22+
}
23+
24+
check_and_set_next_port() {
25+
if lsof -Pi :$NEXTJS_PORT -sTCP:LISTEN -t >/dev/null && [ "$counter" -lt $watchdog ]; then
26+
echo "NextJS port $NEXTJS_PORT already occupied, changing to the next consecutive port"
27+
NEXTJS_PORT=$((NEXTJS_PORT+1))
28+
counter=$((counter+1))
29+
check_and_set_next_port
30+
elif [ "$counter" -ge $watchdog ]; then
31+
echo "\033[31mUnable to allocate an empty port for NextJS, the last tried port was $NEXTJS_PORT\e[0m"
32+
echo "Please either change the $NEXTJS_PORT to an other random number/unused port number"
33+
echo "After changes re-run the script"
34+
exit 1
35+
else
36+
echo "🚀 An empty port found for NextJS 🚀"
37+
fi
38+
}
39+
40+
sh startBackend.sh
41+
42+
check_and_set_strapi_port
43+
counter=0
44+
check_and_set_next_port
45+
46+
export NEXT_PUBLIC_PORT=$NEXTJS_PORT
47+
48+
printf '\nNEXT_PUBLIC_STRAPI_API_URL'="http://127.0.0.1:$STRAPI_PORT" >> app/.env
49+
printf '\nNEXT_PUBLIC_EVENT_SPK_MAIL'="[email protected]" >> app/.env
50+
51+
sh strapi.sh $STRAPI_PORT &
52+
53+
cd app
54+
export PORT=$NEXTJS_PORT
55+
export NEXT_PUBLIC_PORT=$NEXTJS_PORT
56+
npm i
57+
npm run dev
58+
59+

strapi.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cd cms
2+
npm i
3+
export PORT=$1
4+
npm run build
5+
INITIALIZE_DATA=true npm run develop

superprofile/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
init_flag
2+
init_key_flag
3+
gc.log.*
4+
*.log
5+
dbkey
6+
db/

superprofile/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### SuperProfile Setup
2+
3+
1. In the directory `/superprofile`, run
4+
```
5+
sh initFaunaOnce.sh
6+
```
7+
8+
On a successful run two flag files would be created an flag file `init_flag` and `init_key_flag` to track the first time data initialization run, and...
9+
```
10+
NEXT_PUBLIC_FAUNA_SECRET
11+
NEXT_PUBLIC_FAUNA_DOMAIN
12+
```
13+
...the above two variables with some values would be appended inside the `.env` file inside the `app` directory.
14+
15+
Congrats 🎉! The Superprofile setup is done.

superprofile/cloud/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
### Fauna Superprofile Saas Setup
2+
To access the Admin section which includes the Event Create Menu, there would be a need to setup the Superprofile, currently we use FaunaDB for handling this.
3+
Follow this [link](https://graphql.workshops.fauna.com/building/build-with-nextjs/client-setup/#creating-a-front-end-role) instructions to get the Fauna key, then paste it in the `.env` as:
4+
```
5+
NEXT_PUBLIC_FAUNA_SECRET="your private key"
6+
```
7+
To get more familiar with Fauna here is a quick short [workshop link](https://graphql.workshops.fauna.com/getting-started/)
8+
9+
Or here is a quick guide to get started with Fauna:
10+
1. Head to [dashboard.fauna.com](https://dashboard.fauna.com/), if you are logged in, click __CREATE DATABASE__, choose the US server.
11+
<img width="356" alt="image" src="https://user-images.githubusercontent.com/61188295/178947597-158a4c05-3c92-4ba6-87df-c3a808f79134.png">
12+
2. In the left section click the __GraphQL__ and then __IMPORT SCHEMA__; schema to be uploaded is located in `../assets/rc4community-schema.graphql`.
13+
3. Go to the __Functions__ tab, there will be two functions, in the UpserUser, replace it with the following function code.
14+
<details>
15+
<summary>UpsertUser function</summary>
16+
17+
```
18+
Query(
19+
Lambda(
20+
["uid", "email", "displayName", "phoneNumber", "photoURL"],
21+
Let(
22+
{
23+
user: Match(Index("getByEmail"), Var("email")),
24+
upsert: If(
25+
Exists(Var("user")),
26+
Update(Select(["ref"], Get(Var("user"))), {
27+
data: {
28+
displayName: Var("displayName"),
29+
phoneNumber: Var("phoneNumber"),
30+
photoURL: Var("photoURL")
31+
}
32+
}),
33+
Create(Collection("User"), {
34+
data: {
35+
uid: Var("uid"),
36+
email: Var("email"),
37+
displayName: Var("displayName"),
38+
phoneNumber: Var("phoneNumber"),
39+
photoURL: Var("photoURL")
40+
}
41+
})
42+
)
43+
},
44+
Var("upsert")
45+
)
46+
)
47+
)
48+
```
49+
</details>
50+
51+
4. Under the __Security__ tab click on __NEW KEY__ no need to modify anything, go with defaults, then hit the __SAVE__, copy the _KEY'S SECRET_ and paste it in the `.env` as:
52+
```
53+
NEXT_PUBLIC_FAUNA_SECRET="your key's secret"
54+
NEXT_PUBLIC_FAUNA_DOMAIN="https://graphql.us.fauna.com/graphql"
55+
56+
```
57+
5. Create the first basic user using the __GraphQL__ tab, following is the mutation schema of a GraphQL query to create a user
58+
```
59+
mutation {
60+
createUser(data: {
61+
displayName: "YOUR_NAME"
62+
email: "NEXT_PUBLIC_EVENT_ADMIN_MAIL"
63+
uid: "ANY_UNIQUE_NUMBER"
64+
events: {
65+
create: {
66+
role: "Admin"
67+
email: "NEXT_PUBLIC_EVENT_ADMIN_MAIL"
68+
}
69+
}
70+
}) {
71+
_id
72+
displayName
73+
}
74+
}
75+
```
76+
77+
6. Congrats! and thank you! for reading this. With this you are all set, to access Admin menus.

superprofile/config.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
cluster_name: rocketchat
3+
storage_data_path: /var/lib/faunadb
4+
log_path: /var/log/faunadb
5+
shutdown_grace_period_seconds: 0

superprofile/docker-compose.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: '3.5'
2+
3+
services:
4+
fauna:
5+
image: fauna/faunadb:4.15.0
6+
container_name: faunadb
7+
restart: unless-stopped
8+
volumes:
9+
- ./db:/var/lib/faunadb
10+
- ./log:/var/log/faunadb
11+
- ./config.yml:/etc/fauna.yml
12+
command: --config /etc/fauna.yml
13+
ports:
14+
- "8443:8443"
15+
- "8444:8444"
16+
- "8084:8084"

superprofile/initFaunaOnce.sh

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
waittime=30
4+
ALREADY_INITIALIZED="log/init_key_flag"
5+
6+
FAUNA_CONTAINER_ID=$( docker ps -q -f name=faunadb )
7+
8+
if [ -e $ALREADY_INITIALIZED ] && [ ! -z $FAUNA_CONTAINER_ID ]; then
9+
echo "-- Superprofile is already up and running --"
10+
exit 0
11+
fi
12+
13+
docker compose up -d
14+
echo "Waiting $waittime seconds for container to get shipped..."
15+
sleep $waittime
16+
17+
DBF="log/init_key_flag"
18+
container_name="faunadb"
19+
healthy="healthy"
20+
container_state="$( docker inspect -f '{{ .State.Health.Status }}' ${container_name} )"
21+
22+
if [ "$container_state" != $healthy ]; then
23+
echo "Docker container needs extra startup time, please increase the \$waittlist value in initFaunaOnce.sh"
24+
echo "Process ended with health status of Container: $container_state"
25+
else
26+
docker exec -it faunadb /bin/sh /var/log/faunadb/initialize.sh
27+
if [ -f log/dbkey ] && [ ! -f log/init_key_flag ]; then
28+
echo "Copying over secrets to ../app/.env"
29+
printf '\nNEXT_PUBLIC_FAUNA_SECRET=' | cat - ./log/dbkey >> ../app/.env &&
30+
printf '\nNEXT_PUBLIC_FAUNA_DOMAIN'="http://localhost:8084/graphql" >> ../app/.env
31+
touch $DBF &&
32+
echo "-- All set, superprofile launch 🚀"
33+
else
34+
echo "-- Env variables are already copied, no need to copy over twice 😉 --"
35+
fi
36+
fi

0 commit comments

Comments
 (0)