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

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

#!/usr/bin/env python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# This program 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 Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# DummySR: an example dummy SR for the SDK 

 

import SR, VDI, SRCommand, util 

import time 

import xs_errors 

 

CAPABILITIES = ["SR_PROBE","VDI_CREATE","VDI_DELETE","VDI_ATTACH","VDI_DETACH", 

                "VDI_ACTIVATE","VDI_DEACTIVATE","VDI_CLONE","VDI_SNAPSHOT","VDI_RESIZE", 

                "VDI_INTRODUCE","VDI_MIRROR"] 

 

CONFIGURATION = [ ] 

 

DRIVER_INFO = { 

    'name': 'dummy', 

    'description': 'SR plugin which manages fake data', 

    'vendor': 'Citrix Systems Inc', 

    'copyright': '(C) 2008 Citrix Systems Inc', 

    'driver_version': '1.0', 

    'required_api_version': '1.1', 

    'capabilities': CAPABILITIES, 

    'configuration': CONFIGURATION 

    } 

 

TYPE = 'dummy' 

 

class DummySR(SR.SR): 

    """dummy storage repository""" 

    def handles(type): 

        if type == TYPE: 

            return True 

        return False 

    handles = staticmethod(handles) 

 

    def load(self, sr_uuid): 

        self.sr_vditype = 'phy' 

 

    def content_type(self, sr_uuid): 

        return super(DummySR, self).content_type(sr_uuid) 

 

    def create(self, sr_uuid, size): 

        self._assertValues(['sr_uuid','args','host_ref','session_ref','device_config','command','sr_ref']) 

        assert(len(self.srcmd.params['args'])==1) 

 

    def delete(self, sr_uuid): 

        self._assertValues(['sr_uuid','args','host_ref','session_ref','device_config','command','sr_ref']) 

        assert(len(self.srcmd.params['args'])==0) 

 

    def attach(self, sr_uuid): 

        self._assertValues(['sr_uuid','args','host_ref','session_ref','device_config','command','sr_ref']) 

        assert(len(self.srcmd.params['args'])==0) 

 

    def detach(self, sr_uuid): 

        self._assertValues(['sr_uuid','args','host_ref','session_ref','device_config','command','sr_ref']) 

        assert(len(self.srcmd.params['args'])==0) 

 

    def probe(self): 

        # N.B. There are no SR references 

        self._assertValues(['args','host_ref','session_ref','device_config','command']) 

        assert(len(self.srcmd.params['args'])==0) 

 

        # Create some Dummy SR records 

        entry = {} 

        entry['size'] = 1024 

        SRlist = {} 

        SRlist[util.gen_uuid()] = entry 

 

        # Return the Probe XML 

        return util.SRtoXML(SRlist) 

 

    def vdi(self, uuid): 

        return DummyVDI(self, uuid) 

 

    def scan(self, sr_uuid): 

        self._assertValues(['sr_uuid','args','host_ref','session_ref','device_config','command','sr_ref']) 

        assert(len(self.srcmd.params['args'])==0) 

 

        # The list of VDIs comes from the XenAPI - we have no state 

        for v in self._getallVDIrecords(): 

            x = DummyVDI(self, v['uuid']) 

            x.size = v['virtual_size'] 

            x.utilisation = v['physical_utilisation'] 

            self.vdis[x.uuid] = x 

 

        self.physical_size = 2000000000000L 

        self.physical_utilisation = 0L 

        self.virtual_allocation = 0L 

        return super(DummySR, self).scan(sr_uuid) 

 

    def _assertValues(self, vals): 

        for attr in vals: 

            assert(self.srcmd.params.has_key(attr)) 

            util.SMlog("%s param %s: [%s]" % (self.cmd,attr,self.srcmd.params[attr])) 

 

        # Iterate through the device_config dictionary 

        for key in self.dconf.iterkeys(): 

            util.SMlog("\tdevice_config: [%s:%s]" % (key,self.dconf[key])) 

 

        # Query the sm_config; parameters can be set at Create time. Iterate through keys 

        self.sm_config = self.session.xenapi.SR.get_sm_config(self.sr_ref) 

        for key in self.sm_config.iterkeys(): 

            util.SMlog("\tsm_config: [%s:%s]" % (key,self.sm_config[key])) 

 

    def _getallVDIrecords(self): 

        """Helper function which returns a list of all VDI records for this SR 

        stored in the XenAPI server""" 

        # Returns a list of (reference, record) pairs: we only need the records 

        vdis = self.session.VDI.get_all_records(self.session_ref)['Value'].values() 

        # We only need the VDIs corresponding to this SR 

        return filter(lambda v: v['SR'] == self.sr_ref, vdis) 

 

 

class DummyVDI(VDI.VDI): 

    def load(self, vdi_uuid): 

        self.path = "/dev/null" # returned on attach 

        self.uuid = vdi_uuid 

        self.size = 0 

        self.utilisation = 0 

        self.location = vdi_uuid 

        self.sm_config = {} 

 

    def create(self, sr_uuid, vdi_uuid, size): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_sm_config']) 

        assert(len(self.sr.srcmd.params['args']) == 8) 

 

        self.vdi_sm_config = self.sr.srcmd.params['vdi_sm_config'] 

        for key in self.vdi_sm_config.iterkeys(): 

            util.SMlog("\tvdi_sm_config: [%s:%s]" % (key,self.vdi_sm_config[key])) 

 

        for v in self.sr._getallVDIrecords(): 

            if v['uuid'] == vdi_uuid: 

                raise xs_errors.XenError('VDIExists') 

 

        self.size = size 

        self.utilisation = size 

        self.sm_config['samplekey'] = "This is a dummy SR VDI" 

        self._db_introduce() 

        self.run_corner_cases_tests() 

        return self.get_params() 

 

    def delete(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        assert(len(self.sr.srcmd.params['args'])==0) 

 

        # Assert that the VDI record exists 

        self.session.VDI.get_record(self.sr.session_ref) 

        self.run_corner_cases_tests() 

        self._db_forget() 

 

    def introduce(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_sm_config','new_uuid']) 

        assert(len(self.sr.srcmd.params['args'])==0) 

        self.vdi_sm_config = self.sr.srcmd.params['vdi_sm_config'] 

        for key in self.vdi_sm_config.iterkeys(): 

            util.SMlog("\tvdi_sm_config: [%s:%s]" % (key,self.vdi_sm_config[key])) 

 

        for v in self.sr._getallVDIrecords(): 

            if v['uuid'] == vdi_uuid: 

                raise xs_errors.XenError('VDIExists') 

        self.uuid = vdi_uuid 

        self.location = self.sr.srcmd.params['vdi_location'] 

        self._db_introduce() 

        self.run_corner_cases_tests() 

        return  super(DummyVDI, self).get_params() 

 

    def attach(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        assert(len(self.sr.srcmd.params['args'])==1) 

        vdi = super(DummyVDI, self).attach(sr_uuid, vdi_uuid) 

        self.run_corner_cases_tests() 

        return vdi 

 

    def detach(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        self.run_corner_cases_tests() 

        assert(len(self.sr.srcmd.params['args'])==0) 

 

    def activate(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        assert(len(self.sr.srcmd.params['args'])==1) 

        self.vdi_ref = self.sr.srcmd.params['vdi_ref'] 

        self.other_config = self.session.xenapi.VDI.get_other_config(self.vdi_ref) 

        self.run_corner_cases_tests() 

        for key in self.other_config.iterkeys(): 

            util.SMlog("\tvdi_other_config: [%s:%s]" % (key,self.other_config[key])) 

 

    def deactivate(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        self.run_corner_cases_tests() 

        assert(len(self.sr.srcmd.params['args'])==0) 

 

    def resize(self, sr_uuid, vdi_uuid, size): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref','vdi_ref','vdi_location','vdi_uuid']) 

        assert(len(self.sr.srcmd.params['args'])==1) 

 

        self.size = size 

        self.utilisation = size 

        self._db_update() 

        self.run_corner_cases_tests() 

        return super(DummyVDI, self).get_params() 

 

    def snapshot(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref']) 

        assert(len(self.sr.srcmd.params['args'])==0) 

 

        dest = util.gen_uuid() 

        vdi = VDI.VDI(self.sr, dest) 

        vdi.read_only = True 

        vdi.location = dest 

        vdi.size = 0 

        vdi.utilisation = 0 

        vdi._db_introduce() 

        self.run_corner_cases_tests() 

        return vdi.get_params() 

 

    def clone(self, sr_uuid, vdi_uuid): 

        self.sr._assertValues(['sr_uuid','args','host_ref','device_config','command','sr_ref']) 

        assert(len(self.sr.srcmd.params['args'])==0) 

 

        dest = util.gen_uuid() 

        vdi = VDI.VDI(self.sr, dest) 

        vdi.read_only = False 

        vdi.location = dest 

        vdi.size = 0 

        vdi.utilisation = 0 

        vdi._db_introduce() 

        self.run_corner_cases_tests() 

        return vdi.get_params() 

 

    def check_no_other_vdi_operation_in_progress(self): 

        vdis = util.list_VDI_records_in_sr(self.sr) 

        vdi_ref = self.session.xenapi.VDI.get_by_uuid(self.uuid) 

        del vdis[vdi_ref] 

        active_vdis = filter(lambda v: v['current_operations'] != {}, vdis.values()) 

        if len(active_vdis) != 0: 

            msg = "LVHDRT: found other operations in progress for VDI: %s" % active_vdis[0]['uuid'] 

            util.SMlog(msg) 

            raise xs_errors.XenError('OtherVDIOperationInProgress') 

 

    def get_attached_vbds(self): 

        vdi_ref = self.session.xenapi.VDI.get_by_uuid(self.uuid) 

        vbds = self.session.xenapi.VBD.get_all_records_where("field \"VDI\" = \"%s\"" % vdi_ref) 

        return filter(lambda v: v['currently_attached'] == "true", vbds.values()) 

 

    def check_vbd_list_is_stable(self, attached_vbds): 

        newly_attached_vbds = self.get_attached_vbds() 

        old_set = set(attached_vbds) 

        new_set = set(newly_attached_vbds) 

        diff_set = old_set.difference(new_set) | new_set.difference(old_set) 

        if len(diff_set) != 0: 

            msg = "LVHDRT: found a non-stable VBD: %s" % (diff_set.pop()) 

            util.SMlog(msg) 

            raise xs_errors.XenError('VBDListNotStable') 

 

    def run_corner_cases_tests(self): 

 

        def fn(): 

            attached_vbds = self.get_attached_vbds() 

            for i in range(0,10): 

                time.sleep(2) 

                self.check_no_other_vdi_operation_in_progress() 

                self.check_vbd_list_is_stable(attached_vbds) 

 

        util.fistpoint.activate_custom_fn("LVHDRT_xapiSM_serialization_tests", fn) 

 

283if __name__ == '__main__': 

    SRCommand.run(DummySR, DRIVER_INFO) 

else: 

    SR.registerSR(DummySR)