diff --git a/jobs/mongodb_node_ng/spec b/jobs/mongodb_node_ng/spec index 388a7edd55..c330a0e57c 100644 --- a/jobs/mongodb_node_ng/spec +++ b/jobs/mongodb_node_ng/spec @@ -6,8 +6,7 @@ templates: mongodb_worker_ctl: bin/mongodb_worker_ctl mongodb_migration_util.erb: bin/mongodb_migration_util - create_mongodb_tmp_dir.erb: bin/create_mongodb_tmp_dir - remove_mongodb_tmp_dir.erb: bin/remove_mongodb_tmp_dir + tmp_dir_ctl.erb: bin/tmp_dir_ctl mongodb_node.yml.erb: config/mongodb_node.yml mongodb_worker.yml.erb: config/mongodb_worker.yml mongodb_backup.yml.erb: config/mongodb_backup.yml diff --git a/jobs/mongodb_node_ng/templates/create_mongodb_tmp_dir.erb b/jobs/mongodb_node_ng/templates/create_mongodb_tmp_dir.erb deleted file mode 100644 index 66d1618949..0000000000 --- a/jobs/mongodb_node_ng/templates/create_mongodb_tmp_dir.erb +++ /dev/null @@ -1,138 +0,0 @@ -#!/bin/sh -LOG_DIR=/var/vcap/sys/log/mongodb_node - -mkdir -p $LOG_DIR -log_echo() { - echo "`date` $1" >> $LOG_DIR/mongodb_create_tmp_dir.log -} - -create_tmp_file() { - size=$1 - file_path=$2 - /usr/bin/truncate -s ${size}M ${file_path} - if [ ! $? -eq 0 ]; then - log_echo "ERROR: fail to create the loop file for mongodb tmp directory" - # delete the incomplete file - rm -rf $file_path - exit 1 - else - chmod 0700 $file_path - fi -} - -mongodb_tmp_in=/var/vcap/store -mongodb_tmp_loop_file="$mongodb_tmp_in/mongodb_tmp_loop" -mongodb_tmp_dir="/var/vcap/store/tmp_dir" -# unit of size should be MB, default size is 10GB -mongodb_tmp_size=<%= properties.mongodb_node.max_tmp || 10240 %> -# greedy_level, recreate files and directories if possible, may delete the file and direcotry with the same names or, just report -greedy_level=<%= properties.mongodb_node.tmp_greedy_level || 1 %> - - -will_create_file=0 -will_setup_loop=0 -will_create_dir=0 - -# check whether the mongodb_tmp_loop has been created, if not, try to create it - -if [ ! -f "$mongodb_tmp_loop_file" ]; then - create_tmp_file $mongodb_tmp_size $mongodb_tmp_loop_file -fi - -size=`stat -c %s $mongodb_tmp_loop_file` -expect_size=`expr $mongodb_tmp_size \* 1024 \* 1024` - -if [ ! "$size" = "$expect_size" ]; then - if [ $greedy_level -eq 0 ]; then - # graceful, just notify - log_echo "ERROR: there is a tmp loop file which has different size" - exit 1 - else - # not graceful, will delete the old file and recreate a new one if possible - will_create_file=1 - fi -fi - -ret=`losetup -a | grep "$mongodb_tmp_loop_file"` -if [ ! -z "$ret" ]; then - loop_dev=`echo "$ret" | cut -d: -f1` - if [ -d "$mongodb_tmp_dir" ]; then - check_mount=`mount -l | grep "$loop_dev" | grep "$mongodb_tmp_dir"` - if [ ! -z "$check_mount" ]; then - if [ $will_create_file -eq 0 ]; then - # usually, starting mongodb will come here - chown vcap $mongodb_tmp_dir - exit 0 - fi - else - if [ $greedy_level -eq 0]; then - log_echo "ERROR: $mongodb_tmp_dir exists but not mounted to $loop_dev" - exit 1 - else - # must recreate the directory for mounting - will_create_dir=1 - fi - fi - else - # must create a new directory for mounting - will_create_dir=1 - fi -else - loop_dev=`losetup -f` - if [ $? -ne 0 ]; then - log_echo "ERROR: fail to get one free loop device" - exit 1 - fi - will_setup_loop=1 - if [ -d "$mongodb_tmp_dir" ]; then - if [ $greedy_level -eq 0]; then - log_echo "ERROR: $mongodb_tmp_dir exists but not mounted to the new loop device: $loop_dev" - exit 1 - else - # must recreate the directory for mounting - will_create_dir=1 - fi - else - # must create a new directory for mounting - will_create_dir=1 - fi -fi - -if [ $will_create_file -eq 1 ]; then - # delete the old file - umount -d $mongodb_tmp_dir - rm -rf $mongodb_tmp_dir - losetup -d $loop_dev - rm -rf $mongodb_tmp_loop_file - - create_tmp_file $mongodb_tmp_size $mongodb_tmp_loop_file - - will_setup_loop=1 - will_create_dir=1 -fi - -if [ $will_setup_loop -eq 1 ]; then - losetup $loop_dev $mongodb_tmp_loop_file - if [ $? -ne 0 ]; then - log_echo "ERROR: fail to bind the loop device with the loop file" - exit 1 - fi - mke2fs -t ext4 $loop_dev - if [ $? -ne 0 ]; then - log_echo "ERROR: fail to make ext4 filesystem" - exit 1 - fi -fi - -if [ $will_create_dir -eq 1 ]; then - umount -d $mongodb_tmp_dir - mkdir -p $mongodb_tmp_dir - mount -t ext4 $loop_dev $mongodb_tmp_dir - if [ $? -ne 0 ]; then - log_echo "ERROR: fail to mount $loop_dev to $mongodb_tmp_dir" - exit 1 - fi -fi - -chown vcap $mongodb_tmp_dir -exit 0 diff --git a/jobs/mongodb_node_ng/templates/mongodb_node.yml.erb b/jobs/mongodb_node_ng/templates/mongodb_node.yml.erb index 954f9a33b9..2a3d792e55 100644 --- a/jobs/mongodb_node_ng/templates/mongodb_node.yml.erb +++ b/jobs/mongodb_node_ng/templates/mongodb_node.yml.erb @@ -66,6 +66,7 @@ max_nats_payload: <%= nats_props.max_payload || 1048576 %> fqdn_hosts: <%= properties.services && properties.services.fqdn_hosts || "false" %> op_time_limit: <%= node.op_time_limit || 6 %> service_start_timeout: <%= node.service_start_timeout || 4 %> +tmp_dir_script: /var/vcap/jobs/mongodb_node_ng/bin/tmp_dir_ctl m_interval: <%= node.m_interval || 10 %> <% if node.m_actions %> m_actions: diff --git a/jobs/mongodb_node_ng/templates/mongodb_node_ctl b/jobs/mongodb_node_ng/templates/mongodb_node_ctl index 389ced7b60..d7d2aa08e8 100755 --- a/jobs/mongodb_node_ng/templates/mongodb_node_ctl +++ b/jobs/mongodb_node_ng/templates/mongodb_node_ctl @@ -45,13 +45,6 @@ case $1 in chmod +x $JOB_DIR/config/mongodb_backup $JOB_DIR/config/mongodb_backup start - /var/vcap/jobs/mongodb_node_ng/bin/create_mongodb_tmp_dir - return_value=$? - if [ $return_value -ne 0 ]; then - echo "Failed to create mongodb tmp directory" - exit $return_value - fi - (crontab -l | sed /mongodb_logrotate/d; cat /var/vcap/jobs/mongodb_node_ng/config/mongodb_logrotate.cron) | sed /^$/d | crontab mkdir -p $COMMON_DIR/bin @@ -75,7 +68,6 @@ case $1 in kill_and_wait $PIDFILE 60 crontab -l | sed /mongodb_logrotate/d | crontab - /var/vcap/jobs/mongodb_node_ng/bin/remove_mongodb_tmp_dir chmod +x $JOB_DIR/config/mongodb_backup $JOB_DIR/config/mongodb_backup stop diff --git a/jobs/mongodb_node_ng/templates/remove_mongodb_tmp_dir.erb b/jobs/mongodb_node_ng/templates/remove_mongodb_tmp_dir.erb deleted file mode 100644 index 4075dd9d45..0000000000 --- a/jobs/mongodb_node_ng/templates/remove_mongodb_tmp_dir.erb +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh -LOG_DIR=/var/vcap/sys/log/mongodb_node -mkdir -p $LOG_DIR -log_echo() { - echo "`date` $1" >> $LOG_DIR/mongodb_create_tmp_dir.log -} -mongodb_tmp_dir="/var/vcap/store/tmp_dir" - -ret=$(mountpoint "$mongodb_tmp_dir") -if [ $? -eq "0" ]; then - ret=$(umount -d $mongodb_tmp_dir 2>&1) - if [ $? -eq "0" ]; then - log_echo "successfully umount $mongodb_tmp_dir" - else - log_echo "failed to umount $mongodb_tmp_dir: $ret" - fi -fi - -exit 0 diff --git a/jobs/mongodb_node_ng/templates/tmp_dir_ctl.erb b/jobs/mongodb_node_ng/templates/tmp_dir_ctl.erb new file mode 100755 index 0000000000..eeacefce6b --- /dev/null +++ b/jobs/mongodb_node_ng/templates/tmp_dir_ctl.erb @@ -0,0 +1,160 @@ +#!/bin/bash + +LOG_DIR=/var/vcap/sys/log/mongodb_node + +mkdir -p $LOG_DIR +log_echo() { + echo "`date` $1" >> $LOG_DIR/tmp_dir_ctl.log +} + +create_tmp_file() { + size=$1 + file_path=$2 + /usr/bin/truncate -s ${size}M ${file_path} + if [ ! $? -eq 0 ]; then + log_echo "ERROR: fail to create the loop file for mongodb tmp directory" + # delete the incomplete file + rm -rf $file_path + exit 1 + else + chmod 0700 $file_path + fi +} + +mongodb_tmp_in=/var/vcap/store +mongodb_tmp_loop_file="$mongodb_tmp_in/mongodb_tmp_loop" +mongodb_tmp_dir="/var/vcap/store/tmp_dir" +# unit of size should be MB, default size is 10GB +mongodb_tmp_size=<%= properties.mongodb_node.max_tmp || 10240 %> +# greedy_level, recreate files and directories if possible, may delete the file and direcotry with the same names or, just report +greedy_level=<%= properties.mongodb_node.tmp_greedy_level || 1 %> + + + +case $1 in + create) + will_create_file=0 + will_setup_loop=0 + will_create_dir=0 + # check whether the mongodb_tmp_loop has been created, if not, try to create it + + if [ ! -f "$mongodb_tmp_loop_file" ]; then + create_tmp_file $mongodb_tmp_size $mongodb_tmp_loop_file + fi + + size=`stat -c %s $mongodb_tmp_loop_file` + expect_size=`expr $mongodb_tmp_size \* 1024 \* 1024` + + if [ ! "$size" = "$expect_size" ]; then + if [ $greedy_level -eq 0 ]; then + # graceful, just notify + log_echo "ERROR: there is a tmp loop file which has different size" + exit 1 + else + # not graceful, will delete the old file and recreate a new one if possible + will_create_file=1 + fi + fi + + ret=`losetup -a | grep "$mongodb_tmp_loop_file"` + if [ ! -z "$ret" ]; then + loop_dev=`echo "$ret" | cut -d: -f1` + if [ -d "$mongodb_tmp_dir" ]; then + check_mount=`mount -l | grep "$loop_dev" | grep "$mongodb_tmp_dir"` + if [ ! -z "$check_mount" ]; then + if [ $will_create_file -eq 0 ]; then + # usually, starting mongodb will come here + chown vcap $mongodb_tmp_dir + exit 0 + fi + else + if [ $greedy_level -eq 0]; then + log_echo "ERROR: $mongodb_tmp_dir exists but not mounted to $loop_dev" + exit 1 + else + # must recreate the directory for mounting + will_create_dir=1 + fi + fi + else + # must create a new directory for mounting + will_create_dir=1 + fi + else + loop_dev=`losetup -f` + if [ $? -ne 0 ]; then + log_echo "ERROR: fail to get one free loop device" + exit 1 + fi + will_setup_loop=1 + if [ -d "$mongodb_tmp_dir" ]; then + if [ $greedy_level -eq 0]; then + log_echo "ERROR: $mongodb_tmp_dir exists but not mounted to the new loop device: $loop_dev" + exit 1 + else + # must recreate the directory for mounting + will_create_dir=1 + fi + else + # must create a new directory for mounting + will_create_dir=1 + fi + fi + + if [ $will_create_file -eq 1 ]; then + # delete the old file + umount -d $mongodb_tmp_dir + rm -rf $mongodb_tmp_dir + losetup -d $loop_dev + rm -rf $mongodb_tmp_loop_file + + create_tmp_file $mongodb_tmp_size $mongodb_tmp_loop_file + + will_setup_loop=1 + will_create_dir=1 + fi + + if [ $will_setup_loop -eq 1 ]; then + losetup $loop_dev $mongodb_tmp_loop_file + if [ $? -ne 0 ]; then + log_echo "ERROR: fail to bind the loop device with the loop file" + exit 1 + fi + mke2fs -t ext4 $loop_dev + if [ $? -ne 0 ]; then + log_echo "ERROR: fail to make ext4 filesystem" + exit 1 + fi + fi + + if [ $will_create_dir -eq 1 ]; then + umount -d $mongodb_tmp_dir + mkdir -p $mongodb_tmp_dir + mount -t ext4 $loop_dev $mongodb_tmp_dir + if [ $? -ne 0 ]; then + log_echo "ERROR: fail to mount $loop_dev to $mongodb_tmp_dir" + exit 1 + fi + fi + + chown $SUDO_USER $mongodb_tmp_dir + ;; + + remove) + ret=$(mountpoint "$mongodb_tmp_dir") + if [ $? -eq "0" ]; then + ret=$(umount -d $mongodb_tmp_dir 2>&1) + if [ $? -eq "0" ]; then + log_echo "successfully umount $mongodb_tmp_dir" + else + log_echo "failed to umount $mongodb_tmp_dir: $ret" + fi + fi + + ;; + + *) + echo "Usage: $0 {create|remove}" + + ;; +esac diff --git a/src/services b/src/services index a021c810d4..240d0570b5 160000 --- a/src/services +++ b/src/services @@ -1 +1 @@ -Subproject commit a021c810d497e86bfecb0089d4b0ba438ead5c0a +Subproject commit 240d0570b5b64bc6e061515c2057d3c0a2a8ea0c