def test_miss(sid, root, home): sroot = 'afos://{}'.format(root) shome = 'afos://{}/{}'.format(root, home) store = DStore(sid, sroot, home, 1024) uri_prefix = 'afos://{0}/{1}-{2}'.format(root, home, sid) id = 100 + int(sid) val = { 'id': id, 'kind': 'info', 'value': 'am a store afos://{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) print('Store written, press a key to continue') input() # local get v = store.get(uri_prefix) vt = store.get(test_uri) print('=========> store[{0}] = {1}'.format(uri_prefix, v)) print('=========> store[{0}] = {1}'.format(test_uri, vt)) print('\nStore get exectured, press a key to continue') input() # try get that need resolving for id in store.discovered_stores: uri = 'afos://{0}/{1}-{2}/savia'.format(root, home, id) v = store.get(uri) print('=========> store[{0}] = {1}'.format(uri, v)) print('\nStore remote get exectured, press a key to continue') input() # try to get them in a single shot -- locally: uri = 'afos://{0}/*'.format(root, home) vs = store.getAll(uri) print('=========> store[{0}] = {1}'.format(uri, vs)) print('\nStore local get-all exectured, press a key to continue') input() vs = store.resolveAll(uri) print('=========> store[{0}] = {1}'.format(uri, vs)) print('\nStore remote resolve-all exectured, press a key to continue') input()
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 readFile(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 lf_ros2(self, node_uuid): print("Make node load ros2 plugin") val = { 'plugins': [{ 'name': 'ros2', 'version': 1, 'uuid': '', 'type': 'runtime', 'status': 'add' }] } uri = str('dfos://<sys-id>/%s/plugins' % node_uuid) self.dstore.dput(uri, json.dumps(val)) time.sleep(1) print("Looking if native 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 ros2 plugin") search = [x for x in runtimes if 'ros2' in x.get('name')] if len(search) == 0: print("Plugin was not loaded") exit() else: native = search[0] app_name = 'examples_rclcpp_minimal_publisher' app_uuid = str(uuid.uuid4()) app_definition = { 'name': app_name, 'command': 'publisher_member_function', 'args': [], 'url': 'http://192.168.1.142/minimal_publisher.zip', 'uuid': app_uuid } entity_definition = { 'status': 'define', 'name': app_name, 'version': 1, 'entity_data': app_definition } json_data = json.dumps(entity_definition) uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' % (node_uuid, native.get('uuid'), app_uuid)) self.dstore.put(uri, json_data) while True: print("Waiting ROS2 to defined...") time.sleep(1) uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, native.get('uuid'), app_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, native.get('uuid'), app_uuid)) self.dstore.dput(uri) while True: print("Waiting ROS2 to configured...") time.sleep(1) uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, native.get('uuid'), app_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, native.get('uuid'), app_uuid)) self.dstore.dput(uri) while True: print("Waiting ROS2 to run...") time.sleep(1) uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, native.get('uuid'), app_uuid)) vm_info = json.loads(self.astore.get(uri)) if vm_info is not None and vm_info.get("status") == "run": break print("Press enter to stop the ROS2") input() uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=stop' % (node_uuid, native.get('uuid'), app_uuid)) self.dstore.dput(uri) while True: print("Waiting ROS2 to stop...") time.sleep(1) uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, native.get('uuid'), app_uuid)) vm_info = json.loads(self.astore.get(uri)) if vm_info is not None and vm_info.get("status") == "stop": break input("press enter to clean") uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s#status=clean' % (node_uuid, native.get('uuid'), app_uuid)) self.dstore.dput(uri) while True: print("Waiting ROS2 to clean...") time.sleep(1) uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (node_uuid, native.get('uuid'), app_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#status=undefine' % (node_uuid, native.get('uuid'), app_uuid)) self.dstore.dput(uri) def main(self): uri = str('afos://<sys-id>/*/') self.astore.observe(uri, self.nodeDiscovered) while len(self.nodes) < 1: time.sleep(2) self.show_nodes() na_node = self.nodes.get(1) if na_node is not None: na_node = list(na_node.keys())[0] input() self.lf_ros2(na_node) input() exit(0)
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)
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.count = 0 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): 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) 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(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 = 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 # http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img # http://172.16.7.128/virt-cirros-0.3.4-x86_64-disk.img # https://www.dropbox.com/s/z3mg72t2pgkz621/virt-cirros-0.3.4-x86_64-disk.img #vm_definition = {'name': vm_name, 'uuid': vm_uuid, 'cpu': 1, 'memory': 512, 'disk_size': 10, 'base_image': # 'http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img', 'networks': [{ # 'mac': "d2:e3:ed:6f:e3:ef", 'intf_name': "br0"}], "user-data": cinit, "ssh-key": sshk} vm_definition = { 'name': vm_name, 'uuid': vm_uuid, 'cpu': 1, 'memory': 512, 'disk_size': 10, 'base_image': 'http://172.16.7.128/virt-cirros-0.3.4-x86_64-disk.img', 'networks': [{ 'intf_name': "br0" }], "user-data": None, "ssh-key": None } 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 migrate_vm(self, src, dst, vm_uuid): uri = str('afos://<sys-id>/%s/plugins' % src) 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_src = search[0] uri = str('afos://<sys-id>/%s/runtime/%s/entity/%s' % (src, kvm_src.get('uuid'), vm_uuid)) vm_info = json.loads(self.astore.get(uri)) print(vm_info) #input() vm_info.update({"status": "taking_off"}) vm_info.update({"dst": dst}) vm_info_dst = vm_info.copy() vm_info_dst.update({"status": "landing"}) print(vm_info) print(vm_info_dst) #input() print("Make node load kvm plugin") val = { 'plugins': [{ 'name': 'KVMLibvirt', 'version': 1, 'uuid': '', 'type': 'runtime', 'status': 'add' }] } uri = str('dfos://<sys-id>/%s/plugins' % dst) self.dstore.put(uri, json.dumps(val)) time.sleep(1) val = { 'plugins': [{ 'name': 'brctl', 'version': 1, 'uuid': '', 'type': 'network', 'status': 'add' }] } uri = str('dfos://<sys-id>/%s/plugins' % dst) self.dstore.put(uri, json.dumps(val)) time.sleep(1) print("Looking if kvm plugin loaded") uri = str('afos://<sys-id>/%s/plugins' % dst) 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_dst = search[0] uri = str('fos://<sys-id>/%s/runtime/%s/entity/%s' % (dst, kvm_dst.get('uuid'), vm_uuid)) json_data = json.dumps(vm_info_dst) print("Press enter to migrate a vm") #input() uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' % (dst, kvm_dst.get('uuid'), vm_uuid)) self.dstore.put(uri, json_data) json_data = json.dumps(vm_info) uri = str('dfos://<sys-id>/%s/runtime/%s/entity/%s' % (src, kvm_src.get('uuid'), vm_uuid)) self.dstore.dput(uri, json_data) #while True: # print("Waiting migration to complete...") # time.sleep(1) # uri = str("afos://<sys-id>/%s/runtime/%s/entity/%s" % (dst, kvm_dst.get('uuid'), vm_uuid)) # print(uri) # vm_info = json.loads(self.astore.get(uri)) # if vm_info is not None and vm_info.get("status") == "run": # break #input("press enter to destroy vm") #self.vm_destroy(dst, vm_uuid) def catch_signal(self, signal, frame): if signal == 2: if self.count == 0: self.flag = not self.flag self.count = self.count + 1 else: exit(0) 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() vm_src_node = self.nodes.get(1) if vm_src_node is not None: vm_src_node = list(vm_src_node.keys())[0] input() 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] vm_uuid = str(uuid.uuid4()) self.vm_deploy(vm_src_node, vm_uuid) signal.signal(signal.SIGINT, self.catch_signal) src_id = vm_src_node dst_id = vm_dst_node self.flag = True while self.flag: print( "########################################################################" ) print( " press enter to migrate #####################################" ) print("####################################################") print("########### source %s" % src_id) print("########### destination %s" % dst_id) input() self.migrate_vm(src_id, dst_id, vm_uuid) time.sleep(5) temp = src_id src_id = dst_id dst_id = temp #self.migrate_vm(vm_src_node, vm_dst_node, vm_uuid) input() self.vm_destroy(src_id, vm_uuid) exit(0)
class Controll(): def __init__(self): self.uuid = uuid.uuid4() sid = str(self.uuid) self.root = "fos://<sys-id>" self.home = str("fos://<sys-id>/{}".format(sid)) self.nodes = {} # Notice that a node may have multiple caches, but here we are # giving the same id to the nodew and the cache self.store = DStore(sid, self.root, self.home, 1024) def nodeDiscovered(self, uri, value, v=None): value = json.loads(value) print(value) if uri != str('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 }}) self.show_nodes() def test_observer(self, key, value, v): print("###########################") print("##### I'M an Observer #####") print("## Key: {}".format(key)) print("## Value: {}".format(value)) print("## V: {}".format(v)) print("###########################") print("###########################") 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 lot_of_puts(self, node_uuid): for i in range(0, 2): val = {'test': "put no: {}".format(i)} uri = 'fos://<sys-id>/{}/info'.format(node_uuid) self.store.put(uri, json.dumps(val)) def main(self, master=True): uri = str('fos://<sys-id>/*') self.store.observe(uri, self.nodeDiscovered) key42 = 'fos://<sys-id>/42' key21 = 'fos://<sys-id>/21' if master: print("Running as Master") while len(self.nodes) == 0: print("Looking for stores...") self.show_nodes() time.sleep(2) node_uuid = self.nodes.get(1) if node_uuid is not None: node_uuid = list(node_uuid.keys())[0] if node_uuid is not None: pass else: exit() input("Press enter to send") print(" >>>> This should not cause a miss! <<<<< ") v = self.store.get(key42) print("Reolved: {0}".format(v)) print(" >>>> This shoud miss! <<<<< ") v = self.store.get(key21) print("Reolved: {0}".format(v)) self.lot_of_puts(node_uuid) else: print("Running as delegate") val = {'uuid': str(self.uuid), "name": "slave"} uri = str('fos://<sys-id>/{}/'.format(self.uuid)) val42 = {'key': key42, 'value': 'the ultimate truth'} self.store.put(key42, json.dumps(val42)) self.store.put(uri, json.dumps(val)) uri = str('fos://<sys-id>/{}/info'.format(self.uuid)) self.store.observe(uri, self.test_observer) while True: print("Delegate done with writing...") time.sleep(10) input() exit(0)
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 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 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) while True: uri = str('afos://<sys-id>/*/') nodes = json.loads(self.astore.get(uri)) print(nodes) time.sleep(10)
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)
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 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': [{ '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 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) vm_uuid = str(uuid.uuid4()) vm2_uuid = str(uuid.uuid4()) input("Press enter to create vms") 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.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)