Skip to content

Commit

Permalink
Backport substantially more functional build-deploy script from RC4Co…
Browse files Browse the repository at this point in the history
…nferences.

Part of RocketChat#182
  • Loading branch information
Sing-Li authored and Dnouv committed Nov 3, 2022
1 parent 2f01e62 commit a71775e
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ dist
.pnp.*

package-lock.json
cms/package-lock.json
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
"eslint-config-next": "12.0.7"
},
"volta": {
"node": "16.4.0"
"node": "16.13.0"
}
}
3 changes: 3 additions & 0 deletions cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
"node": ">=12.x.x <=16.x.x",
"npm": ">=6.0.0"
},
"volta": {
"node": "16.13.0"
},
"license": "MIT"
}
13 changes: 13 additions & 0 deletions startBackend.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
FAUNA_CONTAINER_ID=$( docker ps -q -f name=faunadb )


echo "--Starting Superprofile Backend--"
cd ../superprofile
sh initFaunaOnce.sh
cd ..

if [ -z $FAUNA_CONTAINER_ID ]; then
echo $FAUNA_CONTAINER_ID
echo "\033[31m***FaunaDB container was unable to install and start, please rerun the script***\e[0m"
fi
59 changes: 59 additions & 0 deletions startdevenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/sh

STRAPI_PORT=1337
NEXTJS_PORT=3000
counter=0
watchdog=5

check_and_set_strapi_port() {

if lsof -Pi :$STRAPI_PORT -sTCP:LISTEN -t >/dev/null && [ "$counter" -lt $watchdog ]; then
echo "Strapi port $STRAPI_PORT already occupied, changing to the next consecutive port"
STRAPI_PORT=$((STRAPI_PORT+1))
counter=$((counter+1))
check_and_set_strapi_port
elif [ "$counter" -ge $watchdog ]; then
echo "\033[31m Unable to allocate an empty port for Strapi, the last tried port was $STRAPI_PORT\e[0m"
echo "Please either change the $STRAPI_PORT to an other random number or to an unused port number"
exit 1
else
echo "🚀 An empty port found for Strapi🚀"
fi
}

check_and_set_next_port() {
if lsof -Pi :$NEXTJS_PORT -sTCP:LISTEN -t >/dev/null && [ "$counter" -lt $watchdog ]; then
echo "NextJS port $NEXTJS_PORT already occupied, changing to the next consecutive port"
NEXTJS_PORT=$((NEXTJS_PORT+1))
counter=$((counter+1))
check_and_set_next_port
elif [ "$counter" -ge $watchdog ]; then
echo "\033[31mUnable to allocate an empty port for NextJS, the last tried port was $NEXTJS_PORT\e[0m"
echo "Please either change the $NEXTJS_PORT to an other random number/unused port number"
echo "After changes re-run the script"
exit 1
else
echo "🚀 An empty port found for NextJS 🚀"
fi
}

sh startBackend.sh

check_and_set_strapi_port
counter=0
check_and_set_next_port

export NEXT_PUBLIC_PORT=$NEXTJS_PORT

printf '\nNEXT_PUBLIC_STRAPI_API_URL'="http://127.0.0.1:$STRAPI_PORT" >> app/.env
printf '\nNEXT_PUBLIC_EVENT_SPK_MAIL'="dhgysfmedomihkzkwv@kvhrr.com" >> app/.env

sh strapi.sh $STRAPI_PORT &

cd app
export PORT=$NEXTJS_PORT
export NEXT_PUBLIC_PORT=$NEXTJS_PORT
npm i
npm run dev


5 changes: 5 additions & 0 deletions strapi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cd cms
npm i
export PORT=$1
npm run build
INITIALIZE_DATA=true npm run develop
6 changes: 6 additions & 0 deletions superprofile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
init_flag
init_key_flag
gc.log.*
*.log
dbkey
db/
15 changes: 15 additions & 0 deletions superprofile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### SuperProfile Setup

1. In the directory `/superprofile`, run
```
sh initFaunaOnce.sh
```

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...
```
NEXT_PUBLIC_FAUNA_SECRET
NEXT_PUBLIC_FAUNA_DOMAIN
```
...the above two variables with some values would be appended inside the `.env` file inside the `app` directory.

Congrats 🎉! The Superprofile setup is done.
77 changes: 77 additions & 0 deletions superprofile/cloud/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
### Fauna Superprofile Saas Setup
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.
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:
```
NEXT_PUBLIC_FAUNA_SECRET="your private key"
```
To get more familiar with Fauna here is a quick short [workshop link](https://graphql.workshops.fauna.com/getting-started/)

Or here is a quick guide to get started with Fauna:
1. Head to [dashboard.fauna.com](https://dashboard.fauna.com/), if you are logged in, click __CREATE DATABASE__, choose the US server.
<img width="356" alt="image" src="https://user-images.githubusercontent.com/61188295/178947597-158a4c05-3c92-4ba6-87df-c3a808f79134.png">
2. In the left section click the __GraphQL__ and then __IMPORT SCHEMA__; schema to be uploaded is located in `../assets/rc4community-schema.graphql`.
3. Go to the __Functions__ tab, there will be two functions, in the UpserUser, replace it with the following function code.
<details>
<summary>UpsertUser function</summary>

```
Query(
Lambda(
["uid", "email", "displayName", "phoneNumber", "photoURL"],
Let(
{
user: Match(Index("getByEmail"), Var("email")),
upsert: If(
Exists(Var("user")),
Update(Select(["ref"], Get(Var("user"))), {
data: {
displayName: Var("displayName"),
phoneNumber: Var("phoneNumber"),
photoURL: Var("photoURL")
}
}),
Create(Collection("User"), {
data: {
uid: Var("uid"),
email: Var("email"),
displayName: Var("displayName"),
phoneNumber: Var("phoneNumber"),
photoURL: Var("photoURL")
}
})
)
},
Var("upsert")
)
)
)
```
</details>

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:
```
NEXT_PUBLIC_FAUNA_SECRET="your key's secret"
NEXT_PUBLIC_FAUNA_DOMAIN="https://graphql.us.fauna.com/graphql"
```
5. Create the first basic user using the __GraphQL__ tab, following is the mutation schema of a GraphQL query to create a user
```
mutation {
createUser(data: {
displayName: "YOUR_NAME"
email: "NEXT_PUBLIC_EVENT_ADMIN_MAIL"
uid: "ANY_UNIQUE_NUMBER"
events: {
create: {
role: "Admin"
email: "NEXT_PUBLIC_EVENT_ADMIN_MAIL"
}
}
}) {
_id
displayName
}
}
```

6. Congrats! and thank you! for reading this. With this you are all set, to access Admin menus.
5 changes: 5 additions & 0 deletions superprofile/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
cluster_name: rocketchat
storage_data_path: /var/lib/faunadb
log_path: /var/log/faunadb
shutdown_grace_period_seconds: 0
16 changes: 16 additions & 0 deletions superprofile/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3.5'

services:
fauna:
image: fauna/faunadb:4.15.0
container_name: faunadb
restart: unless-stopped
volumes:
- ./db:/var/lib/faunadb
- ./log:/var/log/faunadb
- ./config.yml:/etc/fauna.yml
command: --config /etc/fauna.yml
ports:
- "8443:8443"
- "8444:8444"
- "8084:8084"
36 changes: 36 additions & 0 deletions superprofile/initFaunaOnce.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/sh

waittime=30
ALREADY_INITIALIZED="log/init_key_flag"

FAUNA_CONTAINER_ID=$( docker ps -q -f name=faunadb )

if [ -e $ALREADY_INITIALIZED ] && [ ! -z $FAUNA_CONTAINER_ID ]; then
echo "-- Superprofile is already up and running --"
exit 0
fi

docker compose up -d
echo "Waiting $waittime seconds for container to get shipped..."
sleep $waittime

DBF="log/init_key_flag"
container_name="faunadb"
healthy="healthy"
container_state="$( docker inspect -f '{{ .State.Health.Status }}' ${container_name} )"

if [ "$container_state" != $healthy ]; then
echo "Docker container needs extra startup time, please increase the \$waittlist value in initFaunaOnce.sh"
echo "Process ended with health status of Container: $container_state"
else
docker exec -it faunadb /bin/sh /var/log/faunadb/initialize.sh
if [ -f log/dbkey ] && [ ! -f log/init_key_flag ]; then
echo "Copying over secrets to ../app/.env"
printf '\nNEXT_PUBLIC_FAUNA_SECRET=' | cat - ./log/dbkey >> ../app/.env &&
printf '\nNEXT_PUBLIC_FAUNA_DOMAIN'="http://localhost:8084/graphql" >> ../app/.env
touch $DBF &&
echo "-- All set, superprofile launch 🚀"
else
echo "-- Env variables are already copied, no need to copy over twice 😉 --"
fi
fi

0 comments on commit a71775e

Please sign in to comment.