示例#1
0
def __actiongroup_execute(target, shell_str):
    '''
        操作组执行
    '''
    shell_result = {}
    client = get_salt_client()
    target = 'L@' + ','.join(target)
    func = 'cmd.run'
    arg = [shell_str]
    shell_result = client.cmd(target, func, arg, expr_form = 'compound', ret = 'json')    
    yaml_str = yaml.dump(shell_result, default_flow_style=False)
    return yaml_str
示例#2
0
def __pkg_install(target, argument):
    '''
    安装第三方软件
    '''
    shell_result = {}
    client = get_salt_client()
    target = 'L@' + ','.join(target)
    func = 'state.high'
    arg = [argument]
    
    shell_result = client.cmd(target, func, arg, expr_form = 'compound', ret = 'json')    
    yaml_str = yaml.dump(shell_result, default_flow_style=False)
    return yaml_str
示例#3
0
def __file_transfer(target, apppackage):
    '''
        文件传输
    '''
    shell_result = {}
    client = get_salt_client()
    minion_list = target
    
    # 把文件传送到所有的跳板机上
    minion_list_syndic = []
    try:
        for saltid in minion_list:
            server = Server.objects.get(saltid=saltid)
            if server.agent_flag == 1:
                if server.agent_server.saltid not in minion_list_syndic:
                        minion_list_syndic.append(server.agent_server.saltid)
    except Exception, e:
        print e
示例#4
0
def go_schedule(request, id):
    '''
   执行上线计划
    '''
    client = get_salt_client()
    schedule = get_object_or_404(Schedule, pk=int(id))
    
    '''过滤已选server'''
    servergroup = schedule.server_group.all().order_by('saltid')
    if servergroup.count() > 0:
        sql = ''
        for server in servergroup:
            sql += '~Q(id=' + str(server.id )+ ') & '
        sql = sql[0:-2]
        servers = Server.objects.filter(eval(sql)).order_by('saltid')
    else :
        servers = Server.objects.order_by('saltid')
    
    '''过滤已选softform'''
    softformgroup = schedule.softform_group.all().order_by('soft_type')
    if softformgroup.count() > 0:
        sql = ''
        for softform in softformgroup:
            sql += '~Q(id=' + str(softform.id) + ') &'
        sql = sql[0:-2]
        softforms = SoftForm.objects.filter(eval(sql)).order_by('soft_type')  
    else :
        softforms = SoftForm.objects.order_by('soft_type')      
    
    '''过滤已选apppackage'''
    apppackagegroup = schedule.apppackage_group.all().order_by('package_name')
    if apppackagegroup.count() > 0:
        sql = ''
        for apppackage in apppackagegroup:
            sql += '~Q(id=' + str(apppackage.id) + ') & '
        sql = sql[0:-2]
        apppackages = AppPackage.objects.filter(eval(sql)).order_by('package_name')
    else :
        apppackages = AppPackage.objects.order_by('package_name')
    
    '''过滤已选actiongroup'''
    actiongroup_s = schedule.actiongroups.all().order_by('name')
    if actiongroup_s.count() > 0:
        sql = ''
        for actiongroup in actiongroup_s:
            sql += '~Q(id=' + str(actiongroup.id) + ') & '
        sql = sql[0:-2]
        actiongroupss = ActionGroup.objects.filter(eval(sql)).order_by('name')
    else :
        actiongroupss = ActionGroup.objects.order_by('name')
    
    '''过滤已选testcase'''
    testcasegroup = schedule.testcase_group.all().order_by('name')
    if testcasegroup.count() > 0:
        sql = ''
        for testcase in testcasegroup:
            sql += '~Q(id=' + str(testcase.id) + ') &'
        sql = sql[0:-2]
        testcases = Testcase.objects.filter(eval(sql)).order_by('name')
    else :
        testcases = Testcase.objects.order_by('name')
    
    
    if request.POST:
        department_dep = request.POST.get('department_dep', None)  # 研发部门
        contact = request.POST.get('contact', None)  # 联系人
        contact_info = request.POST.get('contact_info', None)  # 联系信息
        auto_rollback = request.POST.get('auto_rollback', None)  # 自动回滚标识    
        
        server_ids = request.POST.getlist('server_ids', None)  # 服务器组 
        softform_ids = request.POST.getlist('softform_ids', None)  # 基础应用模板组
        apppackage_ids = request.POST.getlist('apppackage_ids', None)  # 应用服务组
        actiongroup_ids = request.POST.getlist('actiongroup_ids', None)  # 操作流程
        testcase_ids = request.POST.getlist('testcase_ids', None)  # 测试用例组 
        
        schedule.department_dep = department_dep
        schedule.contact = contact
        schedule.contact_info = contact_info
        schedule.auto_rollback = auto_rollback
        schedule.user_name = request.user.username  # 操作用户。执行自动上线部署的操作人员。
        schedule.executive_state = 3  # 全上执行中
        schedule.save()
        
        schedule.server_group.clear()
        schedule.softform_group.clear()
        schedule.apppackage_group.clear()
        schedule.actiongroups.clear()
        schedule.testcase_group.clear()
        target = []
        if server_ids != None and len(server_ids) != 0:
            for server_id in server_ids:
                server = Server.objects.get(id__exact = server_id)
                schedule.server_group.add(server)
                target.append(server.saltid)
        else:
            return HttpResponse(simplejson.dumps({"statusCode": 302,
                                                  "navTabId": request.POST.get('navTabId', 'scheduleindex'),
                                                  "callbackType": request.POST.get('callbackType', None),
                                                  "message": u'服务器列表不能为空',
                                                  "info": u'服务器列表不能为空',
                                                  "result": u'服务器列表不能为空'}),
                                mimetype='application/json')            
        argument = '{'    
        if softform_ids != None and len(softform_ids) != 0:
            for softform_id in softform_ids:
                softform = SoftForm.objects.get(id__exact = softform_id)
                schedule.softform_group.add(softform)
                # 基础应用模板版本为空时,默认在线安装
                if softform.version == '' or softform.version == None:
                    argument += '\"' + softform.soft_name + '\": {\"pkg\": [\"installed\"]}, '
                    
        apppackage_list = []
        if apppackage_ids != None and len(apppackage_ids) != 0:
            for apppackage_id in apppackage_ids:
                apppackage = AppPackage.objects.get(id__exact = apppackage_id)
                schedule.apppackage_group.add(apppackage)
                apppackage_list.append(apppackage)
                
        actiongroup_list = []
        if actiongroup_ids != None and len(actiongroup_ids) != 0:
            for actiongroup_id in actiongroup_ids:
                actiongroup = ActionGroup.objects.get(id__exact = actiongroup_id)
                schedule.actiongroups.add(actiongroup)
                actiongroup_list.append(actiongroup)
                
        if testcase_ids != None and len(testcase_ids) != 0:
            for testcase_id in testcase_ids:
                testcase = Testcase.objects.get(id__exact = testcase_id)
                schedule.testcase_group.add(testcase)        
        
        '''
            在线安装软件包
        '''
        if target and len(argument) > 1:
            argument = argument[0:-2] + '}'
            ret_pkg_install = __pkg_install(target, argument)
            __schedule_log(schedule.schedule_id, ret_pkg_install)
            
        
        '''
            文件传输
        '''
        for apppackage in apppackage_list:
            ret_file_transfer = __file_transfer(target, apppackage)
            __schedule_log(schedule.schedule_id, ret_file_transfer)
        
        
        '''
            操作组执行
        '''
        shell_str = ''
        for actiongroup in actiongroup_list:
            actionorders = actiongroup.actionorder_set.all()
            count = actionorders.count()
            for i in range(count):
                actionorder = ActionOrder.objects.get(group_id=actiongroup.id, order=i)
                action = actionorder.action
                action_cmd = action.action_cmd
                action_cmd_list = action_cmd.split('\r\n')
                action_cmd_str = ' && '.join([line for line in action_cmd_list if not line.startswith('#') and line != ''])
                shell_str = shell_str + action_cmd_str + ' && '
        if shell_str != '' or shell_str != None:
            shell_str = shell_str[0:-4]
            ret_actiongroup_execute = __actiongroup_execute(target, shell_str)
            __schedule_log(schedule.schedule_id, ret_actiongroup_execute)
        
            
        schedule.executive_state = 5  # 全上完成
        schedule.save()
        Log(username = request.user.username,
            content = u"上线计划" + schedule.schedule_id + u"执行成功").save()
        return HttpResponse(simplejson.dumps({"statusCode": 200,
                                              "navTabId": request.POST.get('navTabId', 'scheduleindex'),
                                              "callbackType": request.POST.get('callbackType', 'closeCurrent'),
                                              "message": u'上线计划' + schedule.schedule_id + u'执行成功'}),
                            mimetype='application/json')
    return render_to_response('schedule/go.html', 
                              {'schedule': schedule, 
                               'servers': servers, 'servergroup': servergroup, 
                               'softforms': softforms, 'softformgroup': softformgroup, 
                               'apppackages': apppackages, 'apppackagegroup': apppackagegroup, 
                               'actiongroupss': actiongroupss, 'actiongroup_s': actiongroup_s, 
                               'testcases': testcases, 'testcasegroup': testcasegroup})