1 6
import logging
2

3 6
from django.db import models
4 6
from cyborgbackup.api.versioning import reverse
5 6
from cyborgbackup.main.models.base import PrimordialModel
6 6
from cyborgbackup.main.utils.common import copy_model_by_class, copy_m2m_relationships
7

8 6
logger = logging.getLogger('cyborgbackup.models.client')
9

10 6
__all__ = ['Client']
11

12

13 6
class Client(PrimordialModel):
14 6
    hostname = models.CharField(
15
        max_length=1024
16
    )
17

18 6
    enabled = models.BooleanField(
19
        default=True
20
    )
21

22 6
    ip = models.TextField(
23
        blank=True,
24
        default=''
25
    )
26

27 6
    version = models.CharField(
28
        max_length=50,
29
        blank=True,
30
        default=''
31
    )
32

33 6
    ready = models.BooleanField(
34
        default=False
35
    )
36

37 6
    hypervisor_ready = models.BooleanField(
38
        default=False
39
    )
40

41 6
    hypervisor_name = models.CharField(
42
        max_length=1024,
43
        blank=True,
44
        default=''
45
    )
46

47 6
    latest_prepare = models.DateTimeField(
48
        null=True,
49
        default=None,
50
        editable=False
51
    )
52

53 6
    bandwidth_limit = models.PositiveIntegerField(
54
        null=True,
55
        default=None,
56
        blank=True
57
    )
58

59 6
    def get_absolute_url(self, request=None):
60 6
        return reverse('api:client_detail', kwargs={'pk': self.pk}, request=request)
61

62 6
    def get_ui_url(self):
63 0
        return "/#/clients/{}".format(self.pk)
64

65 6
    @classmethod
66
    def get_cache_key(self, key):
67 0
        return key
68

69 6
    @classmethod
70
    def get_cache_id_key(self, key):
71 0
        return '{}_ID'.format(key)
72

73 6
    @classmethod
74
    def _get_job_class(cls):
75 0
        from cyborgbackup.main.models.jobs import Job
76 0
        return Job
77

78 6
    def create_prepare_client(self, **kwargs):
79
        '''
80
        Create a new prepare job for this client.
81
        '''
82 0
        eager_fields = kwargs.pop('_eager_fields', None)
83

84 0
        job_class = self._get_job_class()
85 0
        fields = ('extra_vars', 'job_type')
86 0
        unallowed_fields = set(kwargs.keys()) - set(fields)
87 0
        if unallowed_fields:
88 0
            logger.warn('Fields {} are not allowed as overrides.'.format(unallowed_fields))
89 0
            map(kwargs.pop, unallowed_fields)
90

91 0
        job = copy_model_by_class(self, job_class, fields, kwargs)
92

93 0
        if eager_fields:
94 0
            for fd, val in eager_fields.items():
95 0
                setattr(job, fd, val)
96

97
        # Set the job back-link on the job
98 0
        job.client_id = self.pk
99 0
        job.name = "Prepare Client {}".format(self.hostname)
100 0
        job.description = "Client {} Borg Preparation".format(self.hostname)
101 0
        job.save()
102

103 0
        from cyborgbackup.main.signals import disable_activity_stream
104 0
        fields = ()
105 0
        with disable_activity_stream():
106 0
            copy_m2m_relationships(self, job, fields, kwargs=kwargs)
107

108 0
        return job
109

110 6
    def create_prepare_hypervisor(self, **kwargs):
111
        '''
112
        Create a new prepare job for the hypervisor of this client.
113
        '''
114 0
        eager_fields = kwargs.pop('_eager_fields', None)
115

116 0
        job_class = self._get_job_class()
117 0
        fields = ('extra_vars', 'job_type')
118 0
        unallowed_fields = set(kwargs.keys()) - set(fields)
119 0
        if unallowed_fields:
120 0
            logger.warn('Fields {} are not allowed as overrides.'.format(unallowed_fields))
121 0
            map(kwargs.pop, unallowed_fields)
122

123 0
        job = copy_model_by_class(self, job_class, fields, kwargs)
124

125 0
        if eager_fields:
126 0
            for fd, val in eager_fields.items():
127 0
                setattr(job, fd, val)
128

129
        # Set the job back-link on the job
130 0
        job.client_id = self.pk
131 0
        job.name = "Prepare Hypervisor of {}".format(self.hostname)
132 0
        job.description = "Hypervisor of {} Borg Preparation".format(self.hostname)
133 0
        job.save()
134

135 0
        from cyborgbackup.main.signals import disable_activity_stream
136 0
        fields = ()
137 0
        with disable_activity_stream():
138 0
            copy_m2m_relationships(self, job, fields, kwargs=kwargs)
139

140 0
        return job

Read our documentation on viewing source code .

Loading