Hi All,
During my work at Adroitlogic, in one of my tasks I had to start & configure a set of EC2 machines which included our B2B AS2 integration solution AS2Station. This was a recurring task and yes you guessed right it was boring. The configuration steps in summary were follows.
- Extracting the private IP of eth0 interface
- Adding few iptable rules with the extracted private IP
- Mounting a ramdisk (which is used by the UltraESB filecache)
- Starting Apache Zookeeper service (which is the leadership election and coordination service used by UltraESB)
- Starting UltraESB service
- Starting AppServer service (which is AS2Station front-end)
My good friend Janaka, suggested me that I should try to use an startup script to get this done. Turns out it was pretty easy. Let me share the steps with you all as it might be useful to some of you as well.
Creating the script
1. First we need to create the script. I created mine by the name setup-script.sh in /var/tmp/ directory.
#!/bin/bash echo -n "" > /var/tmp/setup-script.out echo "The time the script run was --> `date`" > /var/tmp/setup-script.out echo 'Extracting private ip' >> /var/tmp/setup-script.out PRIVATE_IP=$(curl http://169.254.169.254/latest/meta-data/local-ipv4) echo $PRIVATE_IP >> /var/tmp/setup-script.out echo 'Applying iptable rules' >> /var/tmp/setup-script.out iptables -A FORWARD -d $PRIVATE_IP -p tcp --dport 443 -j ACCEPT >> /var/tmp/setup-script.out iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination $PRIVATE_IP:8443 >> /var/tmp/setup-script.out iptables -t nat -A POSTROUTING -j MASQUERADE >> /var/tmp/setup-script.out echo 'Appled following iptable rules' >> /var/tmp/setup-script.out iptables -S >> /var/tmp/setup-script.out echo 'Appled NAT following iptable rules' >> /var/tmp/setup-script.out iptables -S -t nat >> /var/tmp/setup-script.out echo 'Mounting ram disk' >> /var/tmp/setup-script.out umount /tmp/ram >> /var/tmp/setup-script.out mount -t tmpfs -o size=512m tmpfs /tmp/ram >> /var/tmp/setup-script.out #echo 'Switching to adrt user' >> /var/tmp/setup-script.out sudo su - adrt >> /var/tmp/setup-script.out service zookeeper start >> /var/tmp/setup-script.out echo 'Sleeping 1m after zookeeper start command' >> /var/tmp/setup-script.out sleep 1m service ultraesb stop >> /var/tmp/setup-script.out service appserver stop >> /var/tmp/setup-script.out echo 'Sleeping 1m after ultraesb and appserver stop commands' >> /var/tmp/setup-script.out sleep 1m service ultraesb start >> /var/tmp/setup-script.out echo 'Sleeping 1m after ultraesb start command' >> /var/tmp/setup-script.out sleep 1m service appserver start >> /var/tmp/setup-script.out echo 'Sleeping 1m after appserver start command' >> /var/tmp/setup-script.out sleep 1m service zookeeper status >> /var/tmp/setup-script.out service ultraesb status >> /var/tmp/setup-script.out service appserver status >> /var/tmp/setup-script.out echo 'All should be good to go' >> /var/tmp/setup-script.out
2. Then we need to give executable permissions for the script file.
chmod +x /var/tmp/setup-script.sh
Creating new systemd service unit
Now we have to create service file. I created a file by the name setup.service in /etc/systemd/system/ directory.
[Unit] Description=This is a setup script After=network.target After=systemd-user-sessions.service After=network-online.target
[Service] Type=simple ExecStart=/var/tmp/setup-script.sh TimeoutStartSec=0 [Install] WantedBy=default.target
In this file,
After= : If the script needs any other system facilities (networking, etc), modify the [Unit] section to include appropriate After=, Wants=, or Requires= directives. Type= : Switch Type=simple for Type=idle in the [Service] section to delay execution of the script until all other jobs are dispatched WantedBy= : target to run the sample script in
Enable the systemd service unit
1. Reload the systemd process to consider newly created sample.service OR every time when sample.service gets modified.
# systemctl daemon-reload
2. Enable this service to start after reboot automatically.
# systemctl enable setup.service
3. Start the service.
# systemctl start setup.service
4. Reboot the host to verify whether the scripts are starting as expected during system boot.
# systemctl reboot