File: iscsi_target.python

package info (click to toggle)
pcp 7.0.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,916 kB
  • sloc: ansic: 1,478,844; sh: 177,285; xml: 160,462; cpp: 83,809; python: 24,349; perl: 18,303; yacc: 6,877; lex: 2,864; makefile: 2,694; awk: 165; fortran: 60; java: 52
file content (101 lines) | stat: -rwxr-xr-x 2,978 bytes parent folder | download
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
#!/usr/bin/env pmpython


import sys
import rtslib_fb
import json


class ISCSITarget(object):

    """
    Class used to define an iSCSI target using an json file to govern the
    target name, clients and luns defined.
    """

    def __init__(self):

        self.lio = rtslib_fb.root.RTSRoot()
        self.lun_list = []

        try:
            with open('lio/iscsi_conf.json') as conf:
                config = json.load(conf)
        except ValueError:
            print("invalid JSON")
            sys.exit(4)
        except IOError:
            print("Missing config file")
            sys.exit(4)

        self._create_target(config['target'])
        for lun in config["luns"]:
            self._add_lun(name=lun['name'])

        for client in config["clients"]:
            self._add_client(client_iqn=client["client_iqn"])

    def _create_target(self, iqn):
        iscsi = rtslib_fb.fabric.ISCSIFabricModule()
        self.iqn = iqn
        self.target = rtslib_fb.fabric.Target(iscsi, wwn=self.iqn)
        self.tpg = rtslib_fb.target.TPG(self.target)
        self.portal = rtslib_fb.target.NetworkPortal(self.tpg, '0.0.0.0')
        self.tpg.enable = True

    def _add_lun(self, name='ramdisk', disk_type='ramdisk', size=10485760):

        if disk_type == 'ramdisk':

            try:
                # create the disk (storage object)
                so = rtslib_fb.tcm.RDMCPStorageObject(name, size=size)

                # map to the iscsi tpg
                next_lun = len([disk for disk in self.lio.storage_objects]) - 1
                rtslib_fb.target.LUN(self.tpg, lun=next_lun,
                                     storage_object=so)
                self.lun_list.append(name)

            except rtslib_fb.RTSLibError:
                print("Failed to create LUN {}".format(name))
                sys.exit(4)

        else:
            raise ValueError("LUN type of {} has not been implemented")

    def _add_client(self, client_iqn=None):

        try:
            rtslib_fb.target.NodeACL(self.tpg, client_iqn)
        except rtslib_fb.RTSLibError:
            print("failed to create client {}".format(client_iqn))
            sys.exit(4)

    def _client_count(self):
        return len([client for client in self.lio.node_acls])

    def _lun_count(self):
        return len([lun for lun in self.lio.luns])

    def _tpg_count(self):
        return len([tpg for tpg in self.target.tpgs])

    def drop_test_config(self):

        # delete the test iscsi target definition
        # removing target/tpg/nodeacls/mapped luns
        self.target.delete()

        # delete disks created for the test
        for so in self.lio.storage_objects:
            so.delete()

    client_count = property(_client_count,
                            doc="get client count")

    lun_count = property(_lun_count,
                         doc='get number of luns')

    tpg_count = property(_tpg_count,
                         doc='get number of tpgs')