#!/bin/bash # # ##Instructions for using occi ##https://wiki.egi.eu/wiki/Fedcloud-tf:CLI_Environment ##https://wiki.egi.eu/wiki/HOWTO11 # openjdk needed for vomsproxyinit # export PATH=/etc/alternatives/java_sdk_openjdk/bin:${PATH} # list_chipster_instances() { for endpoint in ${clusters[*]} do echo "Listing Virtual Machines with name: chipster-vm-${user}" echo "in endpoint:" echo "" echo " $endpoint" # echo "occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms -r compute -a list" echo "" # for re in $(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms -r compute -a list | awk -F "/compute/" '{print "/compute/"$2}' ) for re in $(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms -r compute -a list 2>/dev/null ) do rline=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $re -a describe 2>/dev/null | awk '{if ( $3 == "chipster-vm-'$user'") print $0 }'| awk '{if ( $1 == "occi.compute.hostname") print "'$re' "$0 }') if [[ $rline != "" ]] then vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $re | grep occi.networkinterface.address | awk '{print $3}'| head -1) echo "$rline IP: $vmip" fi done done } check_chipster_instances() { for endpoint in ${clusters[*]} do echo "Listing Virtual Machines with name: chipster-vm-${user}" echo "in endpoint $endpoint" echo "This may take some time." echo "" for re in $(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms -r compute -a list 2>/dev/null ) do echo "" echo "-------------------------------------------------------------------------------------" echo "checking: $re" echo "chipster-vm-$user" echo "" rline=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $re -a describe 2>/dev/null | awk '{if ( $3 == "chipster-vm-'$user'") print $0 }' | awk '{if ( $1 == "occi.compute.hostname") print "'$re' "$0 }') if [[ $rline != "" ]] then vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $re | grep occi.networkinterface.address| awk '{print $3}'| head -1) echo "$rline IP: $vmip" ssh -i $keyname ubuntu@$vmip "sudo systemctl status chipster; echo Checking IP in chipster configuration; grep https /opt/chipster/fileserver/conf/chipster-config.xml; echo If the value above is not $vmip, then use FedCloud_chipster_manager to restart the Chipster server." echo "" echo "Cheking ports 8080, 8081 and 61616:" #check ports 8080 and 61616 from_port8080=$(curl ${vmip}:8080 2> /dev/null | wc -l ) if [[ $from_port8080 -eq 1 ]] then echo " server port ${vmip}:8080 OK" else echo "Check the availabality of port ${vmip}:8080 !" echo "Possible need for firewall reconfiguration." fi from_port8081=$(curl ${vmip}:8081 2> /dev/null | wc -l ) if [[ $from_port8081 -gt 10 ]] then echo " server port ${vmip}:8081 OK" else echo "Check the availabality of port ${vmip}:8080 !" echo "Possible need for firewall reconfiguration." fi from_port61616=$(curl ${vmip}:61616 2> /dev/null | wc -l ) if [[ $from_port61616 -eq 1 ]] then echo " server port ${vmip}:61616 OK" else echo "Check the availabality of port ${vmip}:61616 !" echo "Possible need for firewall reconfiguration." fi fi echo "----------------------------------------------------------------------------------" done done } restart_chipster_instance() { echo "Restarting chipster server running in instance: $re" echo "in endpoint $endpoint" echo "This may take some time." echo "" echo "-------------------------------------------------------------------------------------" echo "" rline=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $re -a describe 2>/dev/null | awk '{if ( $3 == "chipster-vm-'$user'") print "'$re' "$0 }') if [[ $rline != "" ]] then occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $re | grep occi.networkinterface.address| awk '{print $3}' > /tmp/vmip_$$_tmp nips=$(cat /tmp/vmip_$$_tmp | wc -l ) if [[ $nips -eq 1 ]] then vmip=$(head -1 /tmp/vmip_$$_tmp) echo "$rline IP: $vmip" ssh -i $keyname ubuntu@$vmip "sudo rm -rf /opt/chipster/fileserver/db-root/ChipsterFilebrokerMetadataDatabase; sudo systemctl restart chipster; sudo systemctl status chipster" fi # in case the virtual machine has both public and internal IP address if [[ $nips -eq 2 ]] then vmip=$(head -1 /tmp/vmip_$$_tmp) vmip_local=$(tail -1 /tmp/vmip_$$_tmp) echo "$rline IP: $vmip_local $vmip" ssh -i $keyname ubuntu@$vmip "sudo rm -rf /opt/chipster/fileserver/db-root/ChipsterFilebrokerMetadataDatabase;cd /opt/chipster; sudo ./configure.sh simple-configure $vmip $vmip_local; sudo systemctl restart chipster ; sudo systemctl status chispter" fi rm -f /tmp/vmip_$$_tmp fi #occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $re -a describe | awk 'BEGIN{id=0}{if ( $1 == "occi.core.id" ) id=$3 }{if ( $3 == "chipster-vm-'$user'") print "'$endpoint'/copute/"id }' } delete_chipster_instance() { echo "Delieting Virtual Machine: $1" echo "in endpoint $endpoint" volume=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $1 -a describe | grep occi.core.target | grep storage | awk '{print $3}') occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $1 -a delete echo "Do you want to remove the Volume $volume that was linked to the deleted virtual machine? "'[n/y]' read yon if [[ $yon == "y" ]] then echo "Deleting volume $volume" echo "occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $volume -a delete" occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $volume -a delete fi } ####### # add users to a running chipster server ####### add_users() { echo "Adding users to chipster server running in instance: $re" echo "in endpoint $endpoint" rline=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred --voms -r $re -a describe | awk '{if ( $3 == "chipster-vm-'$user'") print "'$re' "$0 }') if [[ $rline != "" ]] then vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $re | grep occi.networkinterface.address| awk '{print $3}'| head -1) echo "$rline IP: $vmip" echo "scp -i $keyname $userlist ubuntu@$vmip:new_users_tmp" scp -i $keyname $userlist ubuntu@$vmip:new_users_tmp ssh -i $keyname ubuntu@$vmip "cp /opt/chipster/auth/security/users ./users_tmp_$$ ; sudo cat new_users_tmp >> users_tmp_$$; rm -f new_users_$$; sudo mv -f users_tmp_$$ /opt/chipster/auth/security/users; sudo chown chipster /opt/chipster/auth/security/users; sudo chgrp chipster /opt/chipster/auth/security/users; echo Users:; cut -d : -f1,3 /opt/chipster/auth/security/users " fi } printhelp() { cat <&1 | grep -c OpenJDK) if [[ $ojdk -eq 0 ]] then echo "" echo "The java to be used is not an OpenJDK java that is required by the voms-proxy commands!" echo "" fi # Check for voms chipster_chek=$(grep chipster.csc.fi /etc/vomses | wc -l ) chipster_chek2=$(grep -c chipster.csc.fi /etc/vomses/* | wc -l ) (( c_chek = chipster_chek + chipster_chek2 )) if [[ $c_chek == "0" ]] then cat < 4 ]] then occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume echo "Could not get a volume name" echo "Command: occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume " echo "returns:" occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume exit 1 fi done ################ #make user_data file for launching Chipster in VM ################ #mounting tools directory in the case of NFS setup if [[ $tools_source == "NFS" ]] then cat > /tmp/chipster_server_startup_$$.sh <> /home/ubuntu/.ssh/authorized_keys # NFS-mount tools sudo rm -rf /mnt/tools sudo mkdir /mnt/tools cat /etc/fstab | grep -v LABEL=tools | grep -v LABEL=data > fstab.tmp sudo mv fstab.tmp /etc/fstab #echo "chipsterex.cloud.ba.infn.it:/mnt/data /mnt/data nfs rw 0 0" | sudo tee -a /etc/fstab echo "chipsterex.cloud.ba.infn.it:/mnt/tools /mnt/tools nfs ro 0 0" | sudo tee -a /etc/fstab echo "/dev/vdb /mnt/data xfs defaults,nofail 0 0" | sudo tee -a /etc/fstab sudo mount /mnt/tools sudo chown ubuntu:ubuntu /mnt/tools EOF fi # #wget https://ecsft.cern.ch/dist/cvmfs/cvmfs-config/cvmfs-config-default_latest_all.deb #wget https://ecsft.cern.ch/dist/cvmfs/cvmfs-2.1.20/cvmfs_2.1.20_amd64.deb # #mounting tools directory in the case of CVMFS setup if [[ $tools_source == "CVMFS" ]] then cat > /tmp/chipster_server_startup_$$.sh <> /home/ubuntu/.ssh/authorized_keys echo '
'  > /opt/chipster/webstart/web-root/building.html
date >> /opt/chipster/webstart/web-root/building.html
chmod go+rx /opt/chipster/webstart/web-root/building.html
pwd >> /opt/chipster/webstart/web-root/building.html
whoami >>  /opt/chipster/webstart/web-root/building.html
sudo apt-get update  >>  /opt/chipster/webstart/web-root/building.html
#CVMFS is now included in Chipster image
#echo Y | sudo apt-get install gdebi 2>&1 >> /opt/chipster/webstart/web-root/building.html
#wget https://cvmrepo.web.cern.ch/cvmrepo/deb/cvmfs-config/cvmfs-config-default_latest_all.deb 2>&1 >> /opt/chipster/webstart/web-root/building.html
#wget https://cvmrepo.web.cern.ch/cvmrepo/deb/cvmfs-2.1.20/cvmfs_2.1.20_amd64.deb 2>&1 >> /opt/chipster/webstart/web-root/building.html
#echo y | sudo gdebi cvmfs-config-default_latest_all.deb   2>&1 >> /opt/chipster/webstart/web-root/building.html
#echo y | sudo gdebi cvmfs_2.1.20_amd64.deb  2>&1 >>  /opt/chipster/webstart/web-root/building.html
sudo cvmfs_config setup  2>&1 >>  /opt/chipster/webstart/web-root/building.html
cd /etc/cvmfs
#cp default.local ~/default.local.cvmfs.tmp
echo CVMFS_HTTP_PROXY=DIRECT > /home/ubuntu/default.local.cvmfs.tmp
echo CVMFS_SERVER_URL='http://cvmfs-egi.gridpp.rl.ac.uk:8000/cvmfs/@fqrn@;http://cvmfsrepo.lcg.triumf.ca:8000/cvmfs/@fqrn@;http://cvmfsrep.grid.sinica.edu.tw:8000/cvmfs/@fqrn@' >>  /home/ubuntu/default.local.cvmfs.tmp
sudo cp  /home/ubuntu/default.local.cvmfs.tmp ./default.local
sudo systemctl stop autofs 2>&1 >>  /opt/chipster/webstart/web-root/building.html
sudo systemctl status autofs 2>&1 >> /opt/chipster/webstart/web-root/building.html
sudo systemctl start autofs 2>&1 >> /opt/chipster/webstart/web-root/building.html
ls /cvmfs/chipster.egi.eu 2>&1 >> /opt/chipster/webstart/web-root/building.html
echo "Content of tools:" 2>&1 >>  /opt/chipster/webstart/web-root/building.html
ls -l /cvmfs/chipster.egi.eu/tools 2>&1 >> /opt/chipster/webstart/web-root/building.html 
sudo rm -rf /mnt/tools  2>&1 >> /opt/chipster/webstart/web-root/building.html
sudo ln -s /cvmfs/chipster.egi.eu/tools_3.12.3 /mnt/tools  2>&1 >>  /opt/chipster/webstart/web-root/building.html 
date  >>  /opt/chipster/webstart/web-root/building.html
EOF
fi

cat >> /tmp/chipster_server_startup_$$.sh <> /tmp/chipster_server_startup_$$.sh <&1 > /home/ubuntu/startup.log 
  sudo mount /dev/vdb /mnt/data   2>&1 >>  /home/ubuntu/startup.log
  sudo mv /mnt/data_old/* /mnt/data/
  sudo rm -rf /mnt/data_old
  # use data volume for db-root to be able to preserve cloud sessions
  sudo mv /opt/chipster/fileserver/db-root /mnt/data/db-root
  sudo ln -s /mnt/data/db-root  /opt/chipster/fileserver/db-root
  sudo rm -rf /opt/chipster/fileserver/db-root/ChipsterFilebrokerMetadataDatabase
EOF

else
  cat >> /tmp/chipster_server_startup_$$.sh <&1 >>  /home/ubuntu/startup.log
  sudo rm -rf /mnt/data_old
  # use data volume for db-root to be able to preserve cloud sessions
  sudo rm -rf /opt/chipster/fileserver/db-root
  echo "Old file borker log:"   
  ls -l /mnt/data/db-root/db-root/ChipsterFilebrokerMetadataDatabase/log
  sudo ln -s /mnt/data/db-root  /opt/chipster/fileserver/db-root

EOF

fi

cat >> /tmp/chipster_server_startup_$$.sh <&1 >> /home/ubuntu/startup.log 
#sudo rm -rf /opt/chipster/fileserver/db-root/ChipsterFilebrokerMetadataDatabase  2>&1 >>  /home/ubuntu/startup.log 
sleep 5
cd /opt/chipster 
sudo bash ./configure.sh auto   2>&1 >>  /home/ubuntu/startup.log 
sudo ln -s /mnt/data data   2>&1 >> /home/ubuntu/startup.log 

EOF

if [[ $usersflag -eq 1 ]]
then
  cat >> /tmp/chipster_server_startup_$$.sh </home/ubuntu/users_tmp
  sudo cp -f /home/ubuntu/users_tmp /opt/chipster/auth/security/users
  sudo chown chipster /opt/chipster/auth/security/users
  sudo chgrp chipster /opt/chipster/auth/security/users  
EOF

fi

cat >> /tmp/chipster_server_startup_$$.sh <> /opt/chipster/webstart/web-root/building.html
sudo systemctl restart chipster 2>&1 >> /opt/chipster/webstart/web-root/building.html 
sudo systemctl restart chipster-comp 2>&1 >> /opt/chipster/webstart/web-root/building.html
n=1
while [[ \$n -le 5 ]]
do
  check=$\( sudo systemctl status chipster | grep "Active: inactive" -i -c \)
  if [[ \$check -ne 0 ]]
  then
    sleep 30 
    #sudo rm -rf /opt/chipster/fileserver/db-root/ChipsterFilebrokerMetadataDatabase
    sudo systemctl restart chipster 2>&1 >> /opt/chipster/webstart/web-root/building.html  
    sudo systemctl restart chipster-comp  2>&1 >> /opt/chipster/webstart/web-root/building.html
    let "n++"
  else
    n=6
  fi
done
echo "Startup finished" >> /opt/chipster/webstart/web-root/building.html 
date >>  /opt/chipster/webstart/web-root/building.html
sudo systemctl status chipster 2>&1 >>  /opt/chipster/webstart/web-root/building.html
echo "CHIPSTER_READY" >>  /opt/chipster/webstart/web-root/building.html
chmod go+rx /opt/chipster/webstart/web-root/building.html

EOF

userdata="file:///tmp/chipster_server_startup_$$.sh"

sleep 5
echo
echo "Launching a new virtual machine with command:"
echo "occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action create -r compute -M $chipster_img -M $flavor -t occi.core.title="chipster-vm-$user" --context user_data="$userdata" "
#vmaddress=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action create -r compute -M $chipster_img -M $flavor -t occi.core.title="chipster-vm_$user" --context user_data="$userdata" --link $volume | awk -F "/compute/" '{print "/compute/"$2}' ) 
vmaddress=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action create -r compute -M $chipster_img -M $flavor -t occi.core.title="chipster-vm-$user" --context user_data="$userdata" )
echo ""


if [[ $vmaddress == "" ]]
then
  echo "Could not launch a VM"
  echo "Trying again after 15 seconds."
  sleep 15 
  occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume
  vmaddress=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action create \
-r compute -M $chipster_img -M $flavor -t occi.core.title="chipster-vm-$user" --context user_data="$userdata" )
  if [[ $vmaddress == "" ]]
  then
      echo "Second attempt faided too. Exiting"
      occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action delete $volume 
      exit 1
  fi
fi

#Link the 
## Checking status if possible
vmstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $vmaddress | grep -c occi.networkinterface.state) 
if [[ $vmstate -eq 1  ]]
then
 while [[ $vmstate != "active" ]]
 do
   vmstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $vmaddress | grep occi.networkinterface.state | awk '{print $3}')
   echo  "Virtual machine network interface in state $vmstate"
   echo  "Waiting"
   sleep 5
 done
fi

vmstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $vmaddress | grep -c occi.compute.state)
if [[ $vmstate -eq 1  ]]
then
  while [[ $vmstate != "active" ]]
  do
     vmstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $vmaddress | grep occi.compute.state | awk '{print $3}')
     echo  "Virtual machine in state $vmstate"
     sleep 5
  done
fi  

volstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume |  grep -c occi.storage.state)
if [[ $volstate -eq 1  ]]
then
  while [[ $volstate != "online" ]]
  do 
    volstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $volume |  grep occi.storage.state | awk '{print $3}')
    echo "Volume in state $volstate"
    sleep 5
  done
fi
echo " "
echo "Linking volume: $volume"
echo "to Virtual machine: $vmaddress"


occi -e $endpoint --auth x509 --user-cred $usercred -s --voms -a link -r $vmaddress -j $volume
volstate=""
while [[ $volstate == "" ]]
do
   
   volstate=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe -r $vmaddress | grep occi.storagelink.deviceid | awk '{print $3}')
   echo "Waiting for volume to be linked $volstate"
   sleep 10
done




echo "-------------------------------------------------------------------------------------"
echo New virtual machine launched with following ID:
echo $vmaddress
echo "$vmaddress $volume $(date)" >> ~/.chipster_fedcloud_vms
echo "Linked volume:"
echo "$volume"
echo "Resource template: "$flavor

# Remove user data file
#rm -f /tmp/chipster_server_startup_$$.sh

#get the IP address

rounds=0
volume_name=("")
while [[ $vmip == "" ]]
do
 sleep 5
 vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $vmaddress | grep occi.networkinterface.address| awk '{print $3}')
 (( rounds = rounds + 1 ))
 if  [[ $rounds > 6 ]]
 then
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $vmaddress
   echo "Could not resolve an IP address for the Chipster VM:"
   echo " $vmaddress"
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action delete $volume
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action delete $vmaddress
   exit 1
 fi
done

if  [[ $link_ip == "1" ]]
then
   echo "Linking a publick IP-address"
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action link --resource $vmaddress --link /network/public
   re="$vmaddress"
   vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $vmaddress | grep occi.networkinterface.address| awk '{print $3}' | head -1 )
   #check that correct ip selected as  public IP"
   ip_check=$(ping -c 3 $vmip | grep "100% packet loss" -c)
   if [[ $ip_check -eq 1 ]]
   then
          vmip=$(tail -1 /tmp/vmip_$$_tmp)
          vmip_local=$(head -1 /tmp/vmip_$$_tmp)
   fi          
fi


########
#Wait for the server to start
########
echo "-------------------------------------------------------------------------------------"
echo "Waiting for the server to become active in $vmip"
echo "This can take several minutes (up to 30 min)"

from_port8081=(0)
wait_start=$(date +%s)
while  [[ $from_port8081 -ne 1 ]]
do
 from_port8081=$(curl ${vmip}:8081/building.html 2> /dev/null | grep "CHIPSTER_READY" | wc -l )

 sleep 15
 build_last_row=$(curl ${vmip}:8081/building.html 2> /dev/null | tail -1)
 wait_now=$(date +%s)
 (( wait_time =  wait_now - wait_start))
 printf "                                                                                                             \r"
 printf "Time waited ${wait_time}s. Status: $build_last_row \r"
 if [[ $wait_time -gt 1800 ]]
 then
   echo "After 30 min, the Chipster server is still not responding"
   echo "Deleting the Virtual machine:"
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action delete $volume
   occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action delete $vmaddress
   exit 1
 fi 
done


echo "Time waited ${wait_time}s"

#######
#restart if public IP-address was linked
#######

if  [[ $link_ip == "1" ]]
then
   echo "Restart"
   restart_chipster_instance $re
else 
   echo "No restart needed"
fi

vmip=$(occi --endpoint $endpoint --auth x509 --user-cred $usercred -s --voms --action describe --resource $vmaddress | grep occi.networkinterface.address| awk '{print $3}' | head -1 )
echo ""
echo "---------------------------------------------------------------------"
echo "Your new Chipster server is now running in a virtual machine with ID:" 
echo "  $vmaddress"
echo ""
echo "In EGI Federated Cloud endpoint: "
echo "  $endpoint"
echo " "
echo "The IP-addess of the chipster virtual server is:"
echo $vmip
echo ""
echo "You can now connect your virtual machine with command:"
echo " "
echo "  ssh -i $keyname ubuntu@$vmip"
echo ""
echo "The Chipster server can be connected with URL:"
echo ""
echo "  http://${vmip}:8081"
echo ""
exit 0