Esempio n. 1
0
def update_lv_list(request):
    if request.method == 'POST':
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        LOGGER.info(
            "Machine with IP %s poked the web panel interface to update a VM's LV list"
            % ip)
        if 'hostname' in request.POST:
            vm = get_object_or_None(VirtualMachine,
                                    name=request.POST['hostname'])
            if vm and (vm.network_configuration.IPv4 == ip
                       or vm.network_configuration.IPv6 == ip
                       or vm.service.network_configuration.IPv4 == ip
                       or vm.service.network_configuration.IPv6 == ip):
                result = subprocess.check_output([
                    "userv", "mws-admin", "mws_extract_lv_info",
                    vm.network_configuration.name
                ])
                lvlist = []
                first_date = date.today()
                for lv in result.splitlines():
                    lv = lv.strip()
                    if re.search("^mws-snapshot-[0-9]{4}-[0-9]{2}-[0-9]{2}$",
                                 lv):
                        lvdate = datetime.strptime(
                            lv.replace("mws-snapshot-", ""),
                            '%Y-%m-%d').date()
                        if lvdate < first_date:
                            first_date = lvdate
                    elif re.search("^mws-snapshot-.+", lv):
                        lvlist.append(lv.replace("mws-snapshot-", ""))
                # Store the first date of an available backup in the database to be used by the front end
                backup_first_date = get_object_or_None(AnsibleConfiguration,
                                                       service=vm.service,
                                                       key="backup_first_date")
                if backup_first_date:
                    backup_first_date.value = first_date.isoformat()
                    backup_first_date.save()
                else:
                    AnsibleConfiguration.objects.create(
                        service=vm.service,
                        key="backup_first_date",
                        value=first_date.isoformat())
                # Delete entries in the DB related to backups no longer present on the client.
                for lv in vm.service.snapshots.all():
                    if lv.name not in lvlist:
                        lv.delete()
                LOGGER.info("VM %s LV list updated: %s" % (vm.name, lvlist))
                return HttpResponse(ip + ' %s' % vm.name)
    return HttpResponseNotFound()
Esempio n. 2
0
 def valid_fromdate(self):
     start_date = get_object_or_None(AnsibleConfiguration,
                                     service=self.service,
                                     key="backup_first_date")
     if start_date:
         start_date = datetime.datetime.strptime(start_date.value,
                                                 '%Y-%m-%d').date()
     else:
         if self.site.exmws2:
             start_date = self.site.exmws2 + datetime.timedelta(days=1)
         else:
             start_date = self.site.start_date + datetime.timedelta(days=1)
     if start_date < datetime.date.today() - datetime.timedelta(days=30):
         return datetime.date.today() - datetime.timedelta(days=30)
     else:
         return start_date
Esempio n. 3
0
 def test_get_object_or_none(self):
     self.assertIsNone(get_object_or_None(User, username="******"))
     User.objects.create_user(username="******")
     self.assertIsNotNone(get_object_or_None(User, username="******"))
Esempio n. 4
0
 def operating_system(self):
     from apimws.models import AnsibleConfiguration
     ansible_configuraton = get_object_or_None(AnsibleConfiguration,
                                               service=self,
                                               key="os")
     return ansible_configuraton.value if ansible_configuraton else None