# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 28 def self.instance StickShift::MongoDataStore.new end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 12 def initialize(access_info = nil) if access_info != nil # no-op elsif defined? Rails access_info = Rails.application.config.datastore else raise Exception.new("Mongo DataStore service is not inilialized") end @replica_set = access_info[:replica_set] @host_port = access_info[:host_port] @user = access_info[:user] @password = access_info[:password] @db = access_info[:db] @collections = access_info[:collections] end
Ensure retry upon connection failure
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 213 def self.rescue_con_failure(max_retries=MAX_CON_RETRIES, retry_wait_tm=CON_RETRY_WAIT_TM) retries = 0 begin yield rescue Mongo::ConnectionFailure => ex retries += 1 raise ex if retries > max_retries sleep(retry_wait_tm) retry end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 298 def activate_district_node(uuid, server_identity) Rails.logger.debug "activate_district_node(#{uuid},#{server_identity})\n\n" update_district({"_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => false}}}, {"$set" => { "server_identities.$.active" => true}, "$inc" => { "active_server_identities_size" => 1 }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 280 def add_district_node(uuid, server_identity) Rails.logger.debug "add_district_node(#{uuid},#{server_identity})\n\n" update_district({"_id" => uuid, "server_identities.name" => { "$ne" => server_identity }}, {"$push" => { "server_identities" => {"name" => server_identity, "active" => true}}, "$inc" => { "active_server_identities_size" => 1 }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 303 def add_district_uids(uuid, uids) Rails.logger.debug "add_district_capacity(#{uuid},#{uids})\n\n" update_district({"_id" => uuid}, {"$pushAll" => { "available_uids" => uids }, "$inc" => { "available_capacity" => uids.length, "max_uid" => uids.length, "max_capacity" => uids.length }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 131 def create(obj_type, user_id, id, obj_attrs) Rails.logger.debug "MongoDataStore.create(#{obj_type}, #{user_id}, #{id}, #hidden)\n\n" case obj_type when "CloudUser" add_user(user_id, obj_attrs) when "Application" add_app(user_id, id, obj_attrs) when "Domain" add_domain(user_id, id, obj_attrs) when "ApplicationTemplate" save_application_template(id, obj_attrs) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 293 def deactivate_district_node(uuid, server_identity) Rails.logger.debug "deactivate_district_node(#{uuid},#{server_identity})\n\n" update_district({"_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => true}}}, {"$set" => { "server_identities.$.active" => false}, "$inc" => { "active_server_identities_size" => -1 }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 145 def delete(obj_type, user_id, id=nil) Rails.logger.debug "MongoDataStore.delete(#{obj_type}, #{user_id}, #{id})\n\n" case obj_type when "CloudUser" delete_user(user_id) when "Application" delete_app(user_id, id) when "Domain" delete_domain(user_id, id) when "ApplicationTemplate" delete_application_template(id) when "UsageRecord" delete_usage_record(user_id, id) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 261 def delete_district(uuid) Rails.logger.debug "delete_district(#{uuid})\n\n" remove_district({ "_id" => uuid, "active_server_identities_size" => 0 }) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 161 def delete_usage_record_by_gear_uuid(user_id, gear_uuid, usage_type) Rails.logger.debug "MongoDataStore.delete_usage_record_by_gear_uuid(#{user_id}, #{gear_uuid}, #{usage_type})\n\n" update( user_collection, { "_id" => user_id }, { "$pull" => { "usage_records" => {"gear_uuid" => gear_uuid, "usage_type" => usage_type}}} ) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 32 def find(obj_type, user_id, id) Rails.logger.debug "MongoDataStore.find(#{obj_type}, #{user_id}, #{id})\n\n" case obj_type when "CloudUser" get_user(user_id) when "Application" get_app(user_id, id) when "Domain" get_domain(user_id, id) when "ApplicationTemplate" find_application_template(id) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 46 def find_all(obj_type, user_id=nil, opts=nil, &block) Rails.logger.debug "MongoDataStore.find_all(#{obj_type}, #{user_id}, #{opts})\n\n" case obj_type when "CloudUser" get_users(opts, &block) when "Application" get_apps(user_id, &block) when "Domain" get_domains(user_id, &block) when "ApplicationTemplate" if opts.nil? || opts.empty? find_all_application_templates(&block) else find_application_template_by_tag(opts[:tag], &block) end end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 237 def find_all_districts() Rails.logger.debug "find_all_districts()\n\n" MongoDataStore.rescue_con_failure do mcursor = district_collection.find() cursor_to_district_hash(mcursor) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 64 def find_all_logins(opts) Rails.logger.debug "MongoDataStore.find_all_logins()\n\n" query = {} if opts if opts[:with_gears] query["apps.group_instances.gears.0"] = {"$exists" => true} end if opts[:with_usage] query["usage_records.0"] = {"$exists" => true} end end mcursor = user_collection.find(query, {:fields => []}) ret = [] mcursor.each do |hash| ret.push(hash['_id']) end ret end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 188 def find_and_modify(collection, *args) MongoDataStore.rescue_con_failure do collection.find_and_modify(*args) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 318 def find_available_district(node_profile=nil) node_profile = node_profile ? node_profile : "small" MongoDataStore.rescue_con_failure do hash = district_collection.find( { "available_capacity" => { "$gt" => 0 }, "active_server_identities_size" => { "$gt" => 0 }, "node_profile" => node_profile}).sort(["available_capacity", "descending"]).limit(1).next hash_to_district_ret(hash) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 83 def find_by_gear_uuid(gear_uuid) Rails.logger.debug "MongoDataStore.find_by_gear_uuid(#{gear_uuid})\n\n" hash = find_one( user_collection, { "apps.group_instances.gears.uuid" => gear_uuid } ) return nil unless hash user_hash_to_ret(hash) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 90 def find_by_uuid(obj_type_of_uuid, uuid) Rails.logger.debug "MongoDataStore.find_by_uuid(#{obj_type_of_uuid}, #{uuid})\n\n" case obj_type_of_uuid when "CloudUser" get_user_by_uuid(uuid) when "Application" get_user_by_app_uuid(uuid) when "Domain" get_user_by_domain_uuid(uuid) when "ApplicationTemplate" find_application_template(uuid) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 225 def find_district(uuid) Rails.logger.debug "find_district(#{uuid})\n\n" hash = find_one_district( "_id" => uuid ) hash_to_district_ret(hash) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 231 def find_district_by_name(name) Rails.logger.debug "find_district_by_name(#{name})\n\n" hash = find_one_district( "name" => name ) hash_to_district_ret(hash) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 245 def find_district_with_node(server_identity) Rails.logger.debug "find_district_with_node(#{server_identity})\n\n" hash = find_one_district({"server_identities.name" => server_identity }) hash_to_district_ret(hash) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 182 def find_one(collection, *args) MongoDataStore.rescue_con_failure do collection.find_one(*args) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 104 def find_subaccounts_by_parent_login(parent_id) Rails.logger.debug "MongoDataStore.find_subaccounts_by_parent_login(#{parent_id})\n\n" cur = MongoDataStore.rescue_con_failure { user_collection.find({ "parent_user_login" => parent_id }) } return [] unless cur hash_list = [] cur.each do |hash| hash.delete("_id") hash_list << hash end hash_list end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 313 def inc_district_externally_reserved_uids_size(uuid) Rails.logger.debug "inc_district_externally_reserved_uids_size(#{uuid})\n\n" update_district({"_id" => uuid}, {"$inc" => { "externally_reserved_uids_size" => 1 }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 194 def insert(collection, *args) MongoDataStore.rescue_con_failure do collection.insert(*args) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 206 def remove(collection, *args) MongoDataStore.rescue_con_failure do collection.remove(*args) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 285 def remove_district_node(uuid, server_identity) Rails.logger.debug "remove_district_node(#{uuid},#{server_identity})\n\n" hash = find_and_modify_district({ :query => { "_id" => uuid, "server_identities" => {"$elemMatch" => {"name" => server_identity, "active" => false}}}, :update => { "$pull" => { "server_identities" => {"name" => server_identity }}} }) return hash != nil end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 308 def remove_district_uids(uuid, uids) Rails.logger.debug "remove_district_capacity(#{uuid},#{uids})\n\n" update_district({"_id" => uuid, "available_uids" => uids[0]}, {"$pullAll" => { "available_uids" => uids }, "$inc" => { "available_capacity" => -uids.length, "max_uid" => -uids.length, "max_capacity" => -uids.length }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 266 def reserve_district_uid(uuid) Rails.logger.debug "reserve_district_uid(#{uuid})\n\n" hash = find_and_modify_district({ :query => {"_id" => uuid, "available_capacity" => {"$gt" => 0}}, :update => {"$pop" => { "available_uids" => -1}, "$inc" => { "available_capacity" => -1 }}, :new => false }) hash["available_uids"][0] end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 117 def save(obj_type, user_id, id, obj_attrs) Rails.logger.debug "MongoDataStore.save(#{obj_type}, #{user_id}, #{id}, #hidden)\n\n" case obj_type when "CloudUser" put_user(user_id, obj_attrs) when "Application" put_app(user_id, id, obj_attrs) when "Domain" put_domain(user_id, id, obj_attrs) when "UsageRecord" put_usage_record(user_id, id, obj_attrs) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 251 def save_district(uuid, district_attrs) Rails.logger.debug "save_district(#{uuid}, #{district_attrs.pretty_inspect})\n\n" district_attrs["_id"] = uuid orig_server_identities = district_attrs["server_identities"] district_attrs_to_internal(district_attrs) update_district({ "_id" => uuid }, district_attrs, { :upsert => true }) district_attrs.delete("_id") district_attrs["server_identities"] = orig_server_identities end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 275 def unreserve_district_uid(uuid, uid) Rails.logger.debug "unreserve_district_uid(#{uuid})\n\n" update_district({"_id" => uuid, "available_uids" => {"$ne" => uid}}, {"$push" => { "available_uids" => uid}, "$inc" => { "available_capacity" => 1 }}) end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 200 def update(collection, *args) MongoDataStore.rescue_con_failure do collection.update(*args) end end
# File lib/stickshift-controller/lib/stickshift/mongo_data_store.rb, line 178 def user_collection db.collection(@collections[:user]) end