Exemple #1
0
def redis_handler(config_section, db):
    c = config.config("config_db.ini")
    ip = c.getOption(config_section, "ip")
    password = c.getOption(config_section, "password")
    port = c.getOption(config_section, "port")
    r = my_redis.Redis(host=ip, password=password, port=port, db=db)
    return r
Exemple #2
0
def passwd_deaes(db_passwd_aes):
    _config = config.config("../conf/crypto.ini")
    print
    crypto_str = _config.getOption(section="crypto", option="key_str")
    pc = prpcrypt(crypto_str)  # 初始化密钥
    d = pc.decrypt(db_passwd_aes)
    return d
def get_show_type():
    try:
        c = config.config("ip_type.ini")
        #c.getOption('SHOWTYPE', 'ipshowtype')
        return  str(c.getOption('SHOWTYPE', 'ipshowtype'))
    except Exception:
        print("Env_type get error: %s" % traceback.format_exc())
        return 'outer_ip'
Exemple #4
0
def passwd_aes(db_passwd):
    '''
    AES加密密码
    :return:
    '''
    _config = config.config("../conf/crypto.ini")
    crypto_str = _config.getOption(section="crypto", option="key_str")
    pc = prpcrypt(crypto_str)  # 初始化密钥
    e = pc.encrypt(db_passwd)
    return e
Exemple #5
0
 def __init__(self, db, config_section):
     """
     数据库构造函数,从连接池中取出连接,并生成操作游标
     """
     try:
         c = config.config("../conf/mysql.ini")
         user = c.getOption(config_section, "username")
         pwd = c.getOption(config_section, "password")
         host = c.getOption(config_section, "host")
         port = c.getOption(config_section, "port", "int")
         self.connect = self.db = MySQLdb.connect(host=host,
                                                  user=user,
                                                  passwd=pwd,
                                                  db=db,
                                                  port=port,
                                                  charset='utf8')
     except Exception, e:
         print traceback.format_exc()
         #logger.error("connect database error - %s" % str(e))
         return
Exemple #6
0
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os
from classes import config
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

settings_c = config.config("settings.ini")
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = settings_c.getOption('SECRET_KEY', 'secret_key')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

SITE_ID = 1

ALLOWED_HOSTS = settings_c.getOption('AllowHost', 'iplist').strip().split(',')

# Application definition

INSTALLED_APPS = [
    'django.contrib.sites',
    'django.contrib.admin',
Exemple #7
0
def db_backup_job(job_list, db_ip_port):
    '''
    执行db备份方法
    :return:
    '''
    try:
        db_ip = db_ip_port.strip().split(':')[0]
        db_port = db_ip_port.strip().split(':')[1]
        if job_list['db'] is None or job_list['table'] is None:
            return json.dumps({
                "result": "failed",
                "info": "%s db or table is none" % db_ip_port
            })
        else:
            # print job_list
            db_instance_id = job_list['db_instance_id']
            db_instance_name = job_list['db_mark']
            db_container_name = job_list['db_container_name_slave'] if job_list['db_container_name_slave'] else\
                job_list['db_container_name']
            db_service_type = job_list['db_service_type']
            db_passwd = crypto.passwd_deaes(job_list['db_passwd'])
            db_user_name = job_list['db_user_name']
            db = job_list['db']
            table = job_list['table']
            ansible_interface = ansible_api.AnsiInterface(become=True,
                                                          become_method='sudo',
                                                          become_user='******')
            time_now = time.strftime("%Y%m%d%H%M%S",
                                     time.localtime(time.time()))

            if db_service_type == 'container':
                # mk backup dir
                ansible_interface.make_dir(
                    db_ip, '/data/mysqlbackup/ state=directory')
                ansible_interface.make_dir(
                    db_ip,
                    '/data/mysqlbackup/%s state=directory' % db_instance_name)
                ansible_interface.make_dir(
                    db_ip, '/data/mysqlbackup/%s/%s_%s state=directory' %
                    (db_instance_name, db, table))
                cmd = "docker exec %s  bash -c 'mysqldump --defaults-extra-file=/etc/my.cnf -h127.0.0.1 " \
                      "-u%s -P%s -p%s --opt --skip-lock-tables %s %s' > /data/mysqlbackup/%s/%s_%s/%s.sql" \
                      % (db_container_name, db_user_name, db_port, db_passwd, db, table, db_instance_name,
                         db, table, time_now)
                result = ansible_interface.exec_shell(db_ip, cmd)
            elif db_service_type in ['service', 'RDS']:
                # mk backup dir
                my_cmd(
                    'if [[ ! -d /data/mysqlbackup/ ]];then mkdir /data/mysqlbackup/; fi'
                )
                my_cmd(
                    'if [[ ! -d /data/mysqlbackup/%s ]];then mkdir /data/mysqlbackup/%s; fi'
                    % (db_instance_name, db_instance_name))
                my_cmd(
                    'if [[ ! -d /data/mysqlbackup/%s/%s_%s ]];then mkdir /data/mysqlbackup/%s/%s_%s; fi'
                    %
                    (db_instance_name, db, table, db_instance_name, db, table))
                cmd = "mysqldump -h%s " \
                      "-u%s -P%s -p%s --opt --skip-lock-tables %s %s > /data/mysqlbackup/%s/%s_%s/%s.sql" \
                      % (db_ip, db_user_name, db_port, db_passwd, db, table, db_instance_name,
                         db, table, time_now)
                _result = my_cmd(cmd)
                result = {
                    "host_failed": {},
                    "host_ok": {},
                    "host_unreachable": {}
                }
                if _result == 1:
                    result['host_failed'] = {db_ip: {'stderr': u'备份失败'}}
                else:
                    result['host_ok'] = {db_ip: {}}
            else:
                return json.dumps({"result": "failed", "info": u"没有该服务类型对应命令"})

            # parse result
            if result['host_failed']:
                _ret = {
                    "result":
                    "failed",
                    "info":
                    "ip: %s, db: %s, failed_info: %s" %
                    (db_ip, db, result['host_failed'][db_ip]['stderr'])
                }  # 执行结果放入Queue
            elif result['host_unreachable']:
                _ret = {
                    "result":
                    "failed",
                    "info":
                    " ip: %s, ureachable: %s" %
                    (db_ip, result['host_unreachable'])
                }  # 执行结果放入Queue
            elif result['host_ok']:
                ok_ip = result['host_ok'].keys()
                _ret = {
                    "result":
                    "success",
                    "info":
                    "ip: %s, backup_file: %s" %
                    (','.join(ok_ip), "/data/mysqlbackup/%s/%s_%s/%s.sql" %
                     (db_instance_name, db, table, time_now))
                }
                dteventtime = str(
                    time.strftime("%Y-%m-%d %H:%M:%S",
                                  time.localtime(time.time())))
                data = [(dteventtime, db_instance_id, db, table,
                         "/data/mysqlbackup/%s/%s_%s/%s.sql" %
                         (db_instance_name, db, table, time_now))]
                c = config.config('mysql.ini')
                db_name = c.getOption(RUN_MODE, 'dbname')
                with mysql_db.conn_db(db_name, RUN_MODE) as _db:
                    sql = "insert into db_job_db_backup_history (`dtEventTime`,`db_instance_id`,`dbs`,`tables`,`result`) values (%s,%s,%s,%s,%s)"
                    _db.executemany(sql, data)
                return json.dumps(_ret)
            else:
                _ret = {
                    "result":
                    "failed",
                    "info":
                    "ip: %s, db: %s, failed_info: %s" %
                    (db_ip, db, u"没有执行结果,请检查密码")
                }
            return json.dumps(_ret)
    except Exception:
        try:
            table = job_list.get('table', 'None')
            db = job_list.get('db', 'None')
            db_ip = db_ip_port.strip().split(':')[0]
        except:
            table = 'None'
            db = 'None'
            db_ip = 'None'
        _logger.error(traceback.format_exc())
        return json.dumps({
            "result":
            "failed",
            "info":
            "ip: %s, db: %s,table: %s, failed_info: %s" %
            (db_ip, db, table, traceback.format_exc())
        })