This guide provides step-by-step instructions for installing and setting up mlab_api.
Before installing mlab_api, ensure that the following requirements are met:
- Ruby 3.2.0
- MySQL 8
- Rails 7
- Redis version 6+
- Add the Redis repository:
sudo apt install lsb-release curl gpg
curl -fsSL https://packages.redis.io/gpg | sudo gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/redis.list
sudo apt-get update - Install Redis:
sudo apt-get install redis- Enable and start the Redis server:
sudo systemctl enable --now redis-server- Verify the status of the Redis server:
sudo systemctl status redis-server- Verifiy redis version to be 6+
redis-server --version
- Install via docker
docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=root -d -p 3308:3306 --restart always mysql:8.0.35 --default-authentication-plugin=mysql_native_password
- Check if correctly installed
mysql -uroot -p -h127.0.0.1 -P3308
- Should you want to backup your database, you can use the following command:
mysqldump -u root -p -h127.0.0.1 -P3308 mlab_api > mlab_api.sql
- Clone the
mlab_apirepository:
git clone https://github.com/EGPAFMalawiHIS/mlab_api.git- Checkout the
mainbranch:
git checkout main- Copy the
application.yml.examplefile toapplication.yml:
cp application.yml.example application.yml- Edit the
application.ymlfile to configure the DDE and NLIMS settings and default settings:
vim application.ymlInstalling ElasticSearch - If you set use_elasticsearch to true in the application.yml in the default block
A. Via docker: https://hub.docker.com/_/elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -e "discovery.type=single-node" --restart always elasticsearch:7.17.18Test elasticsearch Installation
curl -X GET 'http://localhost:9200' #This should output something about name, cluster_name, tagline, versionOR
- Install Java
java -version # Check if java is already installed, if not install by running below commands
sudo apt install default-jre
sudo apt install default-jdk
javac -version # Check if the installation was a success- Add elasticsearch repository
curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic.gpg
echo "deb [signed-by=/usr/share/keyrings/elastic.gpg] https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list
- Install Elasticsearch
sudo apt update
sudo apt install elasticsearch
sudo systemctl start elasticsearch- Test elasticsearch installation
curl -X GET 'http://localhost:9200' #This should output something about name, cluster_name, tagline, version- Enable elasticsearch
sudo systemctl enable elasticsearchIf you are running elasticsearch on a machine with limited memory or you want to limit the memory usage of elasticsearch, you can configure the memory settings in the /etc/elasticsearch/jvm.options file.
Look for the following lines:
-Xms4g
-Xmx4g
These set the heap size to 4GB. Reduce them to match your workload. For example:
-Xms1g
-Xmx1g
Ensure -Xms (minimum) and -Xmx (maximum) are the same value.
You can know the current memory usage of elasticsearch by running the following command:
top -o %MEMThis will show you the memory usage of all running processes. Look for the Java process and see the memory usage.
-
For a new fresh setup without a previous iBLIS installation:
- Only set the database connections for the
defaultblock indatabase.ymlor the database name in either production or development block depending on the environment your application will be run in.
- Only set the database connections for the
-
For sites that had iBLIS previously:
- Set up the database connections for both the
defaultblock and theiblis_dbblock found at the bottom of thedatabase.ymlfile. - The
iBLISblock should be connected to MySQL 8 iBLIS database, which you will load from the iBLIS dump.
- Set up the database connections for both the
Copy the master.key.example file in the config folder to master.key:
cp master.key.example master.key
export SECRET_KEY_BASE=$(bundle exec rails secret)- Install the required dependencies:
rvm use 3.2.0
bundle install --local
npm install- Create and migrate the
mlab_apidatabase:
RAILS_ENV=production rails db:create && RAILS_ENV=production rails db:migrate && RAILS_ENV=production rails db:seed #production can be replaced with development depending on your environment- Create the iBLIS database as specified in the
iblis_dbblock ofdatabase.yml. - Load the iBLIS dump into this database.
- Run the migration scripts to migrate data from the iBLIS database to the
mlab_apidatabase:
bash load_iblis_data.shMigration of data that remained or was done after first migration for Sites with Previous iBLIS Installation
This is the migration of data that was entered or updated in iblis after you have already migrated the data you took earlier. This migration assumes that you have not done any new orders in the new iblis(mlab). This ensures that user continue to use the previous iBLIS system while migrating data.
RAILS_ENV=production rails r iblis_migration/iblis/remaining_data/update.rb #production can be replaced with development depending on your environmentRun the following command to initialize the database:
./bin/initialize_db.sh development # development can be replace by production or test depending on the enviroment you have set you application./bin/update_metadata.sh development # development can be replace by production or test depending on the enviroment you have set you application- Edit the
mlab_api.servicefile and replace the following placeholders:WorkingDirectorywith your app directoryExecStartPrewith your app directory (listed in the file)ExecStartwith your app directory (listed in the file)Userwith your PC usernameEnvironment="RAILS_ENV=production"by replacing production with development if you are running the api in development mode
vim mlab_api.service- Copy the
mlab_api.servicefile to/etc/systemd/system:
cp mlab_api.service /etc/systemd/system- Start the mlab_api puma service:
sudo systemctl daemon-reload
sudo systemctl start mlab_api.service- Verify the status of the mlab_api puma service:
sudo systemctl status mlab_api.service- Enable the mlab_api puma service to start on system boot:
sudo systemctl enable mlab_api.service- Edit the
sidekiq.servicefile and replace the following placeholders:WorkingDirectorywith your app directoryExecStartwith your app directory (listed in the file)Userwith your PC usernameEnvironmentby replacing production with development if you are running the api in development mode
vim sidekiq.service- Copy the
sidekiq.servicefile to/etc/systemd/system:
cp sidekiq.service /etc/systemd/system- Start the Sidekiq service:
sudo systemctl daemon-reload
sudo systemctl start sidekiq.service- Verify the status of the Sidekiq service:
sudo systemctl status sidekiq.service- Enable the Sidekiq service to start on system boot:
sudo systemctl enable sidekiq.service- Check if sidekiq and redis are running properly, Navigate to: SERVER_IP_ADDRESS:API_PORT/sidekiq
- Indexing all data in elasticsearch
rails r iblis_migration/elasticsearch_index.rb- Update all later data in elasticsearch
rails r iblis_migration/elasticsearch_update.rb- Add elasticsearch update to crontab to be running every 2 minutes. But by default, updating elasticsearch is done via sidekiq. This is just to ensure that the data is updated in elasticsearch even if sidekiq is not running.
./bin/add_cronjob.shCongratulations! You have successfully installed and set up mlab_api.