Skip to content

Commit

Permalink
remove fontawesome packags, use js. Add kill script. Fix Mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
JessicaMulein committed Dec 19, 2024
1 parent 977c1de commit ce751d6
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/Mongo.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ RUN rm -rf /docker-entrypoint-initdb.d/*
COPY mongodb_entrypoint.sh /usr/local/bin/mongodb_entrypoint.sh
RUN chmod +x /usr/local/bin/mongodb_entrypoint.sh

COPY mongodb_healthcheck.sh /usr/local/bin/mongodb_healthcheck.sh
RUN chmod +x /usr/local/bin/mongodb_healthcheck.sh

# Override the default entrypoint
ENTRYPOINT ["/usr/local/bin/mongodb_entrypoint.sh"]

Expand Down
7 changes: 4 additions & 3 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ services:
hostname: db # Sets the internal hostname to 'db'
restart: unless-stopped
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 10s
timeout: 5s
test: ["CMD", "bash", "/usr/local/bin/mongodb_healthcheck.sh"]
interval: 30s
timeout: 10s
retries: 5
start_period: 40s
ports:
- "27017:27017"
volumes:
Expand Down
148 changes: 84 additions & 64 deletions .devcontainer/mongodb_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,100 +1,120 @@
#!/bin/bash
set -e
set -x # Enable debug mode for logging each command

echo "Hostname: $(hostname)"

# Environment Variables with Defaults
MONGO_LOG="/var/log/mongodb/mongod.log"
MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME:-"root"}
MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD:-"rootpassword"}
MONGO_LOG=${MONGO_LOG:-"/var/log/mongodb/mongod.log"}
MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME:-"chilicilantro"}
MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD:-"Ch1l1C1l4ntr0"}
MONGO_REPLICA_SET_NAME=${MONGO_REPLICA_SET_NAME:-"rs0"}
MONGO_BIND_IP=${MONGO_BIND_IP:-"0.0.0.0"}
MONGO_PORT=${MONGO_PORT:-27017}
MONGO_KEYFILE=${MONGO_KEYFILE:-"/tmp/replica.key"}
MONGO_DB_PATH=${MONGO_DB_PATH:-"/data/db"}
MAX_ATTEMPTS=30
RETRY_INTERVAL=${RETRY_INTERVAL:-30}
SETUP_COMPLETE_FILE="/data/db/.setup_complete"

start_mongo_noauth() {
echo "Starting MongoDB without authentication..."
mongod --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --fork
echo "Starting MongoDB without authentication..."
mongod --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --fork
}

start_mongo_auth_foreground() {
echo "Starting MongoDB with authentication in the foreground..."
exec mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --keyFile "$MONGO_KEYFILE"
wait_for_mongo() {
echo "Waiting for MongoDB to be ready..."
for i in $(seq 1 $MAX_ATTEMPTS); do
if mongosh --quiet --eval "db.runCommand({ ping: 1 })" &>/dev/null; then
echo "MongoDB is ready."
return 0
fi
echo "Attempt $i: MongoDB not ready yet, retrying in $RETRY_INTERVAL seconds..."
sleep $RETRY_INTERVAL
done
echo "Failed to connect to MongoDB after $MAX_ATTEMPTS attempts."
return 1
}

stop_mongo() {
echo "Stopping MongoDB..."
mongosh --quiet --eval 'db.getSiblingDB("admin").shutdownServer({ force: true });' || true
sleep 5
initialize_replica_set() {
echo "Initializing replica set..."
local host="localhost"
mongosh --quiet --eval "
rs.initiate({
_id: '$MONGO_REPLICA_SET_NAME',
members: [{ _id: 0, host: '$host:$MONGO_PORT' }]
})
"
}

wait_for_mongo() {
echo "Waiting for MongoDB to be ready..."
until mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; do
echo "MongoDB not ready yet, retrying..."
sleep 2
done
echo "MongoDB is ready."
wait_for_primary() {
echo "Waiting for replica set to become PRIMARY..."
for i in $(seq 1 $MAX_ATTEMPTS); do
if [ "$1" = "auth" ]; then
status=$(mongosh admin --quiet --eval "db.auth('$MONGO_INITDB_ROOT_USERNAME', '$MONGO_INITDB_ROOT_PASSWORD'); rs.status().myState")
else
status=$(mongosh --quiet --eval "rs.status().myState")
fi
if [ "$status" = "1" ]; then
echo "Replica set is now PRIMARY."
return 0
fi
echo "Attempt $i: Replica set is not PRIMARY yet, retrying in $RETRY_INTERVAL seconds..."
sleep $RETRY_INTERVAL
done
echo "Replica set did not become PRIMARY within the expected time."
return 1
}

create_root_user() {
echo "Creating root user if it does not exist..."
mongosh --quiet --eval "
const adminDb = db.getSiblingDB('admin');
if (!adminDb.getUser('$MONGO_INITDB_ROOT_USERNAME')) {
adminDb.createUser({
user: '$MONGO_INITDB_ROOT_USERNAME',
pwd: '$MONGO_INITDB_ROOT_PASSWORD',
roles: [{ role: 'root', db: 'admin' }]
});
print('Root user created.');
} else {
print('Root user already exists.');
}
"
echo "Creating root user..."
mongosh admin --quiet --eval "
db.createUser({
user: '$MONGO_INITDB_ROOT_USERNAME',
pwd: '$MONGO_INITDB_ROOT_PASSWORD',
roles: ['root']
});
"
}

initialize_replica_set() {
echo "Initializing replica set..."
mongosh --quiet --eval "
const status = rs.status();
if (status.codeName === 'NotYetInitialized') {
rs.initiate({
_id: '$MONGO_REPLICA_SET_NAME',
members: [{ _id: 0, host: 'localhost:$MONGO_PORT' }]
});
print('Replica set initialized.');
} else {
print('Replica set already initialized.');
}
"
start_mongo_auth() {
echo "Starting MongoDB with authentication..."
mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --keyFile "$MONGO_KEYFILE" --fork
}

wait_for_primary() {
echo "Waiting for replica set to become PRIMARY..."
until mongosh --quiet --eval 'db.isMaster().ismaster' | grep -q 'true'; do
echo "Replica set is not PRIMARY yet, retrying..."
sleep 2
done
echo "Replica set is now PRIMARY."
start_mongo_auth_foreground() {
echo "Starting MongoDB with authentication in the foreground..."
exec mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --keyFile "$MONGO_KEYFILE"
}

# Main Execution Flow
if [ -f "$SETUP_COMPLETE_FILE" ]; then
echo "Setup already completed. Starting MongoDB in foreground mode."
start_mongo_auth_foreground
exit 0
fi

# Main execution flow
echo "Starting MongoDB setup..."

# Start MongoDB without authentication
start_mongo_noauth
wait_for_mongo
wait_for_mongo || exit 1

# Initialize the replica set
echo "Initializing replica set..."
initialize_replica_set
wait_for_primary
wait_for_primary || exit 1

# Create the root user
echo "Creating root user..."
create_root_user

# Restart MongoDB with authentication
stop_mongo
start_mongo_auth_foreground
echo "Shutting down MongoDB to restart with authentication..."
mongod --shutdown

echo "Starting MongoDB with authentication..."
start_mongo_auth
wait_for_mongo || exit 1
wait_for_primary "auth" || exit 1

# Mark setup as complete
touch "$SETUP_COMPLETE_FILE"

echo "MongoDB setup completed successfully. Starting MongoDB in foreground mode."
start_mongo_auth_foreground
39 changes: 29 additions & 10 deletions .devcontainer/mongodb_healthcheck.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
#!/bin/bash

MAX_ATTEMPTS=30
RETRY_INTERVAL=10

# Wait for MongoDB to start
until mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; do
echo "Waiting for MongoDB to start..."
sleep 5
for i in $(seq 1 $MAX_ATTEMPTS); do
if mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; then
echo "[HC] MongoDB is responsive."
break
fi
echo "[HC] Attempt $i/$MAX_ATTEMPTS: Waiting for MongoDB to start..."
sleep $RETRY_INTERVAL
if [ $i -eq $MAX_ATTEMPTS ]; then
echo "[HC] MongoDB failed to start after $MAX_ATTEMPTS attempts."
exit 1
fi
done

# Check if MongoDB replica set is ready
is_master=$(mongosh --quiet --eval 'db.isMaster().ismaster' --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD")
if [ "$is_master" != "true" ]; then
echo "MongoDB replica set is not yet ready."
exit 1
fi
for i in $(seq 1 $MAX_ATTEMPTS); do
rs_status=$(mongosh --quiet --eval 'rs.status().ok' --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" || echo "0")
if [ "$rs_status" = "1" ]; then
echo "[HC] MongoDB replica set is ready."
exit 0
fi
echo "[HC] Attempt $i/$MAX_ATTEMPTS: Waiting for MongoDB replica set to be ready..."
sleep $RETRY_INTERVAL
if [ $i -eq $MAX_ATTEMPTS ]; then
echo "[HC] MongoDB replica set failed to initialize after $MAX_ATTEMPTS attempts."
exit 1
fi
done

echo "MongoDB is up and running with replica set."
exit 0
echo "[HC] MongoDB healthcheck failed."
exit 1
Binary file modified .yarn/install-state.gz
Binary file not shown.
3 changes: 1 addition & 2 deletions chili-and-cilantro-react/src/app/components/game-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { Box, IconButton, Menu, MenuItem } from '@mui/material';
import {
FC,
Expand Down Expand Up @@ -50,7 +49,7 @@ export const GameMenu: FC = () => {
return (
<Box ref={dropdownRef}>
<IconButton color="inherit" onClick={handleClick}>
<FontAwesomeIcon icon={['fas', 'pepper-hot']} />
<i className="fa-duotone fa-pepper-hot" />
</IconButton>
<Menu anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
{gameOptions.map((option, index) => (
Expand Down
39 changes: 31 additions & 8 deletions chili-and-cilantro-react/src/app/components/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
StringNames,
constants,
} from '@chili-and-cilantro/chili-and-cilantro-lib';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
Box,
Button,
FormControlLabel,
Switch,
TextField,
Typography,
styled,
} from '@mui/material';
import { AxiosResponse } from 'axios';
import { useFormik } from 'formik';
Expand All @@ -34,6 +34,7 @@ interface IFormValues {
const GameSetup: React.FC = () => {
const { t } = useAppTranslation();
const { user } = useAuth();

const validationSchema = Yup.object().shape({
gameName: Yup.string().test(
'gameName',
Expand Down Expand Up @@ -173,6 +174,28 @@ const GameSetup: React.FC = () => {
formik.setFieldValue('maxChefs', newValue);
};

const StyledButtonPrimary = styled(Button)(({ theme }) => ({
minWidth: '50px',
minHeight: '50px',
fontSize: '1.5rem',
backgroundColor: theme.palette.primary.main,
color: theme.palette.primary.contrastText,
'&:hover': {
backgroundColor: theme.palette.primary.dark,
},
}));

const StyledButtonSecondary = styled(Button)(({ theme }) => ({
minWidth: '50px',
minHeight: '50px',
fontSize: '1.5rem',
backgroundColor: theme.palette.secondary.main,
color: theme.palette.secondary.contrastText,
'&:hover': {
backgroundColor: theme.palette.secondary.dark,
},
}));

return (
<Box
component="form"
Expand Down Expand Up @@ -256,14 +279,15 @@ const GameSetup: React.FC = () => {
<Typography variant="body1" mr={2}>
Max Chefs:
</Typography>
<Button
<StyledButtonSecondary
size="small"
variant="outlined"
onClick={handleChefDecrement}
disabled={formik.values.maxChefs <= constants.MIN_CHEFS}
style={{ minWidth: '50px', minHeight: '50px' }}
>
<FontAwesomeIcon icon={['fas', 'knife-kitchen']} />
</Button>
<i className="fa-duotone fa-knife-kitchen" />
</StyledButtonSecondary>
<TextField
name="maxChefs"
value={formik.values.maxChefs}
Expand All @@ -272,22 +296,21 @@ const GameSetup: React.FC = () => {
}}
sx={{ width: '60px', mx: 1, '& input': { textAlign: 'center' } }}
/>
<Button
<StyledButtonPrimary
size="small"
variant="outlined"
onClick={handleChefIncrement}
disabled={formik.values.maxChefs >= constants.MAX_CHEFS}
>
<FontAwesomeIcon icon={['fas', 'user-chef']} />
</Button>
<i className="fa-duotone fa-user-chef" />
</StyledButtonPrimary>
</Box>
)}
<Button type="submit" fullWidth variant="contained" sx={{ mt: 3, mb: 2 }}>
{formik.values.gameMode === 'CREATE'
? t(StringNames.Game_CreateGame)
: t(StringNames.Game_JoinGame)}
</Button>
<FontAwesomeIcon icon={['fas', 'user-chef']} />
</Box>
);
};
Expand Down
3 changes: 1 addition & 2 deletions chili-and-cilantro-react/src/app/components/user-menu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StringNames } from '@chili-and-cilantro/chili-and-cilantro-lib';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
ExitToApp as LogoutIcon,
VpnKey as VpnKeyIcon,
Expand Down Expand Up @@ -27,7 +26,7 @@ export const UserMenu: FC = () => {
return (
<Box>
<IconButton color="inherit" onClick={handleClick}>
<FontAwesomeIcon icon={['fas', 'user-circle']} />
<i className="fa-duotone fa-user-circle" />
</IconButton>
<Menu anchorEl={anchorEl} open={Boolean(anchorEl)} onClose={handleClose}>
<MenuItem
Expand Down
Loading

0 comments on commit ce751d6

Please sign in to comment.