Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Merge "create/remove tmp_dir in monogdb-node code"
Browse files Browse the repository at this point in the history
  • Loading branch information
kushmerick authored and Gerrit Code Review committed Jan 24, 2013
2 parents 7c934e5 + 6c62686 commit ea15b2e
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 168 deletions.
3 changes: 1 addition & 2 deletions jobs/mongodb_node_ng/spec
Expand Up @@ -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
Expand Down
138 changes: 0 additions & 138 deletions jobs/mongodb_node_ng/templates/create_mongodb_tmp_dir.erb

This file was deleted.

1 change: 1 addition & 0 deletions jobs/mongodb_node_ng/templates/mongodb_node.yml.erb
Expand Up @@ -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:
Expand Down
8 changes: 0 additions & 8 deletions jobs/mongodb_node_ng/templates/mongodb_node_ctl
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 0 additions & 19 deletions jobs/mongodb_node_ng/templates/remove_mongodb_tmp_dir.erb

This file was deleted.

160 changes: 160 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion src/services

0 comments on commit ea15b2e

Please sign in to comment.