forked from rockstor/rockstor-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.py
122 lines (103 loc) · 4.18 KB
/
share.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Copyright (c) 2012-2017 RockStor, Inc. <http://rockstor.com>
This file is part of RockStor.
RockStor is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
RockStor is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from django.conf import settings
from django.db import models
from django.db.models.signals import (post_save, post_delete)
from django.dispatch import receiver
from fs.btrfs import qgroup_exists
from storageadmin.models import Pool
from system.osi import mount_status
from .netatalk_share import NetatalkShare
from system.services import (systemctl, refresh_afp_config)
RETURN_BOOLEAN = True
class Share(models.Model):
"pool that this share is part of"""
pool = models.ForeignKey(Pool)
"""auto created 0/x qgroup"""
qgroup = models.CharField(max_length=100)
"""quota group y/x explicitly created for this Share"""
pqgroup = models.CharField(max_length=32,
default=settings.MODEL_DEFS['pqgroup'])
"""name of the share, kind of like id"""
name = models.CharField(max_length=4096, unique=True)
"""id of the share. numeric in case of btrfs"""
uuid = models.CharField(max_length=100, null=True)
"""total size in KB"""
size = models.BigIntegerField(default=0)
owner = models.CharField(max_length=4096, default='root')
group = models.CharField(max_length=4096, default='root')
perms = models.CharField(max_length=9, default='755')
toc = models.DateTimeField(auto_now=True)
subvol_name = models.CharField(max_length=4096)
replica = models.BooleanField(default=False)
compression_algo = models.CharField(max_length=1024, null=True)
# rusage and eusage reports original 0/x qgroup size
# and this has only current share content without snapshots
rusage = models.BigIntegerField(default=0)
eusage = models.BigIntegerField(default=0)
# Having Rockstor vol/subvols overriding btrfs standards
# with snapshots(subvols) not under their vols, we use qgroup sizes
# to report correct real vol sizes
pqgroup_rusage = models.BigIntegerField(default=0)
pqgroup_eusage = models.BigIntegerField(default=0)
@property
def size_gb(self):
return self.size / (1024.0 * 1024.0)
@property
def mount_status(self, *args, **kwargs):
# Presents raw string of active mount options
try:
return mount_status('%s%s' % (settings.MNT_PT, self.name))
except:
return None
@property
def is_mounted(self, *args, **kwargs):
# Calls mount_status in return boolean mode.
try:
return mount_status('%s%s' % (settings.MNT_PT, self.name),
RETURN_BOOLEAN)
except:
return False
@property
def pqgroup_exist(self, *args, **kwargs):
# Returns boolean status of pqgroup existence
try:
if str(self.pqgroup) == '-1/-1':
return False
else:
return qgroup_exists(
'%s%s' % (settings.MNT_PT, self.pool.name),
'%s' % self.pqgroup)
except:
return False
class Meta:
app_label = 'storageadmin'
def refresh_afp():
refresh_afp_config(list(NetatalkShare.objects.all()))
systemctl('netatalk', 'reload-or-restart')
@receiver(post_save, sender=Share, dispatch_uid='update_afp')
def update_afp(sender, instance, **kwargs):
try:
# throws exception and short circuits if netatalkshare doesn't exist.
instance.netatalkshare
refresh_afp()
except Exception:
pass
@receiver(post_delete, sender=Share, dispatch_uid='clean_afp')
def clean_afp(sender, instance, **kwargs):
try:
refresh_afp()
except Exception:
pass