Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .releaserc.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const config = {
{
"assets": [
{ "path": "dist/db.base.tar.gz", "label": "Base (empty) database." },
{ "path": "dist/db.sample.tar.gz", "label": "Sample database." },
],
},
],
Expand Down
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ Code and tools for building the sample databases used for FarmData2 development.

All of the following documentation assumes that operations are performed within the FarmData2 Development Environment.

## Preliminaries

Install the dependencies by:
- Change into the `FD2-SampleDBs` directory
- run `npm install`

## Building the Databases

The following scripts contained in the `bin` directory are used to build the sample databases:
The following scripts contained in the `src` directory are used to build the sample databases:

- `buildBaseDB.bash`: Builds an empty farmOS database. This is used as a base for building other sample databases.
- `baseDB/buildBaseDB.bash`: Builds an empty farmOS database. This is used as a base for building other sample databases.
- `sampleDB/buildSampleDB.bash`: Builds a sample farmOS database populated with approximately 18 months of anonymized data from 2019-2020 at the Dickinson College farm.

## Manually Installing a Database
## Installing a Database

The following steps can be adapted to install a database into the currently running farmOS instance:
The `bin/installDB.bash` script can be used to install any of the compressed database in the `dist` directory.

A database can be installed into the currently running farmOS instance manually by using the commands below and changing the `db.X.tar.gz` filename in the `sudo tar` command to match the desired database:

```
docker stop fd2_postgres
Expand All @@ -23,8 +32,6 @@ sudo tar -xzf $HOME/FD2-SampleDBs/dist/db.base.tar.gz
docker start fd2_postgres
```

Change the `db.X.tar.gz` filename in the `sudo tar` command to match the desired database.

## Development

To change, modify, update, add a database:
Expand All @@ -35,7 +42,7 @@ To change, modify, update, add a database:
1. Ensure that your `development` branch is synchronized with the `upstream`
2. Create a new feature branch from the `development` branch
3. Make and test changes in your feature branch
4. Run the `bin/buildDBs.bash` script
4. Run the appropriate script(s) to build the database
5. Install and test that the new database works
- See [Manually Installing a Database](#manually-installing-a-database) above
6. Commit to your feature branch:
Expand Down
84 changes: 84 additions & 0 deletions bin/installDB.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash
# shellcheck disable=SC1091 # Make sources okay.

# Define some useful variables, import libraries and
# check some common pre-conditions.
REPO_DIR=$(git rev-parse --show-toplevel)
source "$REPO_DIR/bin/preflight.bash"

# Determine the database to be installed.
if [ ! "$1" == "" ]; then
# DB was specified on the command line.
if [ ! -f "$REPO_DIR/dist/$1" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The file $REPO_DIR/dist/$1 does not exist."
exit 255
else
DB="$1"
fi
else
# Pick the database to be installed
# shellcheck disable=SC2207
AVAILABLE_DB=( $(ls "$REPO_DIR/dist" ))
if [ ${#AVAILABLE_DB[@]} == 0 ]; then
echo -e "${RED}ERROR:${NO_COLOR} No db.X.tar.gz files found in dist."
echo -e "${RED}ERROR:${NO_COLOR} Build a database (e.g. base, sample) before installing."
exit 255
fi
echo "Choose the database to be installed."
select DB in "${AVAILABLE_DB[@]}"; do
if (("$REPLY" <= 0 || "$REPLY" > "${#AVAILABLE_DB[@]}")); then
echo -e "${ON_RED}ERROR:${NO_COLOR} Invalid choice. Please try again."
else
break
fi
done
echo ""
fi

echo -e "${UNDERLINE_GREEN}Installing the $DB database${NO_COLOR}"

echo "Stopping farmOS..."
docker stop fd2_farmos > /dev/null
error_check
echo " Stopped."

echo "Stopping Postgres..."
docker stop fd2_postgres > /dev/null
error_check
echo " Stopped."

# Make sure that the FarmData2/docker/db directory has appropriate permissions.
echo "Setting permissions on $HOME/FarmData2/docker/db..."
sudo chmod g+rwx "$HOME/FarmData2/docker/db"
error_check
sudo chgrp fd2dev "$HOME/FarmData2/docker/db"
error_check
echo " Set."

safe_cd "$HOME/FarmData2/docker/db"

echo "Deleting current databae..."
sudo rm -rf ./*
error_check
echo " Deleted."

echo "Extracting $DB..."
sudo tar -xzf "$REPO_DIR/dist/$DB" > /dev/null
error_check
echo " Extracted."

echo "Restarting Postgres..."
docker start fd2_postgres > /dev/null
error_check
STATUS=$(docker exec fd2_postgres pg_isready)
while [[ ! "$STATUS" == *"accepting connections"* ]]; do
STATUS=$(docker exec fd2_postgres pg_isready)
done
echo " Started."

echo "Restarting farmOS..."
docker start fd2_farmos > /dev/null
error_check
echo " Started."

echo -e "${UNDERLINE_GREEN}Installed the $DB database${NO_COLOR}"
26 changes: 26 additions & 0 deletions bin/preflight.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Script that can be sourced at the start of other scripts
# to setup some useful variables, import some useful libraries
# and to check that necessary conditions hold.

# shellcheck disable=SC1091 # Make sources okay.

SCRIPT_PATH=$(readlink -f "$0") # Path to this script.
# shellcheck disable=SC2034
SCRIPT_DIR=$(dirname "$SCRIPT_PATH") # Path to directory containing this script.
REPO_DIR=$(git rev-parse --show-toplevel)

source "$REPO_DIR/bin/colors.bash"
source "$REPO_DIR/bin/lib.bash"

# Ensure that this script is being run in the development container.
HOST=$(docker inspect -f '{{.Name}}' "$HOSTNAME" 2> /dev/null)
if [ ! "$HOST" == "/fd2_dev" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The installDB.bash script must be run in the fd2dev container."
exit 255
fi

# Ensure that the FarmData2 repository exits
if [ ! -d "$HOME/FarmData2" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The FarmData2 repository must be in /home/fd2dev/FarmData2".
exit 255
fi
2 changes: 1 addition & 1 deletion cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = defineConfig({
video: false,
trashAssetsBeforeRuns: true,
e2e: {
specPattern: "**/bin/*.cy.js",
specPattern: "**/*.cy.js",
},
});
Binary file added dist/db.sample.tar.gz
Binary file not shown.
55 changes: 27 additions & 28 deletions bin/buildBaseDB.bash → src/baseDB/buildBaseDB.bash
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
#!/bin/bash
# shellcheck disable=SC1091 # Make sources okay.

SCRIPT_PATH=$(readlink -f "$0") # Path to this script.
SCRIPT_DIR=$(dirname "$SCRIPT_PATH") # Path to directory containing this script.
REPO_DIR=$(dirname "$SCRIPT_DIR") # Path to the main repo directory

source "$SCRIPT_DIR/colors.bash"
source "$SCRIPT_DIR/lib.bash"

# Ensure that this script is being run in the development container.
HOST=$(docker inspect -f '{{.Name}}' "$HOSTNAME" 2> /dev/null)
if [ ! "$HOST" == "/fd2_dev" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The buildBaseDB.bash script must be run in the fd2dev container."
exit 255
fi

# Ensure that the FarmData2 repository exits
if [ ! -d "$HOME/FarmData2" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The FarmData2 repository must be in /home/fd2dev/FarmData2".
exit 255
fi
# Define some useful variables, import libraries and
# check some common pre-conditions.
REPO_DIR=$(git rev-parse --show-toplevel)
source "$REPO_DIR/bin/preflight.bash"

echo -e "${UNDERLINE_GREEN}Building the base (empty) database${NO_COLOR}"

Expand All @@ -28,6 +13,11 @@ echo "Clearing the drupal cache..."
docker exec -it fd2_farmos drush cr
echo " Cleared."

# Shut down the farmOS container
echo "Stopping the farmOS container..."
docker stop fd2_farmos > /dev/null
echo " Stopped."

# Shut down database container
echo "Stopping the database container..."
docker stop fd2_postgres > /dev/null
Expand Down Expand Up @@ -76,6 +66,22 @@ sudo rm -rf ./*
error_check
echo " Deleted."

# Bring the farmOS container back up.
echo "Bringing the farmos container back up..."
docker start fd2_farmos > /dev/null
error_check
echo " Up."

# Bring the database container back up.
echo "Bringing the database container back up..."
docker start fd2_postgres > /dev/null
error_check
STATUS=$(docker exec fd2_postgres pg_isready)
while [[ ! "$STATUS" == *"accepting connections"* ]]; do
STATUS=$(docker exec fd2_postgres pg_isready)
done
echo " Up."

# Reset the drupal settigns.php file
echo "Resetting the drupal settings.php file..."
docker exec -it fd2_farmos rm /opt/drupal/web/sites/default/settings.php
Expand All @@ -86,20 +92,13 @@ docker exec -it fd2_farmos chown www-data /opt/drupal/web/sites/default/settings
error_check
docker exec -it fd2_farmos chgrp www-data /opt/drupal/web/sites/default/settings.php
error_check
sleep 5
sleep 10
echo " Reset."

# Bring the database container back up.
echo "Bringing the database container back up..."
docker start fd2_postgres > /dev/null
error_check
sleep 5
echo " Up."

# Doing farmOS Configure Site
echo "Configuring the farmOS site..."
safe_cd "$REPO_DIR"
npx cypress run --spec=bin/farmOS.configBaseDB.cy.js
npx cypress run --spec=src/baseDB/farmOS.configBaseDB.cy.js
error_check
echo " Configured."

Expand Down
File renamed without changes.
64 changes: 64 additions & 0 deletions src/sampleDB/addUsers.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
# shellcheck disable=SC1091 # Make sources okay.

# Define some useful variables, import libraries and
# check some common pre-conditions.
REPO_DIR=$(git rev-parse --show-toplevel)
source "$REPO_DIR/bin/preflight.bash"

echo "Adding 'People' and assigning Roles..."

echo " Assigning roles to existing admin user..."
docker exec -it fd2_farmos drush user:role:add "farm_manager" "admin"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_worker" "admin"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_viewer" "admin"
error_check
echo " Assigned"

echo " Creating the Farm Manager users..."
for i in {1..2}
do
docker exec -it fd2_farmos drush user:create "manager$i" --password="farmdata2"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_manager" "manager$i"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_worker" "manager$i"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_viewer" "manager$i"
error_check
echo ""
done
echo " Created."

echo " Creating the Farm Worker users..."
for i in {1..5}
do
docker exec -it fd2_farmos drush user:create "worker$i" --password="farmdata2"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_worker" "worker$i"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_viewer" "worker$i"
error_check
echo ""
done
echo " Created."

# Create the guest
echo " Creating the guest user..."
docker exec -it fd2_farmos drush user:create "guest" --password="farmdata2"
error_check
docker exec -it fd2_farmos drush user:role:add "farm_viewer" "guest"
error_check
echo " Created."

# Create the user that will be allowed to login with basic authentication
# This username must begin with restws (see: https://www.drupal.org/node/3016570)
#docker exec -it fd2_farmos drush user-create "restws1" --password="farmdata2"
#docker exec -it fd2_farmos drush urol "Farm Manager" --name="restws1"
#docker exec -it fd2_farmos drush urol "Farm Worker" --name="restws1"
#docker exec -it fd2_farmos drush urol "Farm Viewer" --name="restws1"
#echo ""

echo "People created and Roles assigned."
64 changes: 64 additions & 0 deletions src/sampleDB/buildSampleDB.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash
# shellcheck disable=SC1091 # Make sources okay.

# Define some useful variables, import libraries and
# check some common pre-conditions.
REPO_DIR=$(git rev-parse --show-toplevel)
source "$REPO_DIR/bin/preflight.bash"

echo -e "${UNDERLINE_GREEN}Building the sample database${NO_COLOR}"

# Ensure that the base (empty) database exits
if [ ! -f "$REPO_DIR/dist/db.base.tar.gz" ]; then
echo -e "${RED}ERROR:${NO_COLOR} The db.base.tar.gz database must exist."
echo -e "${RED}ERROR:${NO_COLOR} Use bin/buildBaseDB.bash to create it."
exit 255
fi

# Install the base database so we can build the sample database on top.
"$REPO_DIR/bin/installDB.bash" db.base.tar.gz
error_check
echo ""

# Set the farm name and "slogan"
"$SCRIPT_DIR/setFarmInfo.bash"
error_check
echo ""

# Enable the FarmData2 modules in farmos
"$SCRIPT_DIR/enableFarmData2Modules.bash"
error_check
echo ""

# Add the users and assign their roles
"$SCRIPT_DIR/addUsers.bash"
error_check
echo ""




# Make sure that the new FarmData2/docker/db directory has appropriate permissions.
echo "Setting permissions on $HOME/FarmData2/docker/db..."
sudo chmod g+rwx "$HOME/FarmData2/docker/db"
error_check
sudo chgrp fd2dev "$HOME/FarmData2/docker/db"
error_check
echo " Set."

# Compress the sample database into the dist directory
echo "Compressing base database..."
safe_cd "$HOME/FarmData2/docker/db"
DIST_FILE="$REPO_DIR/dist/db.sample.tar.gz"
rm "$DIST_FILE" > /dev/null
sudo tar czvf "$DIST_FILE" ./* > /dev/null
error_check
sudo chown fd2dev "$DIST_FILE"
error_check
sudo chgrp fd2dev "$DIST_FILE"
error_check
sudo chmod g+w "$DIST_FILE"
error_check
echo " Compressed into $DIST_FILE"

echo -e "${UNDERLINE_GREEN}Sample database has been built.${NO_COLOR}"
Loading