SaltStack源码分析之使用MongoDB模块-创新互联
MongoDB模块/usr/lib/python2.6/site-packages/salt/modules/mongodb.py
成都服务器托管,创新互联建站提供包括服务器租用、成都服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、国际域名空间等业务的一体化完整服务。电话咨询: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__函数,可以控制是否允许这个模块可见。如果返回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已经弃用,使用新的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数据库
$ 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模块,如果有就执行没有就不执行。
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创建账号
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另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
新闻标题:SaltStack源码分析之使用MongoDB模块-创新互联
本文来源:http://azwzsj.com/article/gicce.html