Skip to content

Commit

Permalink
VH 1303 - Updated the script to generate a .env file based on user in…
Browse files Browse the repository at this point in the history
…put (#615)

<!-- Thanks for the contribution, this is awesome. -->

# PR Details
## Description

In this PR, initialization script is updated to generate a .env file
based on user input.
<!--- Describe your changes in detail -->

## Related Issue
[VH-1303](https://usdot-carma.atlassian.net/browse/VH-1303)
<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an
issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps
to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?
This update has been tested by running the script locally to check that
user input is taken and .env file created.

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->

- [ ] Defect fix (non-breaking change that fixes an issue)
- [] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that cause existing functionality
to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->

- [ ] I have added any new packages to the sonar-scanner.properties file
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
[V2XHUB Contributing
Guide](https://github.com/usdot-fhwa-OPS/V2X-Hub/blob/develop/Contributing.md)
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
  • Loading branch information
DokurOmkar authored Jun 7, 2024
1 parent ab664ac commit 165cb55
Showing 1 changed file with 99 additions and 7 deletions.
106 changes: 99 additions & 7 deletions configuration/initialization.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,88 @@
#!/bin/bash

# Default values for the variables
PORT_DRAYAGE_ENABLED_DEFAULT="FALSE"
INFRASTRUCTURE_ID_DEFAULT="rsu_1234"
INFRASTRUCTURE_NAME_DEFAULT="East Intersection"
V2XHUB_IP_DEFAULT="127.0.0.1"
SIMULATION_MODE_DEFAULT="FALSE"
SIMULATION_IP_DEFAULT="127.0.0.1"
SENSOR_JSON_FILE_PATH_DEFAULT="/var/www/plugins/MAP/sensors.json"

echo "Setting up the environment..."

# Repository URL
repo_url_latest="https://api.github.com/repos/usdot-fhwa-OPS/V2X-Hub/releases/latest"

# Getting the latest release information using curl
release_info=$(curl -sSL $repo_url_latest)

# Parsing the JSON response to get the tag_name (version) of the latest release
latest_version=$(echo "$release_info" | grep -o '"tag_name": *"[^"]*"' | cut -d '"' -f 4)

# Fetching all tags from Git repository
tags=$(git ls-remote --tags https://github.com/usdot-fhwa-OPS/V2X-Hub.git | awk -F/ '{print $3}' | sort -V)

# Displaying all available versions
echo "Available versions:"
echo "$tags"

# select a version or accept the latest version as default
read -r -p "Enter V2X-Hub Version (choose from the above, or press Enter to use latest version $latest_version): " chosen_version
V2XHUB_VERSION=${chosen_version:-$latest_version}

# Enable Port Drayage functionality
read -r -p "Enable Port Drayage functionality (TRUE/FALSE, or press Enter to use default as $PORT_DRAYAGE_ENABLED_DEFAULT): " PORT_DRAYAGE_ENABLED
PORT_DRAYAGE_ENABLED=${PORT_DRAYAGE_ENABLED:-$PORT_DRAYAGE_ENABLED_DEFAULT}

# Infrastructure id
read -r -p "Enter Infrastructure id (or press Enter to use default as $INFRASTRUCTURE_ID_DEFAULT): " INFRASTRUCTURE_ID
INFRASTRUCTURE_ID=${INFRASTRUCTURE_ID:-$INFRASTRUCTURE_ID_DEFAULT}

# Infrastructure name
read -r -p "Enter Infrastructure name (or press Enter to use default as $INFRASTRUCTURE_NAME_DEFAULT): " INFRASTRUCTURE_NAME
INFRASTRUCTURE_NAME=${INFRASTRUCTURE_NAME:-$INFRASTRUCTURE_NAME_DEFAULT}

# V2XHub IP
read -r -p "Enter V2XHub IP (or press Enter to use default as $V2XHUB_IP_DEFAULT): " V2XHUB_IP
V2XHUB_IP=${V2XHUB_IP:-$V2XHUB_IP_DEFAULT}

# Simulation Mode
read -r -p "Simulation Mode (TRUE/FALSE, or press Enter to use default as $SIMULATION_MODE_DEFAULT): " SIMULATION_MODE
SIMULATION_MODE=${SIMULATION_MODE:-$SIMULATION_MODE_DEFAULT}

# In Simulation Mode
if [[ $SIMULATION_MODE == "TRUE" ]]; then
# Simulation IP
read -r -p "Enter Simulation IP (or press Enter to use default is $SIMULATION_IP_DEFAULT): " SIMULATION_IP
SIMULATION_IP=${SIMULATION_IP:-$SIMULATION_IP_DEFAULT}

# Sensor Configuration File Path
read -r -p "Enter Sensor Configuration File Path (or press Enter to use default as $SENSOR_JSON_FILE_PATH_DEFAULT): " SENSOR_JSON_FILE_PATH
SENSOR_JSON_FILE_PATH=${SENSOR_JSON_FILE_PATH:-$SENSOR_JSON_FILE_PATH_DEFAULT}
fi

echo "WARNING: This will overwrite the existing .env file if it exists."
read -r -p "Are you sure you want to continue? (Y/N): " overwrite_confirm
if [[ "$overwrite_confirm" =~ [yY](es)* ]]; then
# Write to .env file
cat <<EOF > .env
V2XHUB_VERSION=$V2XHUB_VERSION
INFRASTRUCTURE_ID=$INFRASTRUCTURE_ID
INFRASTRUCTURE_NAME=$INFRASTRUCTURE_NAME
V2XHUB_IP=$V2XHUB_IP
SIMULATION_MODE=$SIMULATION_MODE
EOF

# Adding Simulation IP and Sensor Path if Simulation Mode is TRUE
if [[ $SIMULATION_MODE == "TRUE" ]]; then
echo "SIMULATION_IP=$SIMULATION_IP" >> .env
echo "SENSOR_JSON_FILE_PATH=$SENSOR_JSON_FILE_PATH" >> .env
fi
else
echo "Aborting. No changes were made to the .env file."
fi

directory=$(pwd)
mysqlDir="$directory/mysql"

Expand All @@ -9,15 +93,15 @@ sudo apt update -y && sudo apt upgrade -y
sudo apt-get install chromium-browser -y

# Make passwords for mysql
mkdir -p secrets && cd secrets
mkdir -p secrets && cd secrets || return # SC2164 - Use return in case cd fails

# Creates password files where user inputs password
FILE1=mysql_root_password.txt
FILE2=mysql_password.txt
if test -f "$FILE1"; then
echo "$FILE1 exists."
else
read -p "enter password for the mysql_root_password: " sql_root_pass
read -r -p "enter password for the mysql_root_password: " sql_root_pass # SC2162 - read without -r will mangle backslashes
echo "$sql_root_pass" > sql_root_pass.txt
# Remove endline characters from password files
tr -d '\n' <sql_root_pass.txt> mysql_root_password.txt && rm sql_root_pass.txt
Expand All @@ -26,15 +110,14 @@ fi
if test -f "$FILE2"; then
echo "$FILE2 exists."
else
read -p "enter password for mysql_password: " sql_pass
read -r -p "enter password for mysql_password: " sql_pass
echo "$sql_pass" > sql_pass.txt
# Remove endline characters from password files
tr -d '\n' <sql_pass.txt> mysql_password.txt && rm sql_pass.txt
fi
# TODO VH-1303 Allow for version and configuration selection in initialization script

# AMD64 initialization
cd $directory
cd "$directory" || return # return in case cd fails
for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
# Add Docker's official GPG key:
sudo apt-get update
Expand All @@ -50,10 +133,19 @@ echo \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker compose up -d

# Check if Port Drayage functionality is enabled
if [[ $PORT_DRAYAGE_ENABLED == "TRUE" ]]; then
sudo docker compose up -d
elif [[ $PORT_DRAYAGE_ENABLED == "FALSE" ]]; then
sudo docker compose up php -d
else
echo "Invalid value for PORT_DRAYAGE_ENABLED. Please provide either TRUE or FALSE."
exit 1
fi

# Create V2X Hub user
cd $mysqlDir
cd "$mysqlDir" || return # return in case cd fails
./add_v2xhub_user.bash

chromium-browser "http://127.0.0.1" > /dev/null 2>&1 &
Expand Down

0 comments on commit 165cb55

Please sign in to comment.