예제 #1
0
 def clean_jboss(form):
     try:
         App = form.Project.data
         Type = int(form.select.data)
         if Type == 1:
             raise AssertionError('暂时不支持该选择!')
         db = db_op.java_list
         project_db = db_op.project_level
         db_haproxy = db_op.haproxy_conf
         val = db.query.with_entities(db.ip,db.user).filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         v = db.query.filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         for c in v:
             db_op.DB.session.delete(c)
             db_op.DB.session.commit()
         p = project_db.query.filter(project_db.project == '%s' % App).all()
         for c in p:
             db_op.DB.session.delete(c)
             db_op.DB.session.commit()
         flash('上线配置表中的{0}相关配置清除完成!'.format(App))
         Info = [(v[0].encode('UTF-8'),v[1].encode('UTF-8')) for v in val if v]
         #删除应用部署表
         dbidc = db_idc.idc_app
         val = dbidc.query.with_entities(distinct(dbidc.domainName)).filter(dbidc.appName == '%s' % App).all()
         Domains = [v[0].encode('UTF-8') for v in val if v]
         v = dbidc.query.filter(dbidc.appName == '%s' % App).all()
         for c in v:
             db_idc.DB.session.delete(c)
             db_idc.DB.session.commit()
         for ip,user in Info:
             ssh = SSH.ssh(user, ip)
             cmd = 'cd /home/' + user + ' && ls |grep jboss-'
             Results = ssh.Run(cmd)
             jbossName = Results['stdout']
             count = len(jbossName)
             if count == 1:
                 War_Path = '/home/%s/%s/server/default/deploy/' % (user, jbossName[0])
                 cmds = ['/bin/rm -rf {0}/{1}'.format(War_Path, App), '/bin/bash  /home/java/stopJboss.sh',
                         '/usr/bin/pkill -9 java']
                 ssh.Run(cmds)
                 flash('{0}主机中的{1}相关配置清除完成!'.format(ip, App))
             else:
                 flash('ERROR:{0}主机中jboss目录有多个!'.format(ip, App))
             for Domain in Domains:
                 if Type == 2:
                     v = db_haproxy.query.filter(and_(db_haproxy.ip == ip, db_haproxy.domain == Domain, db_haproxy.Type == 'cw')).all()
                     for c in v:
                         db_op.DB.session.delete(c)
                         db_op.DB.session.commit()
                     _CURL_API()
                     flash('{0}主机的{1}相关测外haporxy配置清除完毕!'.format(ip,App))
                     db_server_pool = db_op.server_pool
                     db_server_pool.query.filter(and_(db_server_pool.ip == ip, db_server_pool.user == user,db_server_pool.target == 'cw')).update({db_server_pool.status:'0'})
                     db_op.DB.session.commit()
                     flash('{0}主机已重新放入资源池!'.format(ip))
                 if Type == 1:
                     flash('后续需要手动清除线上haporxy中的{0}的相关配置!'.format(App))
     except Exception as e:
         flash(e)
예제 #2
0
 def clean_php(form):
     try:
         App = form.Project.data
         Type = int(form.select.data)
         user = '******'
         if Type == 1:
             raise AssertionError('暂时不支持该选择!')
         db = db_op.php_list
         project_db = db_op.project_level
         db_haproxy = db_op.haproxy_conf
         val = db.query.with_entities(db.ip).filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         if val:
             v = db.query.filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
             for c in v:
                 db_op.DB.session.delete(c)
                 db_op.DB.session.commit()
             p = project_db.query.filter(project_db.project == '%s' %App).all()
             for c in p:
                 db_op.DB.session.delete(c)
                 db_op.DB.session.commit()
             flash('上线配置表中的{0}相关配置清除完成!'.format(App))
             sip = [v[0].encode('UTF-8') for v in val if v]
             # 删除应用部署表
             dbidc = db_idc.idc_app
             val = dbidc.query.with_entities(distinct(dbidc.domainName)).filter(dbidc.appName == '%s' %App).all()
             Domains = [v[0].encode('UTF-8') for v in val if v]
             v = dbidc.query.filter(dbidc.appName == '%s' % App).all()
             for c in v:
                 db_idc.DB.session.delete(c)
                 db_idc.DB.session.commit()
             for ip in sip:
                 for Domain in Domains:
                     cmds = ['/bin/rm -f /home/work/local/nginx/conf/servers/{0}'.format(Domain),
                             '/bin/mv /home/work/baihe/{0} /home/work/baihe/{0}.bak'.format(App)]
                     Ssh = SSH.ssh(user,ip)
                     Ssh.Run(cmds)
                     flash('{0}主机中的{1}相关nginx配置清除完成!'.format(ip,App))
                     if Type == 2:
                         v = db_haproxy.query.filter(and_(db_haproxy.ip == ip,db_haproxy.domain == Domain,db_haproxy.Type== 'cw')).all()
                         for c in v:
                             db_op.DB.session.delete(c)
                             db_op.DB.session.commit()
                         _CURL_API()
                         flash('{0}主机的{1}相关测外haporxy配置清除完毕!'.format(ip, App))
                         val = dbidc.query.filter(dbidc.ip == '%s' % ip).all()
                         if not val:
                             db_server_pool = db_op.server_pool
                             db_server_pool.query.filter(and_(db_server_pool.ip == ip, db_server_pool.user == user,db_server_pool.target == 'cw')).update({db_server_pool.status: '0'})
                             db_op.DB.session.commit()
                             flash('{0}主机已重新放入资源池!'.format(ip))
                     if Type == 1:
                         flash('后续需要手动清除线上haporxy中的{0}的相关配置!'.format(App))
         else:
             flash('%s not fond!' % App)
     except Exception as e:
         flash(e)
예제 #3
0
파일: deploy.py 프로젝트: znavy/opsweb
def deploy_php():
    reload(MyForm)
    form = MyForm.MyForm_deploy_php()
    try:
        if form.submit_produce.data:
            Info = form.select_produce.data.strip()
            Ver = form.ver_produce.data.strip()
            Type = '1'
        elif form.submit_test.data:
            Info = form.select_test.data.strip()
            Ver = form.ver_test.data.strip()
            Type = '2'
        else:
            return render_template('deploy_php.html', form=form)
        assert Info, '服务器选择不能为空!'
        Info = Info.split(':')
        user = Info[1]
        ip = Info[0]
        db = db_op.php_list
        val = db.query.filter(and_(db.ip == ip, db.type == Type)).all()
        if val:
            flash('{0}的PHP环境已存在!'.format(ip))
        else:
            os.system(
                '/usr/bin/wget -P /tmp/ http://172.16.16.160/dw/lnmp/lnmp_{0}.tgz'
                .format(Ver))
            Ssh = SSH.ssh(user, ip)
            Ssh.Scp('/tmp/lnmp_{0}.tgz'.format(Ver),
                    '/home/work/lnmp_{0}.tgz'.format(Ver))
            os.system('/bin/rm -f /tmp/lnmp_{0}.tgz'.format(Ver))
            cmds = [
                '/bin/rm -rf /home/work/local',
                'cd /home/work && /bin/tar -zxvf lnmp_{0}.tgz'.format(Ver),
                '/bin/rm -f /home/work/lnmp_{0}.tgz'.format(Ver),
                '/usr/bin/pkill -9 {php,nginx}',
                '/home/work/local/php/sbin/php-fpm',
                '/home/work/local/nginx/sbin/nginx'
            ]
            for cmd in cmds:
                Ssh.Run(cmd)
            Ssh.Close()
            #修改资源池表
            db = db_op.server_pool
            db.query.filter(and_(db.user == user,
                                 db.ip == ip)).update({db.status: '1'})
            db_op.DB.session.commit()
            flash('PHP环境在{0}部署成功,NGINX虚拟主机部署后可正常访问!'.format(ip))
    except Exception as e:
        flash(e)
    return render_template('deploy_php.html', form=form)
예제 #4
0
파일: deploy.py 프로젝트: newbelee/opsweb
def deploy_php():
    reload(MyForm)
    form = MyForm.MyForm_deploy_php()
    try:
        if form.submit_produce.data:
            Infos = form.select_produce.data
            Ver = form.ver_produce.data.strip()
            Type = '1'
        elif form.submit_test.data:
            Infos = form.select_test.data
            Ver = form.ver_test.data.strip()
            Type = '2'
        else:
            return render_template('deploy_php.html', form=form,Main_Infos=g.main_infos)
        assert Infos, '服务器选择不能为空!'
        for Info in Infos:
            Info = Info.split(':')
            user = Info[1]
            ip = Info[0]
            db = db_op.php_list
            val = db.query.filter(and_(db.ip == ip,db.type == Type)).all()
            if val:
                flash('{0}的PHP环境已存在!'.format(ip))
            else:
                os.system('/usr/bin/wget -P /tmp/ http://172.16.16.160/dw/lnmp/lnmp_{0}.tgz'.format(Ver))
                Ssh = SSH.ssh(user, ip)
                Ssh.Scp('/tmp/lnmp_{0}.tgz'.format(Ver),'/home/work/lnmp_{0}.tgz'.format(Ver))
                os.system('/bin/rm -f /tmp/lnmp_{0}.tgz'.format(Ver))
                cmds = ['/bin/rm -rf /home/work/local',
                        'cd /home/work && /bin/tar -zxvf lnmp_{0}.tgz'.format(Ver),
                        '/bin/rm -f /home/work/lnmp_{0}.tgz'.format(Ver),
                        '/usr/bin/pkill -9 {php,nginx}',
                        '/home/work/local/php/sbin/php-fpm',
                        '/home/work/local/nginx/sbin/nginx']
                for cmd in cmds:
                    Ssh.Run(cmd)
                Ssh.Close()
                #修改资源池表
                db = db_op.server_pool
                db.query.filter(and_(db.user == user, db.ip == ip)).update({db.status: '1'})
                db_op.DB.session.commit()
        flash('PHP环境在{0}部署成功,NGINX虚拟主机部署后可正常访问!'.format(ip))
    except Exception as e:
        flash(e)
    return render_template('deploy_php.html', form=form,Main_Infos=g.main_infos)
예제 #5
0
파일: deploy.py 프로젝트: znavy/opsweb
def deploy_jboss():
    reload(MyForm)
    form = MyForm.MyForm_deploy_jboss()
    try:
        if form.submit_produce.data and form.input_produce.data:
            Warname = form.input_produce.data.strip()
            Info = form.select_produce.data.strip()
            Type = '1'
        elif form.submit_test.data and form.input_test.data:
            Warname = form.input_test.data.strip()
            Info = form.select_test.data.strip()
            Type = '2'
        else:
            return render_template('deploy_jboss.html', form=form)
        assert Info, '服务器选择不能为空!'
        assert Warname.endswith('.war'), 'war包名称应以.war结尾'
        #判断测外环境
        if Type == '2':
            assert form.input_domain.data, '项目域名不能为空!'
            Domain = form.input_domain.data.strip()
            if not form.haproxy.data and not form.haproxy_intranet.data:
                raise AssertionError, 'HAPROXY选择不能为空!'
        Info = Info.split(':')
        user = Info[1]
        ip = Info[0]
        db = db_op.java_list
        val = db.query.filter(
            and_(db.project == Warname, db.ip == ip, db.user == user,
                 db.type == Type)).all()
        if val:
            flash('{0}在{1}的{2}环境已存在!'.format(Warname, ip, user))
        else:
            Ssh = SSH.ssh(user, ip)
            os.system(
                '/usr/bin/wget -P /tmp/ http://172.16.4.138/software/jboss/{0}.zip'
                .format(user))
            Ssh.Scp('/tmp/{0}.zip'.format(user),
                    '/home/{0}/{0}.zip'.format(user))
            os.system('/bin/rm -f /tmp/{0}.zip'.format(user))
            cmds = [
                'cd /home/{0} && /usr/bin/unzip -qo {0}.zip && /bin/rm -f {0}.zip'
                .format(user), '/bin/rm -f /home/{0}/{0}.zip'.format(user),
                'mkdir -p /home/{0}/jboss-baihe/server/default/deploy/{1}'.
                format(user, Warname), '/usr/bin/pkill -9 java',
                'source ~/.bash_profile && /bin/bash /home/{0}/startJboss.sh'.
                format(user)
            ]
            for cmd in cmds:
                Ssh.Run(cmd)
            Ssh.Close()
            c = db(project=Warname,
                   user=user,
                   ip=ip,
                   type=Type,
                   Gray='0',
                   operation='baihe')
            db_op.DB.session.add(c)
            db_op.DB.session.commit()
            # 修改资源池表
            db = db_op.server_pool
            db.query.filter(and_(db.user == user,
                                 db.ip == ip)).update({db.status: '1'})
            db_op.DB.session.commit()
            flash('{0} 在{1}的{2}环境部署成功,上线操作后可正常访问!'.format(Warname, ip, user))
            # 测外发布
            if Type == '2':
                # 调取api接口生成haproxy配置文件
                Type = 'cw'
                ports = {
                    'java': '18080',
                    'java2': '28080',
                    'java3': '38080',
                    'java4': '48080'
                }

                def _CURL_API(Domain, internet=None):
                    Domains = [Domain]
                    if ',' in Domain:
                        Domains = Domain.split(',')
                    for Domain in Domains:
                        # 修改应用部署表
                        dbidc = db_idc.idc_app
                        c = dbidc(ip=ip,
                                  user=user,
                                  appName=Warname,
                                  domainName=Domain)
                        db_idc.DB.session.add(c)
                        db_idc.DB.session.commit()
                        # 调用haproxy APi接口
                        URL = "https://op.baihe.com/haproxy_conf"
                        Params = {
                            'type': Type,
                            'ip': '{0}:{1}'.format(ip, ports[user]),
                            'domain': Domain
                        }
                        if internet:
                            Params['intranet'] = 'True'
                            Arg = '内部'
                        else:
                            Arg = '外部'
                        f = requests.request('get',
                                             URL,
                                             params=Params,
                                             timeout=10)
                        if 'result' in f.json():
                            flash('信息:{0} {1}HAPROXY配置{2}'.format(
                                Domain, Arg,
                                f.json()['result']))

                if form.haproxy_intranet.data:
                    _CURL_API(Domain, internet=True)
                if form.haproxy.data:
                    _CURL_API(Domain)
    except Exception as e:
        flash(e)
    return render_template('deploy_jboss.html', form=form)
예제 #6
0
파일: deploy.py 프로젝트: znavy/opsweb
def deploy_nginx():
    form = MyForm.MyForm_deploy_nginx()
    if form.submit.data:
        try:
            Type = int(form.select.data)
            assert form.input_domain.data, '项目域名不能为空!'
            domains = form.input_domain.data.strip()
            assert form.input_root.data, '项目路径不能为空!'
            root_path = form.input_root.data.strip()
            project = root_path.split('/')
            if len(project) < 5:
                raise ImportError, '非法项目路径!'
            else:
                project = project[4]
            assert form.ip.data, '服务器IP不能为空!'
            ips = form.ip.data.strip()
            if not form.haproxy.data and not form.haproxy_intranet.data and Type == 2:
                raise AssertionError, 'HAPROXY选择不能为空!'
            for ip in ips.splitlines():
                if g.grade == 2 and '172.16.9.' not in ip:
                    flash('非法IP地址,请确认测外服务器IP!')
                else:
                    db = db_op.php_list
                    val = db.query.filter(
                        and_(db.ip == ip, db.project == project,
                             db.type == Type)).all()
                    if val:
                        flash('{0} {1}项目 nginx虚拟主机配置文件已存在!'.format(
                            ip, project))
                    else:
                        ssh = SSH.ssh('work', ip)
                        try:
                            # 生成配置文件
                            for domain in domains.strip().split(','):
                                # 修改应用部署表
                                dbidc = db_idc.idc_app
                                c = dbidc(ip=ip,
                                          user='******',
                                          appName=project,
                                          domainName=domain)
                                db_idc.DB.session.add(c)
                                db_idc.DB.session.commit()
                                # 调用haproxy APi接口
                                Dst_path = '/tmp/{0}'.format(domain)
                                mod_path = "{0}/../conf/nginx_template.cfg".format(
                                    page_deploy.root_path)
                                if os.path.exists(Dst_path):
                                    os.remove(Dst_path)
                                with open(mod_path, 'r') as f:
                                    for line in f:
                                        line = line.strip().replace(
                                            'DOMAIN', domain).replace(
                                                'ROOT_PATH', root_path)
                                        with open(Dst_path, 'a+') as F:
                                            F.write('{0}\n'.format(line))
                                ssh.Scp(
                                    Dst_path,
                                    '/home/work/local/nginx/conf/servers/{0}'.
                                    format(domain))
                                flash('{0} {1} nginx虚拟主机部署完毕!'.format(
                                    ip, domain))
                        except Exception as e:
                            flash(e)
                        else:
                            cmds = [
                                "[ -z $(/bin/netstat -lntp|grep nginx|awk '{print $1}') ] && /home/work/local/nginx/sbin/nginx",
                                "/home/work/local/nginx/sbin/nginx -s reload"
                            ]
                            for cmd in cmds:
                                ssh.Run(cmd)
                            #在上线配置表中插入数据
                            c = db(project=project,
                                   user='******',
                                   ip=ip,
                                   type=Type,
                                   Gray='0',
                                   operation='baihe')
                            db_op.DB.session.add(c)
                            db_op.DB.session.commit()
                            if Type == 2:
                                # 调取api接口生成haproxy配置文件
                                Type = 'cw'

                                def _CURL_API(Domain, internet=None):
                                    Domains = [Domain]
                                    if ',' in Domain:
                                        Domains = Domain.split(',')
                                    for Domain in Domains:
                                        URL = "https://op.baihe.com/haproxy_conf"
                                        Params = {
                                            'type': Type,
                                            'ip': '{0}:80'.format(ip),
                                            'domain': Domain
                                        }
                                        if internet:
                                            Params['intranet'] = 'True'
                                            Arg = '内部'
                                        else:
                                            Arg = '外部'
                                        f = requests.request('get',
                                                             URL,
                                                             params=Params,
                                                             timeout=10)
                                        if 'result' in f.json():
                                            flash('信息:{0} {1}HAPROXY配置{2}'.
                                                  format(
                                                      Domain, Arg,
                                                      f.json()['result']))

                                if form.haproxy_intranet.data:
                                    _CURL_API(domain, internet=True)
                                if form.haproxy.data:
                                    _CURL_API(domain)
        except Exception as e:
            flash(e)
    return render_template('deploy_nginx.html', form=form)
예제 #7
0
파일: deploy.py 프로젝트: newbelee/opsweb
def deploy_jboss():
    reload(MyForm)
    form = MyForm.MyForm_deploy_jboss()
    try:
        if form.submit_produce.data and form.input_produce.data:
            Warname = form.input_produce.data.strip()
            Infos = form.select_produce.data
            Type = '1'
            project_level = form.select_level.data
        elif form.submit_test.data and form.input_test.data:
            Warname = form.input_test.data.strip()
            Infos = form.select_test.data
            Type = '2'
        else:
            return render_template('deploy_jboss.html',Main_Infos=g.main_infos, form=form)
        assert Infos,'服务器选择不能为空!'
        assert Warname.endswith('.war') ,'war包名称应以.war结尾'
        #判断测外环境
        if Type == '2':
            assert form.input_domain.data,'项目域名不能为空!'
            Domain = form.input_domain.data.strip()
            if not form.haproxy.data and not form.haproxy_intranet.data:
                raise AssertionError('HAPROXY选择不能为空!')
        for Info in Infos:
            Info = Info.split(':')
            user = Info[1]
            ip = Info[0]
            db = db_op.java_list
            project_db = db_op.project_level
            val = db.query.filter(and_(db.project == Warname,db.ip == ip,db.user == user, db.type == Type)).all()
            if val:
                flash('{0}在{1}的{2}环境已存在!'.format(Warname,ip,user))
            else:
                Ssh = SSH.ssh(user,ip)
                os.system('/usr/bin/wget -P /tmp/ http://172.16.4.138/software/jboss/{0}.zip'.format(user))
                Ssh.Scp('/tmp/{0}.zip'.format(user),'/home/{0}/{0}.zip'.format(user))
                os.system('/bin/rm -f /tmp/{0}.zip'.format(user))
                cmds = ['cd /home/{0} && /usr/bin/unzip -qo {0}.zip && /bin/rm -f {0}.zip'.format(user),
                        '/bin/rm -f /home/{0}/{0}.zip'.format(user),
                        'mkdir -p /home/{0}/jboss-baihe/server/default/deploy/{1}'.format(user,Warname),
                        '/usr/bin/pkill -9 java',
                        'source ~/.bash_profile && /bin/bash /home/{0}/startJboss.sh'.format(user)]
                for cmd in cmds:
                    Ssh.Run(cmd)
                Ssh.Close()
                c = db(project=Warname, user=user, ip=ip, type=Type, Gray='0',operation = 'baihe')
                db_op.DB.session.add(c)
                db_op.DB.session.commit()
                # 修改资源池表
                db = db_op.server_pool
                db.query.filter(and_(db.user == user,db.ip == ip)).update({db.status:'1'})
                db_op.DB.session.commit()
                flash('{0} 在{1}的{2}环境部署成功!'.format(Warname, ip, user))
                # 测外发布
                if Type == '2':
                    # 调取api接口生成haproxy配置文件
                    Type = 'cw'
                    ports = {'java': '18080', 'java2': '28080', 'java3': '38080', 'java4': '48080'}
                    def _CURL_API(Domain,internet=None):
                        Domains = [Domain]
                        if ',' in Domain:
                            Domains = Domain.split(',')
                        for Domain in Domains:
                            # 修改应用部署表
                            dbidc = db_idc.idc_app
                            c = dbidc(ip=ip, user=user,appName=Warname,domainName=Domain)
                            db_idc.DB.session.add(c)
                            db_idc.DB.session.commit()
                            # 调用haproxy APi接口
                            URL = app.config.get('HAPROXY_API')
                            Params = {'type':Type, 'ip':'{0}:{1}'.format(ip,ports[user]),'domain':Domain}
                            if internet:
                                Params['intranet'] = 'True'
                                Arg = '内部'
                            else:
                                Arg = '外部'
                            f = requests.request('get', URL,params=Params, timeout=10,verify=False)
                            if 'result' in f.json():
                                flash('信息:{0} {1}HAPROXY配置{2}'.format(Domain,Arg,f.json()['result']))
                    if form.haproxy_intranet.data:
                        _CURL_API(Domain,internet=True)
                    if form.haproxy.data:
                        _CURL_API(Domain)
        if Type == '1':
            p = project_db(project=Warname, level=project_level)
            db_op.DB.session.add(p)
            db_op.DB.session.commit()
        flash('内部需要配置DNS,进行一次项目上线操作后方可正常访问!')
    except Exception as e:
        flash(e)
    return render_template('deploy_jboss.html',form=form,Main_Infos=g.main_infos)
예제 #8
0
파일: deploy.py 프로젝트: newbelee/opsweb
def deploy_nginx():
    form = MyForm.MyForm_deploy_nginx()
    if form.submit.data:
        try:
            Type = int(form.select.data)
            assert form.input_domain.data,'项目域名不能为空!'
            domains = form.input_domain.data.strip()
            assert form.input_root.data,'项目路径不能为空!'
            project_level = form.select_level.data
            project_type = form.select_project.data
            root_path = form.input_root.data.strip()
            project = root_path.split('/')
            if len(project) < 5:
                raise ImportError('非法项目路径!')
            else:
                project = project[4]
            assert form.ip.data,'服务器IP不能为空!'
            ips = form.ip.data.strip()
            if not form.haproxy.data and not form.haproxy_intranet.data and Type == 2:
                raise AssertionError('HAPROXY选择不能为空!')
            for ip in ips.splitlines():
                if g.grade == 2 and '172.16.9.' not in ip:
                    flash('非法IP地址,请确认测外服务器IP!')
                else:
                    db = db_op.php_list
                    project_db = db_op.project_level
                    val = db.query.filter(and_(db.ip == ip, db.project == project, db.type == Type)).all()
                    if val:
                        flash('{0} {1}项目 nginx虚拟主机配置文件已存在!'.format(ip,project))
                    else:
                        ssh = SSH.ssh('work',ip)
                        try:
                            # 生成配置文件
                            for domain in domains.strip().split(','):
                                # 修改应用部署表
                                dbidc = db_idc.idc_app
                                c = dbidc(ip=ip, user='******', appName=project, domainName=domain)
                                db_idc.DB.session.add(c)
                                db_idc.DB.session.commit()
                                # 调用haproxy APi接口
                                Dst_path = '/tmp/{0}'.format(domain)
                                mod_path = "{0}/../conf/nginx_template.cfg".format(page_deploy.root_path)
                                if os.path.exists(Dst_path):
                                    os.remove(Dst_path)
                                with open(mod_path, 'r') as f:
                                    for line in f:
                                        line = line.strip().replace('DOMAIN', domain).replace('ROOT_PATH', root_path)
                                        with open(Dst_path, 'a+') as F:
                                            F.write('{0}\n'.format(line))
                                ssh.Scp(Dst_path,'/home/work/local/nginx/conf/servers/{0}'.format(domain))
                                flash('{0} {1} nginx虚拟主机部署完毕!'.format(ip, domain))
                        except Exception as e:
                            flash(e)
                        else:
                            cmds = ["[ -z $(/bin/netstat -lntp|grep nginx|awk '{print $1}') ] && /home/work/local/nginx/sbin/nginx","/home/work/local/nginx/sbin/nginx -s reload"]
                            for cmd in cmds:
                                ssh.Run(cmd)
                            #在上线配置表中插入数据
                            c = db(project=project,user='******',ip=ip,type=Type,Gray='0',operation = project_type)
                            db_op.DB.session.add(c)
                            db_op.DB.session.commit()
                            if Type == 2:
                                # 调取api接口生成haproxy配置文件
                                Type = 'cw'
                                def _CURL_API(Domain, internet=None):
                                    Domains = [Domain]
                                    if ',' in Domain:
                                        Domains = Domain.split(',')
                                    for Domain in Domains:
                                        URL = app.config.get('HAPROXY_API')
                                        Params = {'type': Type, 'ip': '{0}:80'.format(ip),'domain': Domain}
                                        if internet:
                                            Params['intranet'] = 'True'
                                            Arg = '内部'
                                        else:
                                            Arg = '外部'
                                        f = requests.request('get', URL,params=Params,timeout=10,verify=False)
                                        if 'result' in f.json():
                                            flash('信息:{0} {1}HAPROXY配置{2}'.format(Domain, Arg, f.json()['result']))
                                if form.haproxy_intranet.data:
                                    _CURL_API(domain, internet=True)
                                if form.haproxy.data:
                                    _CURL_API(domain)
            if Type == 1:
                p = project_db(project=project, level=project_level)
                db_op.DB.session.add(p)
                db_op.DB.session.commit()
        except Exception as e:
            flash(e)
    return render_template('deploy_nginx.html', form=form,Main_Infos=g.main_infos)
예제 #9
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def get_mysqldb_info():
    try:
        db_third = db_idc.third_resource
        db_mysqldb = db_idc.idc_mysqldb
        IDC_MySql = Mysql.MYSQL(MYSQL_USER, MYSQL_PW, MYSQL_HOST, MYSQL_PORT, 'idc')
        IDC_MySql.Run("TRUNCATE TABLE tableinfo;")
        infos = IDC_MySql.Run("select ip,port from mysqldb where master = '是' or slave = '是';")
    except Exception as e:
        logging.error(e)
    else:
        for info in infos:
            ip,port = info
            try:
                INFO_MySql = Mysql.MYSQL(MYSQL_INFO_USER, MYSQL_INFO_PW, ip, port, 'mysql')
                exclude_db = ('mysql', 'test', 'information_schema', 'performance_schema')
                cmd = "show databases;"
                Lists = INFO_MySql.Run(cmd)
                Lists = [db[0] for db in Lists if db[0] not in exclude_db]
                db_lists = ','.join(Lists)
            except:
                continue
            else:
                try:
                    cmd = "SHOW SLAVE STATUS;"
                    result = INFO_MySql.Run(cmd)
                    if result:
                        result = list(result[0])
                        m_ip = result[1].strip()
                        m_user = result[2].strip()
                        m_port = result[3]
                        s_io = result[10].strip()
                        s_sql = result[11].strip()
                        #判断获取的master主机信息是否需要解析
                        if len(m_ip.split('.')) != 4:
                            ssh_port = db_third.query.with_entities(db_third.ssh_port).filter(and_(db_third.ip==ip,db_third.app_port==port)).all()
                            ssh_port = ssh_port[0][0]
                            Ssh = SSH.ssh(ip=ip, ssh_port=ssh_port)
                            ssh_cmd = "/bin/cat /etc/hosts|/bin/grep {0}".format(str(m_ip))
                            values = Ssh.Run(ssh_cmd)
                            if values['stdout']:
                                m_ip = values['stdout'][0].split()[0]
                            else:
                                m_ip = None
                        if m_ip:
                            if s_io == 'Yes' and s_sql == 'Yes':
                                #判断真实的主库服务器信息
                                values = db_mysqldb.query.with_entities(db_mysqldb.Master_Host,db_mysqldb.Master_Port).filter(
                                    and_(db_mysqldb.ip == m_ip, db_mysqldb.port == m_port,db_mysqldb.slave == '是')).all()
                                if values:
                                    m_ip,m_port = values[0]
                                cmd = "update mysqldb set slave='是',master='否',db='%s',Master_Host='%s',Master_Port='%s',Master_User='******' where ip='%s' and port=%i;" % \
                                      (db_lists, m_ip, m_port, m_user, ip, int(port))
                            else:
                                check_cmd = "show slave hosts;"
                                check_result = IDC_MySql.Run(check_cmd)
                                if check_result:
                                    cmd = "update mysqldb set slave='否',master='是',db='%s',Master_Host='',Master_Port='',Master_User='' where ip='%s' and port=%i;" % (
                                    db_lists, ip, int(port))
                    else:
                        cmd = "update mysqldb set slave='否',master='是',db='%s',Master_Host='',Master_Port='',Master_User='' where ip='%s' and port=%i;" % (
                        db_lists, ip, int(port))
                    IDC_MySql.Run(cmd)
                except Exception as e:
                    INFO_MySql.Close()
                    logging.error(e)
                try:
                    version = INFO_MySql.Run("show variables like 'version';")
                    version = version[0][-1]
                    for db in Lists:
                        cmd = "SHOW TABLE STATUS from %s;" % db
                        results = INFO_MySql.Run(cmd)
                        if results:
                            for result in results:
                                table_name = result[0]
                                table_engine = result[1]
                                table_Rows = int(result[4])
                                table_size = int(result[6]) + int(result[8])
                                if len(str(table_size)) > 9:
                                    table_size = '%sGB' % (table_size / 1000 / 1000 / 1000)
                                elif len(str(table_size)) > 6:
                                    table_size = '%sMB' % (table_size / 1000 / 1000)
                                else:
                                    table_size = '%sKB' % (table_size / 1000)
                                table_charset = result[14]
                                cmd = "insert into tableinfo (ip, port, database_name, table_name, Engine_name, Rows,size,Charset, version,update_time) VALUES ('%s',%i,'%s','%s','%s',%i,'%s','%s','%s',now());" % \
                                      (ip, int(port), db, table_name, table_engine, table_Rows, table_size, table_charset, version)
                                IDC_MySql.Run(cmd)
                except Exception as e:
                    logging.error(e)
                INFO_MySql.Close()
    finally:
        IDC_MySql.Close()
        db_idc.DB.session.remove()
예제 #10
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def alarm_load():
    try:
        db_zabbix = db_idc.zabbix_info
        db_project = db_op.project_list
        zabi = tools.zabbix_api()
        redis_key = 'op_alarm_load_hosts'
        token = "https://oapi.dingtalk.com/robot/send?access_token=052f18d93a62110bb5152785bfb1f473f829d183294fbbe6048bc5073ff22cd5"
        host_infos = db_zabbix.query.with_entities(db_zabbix.ip, db_zabbix.ssh_port,db_zabbix.hostname,db_zabbix.update_time).filter(and_(db_zabbix.cpu_load > 100, db_zabbix.icmpping == 1)).all()
        #循环监控疑似问题服务器
        for infos in host_infos:
            try:
                host,ssh_port,hostname,update_time=infos
                loads = []
                Projects = []
                Others = []
                #获取zabbix监控数据
                if time.strftime('%Y-%m-%d',time.localtime()) in update_time:
                    for key_ in ('system.cpu.load[all,avg5]', 'system.cpu.num'):
                        val = zabi.zabbix_history(hostname, key_)
                        if not val:
                            val = zabi.zabbix_history(host, key_)
                            if not val:
                                val = 0
                        loads.append(float(val))
                    if loads[0] > 0 and loads[1] > 0:
                        load = loads[0] / loads[1] * 100
                        if load >100:
                            Key = '%s:%s:%s' %(host,hostname,ssh_port)
                            RC.hincrby(redis_key,Key)
                            #符合条件后进行重启操作
                            if int(RC.hget(redis_key,Key)) >9:
                                #判断是否是java程序
                                ret = db_project.query.filter(and_(db_project.ip==host,db_project.ssh_port==ssh_port)).all()
                                if ret:
                                    try:
                                        Ssh = SSH.ssh(ip=host,ssh_port=ssh_port)
                                    except Exception as e:
                                        logging.error(e)
                                    else:
                                        #筛查可重启服务进程
                                        results = Ssh.Run("ps -aux | sort -k3nr |head -n 1")
                                        if results['stdout']:
                                            results = results['stdout'][0].strip().split()
                                            if results[-1].endswith('-rpc.jar'):
                                                pro_jar = results[-1]
                                                if pro_jar in ['moji-location-rpc.jar']:
                                                    Projects.append(pro_jar.split('.')[0])
                                                else:
                                                    Others.append(pro_jar.split('.')[0])
                                            else:
                                                for line in results:
                                                    if '-Dcatalina.home=' in line :
                                                        Projects.append(line.strip().split('/')[-1])
                                                        break
                                            if Projects:
                                                for project in Projects:
                                                    #重启问题tomcat
                                                    result = Ssh.Run("supervisorctl  restart  {0}".format(project))
                                                    if result['stderr']:
                                                        text = ['**线上服务重启:%s**' % host, "CPU持续使用率:{0}%".format(load),
                                                                "相关进程:{0}".format(project), '**服务重启失败,需手动处理!**']
                                                        tools.dingding_msg(text, alart_token=token)
                                                    else:
                                                        text = ['**线上服务重启:%s**' % host, "CPU持续使用率:{0}%".format(load),
                                                                "相关进程:{0}".format(project), '**服务重启成功!**']
                                                        tools.dingding_msg(text)
                                            else:
                                                project = ' '.join(results[10:])
                                                if Others:
                                                    project = Others[0]
                                                text = ['**线上服务器预警:%s**' % host, "CPU持续使用率:{0}%".format(load),
                                                            "相关进程:{0}".format(project), '**请及时进行处理!**']
                                                tools.dingding_msg(text,alart_token=token)
                                    finally:
                                        Ssh.Close()
            except Exception as e:
                logging.error(e)
        RC.hincrby(redis_key, 'count')
        if int(RC.hget(redis_key, 'count')) >9:
            RC.delete(redis_key, )
    except Exception as e:
        logging.error(e)
    finally:
        db_idc.DB.session.remove()
        db_op.DB.session.remove()
예제 #11
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def get_redis_info():
    db_third = db_idc.third_resource
    db_redis = db_idc.redis_info
    db_servers = db_idc.idc_servers
    update_date = time.strftime('%Y-%m-%d', time.localtime())
    try:
        server_ids = db_servers.query.with_entities(db_servers.id,db_servers.ip,db_servers.ssh_port,db_servers.hostname).all()
        for infos in server_ids:
            RC_CLUSTER.hset('op_server_hostnames',infos[0],infos[-1])
        server_ids = {"%s:%s"%(infos[1],infos[2]):infos[0] for infos in server_ids}
        redis_list = db_third.query.with_entities(db_third.ip,db_third.ssh_port,db_third.app_port).filter(db_third.resource_type == 'redis').all()
        ssh_ports = {"%s:%s" %(infos[0],infos[2]):infos[1] for infos in redis_list}
        for ip,ssh_port,app_port in set(redis_list):
            #初始化参数
            masterauth = None
            requirepass = None
            pid = None
            conf_dir = None
            conf_file = ""
            redis_type = {'master': '否', 'slave': '否', 'cluster': '否'}
            #判断ssh端口是否连通
            if tcpping(host=ip, port=app_port, timeout=3):
                try:
                    Ssh = SSH.ssh(ip=ip, ssh_port=ssh_port)
                    cmd = "netstat -lntp|grep :%s" % app_port
                    results = Ssh.Run(cmd)
                    if results['stdout']:
                        for line in results['stdout'][0].split():
                            if '/redis' in line:
                                pid = line.split('/')[0]
                                break
                        if pid:
                            cmd = "/bin/ps -ef|grep -v grep|grep {}".format(pid)
                            results = Ssh.Run(cmd)
                            if results['stdout']:
                                result = results['stdout'][0]
                                if 'cluster' in result:
                                    redis_type['cluster'] = '是'
                                else:
                                    try:
                                        result = results['stdout'][0].split()[-1]
                                        if '/' in result:
                                            conf_file = "/usr/local/redis/etc/{}".format(result.split('/')[-1])
                                        if not conf_file.endswith('.conf'):
                                            cmd = "lsof -p {}|grep 'cwd'".format(pid)
                                            cwd = Ssh.Run(cmd)
                                            if cwd['stdout']:
                                                for line in cwd['stdout']:
                                                    if 'redis' in line:
                                                        conf_dir = line.split()[-1]
                                                        break
                                                if conf_dir:
                                                    cmd = "grep {0} -r {1}/|grep '.conf:port'".format(app_port, conf_dir)
                                                    results = Ssh.Run(cmd)
                                                    if results['stdout']:
                                                        for line in results['stdout']:
                                                            if ':port {}'.format(app_port) in line:
                                                                conf_file = line.split(':')[0]
                                        if conf_file.endswith('.conf'):
                                            cmd = "grep masterauth {}".format(conf_file)
                                            results = Ssh.Run(cmd)
                                            if results['stdout']:
                                                masterauth =  results['stdout'][0].split()[-1].strip()
                                            cmd = "grep requirepass {}".format(conf_file)
                                            pw_result = Ssh.Run(cmd)
                                            if pw_result['stdout']:
                                                requirepass =  pw_result['stdout'][0].split()[-1].strip()
                                        RC = redis.StrictRedis(ip,int(app_port),password=requirepass,decode_responses=True)
                                        Infos = RC.info()
                                        if Infos['role'] == 'master':
                                            redis_type['master'] = '是'
                                        if Infos['role'] == 'slave':
                                            redis_type['slave'] = '是'
                                        counts = int((Infos['connected_slaves']))
                                    except Exception as e:
                                        logging.error(e)
                                    else:
                                        try:
                                            #修改记录slave信息
                                            if counts > 0:
                                                for i in range(counts):
                                                    Info = Infos['slave%s' % i]
                                                    if isinstance(Info,dict):
                                                        slave_ip = Info['ip']
                                                        slave_port = Info['port']
                                                        slave_status = Info['state']
                                                    else:
                                                        slave_ip, slave_port, slave_status = Info.split(',')
                                                    if slave_status == 'online' and int(slave_port) >1024:
                                                        try:
                                                            SSH_port = ssh_ports['%s:%s' % (slave_ip, slave_port)]
                                                        except:
                                                            server_id = slave_ip
                                                        else:
                                                            try:
                                                                server_id = server_ids['%s:%s' %(slave_ip,SSH_port)]
                                                            except:
                                                                server_id = slave_ip
                                                        try:
                                                            master_id = server_ids['%s:%s' % (ip, ssh_port)]
                                                        except:
                                                            master_id = ip
                                                        val = db_redis.query.filter(and_(db_redis.server_id == server_id, db_redis.port == slave_port)).all()
                                                        if val:
                                                            db_redis.query.filter(and_(db_redis.server_id == server_id, db_redis.port == slave_port)).update(
                                                                {db_redis.masterauth: masterauth, db_redis.requirepass: requirepass,
                                                                 db_redis.master: '否',db_redis.slave: '是',db_redis.cluster: '否',
                                                                 db_redis.Master_Host: master_id,db_redis.Master_Port: app_port,db_redis.update_date: update_date})
                                                            db_idc.DB.session.commit()
                                                        else:
                                                            c = db_redis(server_id=server_id, port=slave_port, masterauth=masterauth,
                                                                         requirepass=requirepass, master='否',
                                                                         slave='是',cluster='否', Master_host=master_id,
                                                                         Master_Port=app_port, update_date=update_date)
                                                            db_idc.DB.session.add(c)
                                                            db_idc.DB.session.commit()
                                        except Exception as e:
                                            db_idc.DB.session.rollback()
                                            logging.error(e)
                                try:
                                    #修改记录master或者cluster信息
                                    if redis_type['master'] == '是' or redis_type['cluster'] == '是':
                                        try:
                                            server_id = server_ids['%s:%s' % (ip, ssh_port)]
                                        except:
                                            server_id = ip
                                        val = db_redis.query.filter(and_(db_redis.server_id == server_id, db_redis.port == app_port)).all()
                                        if val:
                                            db_redis.query.filter(
                                                and_(db_redis.server_id == server_id, db_redis.port == app_port)).update(
                                                {db_redis.masterauth: masterauth, db_redis.requirepass: requirepass,
                                                 db_redis.master: redis_type['master'],
                                                 db_redis.slave: redis_type['slave'],
                                                 db_redis.cluster: redis_type['cluster'],
                                                 db_redis.Master_Host: '',
                                                 db_redis.Master_Port: '', db_redis.update_date: update_date})
                                            db_idc.DB.session.commit()
                                        else:
                                            c = db_redis(server_id=server_id, port=app_port, masterauth=masterauth,
                                                         requirepass=requirepass, master=redis_type['master'],
                                                         slave=redis_type['slave'], cluster=redis_type['cluster'],
                                                         Master_host='', Master_Port='',
                                                         update_date=update_date)
                                            db_idc.DB.session.add(c)
                                            db_idc.DB.session.commit()
                                except Exception as e:
                                    db_idc.DB.session.rollback()
                                    logging.error(e)
                except Exception as e:
                    logging.error(e)
                    continue
                finally:
                    Ssh.Close()
            else:
                loging.write("delete not exist redis %s  %s  ......" %(ip,app_port))
                v = db_redis.query.filter(and_(or_(db_redis.server_id==server_ids['%s:%s' %(ip,ssh_port)],
                                                   db_redis.server_id == ip),db_redis.port==app_port)).all()
                for c in v:
                    db_idc.DB.session.delete(c)
                    db_idc.DB.session.commit()
    except Exception as e:
        logging.error(e)
    finally:
        db_idc.DB.session.remove()
예제 #12
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def get_other_info():
    db_project_other = db_op.project_other
    db_crontabs = db_idc.crontabs
    db_servers = db_idc.idc_servers
    db_hosts = db_idc.hosts
    infos = db_servers.query.with_entities(db_servers.id,db_servers.ip,db_servers.ssh_port).filter(and_(db_servers.status !='维护中',db_servers.comment !='跳过')).all()
    try:
        for info in infos:
            server_id,ip,ssh_port = info
            if tcpping(host=ip, port=ssh_port, timeout=3):
                try:
                    update_date = time.strftime('%Y-%m-%d', time.localtime())
                    Ssh = SSH.ssh(ip=ip, ssh_port=ssh_port)
                    #收集crontab信息
                    results = Ssh.Run("cat /var/spool/cron/*")
                    if results['stdout']:
                        v = db_crontabs.query.filter(db_crontabs.server_id==int(server_id)).all()
                        for c in v:
                            db_idc.DB.session.delete(c)
                            db_idc.DB.session.commit()
                        for result in results['stdout']:
                            if not result.startswith('#') and '*' in result:
                                result = result.strip().split()
                                cron = ' '.join(result[:5])
                                action = ' '.join(result[5:])
                                c = db_crontabs(cron=cron,action=action,server_id=int(server_id),update_time=update_date)
                                db_idc.DB.session.add(c)
                                db_idc.DB.session.commit()
                    # 收集jar运行信息
                    results = Ssh.Run("ps -ef|grep java|grep -e '.jar$'")
                    if results['stdout']:
                        vals = []
                        v = db_project_other.query.filter(db_project_other.server_id == int(server_id)).all()
                        for c in v:
                            db_op.DB.session.delete(c)
                            db_op.DB.session.commit()
                        for result in results['stdout']:
                            if 'hadoop' not in result and 'hive' not in result:
                                result = result.strip().split()[-1]
                                if '/' in result:
                                    result = result.split('/')[-1]
                                vals.append(result)
                        for val in set(vals):
                            result = db_project_other.query.filter(and_(db_project_other.project==val,db_project_other.server_id==server_id)).all()
                            if not result:
                                c = db_project_other(lable='java', project=val, server_id=server_id,business_id=0, update_time=update_date)
                                db_op.DB.session.add(c)
                                db_op.DB.session.commit()
                    #收集hosts信息
                    results = Ssh.Run("cat /etc/hosts")
                    if results['stdout']:
                        v = db_hosts.query.filter(db_hosts.server_id == int(server_id)).all()
                        for c in v:
                            db_idc.DB.session.delete(c)
                            db_idc.DB.session.commit()
                        for line in results['stdout']:
                            if not line.startswith('#') and '127.0.0.1' not in line:
                                line = line.strip().split()
                                if line:
                                    if len(line) == 2:
                                        if 'localhost' not in line[1]:
                                            c = db_hosts(line[0],line[1],server_id=server_id,update_time=update_date)
                                            db_idc.DB.session.add(c)
                                            db_idc.DB.session.commit()
                                    if len(line) > 2:
                                        for hostname in line[1:]:
                                            if not hostname.startswith('#') and not 'localhost' in  hostname:
                                                c = db_hosts(line[0],hostname, server_id=server_id,
                                                             update_time=update_date)
                                                db_idc.DB.session.add(c)
                                                db_idc.DB.session.commit()
                    Ssh.Close()
                except Exception as e:
                    logging.error(e)
                    continue
    except Exception as e:
        logging.error(e)
    finally:
        db_idc.DB.session.remove()
        db_op.DB.session.remove()
예제 #13
0
def assets_add():
    try:
        if tools.http_args(request, 'ip'):
            ip = tools.http_args(request, 'ip')
            ssh_port = 20443
            if tools.http_args(request, 'ssh_port'):
                ssh_port = tools.http_args(request, 'ssh_port')
            db_ips = db_idc.resource_ip
            db_idc_id = db_idc.idc_id
            exist_ips = []
            rip = ip.split('.')
            rip[3] = 0
            aid = db_ips.query.with_entities(
                db_ips.aid).filter(db_ips.network == rip).all()
            if aid:
                aid = aid[0][0]
            else:
                return jsonify({'error': "没有找到对应的机房信息!"})
            db_server = db_idc.idc_servers
            m_ips = db_server.query.with_entities(distinct(db_server.ip)).all()
            if m_ips:
                exist_ips = [ip[0] for ip in m_ips]
            e_ips = db_server.query.with_entities(distinct(
                db_server.s_ip)).all()
            for ips in e_ips:
                if None not in ips:
                    if ';' in ips[0]:
                        ips = ips[0].split(';')
                        for ip in ips:
                            exist_ips.append(ip)
                    exist_ips.append(ips[0])
            if ip not in exist_ips:
                if tcpping(host=ip, port=ssh_port, timeout=1):
                    try:
                        Ssh = SSH.ssh(ip=ip, ssh_port=ssh_port)
                        values = Ssh.Run(
                            '/usr/sbin/dmidecode -s system-manufacturer')
                        if values['stdout']:
                            if values['stdout'][0].strip(
                                    '\n') in PHYSICAL_TYPES:
                                v = db_server(idc_id=1053,
                                              ip=ip,
                                              ssh_port=ssh_port,
                                              s_ip='',
                                              host_type='physical',
                                              hostname='',
                                              sn='',
                                              manufacturer='',
                                              productname='',
                                              system='',
                                              cpu_info='',
                                              cpu_core='',
                                              mem='',
                                              disk_count=0,
                                              disk_size='',
                                              idrac='',
                                              purch_date='',
                                              expird_date='',
                                              status='新发现',
                                              comment='')
                                db_idc.DB.session.add(v)
                                db_idc.DB.session.commit()
                            else:
                                #判断机房机柜信息
                                idc_id = db_idc_id.query.with_entities(
                                    db_idc_id.id).filter(
                                        and_(db_idc_id.aid == aid,
                                             db_idc_id.cid == 'KVM'))
                                if not idc_id:
                                    c = db_idc_id(aid=aid, cid='KVM')
                                    db_idc.DB.session.add(c)
                                    db_idc.DB.session.commit()
                                    idc_id = db_idc_id.query.with_entities(
                                        idc_id.id).filter(
                                            and_(db_idc_id.aid == aid,
                                                 db_idc_id.cid == 'KVM'))
                                idc_id = idc_id[0][0]
                                dt = time.strftime('%Y-%m-%d',
                                                   time.localtime())
                                v = db_server(idc_id=idc_id,
                                              ip=ip,
                                              ssh_port=ssh_port,
                                              s_ip='',
                                              host_type='vm',
                                              hostname='',
                                              sn='',
                                              manufacturer='',
                                              productname='',
                                              system='',
                                              cpu_info='',
                                              cpu_core='',
                                              mem='',
                                              disk_count=0,
                                              disk_size='',
                                              idrac='',
                                              purch_date=dt,
                                              expird_date='2999-12-12',
                                              status='使用中',
                                              comment='')
                                db_idc.DB.session.add(v)
                                db_idc.DB.session.commit()
                            for cmd in ("yum -y install dmidecode",
                                        "chmod +s /usr/sbin/dmidecode"):
                                Ssh.Run(cmd)
                    except Exception as e:
                        return jsonify({'error': e})
                    else:
                        return jsonify({
                            'info':
                            '%s add success' % ip,
                            'api':
                            request.url,
                            'time':
                            '%s' % time.strftime('%Y-%m-%d %H:%M:%S',
                                                 time.localtime())
                        })
                    finally:
                        Ssh.Close()
                else:
                    return jsonify({'error': 'ssh port not connect!'})
            else:
                return jsonify({'error': '服务器信息已存在!'})
    except Exception as e:
        logging.error(e)
    finally:
        db_idc.DB.session.remove()
예제 #14
0
 def clean_jboss(form):
     try:
         App = form.Project.data
         Type = int(form.select.data)
         if Type == 1:
             raise AssertionError, '暂时不支持该选择!'
         db = db_op.java_list
         db_haproxy = db_op.haproxy_conf
         val = db.query.with_entities(db.ip, db.user).filter(
             and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         v = db.query.filter(
             and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         for c in v:
             db_op.DB.session.delete(c)
             db_op.DB.session.commit()
         flash('上线配置表中的{0}相关配置清除完成!'.format(App))
         Info = [(v[0].encode('UTF-8'), v[1].encode('UTF-8')) for v in val
                 if v]
         #删除应用部署表
         dbidc = db_idc.idc_app
         val = dbidc.query.with_entities(distinct(
             dbidc.domainName)).filter(dbidc.appName == '%s' % App).all()
         Domains = [v[0].encode('UTF-8') for v in val if v]
         v = dbidc.query.filter(dbidc.appName == '%s' % App).all()
         for c in v:
             db_idc.DB.session.delete(c)
             db_idc.DB.session.commit()
         for ip, user in Info:
             ssh = SSH.ssh(user, ip)
             cmd = 'cd /home/' + user + ' && ls |grep jboss-'
             Results = ssh.Run(cmd)
             jbossName = Results['stdout']
             count = len(jbossName)
             if count == 1:
                 War_Path = '/home/%s/%s/server/default/deploy/' % (
                     user, jbossName[0])
                 cmds = [
                     '/bin/rm -rf {0}/{1}'.format(War_Path, App),
                     '/bin/bash  /home/java/stopJboss.sh',
                     '/usr/bin/pkill -9 java'
                 ]
                 ssh.Run(cmds)
                 flash('{0}主机中的{1}相关配置清除完成!'.format(ip, App))
             else:
                 flash('ERROR:{0}主机中jboss目录有多个!'.format(ip, App))
             for Domain in Domains:
                 if Type == 2:
                     v = db_haproxy.query.filter(
                         and_(db_haproxy.ip == ip,
                              db_haproxy.domain == Domain,
                              db_haproxy.Type == 'cw')).all()
                     for c in v:
                         db_op.DB.session.delete(c)
                         db_op.DB.session.commit()
                     _CURL_API()
                     flash('{0}主机的{1}相关测外haporxy配置清除完毕!'.format(ip, App))
                     db_server_pool = db_op.server_pool
                     db_server_pool.query.filter(
                         and_(db_server_pool.ip == ip,
                              db_server_pool.user == user,
                              db_server_pool.target == 'cw')).update(
                                  {db_server_pool.status: '0'})
                     db_op.DB.session.commit()
                     flash('{0}主机已重新放入资源池!'.format(ip))
                 if Type == 1:
                     flash('后续需要手动清除线上haporxy中的{0}的相关配置!'.format(App))
     except Exception as e:
         loging.write(e)
         flash(e)
예제 #15
0
 def clean_php(form):
     try:
         App = form.Project.data
         Type = int(form.select.data)
         user = '******'
         if Type == 1:
             raise AssertionError, '暂时不支持该选择!'
         db = db_op.php_list
         db_haproxy = db_op.haproxy_conf
         val = db.query.with_entities(db.ip).filter(
             and_(db.project == '%s' % App, db.type == '%i' % Type)).all()
         if val:
             v = db.query.filter(
                 and_(db.project == '%s' % App,
                      db.type == '%i' % Type)).all()
             for c in v:
                 db_op.DB.session.delete(c)
                 db_op.DB.session.commit()
             flash('上线配置表中的{0}相关配置清除完成!'.format(App))
             sip = [v[0].encode('UTF-8') for v in val if v]
             # 删除应用部署表
             dbidc = db_idc.idc_app
             val = dbidc.query.with_entities(distinct(
                 dbidc.domainName)).filter(dbidc.appName == '%s' %
                                           App).all()
             Domains = [v[0].encode('UTF-8') for v in val if v]
             v = dbidc.query.filter(dbidc.appName == '%s' % App).all()
             for c in v:
                 db_idc.DB.session.delete(c)
                 db_idc.DB.session.commit()
             for ip in sip:
                 for Domain in Domains:
                     cmds = [
                         '/bin/rm -f /home/work/local/nginx/conf/servers/{0}'
                         .format(Domain),
                         '/bin/mv /home/work/baihe/{0} /home/work/baihe/{0}.bak'
                         .format(App)
                     ]
                     Ssh = SSH.ssh(user, ip)
                     Ssh.Run(cmds)
                     flash('{0}主机中的{1}相关nginx配置清除完成!'.format(ip, App))
                     if Type == 2:
                         v = db_haproxy.query.filter(
                             and_(db_haproxy.ip == ip,
                                  db_haproxy.domain == Domain,
                                  db_haproxy.Type == 'cw')).all()
                         for c in v:
                             db_op.DB.session.delete(c)
                             db_op.DB.session.commit()
                         _CURL_API()
                         flash('{0}主机的{1}相关测外haporxy配置清除完毕!'.format(
                             ip, App))
                         val = dbidc.query.filter(dbidc.ip == '%s' %
                                                  ip).all()
                         if not val:
                             db_server_pool = db_op.server_pool
                             db_server_pool.query.filter(
                                 and_(
                                     db_server_pool.ip == ip,
                                     db_server_pool.user == user,
                                     db_server_pool.target == 'cw')).update(
                                         {db_server_pool.status: '0'})
                             db_op.DB.session.commit()
                             flash('{0}主机已重新放入资源池!'.format(ip))
                     if Type == 1:
                         flash('后续需要手动清除线上haporxy中的{0}的相关配置!'.format(App))
         else:
             flash('%s not fond!' % App)
     except Exception as e:
         loging.write(e)
         flash(e)