def test_volume(self):
        # Create cluster
        c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY)
        self.assertEqual(True, c != '')

        cluster = c.cluster_create()
        self.assertEqual(True, cluster['id'] != '')

        # Create node request packet
        for i in range(4):
            node_req = {}
            node_req['cluster'] = cluster['id']
            node_req['hostnames'] = {
                "manage": ["node%s-manage.gluster.lab.com" % (i)],
                "storage": ["node%s-storage.gluster.lab.com" % (i)]
            }
            node_req['zone'] = i + 1

            # Create node
            node = c.node_add(node_req)
            self.assertEqual(True, node['id'] != '')

            # Create and add devices
            for i in range(1, 20):
                device_req = {}
                device_req['name'] = "sda%s" % (i)
                device_req['weight'] = 100
                device_req['node'] = node['id']

                device = c.device_add(device_req)
                self.assertEqual(True, device)

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(True, len(list['volumes']) == 0)

        # Create a volume
        volume_req = {}
        volume_req['size'] = 10
        volume = c.volume_create(volume_req)
        self.assertEqual(True, volume['id'] != "")
        self.assertEqual(True, volume['size'] == volume_req['size'])

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(True, len(list['volumes']) == 1)
        self.assertEqual(True, list['volumes'][0] == volume['id'])

        # Get info on incorrect id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_info("badid")

        # Get info
        info = c.volume_info(volume['id'])
        self.assertEqual(True, info == volume)

        # Expand volume with a bad id
        volume_ex_params = {}
        volume_ex_params['expand_size'] = 10

        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_expand("badid", volume_ex_params)

        # Expand volume
        volumeInfo = c.volume_expand(volume['id'], volume_ex_params)
        self.assertEqual(True, volumeInfo['size'] == 20)

        # Delete bad id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_delete("badid")

        # Delete volume
        volume_delete = c.volume_delete(volume['id'])
        self.assertEqual(True, volume_delete)

        clusterInfo = c.cluster_info(cluster['id'])
        for node_id in clusterInfo['nodes']:
            #Get node information
            nodeInfo = c.node_info(node_id)

            # Delete all devices
            for device in nodeInfo['devices']:
                device_delete = c.device_delete(device['id'])
                self.assertEqual(True, device_delete)

            #Delete node
            node_delete = c.node_delete(node_id)
            self.assertEqual(True, node_delete)

        # Delete cluster
        cluster_delete = c.cluster_delete(cluster['id'])
        self.assertEqual(True, cluster_delete)
Beispiel #2
0
    def test_volume(self):
        # Create cluster
        c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY)
        self.assertEqual(True, c != '')

        cluster = c.cluster_create()
        self.assertEqual(True, cluster['id'] != '')

        # Create node request packet
        print "Creating Cluster"
        for i in range(3):
            node_req = {}
            node_req['cluster'] = cluster['id']
            node_req['hostnames'] = {
                "manage": ["node%s-manage.gluster.lab.com" % (i)],
                "storage": ["node%s-storage.gluster.lab.com" % (i)]}
            node_req['zone'] = i + 1

            # Create node
            node = c.node_add(node_req)
            self.assertEqual(True, node['id'] != '')

            # Create and add devices
            for i in range(1, 4):
                device_req = {}
                device_req['name'] = "sda%s" % (i)
                device_req['node'] = node['id']

                device = c.device_add(device_req)
                self.assertEqual(True, device)

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(True, len(list['volumes']) == 0)

        # Create a volume
        print "Creating a volume"
        volume_req = {}
        volume_req['size'] = 10
        volume = c.volume_create(volume_req)
        self.assertEqual(True, volume['id'] != "")
        self.assertEqual(True, volume['size'] == volume_req['size'])

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(True, len(list['volumes']) == 1)
        self.assertEqual(True, list['volumes'][0] == volume['id'])

        # Get info on incorrect id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_info("badid")

        # Get info
        info = c.volume_info(volume['id'])
        self.assertEqual(True, info == volume)

        # Expand volume with a bad id
        volume_ex_params = {}
        volume_ex_params['expand_size'] = 10

        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_expand("badid", volume_ex_params)

        # Expand volume
        print "Expanding volume"
        volumeInfo = c.volume_expand(volume['id'], volume_ex_params)
        self.assertEqual(True, volumeInfo['size'] == 20)

        # Delete bad id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_delete("badid")

        # Delete volume
        print "Deleting volume"
        volume_delete = c.volume_delete(volume['id'])
        self.assertEqual(True, volume_delete)

        print "Deleting Cluster"
        clusterInfo = c.cluster_info(cluster['id'])
        for node_id in clusterInfo['nodes']:
            # Get node information
            nodeInfo = c.node_info(node_id)

            # Delete all devices
            for device in nodeInfo['devices']:
                device_delete = c.device_delete(device['id'])
                self.assertEqual(True, device_delete)

            # Delete node
            node_delete = c.node_delete(node_id)
            self.assertEqual(True, node_delete)

        # Delete cluster
        cluster_delete = c.cluster_delete(cluster['id'])
        self.assertEqual(True, cluster_delete)
Beispiel #3
0
    def test_volume(self):
        # Create cluster
        c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY,
                         poll_delay=TEST_POLL_DELAY)
        self.assertEqual(True, c != '')

        cluster_req = {}
        cluster_req['block'] = True
        cluster_req['file'] = True
        cluster = c.cluster_create(cluster_req)
        self.assertNotEqual(cluster['id'], '')

        # Create node request packet
        print ("Creating Cluster")
        for i in range(3):
            node_req = {}
            node_req['cluster'] = cluster['id']
            node_req['hostnames'] = {
                "manage": ["node%s-manage.gluster.lab.com" % (i)],
                "storage": ["node%s-storage.gluster.lab.com" % (i)]}
            node_req['zone'] = i + 1

            # Create node
            node = c.node_add(node_req)
            self.assertNotEqual(node['id'], '')

            # Create and add devices
            for i in range(1, 4):
                device_req = {}
                device_req['name'] = "/dev/sda%s" % (i)
                device_req['node'] = node['id']

                device = c.device_add(device_req)
                self.assertTrue(device)

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(len(list['volumes']), 0)

        # Create a volume
        print ("Creating a volume")
        volume_req = {}
        volume_req['size'] = 10
        volume = c.volume_create(volume_req)
        self.assertNotEqual(volume['id'], "")
        self.assertEqual(volume['size'], volume_req['size'])

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(len(list['volumes']), 1)
        self.assertEqual(list['volumes'][0], volume['id'])

        # Get info on incorrect id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_info("badid")

        # Get info
        info = c.volume_info(volume['id'])
        self.assertEqual(info, volume)

        # Expand volume with a bad id
        volume_ex_params = {}
        volume_ex_params['expand_size'] = 10

        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_expand("badid", volume_ex_params)

        # Expand volume
        print ("Expanding volume")
        volumeInfo = c.volume_expand(volume['id'], volume_ex_params)
        self.assertEqual(volumeInfo['size'], 20)

        # Delete bad id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_delete("badid")

        # Delete volume
        print ("Deleting volume")
        volume_delete = c.volume_delete(volume['id'])
        self.assertTrue(volume_delete)

        print ("Deleting Cluster")
        clusterInfo = c.cluster_info(cluster['id'])
        for node_id in clusterInfo['nodes']:
            # Get node information
            nodeInfo = c.node_info(node_id)

            # Delete all devices
            for device in nodeInfo['devices']:
                devid = device['id']
                self.assertTrue(c.device_state(devid, {'state': 'offline'}))
                self.assertTrue(c.device_state(devid, {'state': 'failed'}))
                device_delete = c.device_delete(devid)
                self.assertTrue(device_delete)

            # Delete node
            node_delete = c.node_delete(node_id)
            self.assertTrue(node_delete)

        # Delete cluster
        cluster_delete = c.cluster_delete(cluster['id'])
        self.assertTrue(cluster_delete)
Beispiel #4
0
    def test_volume(self):
        # Create cluster
        c = HeketiClient(TEST_SERVER, "admin", TEST_ADMIN_KEY)
        self.assertEqual(True, c != '')

        cluster_req = {}
        cluster_req['block'] = True
        cluster_req['file'] = True
        cluster = c.cluster_create(cluster_req)
        self.assertNotEqual(cluster['id'], '')

        # Create node request packet
        print("Creating Cluster")
        for i in range(3):
            node_req = {}
            node_req['cluster'] = cluster['id']
            node_req['hostnames'] = {
                "manage": ["node%s-manage.gluster.lab.com" % (i)],
                "storage": ["node%s-storage.gluster.lab.com" % (i)]
            }
            node_req['zone'] = i + 1

            # Create node
            node = c.node_add(node_req)
            self.assertNotEqual(node['id'], '')

            # Create and add devices
            for i in range(1, 4):
                device_req = {}
                device_req['name'] = "/dev/sda%s" % (i)
                device_req['node'] = node['id']

                device = c.device_add(device_req)
                self.assertTrue(device)

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(len(list['volumes']), 0)

        # Create a volume
        print("Creating a volume")
        volume_req = {}
        volume_req['size'] = 10
        volume = c.volume_create(volume_req)
        self.assertNotEqual(volume['id'], "")
        self.assertEqual(volume['size'], volume_req['size'])

        # Get list of volumes
        list = c.volume_list()
        self.assertEqual(len(list['volumes']), 1)
        self.assertEqual(list['volumes'][0], volume['id'])

        # Get info on incorrect id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_info("badid")

        # Get info
        info = c.volume_info(volume['id'])
        self.assertEqual(info, volume)

        # Expand volume with a bad id
        volume_ex_params = {}
        volume_ex_params['expand_size'] = 10

        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_expand("badid", volume_ex_params)

        # Expand volume
        print("Expanding volume")
        volumeInfo = c.volume_expand(volume['id'], volume_ex_params)
        self.assertEqual(volumeInfo['size'], 20)

        # Delete bad id
        with self.assertRaises(requests.exceptions.HTTPError):
            c.volume_delete("badid")

        # Delete volume
        print("Deleting volume")
        volume_delete = c.volume_delete(volume['id'])
        self.assertTrue(volume_delete)

        print("Deleting Cluster")
        clusterInfo = c.cluster_info(cluster['id'])
        for node_id in clusterInfo['nodes']:
            # Get node information
            nodeInfo = c.node_info(node_id)

            # Delete all devices
            for device in nodeInfo['devices']:
                devid = device['id']
                self.assertTrue(c.device_state(devid, {'state': 'offline'}))
                self.assertTrue(c.device_state(devid, {'state': 'failed'}))
                device_delete = c.device_delete(devid)
                self.assertTrue(device_delete)

            # Delete node
            node_delete = c.node_delete(node_id)
            self.assertTrue(node_delete)

        # Delete cluster
        cluster_delete = c.cluster_delete(cluster['id'])
        self.assertTrue(cluster_delete)
Beispiel #5
0
class Heketi(HeketiClient):

    def __init__(self, module):
        self.module = module
        self.action = self._get_params('action')
        self.init_heketi()

    def hkt_action(self):
        msg = {
                 'load': self.load_topology,
                 'addnode': self.heketi_add_node,
                 'adddevice': self.heketi_add_device,
                 'createvolume': self.heketi_create_volume
              }[self.action]()
        return msg


    def _get_params(self, opt, reqd=True):
        value = self.module.params[opt]
        if (value is None or not value.strip(" ")) and reqd:
            msg = "Please provide %s option in the playbook!" % opt
            self.module.fail_json(rc=1, msg=msg)
        value = value.strip() if value else value
        return value

    def load_topology(self):
        filename = self._get_params('filename', False)
        if filename:
            op = 'heketi-cli'
            cmd = ' load -json=%s' % filename
            rc, out, err = self.run_command(op, cmd)
            self.get_output(rc, out, err)
        else:
            cluster_id = self.heketi_cluster_create()
            node_id = self.heketi_add_node(cluster_id)
            dev = self.heketi_add_device(node_id)
            result = {"cluster_id": cluster_id, "node_id": node_id,
                    "device_create": dev}
            return result

    def init_heketi(self, server=None):
        user = self._get_params('user')
        key = self._get_params('userkey')
        server = self._get_params('server')
        self.heketi = HeketiClient(server, user, key)

    def heketi_add_node(self, cluster_id=None):
        if not cluster_id:
            cluster_id = self._get_params('cluster')
        hostname = {}
        zone = self._get_params('zone', False)
        hostname["manage"] = [self._get_params('managehost', False)]
        hostname["storage"] = [self._get_params('storagehost', False)]
        if not (zone and hostname["manage"] and hostname["storage"]):
            return False
        node_dict = dict(zone=int(zone),hostnames=hostname,cluster=cluster_id)
        ret = self.heketi.node_add(node_dict)
        return ret['id']

    def heketi_add_device(self, node_id=None):
        if not node_id:
            node_id = self._get_params('node', False)
        if not node_id:
            return ''
        try:
            devices = self._get_params('devices')
        except:
            return False
        devices = literal_eval(devices)
        dev_dict = {}
        dev_dict["node"] = node_id
        for dev in devices:
            dev_dict["name"] = dev
            ret = self.heketi.device_add(dev_dict)
        return True

    def heketi_cluster_create(self):
        cid = self._get_params('cluster', False)
        if cid:
            return cid
        ret = self.heketi.cluster_create()
        return  ret['id']

    def heketi_create_volume(self):
        vol = {}
        vol["size"] = int(self._get_params('size'))

        vname = self._get_params('name', False)
        if vname:
            vol['name'] = vname

        snapshot = self._get_params('snapshot', False)
        if snapshot and snapshot == 'true':
            vol['snapshot'], h = {}, {}
            h['enable'] = "true"
            f = self._get_params('snapshot_factor', False)
            if f:
                h['factor'] = int(f)
            vol['snapshot'] = h

        durability = self._get_params('durability', False)
        if durability and durability.lower() != 'none':
            vol['durability'], h = {}, {}
            if durability.lower() == 'replicate':
                h['type'] = 'replicate'
                c = self._get_params('replica_count', False)
                if c:
                    h['replicate'] = {'replica': int(c)}
            if durability.lower() == 'disperse':
                h['type'] = 'disperse'
                d = self._get_params('disperse_data', False)
                r = self._get_params('redundancy', False)
                h['disperse'] = {'data': int(d), 'redundancy': int(r)}
                h = dict((k, v) for k, v in h.iteritems() if v)
            vol['durability'] = h

        clusters = self._get_params('clusters', False)
        if clusters:
            vol['clusters'] = clusters
        ret = self.heketi.volume_create(vol)
        return ret


    def run_command(self, op, options):
        cmd = self.module.get_bin_path(op, True)  + options
        return self.module.run_command(cmd)

    def get_output(self, output):
        self.module.exit_json(msg=output, changed=1)
Beispiel #6
0
class Heketi(HeketiClient):
    def __init__(self, module):
        self.module = module
        self.action = self._get_params('action')
        self.init_heketi()

    def hkt_action(self):
        msg = {
            'load': self.load_topology,
            'addnode': self.heketi_add_node,
            'adddevice': self.heketi_add_device,
            'createvolume': self.heketi_create_volume
        }[self.action]()
        return msg

    def _get_params(self, opt, reqd=True):
        value = self.module.params[opt]
        if (value is None or not value.strip(" ")) and reqd:
            msg = "Please provide %s option in the playbook!" % opt
            self.module.fail_json(rc=1, msg=msg)
        value = value.strip() if value else value
        return value

    def load_topology(self):
        filename = self._get_params('filename', False)
        if filename:
            op = 'heketi-cli'
            cmd = ' load -json=%s' % filename
            rc, out, err = self.run_command(op, cmd)
            self.get_output(rc, out, err)
        else:
            cluster_id = self.heketi_cluster_create()
            node_id = self.heketi_add_node(cluster_id)
            dev = self.heketi_add_device(node_id)
            result = {
                "cluster_id": cluster_id,
                "node_id": node_id,
                "device_create": dev
            }
            return result

    def init_heketi(self, server=None):
        user = self._get_params('user')
        key = self._get_params('userkey')
        server = self._get_params('server')
        self.heketi = HeketiClient(server, user, key)

    def heketi_add_node(self, cluster_id=None):
        if not cluster_id:
            cluster_id = self._get_params('cluster')
        hostname = {}
        zone = self._get_params('zone', False)
        hostname["manage"] = [self._get_params('managehost', False)]
        hostname["storage"] = [self._get_params('storagehost', False)]
        if not (zone and hostname["manage"] and hostname["storage"]):
            return False
        node_dict = dict(zone=int(zone),
                         hostnames=hostname,
                         cluster=cluster_id)
        ret = self.heketi.node_add(node_dict)
        return ret['id']

    def heketi_add_device(self, node_id=None):
        if not node_id:
            node_id = self._get_params('node', False)
        if not node_id:
            return ''
        try:
            devices = self._get_params('devices')
        except:
            return False
        devices = literal_eval(devices)
        dev_dict = {}
        dev_dict["node"] = node_id
        for dev in devices:
            dev_dict["name"] = dev
            ret = self.heketi.device_add(dev_dict)
        return True

    def heketi_cluster_create(self):
        cid = self._get_params('cluster', False)
        if cid:
            return cid
        ret = self.heketi.cluster_create()
        return ret['id']

    def heketi_create_volume(self):
        vol = {}
        vol["size"] = int(self._get_params('size'))

        vname = self._get_params('name', False)
        if vname:
            vol['name'] = vname

        snapshot = self._get_params('snapshot', False)
        if snapshot and snapshot == 'true':
            vol['snapshot'], h = {}, {}
            h['enable'] = "true"
            f = self._get_params('snapshot_factor', False)
            if f:
                h['factor'] = int(f)
            vol['snapshot'] = h

        durability = self._get_params('durability', False)
        if durability and durability.lower() != 'none':
            vol['durability'], h = {}, {}
            if durability.lower() == 'replicate':
                h['type'] = 'replicate'
                c = self._get_params('replica_count', False)
                if c:
                    h['replicate'] = {'replica': int(c)}
            if durability.lower() == 'disperse':
                h['type'] = 'disperse'
                d = self._get_params('disperse_data', False)
                r = self._get_params('redundancy', False)
                h['disperse'] = {'data': int(d), 'redundancy': int(r)}
                h = dict((k, v) for k, v in h.iteritems() if v)
            vol['durability'] = h

        clusters = self._get_params('clusters', False)
        if clusters:
            vol['clusters'] = clusters
        ret = self.heketi.volume_create(vol)
        return ret

    def run_command(self, op, options):
        cmd = self.module.get_bin_path(op, True) + options
        return self.module.run_command(cmd)

    def get_output(self, output):
        self.module.exit_json(msg=output, changed=1)