def apply_changeset_to_multiple_hosts(request, changeset_pk, template='changesetapplies/apply_changeset_to_multiple_hosts.html'): """Apply changeset POST handler.""" user_has_access = False try: user = request.user.schemanizer_user user_has_access = user.role.name in ( users_models.Role.NAME.developer, users_models.Role.NAME.dba, users_models.Role.NAME.admin) if user_has_access: request_id = helpers.generate_request_id(request) changeset = changesets_models.Changeset.objects.get(pk=int(changeset_pk)) if not privileges_logic.can_user_apply_changeset(user, changeset): raise exceptions.PrivilegeError( 'User is not allowed to apply changeset.') if ( changeset.review_status != changesets_models.Changeset.REVIEW_STATUS_APPROVED): raise exceptions.Error('Cannot apply unapproved changeset.') if request.method == 'POST': log.debug(request.POST) server_ids = [] for k, v in request.POST.iteritems(): if k.startswith('server_'): server_ids.append(int(v)) if not server_ids: raise exceptions.Error('No server was selected.') if not changeset.before_version and len(server_ids) > 1: raise exceptions.Error( 'This changeset is going to be applied for the ' 'first time, select a single server only.') task_ids = [] for server_id in server_ids: result = tasks.apply_changeset.delay( changeset.id, user.id, server_id) task_ids.append(result.task_id) request_id = helpers.generate_request_id(request) request.session[request_id] = task_ids redirect_url = reverse('changesetapplies_changeset_applies') redirect_url = '%s?request_id=%s' % (redirect_url, request_id) messages.info(request, 'Added new changeset apply task(s).') return redirect(redirect_url) else: messages.error(request, MSG_USER_NO_ACCESS) except Exception, e: log.exception('EXCEPTION') messages.error(request, u'%s' % (e,))
def changeset_apply(request, changeset_pk, template='changesetapplies/changeset_apply.html'): """View for applying changeset.""" user_has_access = False try: user = request.user.schemanizer_user user_has_access = user.role.name in ( users_models.Role.NAME.developer, users_models.Role.NAME.dba, users_models.Role.NAME.admin) if user_has_access: request_id = helpers.generate_request_id(request) changeset = changesets_models.Changeset.objects.get(pk=int(changeset_pk)) environments = servers_models.Environment.objects.all() if not privileges_logic.can_user_apply_changeset(user, changeset): raise exceptions.PrivilegeError( 'User is not allowed to apply changeset.') else: messages.error(request, MSG_USER_NO_ACCESS) except Exception, e: log.exception('EXCEPTION') messages.error(request, u'%s' % (e,))
def get_context_data(self, **kwargs): context = super(ChangesetList, self).get_context_data(**kwargs) user = self.request.user.schemanizer_user changeset_qs = self.get_queryset() changeset_list = [] for changeset in changeset_qs: extra=dict( can_apply=privileges_logic.can_user_apply_changeset(user, changeset), can_review=privileges_logic.can_user_review_changeset(user, changeset)) changeset_list.append(dict(changeset=changeset, extra=extra)) context['changeset_list'] = changeset_list return context
def start_changeset_apply_thread(changeset, user, server): changeset = get_model_instance(changeset, Changeset) user = get_model_instance(user, User) server = get_model_instance(server, Server) if not privileges_logic.can_user_apply_changeset(user, changeset): raise PrivilegeError( 'User is not allowed to apply changeset.') connection_options = {} if settings.MYSQL_USER: connection_options['user'] = settings.MYSQL_USER if settings.MYSQL_PASSWORD: connection_options['passwd'] = settings.MYSQL_PASSWORD thread = ChangesetApplyThread(changeset, user, server, connection_options) thread.start() return thread
def apply_changeset(changeset, applied_by, server, message_callback=None, task_id='', request=None, unit_testing=False): """Applies changeset to specified server.""" if not privileges_logic.can_user_apply_changeset(applied_by, changeset): raise exceptions.PrivilegeError( 'User %s is not allowed to apply changeset %s.' % ( applied_by.name, changeset.pk)) connection_options = {} if settings.MYSQL_USER: connection_options['user'] = settings.MYSQL_USER if settings.MYSQL_PASSWORD: connection_options['passwd'] = settings.MYSQL_PASSWORD changeset_apply_obj = ChangesetApply( changeset, applied_by, server, connection_options, message_callback, task_id=task_id, request=request, unit_testing=unit_testing) changeset_apply_obj.run() return changeset_apply_obj
def changeset_view(request, id, template='changesets/changeset_view.html'): user_has_access = False try: user = request.user.schemanizer_user user_has_access = user.role.name in ( users_models.Role.NAME.developer, users_models.Role.NAME.dba, users_models.Role.NAME.admin) if user_has_access: id = int(id) changeset = models.Changeset.objects.select_related().get(id=id) if request.method == 'POST': try: if u'submit_update' in request.POST: # # Update changeset # return redirect( 'changesets_changeset_update', changeset.id) elif u'submit_review' in request.POST: # # Set changeset review status to 'in_progress' # return redirect( 'changesetreviews_changeset_review', changeset.id) elif u'submit_approve' in request.POST: # # Approve changeset. # changeset_functions.approve_changeset( changeset=changeset, approved_by=user, request=request) return redirect( 'changesets_changeset_view', changeset.pk) elif u'submit_reject' in request.POST: # # Reject changeset. # changeset_functions.reject_changeset( changeset=changeset, rejected_by=user, request=request) return redirect( 'changesets_changeset_view', changeset.pk) elif u'submit_delete' in request.POST: # # Delete changeset. # return redirect( 'changesets_changeset_soft_delete', changeset.id) elif u'submit_apply' in request.POST: # # Apply changeset # return redirect( 'changesetapplies_changeset_apply', changeset.id) else: messages.error(request, u'Unknown command.') log.error( u'Invalid post.\nrequest.POST=\n%s' % ( pformat(request.POST),)) except Exception, e: log.exception('EXCEPTION') messages.error(request, u'%s' % (e,)) changeset_applies = ( changesetapplies_models.ChangesetApply.objects.filter( changeset=changeset)) changeset_action_qs = models.ChangesetAction.objects.filter( changeset=changeset) changeset_actions = [] for changeset_action in changeset_action_qs: changeset_action_server_map_qs = ( models.ChangesetActionServerMap.objects.filter( changeset_action=changeset_action)) changeset_action_server_map = None if changeset_action_server_map_qs.exists(): changeset_action_server_map = ( changeset_action_server_map_qs[0]) type_col = changeset_action.type changeset_applies_url = None changeset_apply_qs = changesetapplies_models.ChangesetApply.objects.filter( changeset_action=changeset_action) if changeset_apply_qs.exists(): changeset_apply = changeset_apply_qs[0] if changeset_apply.task_id: changeset_applies_url = '%s?%s' % ( reverse('changesetapplies_changeset_applies'), urllib.urlencode( dict(task_id=changeset_apply.task_id))) if ( type_col == models.ChangesetAction.TYPE_APPLIED and changeset_action_server_map): server = changeset_action_server_map.server server_name = server.name environment_name = None if server.environment: environment_name = server.environment.name type_col = ( u'%s (server: %s, environment: %s)' % ( type_col, server_name, environment_name)) elif( type_col == models.ChangesetAction.TYPE_APPLIED_FAILED and changeset_action_server_map): server = changeset_action_server_map.server server_name = server.name environment_name = None if server.environment: environment_name = server.environment.name type_col = ( u'%s (server: %s, environment: %s)' % ( type_col, server_name, environment_name)) changeset_actions.append( dict( changeset_action=changeset_action, changeset_action_server_map= changeset_action_server_map, type=type_col, changeset_applies_url=changeset_applies_url)) user_privileges = privileges_logic.UserPrivileges(user) can_update = user_privileges.can_update_changeset(changeset) can_set_review_status_to_in_progress = ( privileges_logic.can_user_review_changeset(user, changeset)) can_approve = user_privileges.can_approve_changeset(changeset) can_reject = user_privileges.can_reject_changeset(changeset) can_soft_delete = ( privileges_logic.UserPrivileges(user) .can_soft_delete_changeset(changeset)) can_apply = privileges_logic.can_user_apply_changeset( user, changeset) if ( changeset.review_status in [ models.Changeset.REVIEW_STATUS_IN_PROGRESS, models.Changeset.REVIEW_STATUS_APPROVED, models.Changeset.REVIEW_STATUS_REJECTED] ): show_changeset_detail_test_status = True else: