コード例 #1
0
ファイル: test_remove.py プロジェクト: raghu999/fog05
def test_miss(sid, root, home):

    sroot = 'fos://{0}'.format(root)
    shome= 'fos://{0}-{1}'.format(root, home)

    store = DStore(sid, sroot, home, 1024)

    uri_prefix = 'fos://{0}/{1}-{2}'.format(root, home, sid)
    id = 100 + int(sid)
    val = {'id': id, 'kind': 'info', 'value': 'am a store fos://{0}/{1}-{2}!'.format(root, home, sid)}
    store.put(uri_prefix, json.dumps(val))

    test_uri = uri_prefix+'/savia'
    tid = 200 + int(sid)
    tval = {'id': tid, 'value': 'A Test URI'}
    store.put(test_uri, json.dumps(tval))


    dval = {'pasticceria': '{0}-Cannoli!'.format(sid)}
    store.dput(test_uri, json.dumps(dval))

    delta_tag = '#rosticceria={0}-Arancini!'.format(sid)
    test_uri = test_uri+delta_tag
    store.dput(test_uri)

    test_uri = uri_prefix + '/savia'
    store.observe(test_uri, my_observer)

    print('Store written, press a key to continue')
    input()

    for id in store.discovered_stores:
        uri = 'fos://{0}/{1}-{2}/savia'.format(root, home, id)
        store.remove(uri)
        #print('=========> store[{0}] = {1}'.format(uri, v))

    input()
コード例 #2
0
ファイル: test_cache.py プロジェクト: raghu999/fog05
class TestCache():

    def __init__(self):
        self.uuid = uuid.uuid4()
        sid = (self.uuid)


        # Desidered Store. containing the desidered state
        self.droot = 'dfos://<sys-id>'
        self.dhome = 'dfos://<sys-id>/{}'.format(sid)
        self.dstore = DStore(sid, self.droot, self.dhome, 1024)

        # Actual Store, containing the Actual State
        self.aroot = 'afos://<sys-id>'
        self.ahome = 'afos://<sys-id>/{}'.format(sid)
        self.astore = DStore(sid, self.aroot, self.ahome, 1024)

        self.nodes = {}

        self.populateNodeInformation()

    def populateNodeInformation(self):

        node_info = {}

        node_info.update({'name': 'develop node'})
        uri = '{}/'.format(self.ahome)
        self.astore.put(uri, json.dumps(node_info))

    def test_observer_actual(self, key, value,v):
        print('##################################')
        print('##### I\'M an Observer ACTUAL #####')
        print('## Key: {}'.format(key))
        print('## Value: {}'.format(value))
        print('## V: {}'.format(v))
        print('##################################')
        print('##################################')

    def test_observer_desidered(self, key, value,v):
        print('#####################################')
        print('##### I\'M an Observer DESIDERED #####')
        print('## Key: {}'.format(key))
        print('## Value: {}'.format(value))
        print('## V: {}'.format(v))
        print('#####################################')
        print('#####################################')


    def nodeDiscovered(self, uri, value, v = None):
        value = json.loads(value)
        if uri != 'fos://<sys-id>/{}/'.format(self.uuid):
            print('###########################')
            print('###########################')
            print('### New Node discovered ###')
            print('UUID: {}'.format(value.get('uuid')))
            print('Name: {}'.format(value.get('name')))
            print('###########################')
            print('###########################')
            self.nodes.update({len(self.nodes)+1: {value.get('uuid'): value}})

    def show_nodes(self):
        for k in self.nodes.keys():
            n = self.nodes.get(k)
            id = list(n.keys())[0]
            print('{} - {} : {}'.format(k, n.get(id).get('name'), id))

    def main(self):
        uri = 'afos://<sys-id>/*/'
        self.astore.observe(uri, self.nodeDiscovered)

        print('Putting on dstore')
        val = {'value': 'some value'}
        uri = '{}/test1'.format(self.dhome)
        self.dstore.put(uri, json.dumps(val))

        val = {'value2': 'some value2'}
        uri = '{}/test2'.format(self.dhome)
        self.dstore.put(uri, json.dumps(val))

        val = {'value3': 'some value3'}
        uri = '{}/test3'.format(self.dhome)
        self.dstore.put(uri, json.dumps(val))

        print('Putting on astore')
        val = {'actual value': 'some value'}
        uri = '{}/test1'.format(self.ahome)
        self.astore.put(uri, json.dumps(val))

        val = {'actual value2': 'some value2'}
        uri = '{}/test2'.format(self.ahome)
        self.astore.put(uri, json.dumps(val))

        val = {'actual value32': 'some value3'}
        uri = '{}/test3'.format(self.ahome)
        self.astore.put(uri, json.dumps(val))

        uri = '{}/test3'.format(self.ahome)
        self.astore.put(uri, json.dumps(val))

        while len(self.nodes) < 1:
            time.sleep(2)

        self.show_nodes()

        print('My UUID is {}' % self.uuid)

        print('###################################### Desidered Store ######################################')
        print(self.dstore)
        print('#############################################################################################')
        print('###################################### Actual Store #########################################')
        print(self.astore)
        print('#############################################################################################')
        input()

        uri = '{}/test3'.format(self.ahome)
        self.astore.remove(uri)

        print('###################################### Actual Store #########################################')
        print(self.astore)
        print('#############################################################################################')

        input()
        exit(0)
コード例 #3
0
ファイル: lf_vm.py プロジェクト: haochihlin/fog05
class Controll():
    def __init__(self):
        self.uuid = uuid.uuid4()
        sid = str(self.uuid)

        '''
        Creating the two stores
        
        afos://...  Actual State Store
        In this store you can find all the actual data of the node, this store is written by
        plugins and by the fogagent for update the store after a request.
        All other nodes can read or observe only this store.
        
        
        dfos://... Desidered State Store
        In this store other nodes write the command/request, all plugins and the agent observe this store,
        in a way that they can react to a desidered state
        
        '''


        self.droot = "dfos://<sys-id>"
        self.dhome = str("dfos://<sys-id>/%s" % sid)
        self.dstore = DStore(sid, self.droot, self.dhome, 1024)

        self.aroot = "afos://<sys-id>"
        self.ahome = str("afos://<sys-id>/%s" % sid)
        self.astore = DStore(sid, self.aroot, self.ahome, 1024)

        self.nodes = {}




    def nodeDiscovered(self, uri, value, v = None):
        '''

        This is an observer for discover new nodes on the network
        It is called by the DStore object where it was registered.
        If a new node is discovered it will be added to the self.nodes dict

        :param uri:
        :param value:
        :param v:
        :return:
        '''
        value = json.loads(value)
        if uri != str('fos://<sys-id>/%s/' % self.uuid):
            print ("###########################")
            print ("###########################")
            print ("### New Node discovered ###")
            print ("UUID: %s" % value.get('uuid'))
            print ("Name: %s" % value.get('name'))
            print ("###########################")
            print ("###########################")
            self.nodes.update({ len(self.nodes)+1 : {value.get('uuid'): value}})


    def readFile(self, file_path):
        '''
        Simply method to read a file
        :param file_path: The file path
        :return: The file content
        '''
        with open(file_path,'r') as f:
            data = f.read()
        return data


    def show_nodes(self):
        '''
        Print all nodes discovered by this script

        :return:
        '''
        for k in self.nodes.keys():
            n = self.nodes.get(k)
            id = list(n.keys())[0]
            print("%d - %s : %s" % (k, n.get(id).get('name'), id))


    def vm_deploy(self, node_uuid, vm_uuid):
        '''
        This method make the destination node load the correct plugins,
        and then deploy the vm to the node

        :param node_uuid:
        :param vm_uuid:
        :return:
        '''

        print("Make node load kvm plugin")

        val = {'plugins': [{'name': 'KVMLibvirt', 'version': 1, 'uuid': '',
                            'type': 'runtime', 'status': 'add'}]}
        uri = str('dfos://<sys-id>/%s/plugins' % node_uuid)
        print(uri)
        self.dstore.dput(uri, json.dumps(val))  # Writing to the desidered store of the destination node
                                                # the manifest of the plugin to load, in this case KVM

        time.sleep(1)
        val = {'plugins': [{'name': 'brctl', 'version': 1, 'uuid': '',
                            'type': 'network', 'status': 'add'}]}
        uri = str('dfos://<sys-id>/%s/plugins' % node_uuid)
        self.dstore.dput(uri, json.dumps(val))  # Writing to the desidered store of the destination node
                                                # the manifest of the plugin to load, in this case bridge-utils

        time.sleep(1)

        print("Looking if kvm plugin loaded")

        # reading from the actual store of the node if the KVM plugin was loaded

        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)  # reading from actual state store
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating kvm plugin")
        search = [x for x in runtimes if 'KVMLibvirt' in x.get('name')]
        if len(search) == 0:
            print ("Plugin was not loaded")
            exit()
        else:
            kvm = search[0]

        vm_name = 'test'

        cinit = None  # or self.readFile(cloud_init_file_path) # KVM plugin support cloudinit initializzation
        sshk = None   # or self.readFile(pub key file path)       #

        ##### OTHER IMAGES USED
        #virt-cirros-0.3.4-x86_64-disk.img
        #cirros-0.3.5-x86_64-disk.img
        #xenial-server-cloudimg-amd64-disk1.img
        #####


        # creating the manifest for the vm
        vm_definition = {'name': vm_name, 'uuid': vm_uuid, 'cpu': 1, 'memory': 512, 'disk_size': 10, 'base_image':
            'http://192.168.1.142/virt-cirros-0.3.4-x86_64-disk.img', 'networks': [{
            'mac': "d2:e3:ed:6f:e3:ef", 'br_name': "virbr0"}], "user-data": cinit, "ssh-key": sshk}

        entity_definition = {'status': 'define', 'name': vm_name, 'version': 1, 'entity_data': vm_definition}

        json_data = json.dumps(entity_definition)

        print("Press enter to define a vm")
        input()

        # writing the manifest to the desidered store of the destination node
        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' % (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.put(uri, json_data)

        # busy waiting for the vm to state change
        while True:
            print("Waiting vm defined...")
            time.sleep(1)
            #reading state from actual store
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "defined":
                    break

        # using a dput (delta put) to only update the vm state, so cause a state transition
        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=configure' % (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        # waiting the vm sto state change
        while True:
            print("Waiting vm configured...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "configured":
                    break



        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=run' % (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting vm to boot...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "run":
                    break

        print("vm is running on node")

    def vm_destroy(self, node_uuid, vm_uuid):


        # load the plugin uuid from the actual store of the destination node
        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating kvm plugin")
        search = [x for x in runtimes if 'KVMLibvirt' in x.get('name')]
        if len(search) == 0:
            print ("Plugin was not loaded")
            exit()
        else:
            kvm = search[0]


        print("Press enter to stop vm")
        input()

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=stop' % (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting vm to be stopped...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "stop":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=clean' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting cleaned...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "cleaned":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' % (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.remove(uri)

    def main(self):

        # registering an observer for all actual store root in this system
        # this allow the discovery of new nodes
        uri = str('afos://<sys-id>/*/')
        self.astore.observe(uri, self.nodeDiscovered)



        vm_uuid = str(uuid.uuid4())

        # simple busy wait for a node to appear
        while len(self.nodes) < 1:
            time.sleep(2)

        self.show_nodes()

        input()

        # getting the node uuid
        vm_dst_node = self.nodes.get(1)
        if vm_dst_node is not None:
            vm_dst_node = list(vm_dst_node.keys())[0]

        # deploy the vm
        self.vm_deploy(vm_dst_node, vm_uuid)

        input()

        # offload the vm
        self.vm_destroy(vm_dst_node, vm_uuid)

        input()

        exit(0)
コード例 #4
0
ファイル: vxlan_vms_container.py プロジェクト: raghu999/fog05
class Controll():
    def __init__(self):
        self.uuid = uuid.uuid4()
        sid = str(self.uuid)

        self.droot = "dfos://<sys-id>"
        self.dhome = str("dfos://<sys-id>/%s" % sid)
        self.dstore = DStore(sid, self.droot, self.dhome, 1024)

        self.aroot = "afos://<sys-id>"
        self.ahome = str("afos://<sys-id>/%s" % sid)
        self.astore = DStore(sid, self.aroot, self.ahome, 1024)

        self.nodes = {}

    def nodeDiscovered(self, uri, value, v=None):
        value = json.loads(value)
        if uri != str('fos://<sys-id>/%s/' % self.uuid):
            print("###########################")
            print("###########################")
            print("### New Node discovered ###")
            print("UUID: %s" % value.get('uuid'))
            print("Name: %s" % value.get('name'))
            print("###########################")
            print("###########################")
            self.nodes.update(
                {len(self.nodes) + 1: {
                     value.get('uuid'): value
                 }})

    def read_file(self, file_path):
        with open(file_path, 'r') as f:
            data = f.read()
        return data

    def show_nodes(self):
        for k in self.nodes.keys():
            n = self.nodes.get(k)
            id = list(n.keys())[0]
            print("%d - %s : %s" % (k, n.get(id).get('name'), id))

    def vm_deploy(self, node_uuid, vm_uuid, net_uuid):
        print("Make node load kvm plugin")

        val = {
            'plugins': [{
                'name': 'KVMLibvirt',
                'version': 1,
                'uuid': '',
                'type': 'runtime',
                'status': 'add'
            }]
        }
        uri = str('dfos://<sys-id>/%s/plugins' % node_uuid)
        print(uri)
        # print(self.dstore.get(uri))

        self.dstore.dput(uri, json.dumps(val))

        time.sleep(1)

        print("Looking if kvm plugin loaded")

        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating kvm plugin")
        search = [x for x in runtimes if 'KVMLibvirt' in x.get('name')]
        if len(search) == 0:
            print("Plugin was not loaded")
            exit()
        else:
            kvm = search[0]

        vm_name = 'test'

        cinit = None  # self.read_file(os.path.join(sys.path[0], 'etc', 'cloud_init_demo'))
        sshk = None  #self.read_file(os.path.join(sys.path[0], 'etc', 'example_key.pub'))

        # virt-cirros-0.3.4-x86_64-disk.img
        # cirros-0.3.5-x86_64-disk.img
        # xenial-server-cloudimg-amd64-disk1.img
        # 192.168.1.142
        # 172.16.7.128
        #br_name = str("br-%s" % net_uuid.split('-')[0])

        vm_definition = {
            'name': vm_name,
            'uuid': vm_uuid,
            'cpu': 1,
            'memory': 512,
            'disk_size': 10,
            'base_image': 'http://192.168.1.142/brain.qcow2',
            'networks': [{
                'network_uuid': net_uuid
            }],
            "user-data": cinit,
            "ssh-key": sshk
        }

        entity_definition = {
            'status': 'define',
            'name': vm_name,
            'version': 1,
            'entity_data': vm_definition
        }

        json_data = json.dumps(entity_definition)

        #print("Press enter to define a vm")
        #input()

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.put(uri, json_data)

        while True:
            print("Waiting vm defined...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "defined":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=configure' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting vm configured...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "configured":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=run' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting vm to boot...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "run":
                break

        print("vm is running on node")

    def vm_destroy(self, node_uuid, vm_uuid):

        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating kvm plugin")
        search = [x for x in runtimes if 'KVMLibvirt' in x.get('name')]
        if len(search) == 0:
            print("Plugin was not loaded")
            exit()
        else:
            kvm = search[0]

        #print("Press enter to stop vm")
        #input()

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=stop' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting vm to be stopped...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "stop":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=clean' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting cleaned...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, kvm.get('uuid'), vm_uuid))
            vm_info = json.loads(self.astore.get(uri))
            if vm_info is not None and vm_info.get("status") == "cleaned":
                break

        json_data = json.dumps({'status': 'undefine'})
        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' %
                  (node_uuid, kvm.get('uuid'), vm_uuid))
        self.dstore.dput(uri, json_data)

    def container_deploy(self, node_uuid, container_uuid, manifest):
        # container_uuid, net_uuid, image_name, gw=False):
        '''
        This method make the destination node load the correct plugins,
        and then deploy the container to the node

        :param node_uuid:
        :param container_uuid:
        :return:
        '''

        print("Make node load lxd plugin")

        val = {
            'plugins': [{
                'name': 'LXD',
                'version': 1,
                'uuid': '',
                'type': 'runtime',
                'status': 'add'
            }]
        }
        uri = str('dfos://<sys-id>/%s/plugins' % node_uuid)
        print(uri)
        self.dstore.dput(uri, json.dumps(
            val))  # Writing to the desidered store of the destination node
        # the manifest of the plugin to load, in this case Kcontainer

        time.sleep(1)

        print("Looking if lxd plugin loaded")

        # reading from the actual store of the node if the Kcontainer plugin was loaded

        uri = str('afos://<sys-id>/%s/plugins' %
                  node_uuid)  # reading from actual state store
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating LXD plugin")
        search = [x for x in runtimes if 'LXD' in x.get('name')]
        if len(search) == 0:
            print("Plugin was not loaded")
            exit()
        else:
            lxd = search[0]
        image_name = manifest.get('base_image')
        container_name = image_name.split('.')[0]

        cinit = None  # or self.readFile(cloud_init_file_path) # Kcontainer plugin support cloudinit initializzation
        sshk = None  # or self.readFile(pub key file path)       #

        ##### OTHER IMAGES USED
        # virt-cirros-0.3.4-x86_64-disk.img
        # cirros-0.3.5-x86_64-disk.img
        # ubuntu_container.tar.xz
        #####
        # br_name = str("br-%s" % net_uuid.split('-')[0])

        # creating the manifest for the container
        '''

        if gw:

            container_definition = {'name': container_name, 'uuid': container_uuid, 'base_image':'https://www.dropbox.com/s/7ko6orndmkkekc7/gateway.tar.gz', 'networks': [{'intf_name': 'wan' ,'br_name':'eno1'},{'intf_name': 'mgmt','br_name':br_name}], "user-data": cinit, "ssh-key": sshk}
            #container_definition = {'name': container_name, 'uuid': container_uuid, 'base_image':str('http://172.16.7.128/%s' % image_name), 'networks': [{'intf_name': 'wan' ,'br_name':'eno1'},{'intf_name': 'mgmt','br_name':br_name}], "user-data": cinit, "ssh-key": sshk}
        else:

            container_definition = {'name': container_name, 'uuid': container_uuid, 'base_image':'https://www.dropbox.com/s/53fp1u3jmxjmj6a/brain.tar.gz', 'networks': [{'intf_name': 'mgmt','br_name':br_name}], "user-data": cinit, "ssh-key": sshk}
            #container_definition = {'name': container_name, 'uuid': container_uuid, 'base_image':str('http://172.16.7.128/%s' % image_name), 'networks': [{'intf_name': 'mgmt','br_name':br_name}], "user-data": cinit, "ssh-key": sshk}
        '''
        entity_definition = {
            'status': 'define',
            'name': container_name,
            'version': 1,
            'entity_data': manifest
        }

        json_data = json.dumps(entity_definition)

        # print("Press enter to define a container")
        # input()

        # writing the manifest to the desidered store of the destination node
        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.put(uri, json_data)

        # busy waiting for the container to state change
        while True:
            print("Waiting container defined...")
            time.sleep(1)
            # reading state from actual store
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, lxd.get('uuid'), container_uuid))
            container_info = json.loads(self.astore.get(uri))
            if container_info is not None and container_info.get(
                    "status") == "defined":
                break

        # using a dput (delta put) to only update the container state, so cause a state transition
        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=configure' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.dput(uri)

        # waiting the container sto state change
        while True:
            print("Waiting container configured...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, lxd.get('uuid'), container_uuid))
            container_info = json.loads(self.astore.get(uri))
            if container_info is not None and container_info.get(
                    "status") == "configured":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=run' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting container to boot...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, lxd.get('uuid'), container_uuid))
            container_info = json.loads(self.astore.get(uri))
            if container_info is not None and container_info.get(
                    "status") == "run":
                break

        print("container is running on node")

    def container_destroy(self, node_uuid, container_uuid):

        # load the plugin uuid from the actual store of the destination node
        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')

        runtimes = [x for x in all_plugins if x.get('type') == 'runtime']
        print("locating lxd plugin")
        search = [x for x in runtimes if 'LXD' in x.get('name')]
        if len(search) == 0:
            print("Plugin was not loaded")
            exit()
        else:
            lxd = search[0]

        # print("Press enter to stop container")
        # input()

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=stop' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting container to be stopped...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, lxd.get('uuid'), container_uuid))
            container_info = json.loads(self.astore.get(uri))
            if container_info is not None and container_info.get(
                    "status") == "stop":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=clean' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.dput(uri)

        while True:
            print("Waiting cleaned...")
            time.sleep(1)
            uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" %
                      (node_uuid, lxd.get('uuid'), container_uuid))
            container_info = json.loads(self.astore.get(uri))
            if container_info is not None and container_info.get(
                    "status") == "cleaned":
                break

        uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' %
                  (node_uuid, lxd.get('uuid'), container_uuid))
        self.dstore.remove(uri)

    def create_network(self, node_uuid, net_id, master=True):
        time.sleep(1)
        val = {
            'plugins': [{
                'name': 'brctl',
                'version': 1,
                'uuid': '',
                'type': 'network',
                'status': 'add'
            }]
        }
        uri = str('dfos://<sys-id>/%s/plugins' % node_uuid)
        self.dstore.dput(uri, json.dumps(val))
        time.sleep(4)

        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')
        print(all_plugins)
        nws = [x for x in all_plugins if x.get('type') == 'network']
        print(nws)
        print("locating brctl plugin")
        search = [x for x in nws if 'brctl' in x.get('name')]
        print(search)
        if len(search) == 0:
            print("Plugin was not loaded")
            exit()
        else:
            brctl = search[0]

        if master:
            net_data = {
                'status': 'add',
                'name': 'test',
                'uuid': net_id,
                'ip_range': '192.168.128.0/24',
                'has_dhcp': True
            }
        else:
            net_data = {'status': 'add', 'name': 'test', 'uuid': net_id}

        json_data = json.dumps(net_data)
        uri = str('dfos://<sys-id>/%s/network/%s/networks/%s' %
                  (node_uuid, brctl.get('uuid'), net_id))
        self.dstore.put(uri, json_data)

    def delete_network(self, node_uuid, net_id):
        time.sleep(1)

        uri = str('afos://<sys-id>/%s/plugins' % node_uuid)
        all_plugins = json.loads(self.astore.get(uri)).get('plugins')
        nws = [x for x in all_plugins if x.get('type') == 'network']
        print("locating brctl plugin")
        search = [x for x in nws if 'brctl' in x.get('name')]
        print(search)
        if len(search) == 0:
            print("Plugin was not loaded")
            exit(0)
        else:
            brctl = search[0]

        uri = str('dfos://<sys-id>/%s/network/%s/networks/%s#status=remove' %
                  (node_uuid, brctl.get('uuid'), net_id))
        self.dstore.dput(uri)

    def main(self):

        uri = str('afos://<sys-id>/*/')
        self.astore.observe(uri, self.nodeDiscovered)

        vm_uuid = str(uuid.uuid4())

        while len(self.nodes) < 2:
            time.sleep(2)

        self.show_nodes()

        net_uuid = str(uuid.uuid4())

        vm_src_node = self.nodes.get(1)
        if vm_src_node is not None:
            vm_src_node = list(vm_src_node.keys())[0]

        vm_dst_node = self.nodes.get(2)
        if vm_dst_node is not None:
            vm_dst_node = list(vm_dst_node.keys())[0]

        input("Press enter to create network [vxlan]")

        self.create_network(vm_src_node, net_uuid, False)
        self.create_network(vm_dst_node, net_uuid, False)

        c_uuid = str(uuid.uuid4())
        c2_uuid = str(uuid.uuid4())
        vm_uuid = str(uuid.uuid4())
        vm2_uuid = str(uuid.uuid4())

        br_name = str("br-%s" % net_uuid.split('-')[0])

        gw_manifest = {
            'name':
            'gw',
            'uuid':
            c_uuid,
            'base_image':
            'https://www.dropbox.com/s/7ko6orndmkkekc7/gateway.tar.gz',
            'networks': [{
                'intf_name': 'wan',
                'br_name': 'ens33'
            }, {
                'intf_name': 'mgmt',
                'network_uuid': net_uuid
            }],
            "user-data":
            None,
            "ssh-key":
            None
        }

        brain_manifest = {
            'name': 'brain',
            'uuid': c2_uuid,
            'base_image':
            'https://www.dropbox.com/s/6eoaqhoknp7134t/brain.tar.gz',
            'networks': [{
                'intf_name': 'mgmt',
                'network_uuid': net_uuid
            }],
            "user-data": None,
            "ssh-key": None
        }

        input("Press enter to create vms")
        self.container_deploy(vm_src_node, c_uuid, gw_manifest)
        self.container_deploy(vm_src_node, c2_uuid, brain_manifest)
        self.vm_deploy(vm_src_node, vm_uuid, net_uuid)
        self.vm_deploy(vm_dst_node, vm2_uuid, net_uuid)

        input("press enter to destroy vms")
        self.vm_destroy(vm_dst_node, vm2_uuid)
        self.vm_destroy(vm_src_node, vm_uuid)
        self.container_destroy(vm_src_node, c2_uuid)
        self.container_destroy(vm_src_node, c_uuid)

        #self.migrate_vm(vm_src_node, vm_dst_node, vm_uuid)

        input("press enter to destroy vxlan")

        self.delete_network(vm_src_node, net_uuid)
        self.delete_network(vm_dst_node, net_uuid)

        input("press enter to exit")

        exit(0)