Tuesday, May 31, 2016

WSO2 Depsync with rsync and incron

Setup password less authentication for ubuntu
1. create a private/pub key in master server (use empty passprase)
ssh-keygen

2. Copy the new key to your worker server:
ssh-copy-id worker_username@worker_host

Setup rsync and incron
1. install rsync
sudo apt-get install rsync

2. install incron
sudo apt-get install incron

3. Allow incron
vim /etc/incron.allow
add rootuser username in this file, save and close

4. Create a push_artifacts.sh file with following content in  APIM-HOME directory

#!/bin/bash
# push_artifacts.sh - Push artifact changes to the worker nodes.
master_artifact_path=/apps/wso2/wso2am-1.10.0/repository/deployment/server
worker_artifact_path=/apps/wso2/wso2am-1.10.0/repository/deployment/
worker_nodes=(worker1 worker2 worker3)
while [ -f /tmp/.rsync.lock ]
do
  echo -e ";[WARNING] Another rsync is in progress, waiting...";
  sleep 2
done
mkdir /tmp/.rsync.lock
if [ $? = "1" ]; then
echo ";[ERROR] : can not create rsync lock";
exit 1
else
echo ";INFO : created rsync lock";
fi
for i in ${worker_nodes[@]}; do
echo ";===== Beginning artifact sync for $i =====";
rsync -avzx --delete -e ssh $master_artifact_path rootuser@$i:$worker_artifact_path
if [ $? = "1" ]; then
echo ";[ERROR] : rsync failed for $i";
exit 1
fi
echo ";===== Completed rsync for $i =====";
done
rm -rf /tmp/.rsync.lock
echo ";[SUCCESS] : Artifact synchronization completed successfully";

6. Give it executable rights
chmod +x push_artifacts.sh

7. Execute below command to configure icron.
incrontab -e

8. Add the below text in the prompt opened by above step.
/apps/wso2/wso2am-1.10.0/repository/deployment/server IN_MODIFY,IN_CREATE,IN_DELETE /apps/wso2/wso2am-1.10.0/push_artifacts.sh

9. Test
Add a file in master node /apps/wso2/wso2am-1.10.0/repository/deployment/server  and check in worker node, that same file has been copied there

10. Further you can check the log to see the incron logs 
tail /var/log/syslog