Advertisement
Guest User

cranky-0.0.32

a guest
Jun 15th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.08 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function send_to_log {
  4.     echo $(date +"%R:%S %Z %:z %Y-%m-%d:") "$(basename $0): [$1] $2" >&2
  5. }
  6.  
  7. function log_error {
  8.     send_to_log "ERROR" "$1"
  9. }
  10.  
  11. function log_warning {
  12.     send_to_log "WARNING" "$1"
  13. }
  14.  
  15. function log_info {
  16.     send_to_log "INFO" "$1"
  17. }
  18.  
  19. Theory=("sft.cern.ch" "grid.cern.ch" "cernvm-prod.cern.ch" "alice.cern.ch")
  20.  
  21. function get_boinc_info {
  22.     if [ ! -f init_data.xml ] ; then
  23.     log_error "'init_data.xml' is mising."
  24.         return 1
  25.     fi
  26.     attribute=$(grep "^<$1>" init_data.xml | cut -d '>' -f2 | cut -d '<' -f1)
  27.     echo ${attribute}
  28. }
  29.  
  30. function check_cvmfs {
  31.     log_info "Checking CVMFS."
  32.     cvmfs_config=$(which cvmfs_config 2>/dev/null)
  33.     if [ $? -gt 0 ] ; then
  34.         log_error "'which' could not locate the command 'cvmfs_config'."
  35.         return 1
  36.     fi
  37.  
  38.     if [ ! -d /cvmfs ] ; then
  39.         log_error "CVMFS mount point /cvmfs not found."
  40.         return 1
  41.     fi
  42.  
  43.     repos=$app[@]
  44.     for repo in ${!repos}; do
  45.     for command in probe stat ; do
  46.             cvmfs_config ${command} ${repo}
  47.         if [ $? -gt 0 ] ; then
  48.                 log_error "'cvmfs_config ${command} ${repo}' failed."
  49.                 return 1
  50.         fi
  51.     done
  52.     done
  53. }
  54.  
  55. function check_runc {
  56.     log_info "Checking runc."
  57.     runc="/cvmfs/grid.cern.ch/vc/containers/runc"
  58.     if [ ! -e ${runc} ] ; then
  59.         log_error "/cvmfs/grid.cern.ch/vc/containers/runc does not exist."
  60.         return 1
  61.     fi
  62.     ${runc} -v > /dev/null
  63.     if [ $? -gt 0 ] ; then
  64.         log_error "'runc -v' failed."
  65.         return 1
  66.     fi
  67.     if [ -e /proc/sys/user/max_user_namespaces ] ; then
  68.         value=$(cat /proc/sys/user/max_user_namespaces)
  69.         if [ ${value} -eq 0 ] ; then
  70.             log_error "max_user_namespaces in /proc/sys/user/ is set to 0."
  71.             return 1
  72.     fi
  73.     fi
  74.     return
  75. }
  76.  
  77. function create_filesystem {
  78.     log_info "Creating the filesystem."
  79.     if [ -e cernvm ] ; then
  80.         rm -r cernvm
  81.     fi
  82.     mkdir -p cernvm/rootfs
  83.     root="/cvmfs/cernvm-prod.cern.ch/cvm3"    
  84.     log_info "Using ${root}"
  85.     paths=('/srv' '/tmp' '/etc' '/usr' '/usr/local' '/var' '/var/cache' '/var/cvs' '/var/db' '/var/empty'
  86.            '/var/lib' '/var/local' '/var/lock' '/var/log' '/var/run' '/var/tmp' '/root' '/home' '/var/spool')
  87.     for path in ${paths[*]} ; do
  88.         mkdir cernvm/rootfs${path}
  89.     done
  90.  
  91.     paths=('/bin' '/etc' '/lib' '/lib64' '/opt' '/sbin' '/usr/bin' '/usr/doc' '/usr/etc' '/usr/include'
  92.        '/usr/lib' '/usr/lib64' '/usr/libexec' '/usr/sbin' '/usr/share' '/usr/src' '/usr/vice')
  93.     for path in ${paths[*]} ; do
  94.         ln -sf ${root}${path} cernvm/rootfs${path}
  95.     done
  96.     cp /cvmfs/grid.cern.ch/vc/containers/cernvm/config.json cernvm/config.json
  97.     mkdir -p cernvm/shared/html/job
  98.  
  99. }
  100.  
  101. function update_config {
  102.     log_info "Updating config.json."
  103.  
  104.     sed -i "s/\"hostID\": 122/\"hostID\": $(id -u)/" cernvm/config.json
  105.     sed -i "s/\"hostID\": 129/\"hostID\": $(id -g)/" cernvm/config.json
  106.     slot=$(basename $(pwd))
  107.     if [ -d /sys/fs/cgroup/freezer/boinc/${slot} ]; then
  108.     sed -i "s/\"linux\": {/\"linux\": {\n \t\"cgroupsPath\": \"\/boinc\/${slot}\",/" cernvm/config.json
  109.     fi
  110. }
  111. function prepare_input {
  112.     cp init_data.xml cernvm/shared/init_data.xml
  113.     mkdir cernvm/shared/bin
  114.     cp /cvmfs/grid.cern.ch/vc/containers/cernvm/copilot-config cernvm/shared/bin/copilot-config
  115.     chmod 755 cernvm/shared/bin/copilot-config
  116.     cp input cernvm/shared/job
  117.     chmod 755 cernvm/shared/job
  118.     sed -i 's/tar xzm/tar xzmo/' cernvm/shared/job
  119.     if [ $? -eq 1 ] ; then
  120.         log_error "'sed of input to add no-save-owner."
  121.         return 1
  122.     fi
  123. }
  124.  
  125. function prepare_output {
  126.     log_info "Preparing output."
  127.     if [ -f cernvm/shared/runRivet.log ]; then
  128.     mkdir -p shared
  129.     tar -zcf shared/output.tgz  --exclude bin --exclude runPost.sh  --exclude html --exclude init_data.xml -C cernvm/shared .
  130.     ln -sf shared/output.tgz output.tgz  # To be compatible with the VBox App\
  131.     return 0
  132.     else
  133.         log_error "No output found."
  134.     return 1
  135.     fi
  136. }
  137.  
  138. function pause {
  139.     result_id=$(get_boinc_info result_name)
  140.     log_info "Pausing container ${result_id}."
  141.     if [ -f /sys/fs/cgroup/freezer/boinc/freezer.state ]; then
  142.     /cvmfs/grid.cern.ch/vc/containers/runc -root cernvm/state pause ${result_id}
  143.     else
  144.     log_warning "Cannot pause container as /sys/fs/cgroup/freezer/boinc/freezer.state not exists."
  145.     fi
  146. }
  147.  
  148. function resume {
  149.     result_id=$(get_boinc_info result_name)
  150.     log_info "Resuming container ${result_id}."
  151.     /cvmfs/grid.cern.ch/vc/containers/runc --root cernvm/state resume ${result_id}
  152. }
  153.  
  154. function create_cgroup() {
  155.     if [ ! -d /sys/fs/cgroup/freezer/boinc ]; then
  156.     return 1
  157.     fi
  158.     slot=$(basename $(pwd))
  159.     log_info "Creating cgroup for slot ${slot}"
  160.     CGROUPS=( freezer cpuset devices memory "cpu,cpuacct" pids blkio hugetlb net_cls net_prio perf_event freezer )
  161.     CGROUP_MOUNT="/sys/fs/cgroup"
  162.     CGROUP_PATH="boinc/${slot}"
  163.     for cg in "${CGROUPS[@]}"
  164.     do
  165.     mkdir -p "$CGROUP_MOUNT/$cg/$CGROUP_PATH"
  166.     done
  167.     }
  168.  
  169. function run {
  170.     log_info "Running Container 'runc'."
  171.     runc="/cvmfs/grid.cern.ch/vc/containers/runc"
  172.     result_id=$(get_boinc_info result_name)
  173.     trap pause SIGTSTP
  174.     trap resume SIGCONT
  175.     read_runrivet_log $$ &
  176.     ${runc} --root state run -b cernvm ${result_id} &
  177.     child=$!
  178.     while true
  179.     do
  180.     wait ${child}
  181.     status=$?
  182.     if [ ! ${status} -eq 146 ] && [ ! ${status} -eq 148 ]; then
  183.         log_info "Container 'runc' finished with status code ${status}."
  184.         return
  185.     fi
  186.     done
  187. }
  188.  
  189. function read_runrivet_log {
  190.     while [ ! -e cernvm/shared/runRivet.log ] ; do sleep 2; done
  191.     local logline1="$(head -n 1 cernvm/shared/runRivet.log 2>/dev/null)"
  192.     log_info "${logline1}"
  193. }
  194.  
  195.  
  196. function fail {
  197.     exit 206
  198. }
  199.  
  200. app=$(get_boinc_info app_name)
  201.  if [ $? -gt 0 ] ; then
  202.      fail
  203.  fi
  204. log_info "Detected ${app} App"
  205. check_cvmfs ${app} || fail
  206. check_runc || fail
  207. create_filesystem
  208. create_cgroup
  209. update_config
  210. prepare_input
  211. run
  212. prepare_output || fail
  213. rm -rf cernvm # Clean up
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement