Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

""" 

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 rest_framework.response import Response 

from django.db import transaction 

from django.conf import settings 

from storageadmin.models import (Snapshot, Share, NFSExport, 

                                 NFSExportGroup, AdvancedNFSExport) 

from fs.btrfs import (add_snap, share_id, volume_usage, remove_snap, 

                      umount_root, mount_snap, qgroup_assign) 

from system.osi import refresh_nfs_exports 

from storageadmin.serializers import SnapshotSerializer 

from storageadmin.util import handle_exception 

import rest_framework_custom as rfc 

from share_helpers import toggle_sftp_visibility 

from clone_helpers import create_clone, create_repclone 

from nfs_exports import NFSExportMixin 

 

import logging 

logger = logging.getLogger(__name__) 

 

 

class SnapshotView(NFSExportMixin, rfc.GenericView): 

    serializer_class = SnapshotSerializer 

 

    def get_queryset(self, *args, **kwargs): 

        with self._handle_exception(self.request): 

            try: 

                share = Share.objects.get(id=self.kwargs['sid']) 

            except: 

                if ('sid' not in self.kwargs): 

                    return Snapshot.objects.filter().order_by('-id') 

 

                e_msg = ('Share with id: {} does not exist'.format( 

                    self.kwargs['sid'])) 

                handle_exception(Exception(e_msg), self.request) 

 

            if ('snap_name' in self.kwargs): 

                self.paginate_by = 0 

                try: 

                    return Snapshot.objects.get(share=share, 

                                                name=self.kwargs['snap_name']) 

                except: 

                    return [] 

 

            snap_type = self.request.query_params.get('snap_type', None) 

            if (snap_type is not None and snap_type != ''): 

                return Snapshot.objects.filter( 

                    share=share, snap_type=snap_type).order_by('-id') 

 

            return Snapshot.objects.filter(share=share).order_by('-id') 

 

    @transaction.atomic 

    def _toggle_visibility(self, share, snap_name, on=True): 

        cur_exports = list(NFSExport.objects.all()) 

        snap_mnt_pt = ('%s%s/.%s' % (settings.MNT_PT, share.name, snap_name)) 

        export_pt = snap_mnt_pt.replace(settings.MNT_PT, 

                                        settings.NFS_EXPORT_ROOT) 

        if (on): 

            mount_snap(share, snap_name) 

 

            if (NFSExport.objects.filter(share=share).exists()): 

                se = NFSExport.objects.filter(share=share)[0] 

                export_group = NFSExportGroup( 

                    host_str=se.export_group.host_str, nohide=True) 

                export_group.save() 

                export = NFSExport(share=share, export_group=export_group, 

                                   mount=export_pt) 

                export.full_clean() 

                export.save() 

                cur_exports.append(export) 

        else: 

            for mnt in (snap_mnt_pt, export_pt): 

                try: 

                    export = NFSExport.objects.get(share=share, mount=mnt) 

                    cur_exports.remove(export) 

                    export.export_group.delete() 

                    export.delete() 

                except NFSExport.DoesNotExist: 

                    pass 

                except Exception as e: 

                    logger.exception(e) 

                finally: 

                    umount_root(export_pt) 

                    umount_root(snap_mnt_pt) 

        exports = self.create_nfs_export_input(cur_exports) 

        adv_entries = [x.export_str for x in AdvancedNFSExport.objects.all()] 

        exports_d = self.create_adv_nfs_export_input(adv_entries, self.request) 

        exports.update(exports_d) 

        refresh_nfs_exports(exports) 

 

    @transaction.atomic 

    def _create(self, share, snap_name, request, uvisible, 

                snap_type, writable): 

        if (Snapshot.objects.filter(share=share, name=snap_name).exists()): 

            e_msg = ('Snapshot(%s) already exists for the Share(%s).' % 

                     (snap_name, share.name)) 

            handle_exception(Exception(e_msg), request) 

 

        snap_size = 0 

        qgroup_id = '0/na' 

        if (snap_type == 'replication'): 

            writable = False 

        add_snap(share.pool, share.subvol_name, snap_name, writable) 

        snap_id = share_id(share.pool, snap_name) 

        qgroup_id = ('0/%s' % snap_id) 

        if share.pqgroup is not settings.MODEL_DEFS['pqgroup']: 

            pool_mnt_pt = '{}{}'.format(settings.MNT_PT, share.pool.name) 

            qgroup_assign(qgroup_id, share.pqgroup, pool_mnt_pt) 

        snap_size, eusage = volume_usage(share.pool, qgroup_id) 

        s = Snapshot(share=share, name=snap_name, real_name=snap_name, 

                     size=snap_size, qgroup=qgroup_id, 

                     uvisible=uvisible, snap_type=snap_type, 

                     writable=writable) 

        s.save() 

        return Response(SnapshotSerializer(s).data) 

 

    def post(self, request, sid, snap_name, command=None): 

        with self._handle_exception(request): 

            share = self._validate_share(sid, request) 

            uvisible = request.data.get('uvisible', False) 

            if (type(uvisible) != bool): 

                e_msg = ('uvisible must be a boolean, not %s' % type(uvisible)) 

                handle_exception(Exception(e_msg), request) 

 

            snap_type = request.data.get('snap_type', 'admin') 

            writable = request.data.get('writable', False) 

            if (type(writable) != bool): 

                e_msg = ('writable must be a boolean, not %s' % type(writable)) 

                handle_exception(Exception(e_msg), request) 

            if (command is None): 

                ret = self._create(share, snap_name, request, 

                                   uvisible=uvisible, snap_type=snap_type, 

                                   writable=writable) 

 

                if (uvisible): 

                    try: 

                        self._toggle_visibility(share, ret.data['real_name']) 

                    except Exception as e: 

                        msg = ('Failed to make the Snapshot(%s) visible. ' 

                               'Exception: %s' % (snap_name, e.__str__())) 

                        logger.error(msg) 

                        logger.exception(e) 

 

                    try: 

                        toggle_sftp_visibility(share, ret.data['real_name']) 

                    except Exception as e: 

                        msg = ('Failed to make the Snapshot(%s) visible for ' 

                               'SFTP. Exception: %s' % 

                               (snap_name, e.__str__())) 

                        logger.error(msg) 

                        logger.exception(e) 

 

                return ret 

            if (command == 'clone'): 

                new_name = request.data.get('name', None) 

                snapshot = Snapshot.objects.get(share=share, name=snap_name) 

                return create_clone(share, new_name, request, logger, 

                                    snapshot=snapshot) 

            if (command == 'repclone'): 

                # When repclone is first called the oldest snapshot is 

                # actually a share. This is an artifact of import_shares() 

                # identifying the first snapshot incorrectly as a share. 

                # TODO: As a temporary work around for this, and to avoid 

                # TODO: modifying import_shares / import_snapshots for what is 

                # TODO: currently stable behaviour we simply try for a share 

                # TODO: if no snapshot is found, ie failing over to accommodate 

                # TODO: for the currently quirky behaviour of the imports. 

                # TODO: Note: this snap as share quirk is temporary as once a 

                # TODO: replication cycle has completed it is removed. 

                try: 

                    snapshot = Snapshot.objects.get(share=share, 

                                                    name=snap_name) 

                except Snapshot.DoesNotExist: 

                    # Here we rely on the polymorphism of Share/Snap and that 

                    # this quirky share is located as per regular snapshots. 

                    # The subvol of our snap-as-share-quirk is as per a snap: 

                    # ".snapshots/sharename/snapname" 

                    quirk_snap_share = '.snapshots/{}/{}'.format(share.name, 

                                                                 snap_name) 

                    logger.debug('Fail through for snap-as-share-quirk') 

                    snapshot = Share.objects.get(subvol_name=quirk_snap_share) 

                return create_repclone(share, request, logger, 

                                       snapshot=snapshot) 

            e_msg = ('Unknown command: %s' % command) 

            handle_exception(Exception(e_msg), request) 

 

    @staticmethod 

    def _validate_share(sid, request): 

        try: 

            return Share.objects.get(id=sid) 

        except: 

            e_msg = ('Share with id: {} does not exist'.format(sid)) 

            handle_exception(Exception(e_msg), request) 

 

    @transaction.atomic 

    def _delete_snapshot(self, request, sid, id=None, snap_name=None): 

        share = self._validate_share(sid, request) 

        try: 

            snapshot = None 

            if (id is not None): 

                snapshot = Snapshot.objects.get(id=id) 

            elif (snap_name is not None): 

                snapshot = Snapshot.objects.get(share=share, name=snap_name) 

            else: 

                return True 

        except: 

            e_msg = '' 

            if (id is not None): 

                e_msg = ('Snapshot(%s) does not exist.' % id) 

            else: 

                e_msg = ('Snapshot(%s) does not exist.' % snap_name) 

            handle_exception(Exception(e_msg), request) 

 

        if (snapshot.uvisible): 

            self._toggle_visibility(share, snapshot.real_name, on=False) 

            toggle_sftp_visibility(share, snapshot.real_name, on=False) 

 

        remove_snap(share.pool, share.name, snapshot.name) 

        snapshot.delete() 

        return Response() 

 

    def delete(self, request, sid, snap_name=None): 

        """ 

        deletes a snapshot 

        """ 

        with self._handle_exception(request): 

            if (snap_name is None): 

                snap_qp = self.request.query_params.get('id', None) 

                if (snap_qp is not None): 

                    for si in snap_qp.split(','): 

                        self._delete_snapshot(request, sid, id=si) 

            else: 

                self._delete_snapshot(request, sid, snap_name=snap_name) 

            return Response()