MongoDB模塊/usr/lib/python2.6/site-packages/salt/modules/mongodb.py
成都服務器托管,創(chuàng)新互聯(lián)建站提供包括服務器租用、成都服務器托管、帶寬租用、云主機、機柜租用、主機租用托管、CDN網(wǎng)站加速、國際域名空間等業(yè)務的一體化完整服務。電話咨詢:028-86922220MongoDB模塊會先去檢查是否安裝有PyMongo模塊
# -*- coding: utf-8 -*- ''' Module to provide MongoDB functionality to Salt :configuration: This module uses PyMongo, and accepts configuration details as parameters as well as configuration settings:: mongodb.host: 'localhost' mongodb.port: 27017 mongodb.user: '' mongodb.password: '' This data can also be passed into pillar. Options passed into opts will overwrite options passed into pillar. '''# Import python libs import logging # Import salt libs from salt._compat import string_types # Import third party libs try: import pymongo HAS_MONGODB = True except ImportError: HAS_MONGODB = False log = logging.getLogger(__name__)引入logging模塊處理日志,再引入salt._compat.string_types
對檢查pymongo進行異常處理,如果導入pymongo成功,設置HAS_MONGODB變量為True,導入失敗設置為False
def __virtual__(): ''' Only load this module if pymongo is installed ''' if HAS_MONGODB: return 'mongodb' else: return False如果定義一個__virtual__函數(shù),可以控制是否允許這個模塊可見。如果返回False,SaltStack將會忽略這個模塊,如果返回一個字符串,SaltStack將使用這個字符串。
def _connect(user=None, password=None, host=None, port=None, database='admin'): ''' Returns a tuple of (user, host, port) with config, pillar, or default values assigned to missing values. ''' if not user: user = __salt__['config.option']('mongodb.user') if not password: password = __salt__['config.option']('mongodb.password') if not host: host = __salt__['config.option']('mongodb.host') if not port: port = __salt__['config.option']('mongodb.port') try: conn = pymongo.connection.Connection(host=host, port=port) mdb = pymongo.database.Database(conn, database) if user and password: mdb.authenticate(user, password) except pymongo.errors.PyMongoError: log.error('Error connecting to database {0}'.format(database)) return False return connpymongo.connection.Connection這個方法PyMongo已經(jīng)棄用,使用新的MongoClient代替。
def db_list(user=None, password=None, host=None, port=None): ''' List all Mongodb databases CLI Example: .. code-block:: bash salt '*' mongodb.db_list列出所有的MongoDB數(shù)據(jù)庫
$ sudo salt '*' mongodb.db_list '' '' 10.10.41.17 28018 localhost.localdomain: 'mongodb.db_list' is not available. gintama-qa-server: - pay - clan - test - game1 - admin - gam_server_1SaltStack會先去檢測是否包含有PyMongo模塊,如果有就執(zhí)行沒有就不執(zhí)行。
def db_exists(name, user=None, password=None, host=None, port=None): ''' Checks if a database exists in Mongodb CLI Example: .. code-block:: bash salt '*' mongodb.db_exists檢查一個庫是否存在
$ sudo salt 'gintama-qa-server' mongodb.db_exists pay '' '' 10.10.41.17 28018 gintama-qa-server: True $ sudo salt 'gintama-qa-server' mongodb.db_exists pay3 '' '' 10.10.41.17 28018 gintama-qa-server: Falsedef db_remove(name, user=None, password=None, host=None, port=None): ''' Remove a Mongodb database CLI Example: .. code-block:: bash salt '*' mongodb.db_remove刪除一個庫
def user_list(user=None, password=None, host=None, port=None, database='admin'): ''' List users of a Mongodb database CLI Example: .. code-block:: bash salt '*' mongodb.user_list列出賬號
def user_exists(name, user=None, password=None, host=None, port=None, database='admin'): ''' Checks if a user exists in Mongodb CLI Example: .. code-block:: bash salt '*' mongodb.user_exists查看賬號是否存在
def user_create(name, passwd, user=None, password=None, host=None, port=None, database='admin'): ''' Create a Mongodb user CLI Example: .. code-block:: bash salt '*' mongodb.user_create創(chuàng)建賬號
def user_remove(name, user=None, password=None, host=None, port=None, database='admin'): ''' Remove a Mongodb user CLI Example: .. code-block:: bash salt '*' mongodb.user_remove測試情況
$ sudo salt 'gintama-qa-server' mongodb.user_create gintama gintama123 '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: True $ sudo salt 'gintama-qa-server' mongodb.user_exists gintama '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: True $ sudo salt 'gintama-qa-server' mongodb.user_list '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: |_ |_ - user - gintama |_ - readOnly - None $ sudo salt 'gintama-qa-server' mongodb.user_remove gintama '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: True $ sudo salt 'gintama-qa-server' mongodb.user_list '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: $ sudo salt 'gintama-qa-server' mongodb.user_exists gintama '' '' 10.10.41.17 28018 taiwan_game1 gintama-qa-server: False另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。