def update_cobbler_machine(sender, domain_id, machine_id): """ Create `RegenerateCobbler()` task here. This should be the one and only place for creating this task. """ task = tasks.UpdateCobblerMachine(domain_id, machine_id) TaskManager.add(task)
def regenerate_motd(sender, fqdn, *args, **kwargs): """ Create `RegenerateMOTD()` task here. This should be the one and only place for creating this task. """ task = tasks.RegenerateMOTD(fqdn) TaskManager.add(task)
def regenerate_serialconsole(sender, cscreen_server_fqdn, *args, **kwargs): """ Create `RegenerateSerialConsole()` task here. This should be the one and only place for creating this task. """ if cscreen_server_fqdn is not None: task = tasks.RegenerateSerialConsole(cscreen_server_fqdn) TaskManager.add(task)
def scan(self, action='all', user=None): """Start scanning/checking the machine by creating a task.""" from orthos2.taskmanager import tasks from orthos2.taskmanager.models import TaskManager if action.lower() not in tasks.MachineCheck.Scan.Action.as_list: raise Exception("Unknown scan option '{}'!".format(action)) task = tasks.MachineCheck(self.fqdn, tasks.MachineCheck.Scan.to_int(action)) TaskManager.add(task)
def setup(self, setup_label=None, user=None): """Setup machine (re-install distribution).""" from orthos2.taskmanager import tasks from orthos2.taskmanager.models import TaskManager if self.has_setup_capability(): task = tasks.SetupMachine(self.fqdn, setup_label) TaskManager.add(task) return True return False
def execute(self): """Execute the task.""" from orthos2.taskmanager import tasks for machine in Machine.objects.all(): if machine.administrative or machine.system.administrative: continue if machine.reserved_by and not machine.is_reserved_infinite(): task = tasks.CheckReservationExpiration(machine.fqdn) TaskManager.add(task)
def regenerate_cobbler(sender, domain_id, *args, **kwargs): """ Create `RegenerateCobbler()` task here. This should be the one and only place for creating this task. """ if domain_id is None: task = tasks.RegenerateCobbler() else: task = tasks.RegenerateCobbler(domain_id) TaskManager.add(task)
def users_password_restore(request): if request.method == 'GET': user_id = request.GET.get('user_id', None) username = None if user_id is not None: try: user = User.objects.get(pk=user_id) username = user.username except Exception: pass form = PasswordRestoreForm(username=username) else: form = PasswordRestoreForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'].lower() username = form.cleaned_data['login'] try: user = User.objects.get(email=email, username=username) except User.DoesNotExist: messages.error(request, "E-Mail/login does not exist.") return redirect('frontend:password_restore') password = User.objects.make_random_password() user.set_password(password) user.save() task = tasks.SendRestoredPassword(user.id, password) TaskManager.add(task) # check for multiple accounts from deprecated Orthos task = tasks.CheckMultipleAccounts(user.id) TaskManager.add(task) messages.success(request, "Password restored - check your mails.") return redirect('frontend:login') return render( request, 'registration/password_reset.html', { 'form': form, 'title': 'Reset Password' } )
def scan(self, action='all', user=None): """Start scanning/checking the machine by creating a task.""" from orthos2.taskmanager import tasks from orthos2.taskmanager.tasks.ansible import Ansible from orthos2.taskmanager.models import TaskManager if action.lower() not in tasks.MachineCheck.Scan.Action.as_list: raise Exception("Unknown scan option '{}'!".format(action)) task = tasks.MachineCheck(self.fqdn, tasks.MachineCheck.Scan.to_int(action)) TaskManager.add(task) #ToDo: Better wait until individual machine scans finished task = Ansible([self.fqdn]) TaskManager.add(task)
def get(self, request, *args, **kwargs): """Return reservation history of machine.""" fqdn = request.GET.get('fqdn', None) option = request.GET.get('option', 'all') try: result = get_machine(fqdn, redirect_to='api:rescan', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if option == 'ansible': task = Ansible([fqdn]) TaskManager.add(task) return Message("OK.").as_json elif option not in MachineCheck.Scan.Action.as_list: return ErrorMessage("Unknown option '{}'".format(option)).as_json try: machine.scan(option) # task = MachineCheck(fqdn, tasks.MachineCheck.Scan.to_int(option)) # TaskManager.add(task) if not machine.collect_system_information: return InfoMessage( "Collecting system information is disabled for this machine." ).as_json except Exception as e: return ErrorMessage(str(e)).as_json return Message("OK.").as_json
def reserve(self, reason, until, user=None, reserve_for_user=None): """Reserve machine.""" from orthos2.taskmanager import tasks from orthos2.taskmanager.models import TaskManager from .reservationhistory import ReservationHistory if not reserve_for_user: reserve_for_user = user extension = True if not reason: raise ReserveException("Please provide a reservation reason.") if until == datetime.date.max and not user.is_superuser: raise ReserveException("Infinite reservation is not allowed.") # add to history if a superuser takes over the reservation if self.reserved_by and (self.reserved_by not in {user, reserve_for_user}): reservationhistory = ReservationHistory( machine=self, reserved_by=self.reserved_by, reserved_at=self.reserved_at, reserved_until=timezone.now(), reserved_reason='{} (taken away by {})'.format( self.reserved_reason, user)) reservationhistory.save() self.reserved_at = None self.reserved_by = reserve_for_user self.reserved_reason = reason if not self.reserved_at: self.reserved_at = timezone.now() extension = False # Infinte reservation: # -------------------- # Use `datetime.date.max` for the date and `datetime.time.min` for the time. # The minimum time (0, 0) must be set so that the correct time zone calculation does not # exceed the maximum DateTime limit (CET (UTC+1) of 9999-12-31, 23:59 (UTC) would result # in `OverflowError: date value out of range`). # Normal reservation (not infinite): # ---------------------------------- # Combine the `datetime.date` object with `datetime.time.max` (23, 59) and make the # new `datetime.datetime` object time zone aware using the default time zone (see # `TIME_ZONE` in the django settings). if until == datetime.date.max: until = timezone.datetime.combine(datetime.date.max, timezone.datetime.min.time()) until = timezone.make_aware(until, timezone.utc) else: until = timezone.datetime.combine(until, timezone.datetime.max.time()) until = timezone.make_aware(until, timezone.get_default_timezone()) self.reserved_until = until self.save() self.update_motd() if not extension: task = tasks.SendReservationInformation(reserve_for_user.id, self.fqdn) TaskManager.add(task)