run.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash
  2. exec 2>&1
  3. ROOT_UID="0"
  4. #Check if run as root
  5. if [ "$UID" -ne "$ROOT_UID" ] ; then
  6. echo "You must have 'sudo' right to do that!"
  7. exit 1
  8. fi
  9. rm -rf ./liquidsoap-compile_logs
  10. mkdir -p ./liquidsoap-compile_logs
  11. showhelp () {
  12. echo "Usage: run.sh [options] [parameters]
  13. -c all|ubuntu_lucid_32 Compile liquidsoap on all platforms or specified platform.
  14. -b all|ubuntu_lucid_32 Build shroot environments for all platforms or specified platform.
  15. -u username Local username will be used as sudo user of chroot env. Must be assigned before -b options"
  16. exit 0
  17. }
  18. build_env () {
  19. if [ $sudo_user = "-1" ];then
  20. echo "Please use -u to assign sudo username before build environments."
  21. exit 1
  22. fi
  23. echo "build_env $1"
  24. #exec > >(tee ./liquidsoap_compile_logs/build_env_$1.log)
  25. os=`echo $1 | awk '/(debian)/'`
  26. cpu=`echo $1 | awk '/(64)/'`
  27. dist=`echo $1 | awk -F "_" '{print $2}'`
  28. rm -f /etc/schroot/chroot.d/$1.conf
  29. if cat /etc/passwd | awk -F:'{print $1}' | grep "tmp" >/dev/null 2>&1;then
  30. echo "User tmp exists."
  31. else
  32. useradd tmp
  33. echo "User tmp is created."
  34. fi
  35. apt-get update
  36. apt-get --force-yes -y install debootstrap dchroot
  37. echo [$1] > /etc/schroot/chroot.d/$1.conf
  38. echo description=$1 >> /etc/schroot/chroot.d/$1.conf
  39. echo directory=/srv/chroot/$1 >> /etc/schroot/chroot.d/$1.conf
  40. echo type=directory >> /etc/schroot/chroot.d/$1.conf
  41. echo users=$sudo_user,tmp >> /etc/schroot/chroot.d/$1.conf
  42. echo root-users=$sudo_user >> /etc/schroot/chroot.d/$1.conf
  43. rm -rf /srv/chroot/$1
  44. mkdir -p /srv/chroot/$1
  45. #cp liquidsoap_compile.sh /srv/chroot/$1/
  46. if [ "$os" = "" ];then
  47. if [ "$cpu" = "" ];then
  48. echo "debootstrap --variant=buildd --arch=i386 $dist /srv/chroot/$1 http://archive.ubuntu.com/ubuntu/"
  49. debootstrap --variant=buildd --arch=i386 $dist /srv/chroot/$1 http://archive.ubuntu.com/ubuntu/
  50. else
  51. echo "debootstrap --variant=buildd --arch=amd64 $dist /srv/chroot/$1 http://archive.ubuntu.com/ubuntu/"
  52. debootstrap --variant=buildd --arch=amd64 $dist /srv/chroot/$1 http://archive.ubuntu.com/ubuntu/
  53. fi
  54. else
  55. if [ "$cpu" = "" ];then
  56. echo "debootstrap --variant=buildd --arch=i386 $dist /srv/chroot/$1 http://ftp.debian.com/debian/"
  57. debootstrap --variant=buildd --arch=i386 $dist /srv/chroot/$1 http://ftp.debian.com/debian/
  58. else
  59. echo "debootstrap --variant=buildd --arch=amd64 $dist /srv/chroot/$1 http://ftp.debian.com/debian/"
  60. debootstrap --variant=buildd --arch=amd64 $dist /srv/chroot/$1 http://ftp.debian.com/debian/
  61. fi
  62. fi
  63. }
  64. compile_liq () {
  65. echo "complie_liq $1"
  66. #exec > >(tee ./liquidsoap_compile_logs/compile_liq_$1.log)
  67. binfilename=`echo $1 | sed -e 's/ubuntu/liquidsoap/g' -e 's/debian/liquidsoap/g' -e 's/32/i386/g' -e 's/64/amd64/g'`
  68. rm -f /srv/chroot/$1/liquidsoap-compile.sh
  69. rm -f /srv/chroot/$1/liquidsoap
  70. cp liquidsoap-compile.sh /srv/chroot/$1/
  71. schroot -c $1 -u root -d / -- /liquidsoap-compile.sh
  72. cp /srv/chroot/$1/liquidsoap ./$binfilename
  73. if [ $? = 0 ];then
  74. echo "$binfilename is generated successfully"
  75. else
  76. mv ./liquidsoap-compile_logs/compile_liq_$1.log ./liquidsoap-compile_logs/fail_to_compile_liq_$1.log
  77. fi
  78. }
  79. os_versions=("ubuntu_lucid_32" "ubuntu_lucid_64" "ubuntu_precise_32" "ubuntu_precise_64" "ubuntu_quantal_32" "ubuntu_quantal_64" "ubuntu_raring_32" "ubuntu_raring_64" "debian_squeeze_32" "debian_squeeze_64" "debian_wheezy_32" "debian_wheezy_64")
  80. num=${#os_versions[@]}
  81. flag=
  82. os=
  83. sudo_user="-1"
  84. if [ x$1 = x ];then
  85. showhelp
  86. fi
  87. while getopts b:c:u: arg
  88. do
  89. case $arg in
  90. b)
  91. if [ "$OPTARG" = "all" ];then
  92. echo "Building all platforms on server..."
  93. for i in $(seq 0 $(($num -1)));
  94. do
  95. build_env ${os_versions[$i]} | tee ./liquidsoap-compile_logs/build_env_${os_versions[$i]}.log
  96. done
  97. else
  98. flag=1
  99. for i in $(seq 0 $(($num -1)));
  100. do
  101. if [ "$OPTARG" = ${os_versions[$i]} ];then
  102. echo "Building platform: $OPTARG ..."
  103. build_env ${os_versions[$i]} | tee ./liquidsoap-compile_logs/build_env_${os_versions[$i]}.log
  104. flag=0
  105. fi
  106. done
  107. if [ $flag = 1 ];then
  108. echo "Unsupported Platform from:"
  109. for j in "${os_versions[@]}"
  110. do
  111. echo $j
  112. done
  113. exit 1
  114. fi
  115. fi
  116. ;;
  117. c)
  118. if [ "$OPTARG" = "all" ];then
  119. echo "Compiling liquidsoap for all platforms on server..."
  120. for i in $(seq 0 $(($num -1)))
  121. do
  122. compile_liq ${os_versions[$i]} | tee ./liquidsoap-compile_logs/compile_liq_${os_versions[$i]}.log
  123. done
  124. else
  125. flag=1
  126. for i in $(seq 0 $(($num -1)));
  127. do
  128. if [ "$OPTARG" = ${os_versions[$i]} ];then
  129. echo "Compiling liquidsoap for platform: $OPTARG ..."
  130. compile_liq ${os_versions[$i]} | tee ./liquidsoap-compile_logs/compile_liq_${os_versions[$i]}.log
  131. flag=0
  132. fi
  133. done
  134. if [ $flag = 1 ];then
  135. echo "Unsupported Platform from:"
  136. for k in "${os_versions[@]}"
  137. do
  138. echo $k
  139. done
  140. exit 1
  141. fi
  142. fi
  143. ;;
  144. u)
  145. sudo_user="$OPTARG"
  146. echo "sudo_user is set as $sudo_user."
  147. ;;
  148. ?)
  149. showhelp
  150. ;;
  151. esac
  152. done