Permalink
Browse files

Merge "add mongodb 2.0 in vcap-services"

  • Loading branch information...
2 parents a4bc327 + 8570bea commit 44fa66848e01f20686b55a33251eed402dfae36e Chunjie committed with Gerrit Code Review Oct 8, 2012
@@ -16,6 +16,22 @@ class VCAP::Services::MongoDB::Backup < VCAP::Services::Base::Backup
File.join(File.dirname(__FILE__), '..', 'config', 'mongodb_backup.yml')
end
+ def parse_property(hash, key, type, options = {})
+ obj = hash[key]
+ if obj.nil?
+ raise "Missing required option: #{key}" unless options[:optional]
+ options[:default]
+ elsif type == Range
+ raise "Invalid Range object: #{obj}" unless obj.kind_of?(Hash)
+ first, last = obj["first"], obj["last"]
+ raise "Invalid Range object: #{obj}" unless first.kind_of?(Integer) and last.kind_of?(Integer)
+ Range.new(first, last)
+ else
+ raise "Invalid #{type} object: #{obj}" unless obj.kind_of?(type)
+ obj
+ end
+ end
+
def backup_db
# make sure local_db exists
unless File.exist? @config['local_db'].split(':')[1]
@@ -24,9 +40,21 @@ class VCAP::Services::MongoDB::Backup < VCAP::Services::Base::Backup
end
cmd_timeout = @config['timeout'].to_f
- config = VCAP.symbolize_keys(@config)
- Node::ProvisionedService.init(config)
+ options = {
+ :service_name => parse_property(@config, "service_name", String, :optional => true, :default => "mongodb"),
+ :backup_base_dir => parse_property(@config, "backup_base_dir", String),
+ :local_db => parse_property(@config, "local_db", String),
+ :base_dir => parse_property(@config, "base_dir", String),
+ :mongodump_path => parse_property(@config, "mongodump_path", Hash),
+ :supported_versions => parse_property(@config, "supported_versions", Array),
+ :default_version => parse_property(@config, "default_version", String),
+ :mongod_log_dir => parse_property(@config, "mongod_log_dir", String),
+ :image_dir => parse_property(@config, "image_dir", String),
+ :timeout => parse_property(@config, "timeout", Integer, :optional => true, :default => 10)
+ }
+
+ Node::ProvisionedService.init(options)
Node::ProvisionedService.all.each do |p_service|
path = get_dump_path(p_service.name)
@@ -2,11 +2,14 @@ service_name: mongodb
pid: /var/vcap/sys/run/mongodb_backup.pid
backup_base_dir: /mnt/nfs/servicebackup
local_db: sqlite3:/var/vcap/services/mongodb/mongodb_node.db
-service_base_dir: /var/vcap/services/mongodb/instances/
+base_dir: /var/vcap/services/mongodb/instances/
+image_dir: /var/vcap/services/mongodb/images/
mongod_log_dir: /var/vcap/sys/log/mongodb
mongodump_path:
- "1.8": "mongodump"
-
-timeout: 600
+ "1.8": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-1.8/mongodump"
+ "2.0": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-2.0/mongodump"
+supported_versions: ["1.8", "2.0"]
+default_version: "2.0"
+timeout: 10
logging:
level: debug
@@ -12,16 +12,24 @@ mongod_log_dir: /var/vcap/sys/log/mongodb
pid: /var/vcap/sys/run/mongodb_node.pid
node_id: mongodb_node_free_1
-supported_versions: ["1.8"]
-default_version: "1.8"
+supported_versions: ["1.8", "2.0"]
+default_version: "2.0"
mongod_path:
- "1.8": "mongod"
+ "1.8": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-1.8/mongod"
+ "2.0": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-2.0/mongod"
+
mongorestore_path:
- "1.8": "mongorestore"
+ "1.8": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-1.8/mongorestore"
+ "2.0": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-2.0/mongorestore"
+
+mongodump_path:
+ "1.8": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-1.8/mongodump"
+ "2.0": "/tmp/warden/rootfs/usr/share/mongodb/mongodb-2.0/mongodump"
mongod_options:
"1.8": ""
+ "2.0": ""
port_range:
first: 25001
@@ -364,9 +364,11 @@ def init(args)
@max_disk = args[:max_disk] ? args[:max_disk] : 128
@quota = args[:filesystem_quota] || false
@@mongod_path = args[:mongod_path] ? args[:mongod_path] : { args[:default_version] => 'mongod' }
+ @@mongod_options = args[:mongod_options] ? args[:mongod_options] : { args[:default_version] => '' }
@@mongorestore_path = args[:mongorestore_path] ? args[:mongorestore_path] : { args[:default_version] => 'mongorestore' }
@@mongodump_path = args[:mongodump_path] ? args[:mongodump_path] : { args[:default_version] => 'mongodump' }
@@tar_path = args[:tar_path] ? args[:tar_path] : 'tar'
+ FileUtils.mkdir_p(File.dirname(args[:local_db].split(':')[1]))
DataMapper.setup(:default, args[:local_db])
DataMapper::auto_upgrade!
FileUtils.mkdir_p(base_dir)
@@ -479,12 +481,17 @@ def run
# Container may not have enough space to satisfy the need.
# - when do repair, more mem required (had experience a situation
# where "mongod --repair" hang with mem quota, and it resume when quota increase)
+ # - no repair if journal is enabled
# So to avoid these situation, and make things smooth, do it outside container.
lockfile = File.join(data_dir, "mongod.lock")
if File.size?(lockfile)
- logger.warn("Service #{self[:name]} not properly shutdown, try repairing its db...")
- FileUtils.rm_f(lockfile)
- repair
+ journal_enabled = mongod_exe_options.match(/--journal/) if version == "1.8"
+ journal_enabled = !mongod_exe_options.match(/--nojournal/) if version == "2.0"
+ unless journal_enabled
+ logger.warn("Service #{self[:name]} not properly shutdown, try repairing its db...")
+ FileUtils.rm_f(lockfile)
+ repair
+ end
end
super
end
@@ -494,7 +501,7 @@ def service_port
end
def service_script
- "mongod_startup.sh"
+ "mongod_startup.sh #{version} #{mongod_exe_options}"
end
# diretory helper
@@ -507,7 +514,7 @@ def add_admin(username, password)
warden = self.class.warden_connect
req = Warden::Protocol::RunRequest.new
req.handle = self[:container]
- req.script = "mongo localhost:27017/admin --eval 'db.addUser(\"#{username}\", \"#{password}\")'"
+ req.script = "#{mongo} localhost:27017/admin --eval 'db.addUser(\"#{username}\", \"#{password}\")'"
rsp = warden.call(req)
warden.disconnect
rescue => e
@@ -580,14 +587,22 @@ def get_healthz
end
def mongod
- @@mongod_path[version] || @@mongod_path[version.to_sym]
+ @@mongod_path[version]
+ end
+
+ def mongo
+ "/usr/share/mongodb/mongodb-#{version}/mongo"
+ end
+
+ def mongod_exe_options
+ @@mongod_options[version]
end
def mongorestore
- @@mongorestore_path[version] || @@mongorestore_path[version.to_sym]
+ @@mongorestore_path[version]
end
def mongodump
- @@mongodump_path[version] || @@mongodump_path[version.to_sym]
+ @@mongodump_path[version]
end
end
@@ -3,9 +3,10 @@ backup_base_dir: <%= BACKUP_DIR %>
local_db: <%= DB_URL %>
base_dir: /tmp/mongo/instances
mongodump_path:
- "1.8": <%= BINARY_DIR %>mongodump
-supported_versions: ["1.8"]
-default_version: "1.8"
+ "1.8": <%= BINARY_DIR %>/mongodump
+ "2.0": <%= BINARY_DIR %>/mongodump
+supported_versions: ["1.8", "2.0"]
+default_version: "2.0"
mongod_log_dir: <%= MONGOD_LOG %>
image_dir: /tmp/mongo/images
timeout: 10
@@ -128,6 +128,7 @@ def get_node_config()
:mongod_path => parse_property(config, "mongod_path", Hash),
:mongod_options => parse_property(config, "mongod_options", Hash),
:mongorestore_path => parse_property(config, "mongorestore_path", Hash),
+ :mongodump_path => parse_property(config, "mongodump_path", Hash),
:ip_route => parse_property(config, "ip_route", String, :optional => true),
:node_id => parse_property(config, "node_id", String),
:mbus => parse_property(config, "mbus", String),

0 comments on commit 44fa668

Please sign in to comment.