HowTo/GenerateRSpecUsingGENILIB: examples.py

File examples.py, 4.1 KB (added by xliu@bbn.com, 5 years ago)
Line 
1# -*- coding: utf-8 -*-
2
3'''
4This code is to test geni-lib
5
6by Xuan Liu
7
8July 7, 2014
9'''
10
11import xliu_config
12
13import geni.aggregate.instageni as ig
14import geni.aggregate.exogeni as eg
15import geni.rspec.pg as pg
16
17# User context information
18CONTEXT = xliu_config.buildContext()
19
20
21def get_am_sites(am_type):
22    """
23    get a list of sites of a specific aggregate type
24    ig: instageni
25    eg: exogeni
26    """
27    am_list = []
28    if am_type == 'ig':
29        for idx, site in enumerate(ig.aggregates()):
30            #print idx, site
31            am_list.append(site)
32    elif am_type == 'eg':
33        for idx, site in enumerate(eg.aggregates()):
34            am_list.append(site)
35    return am_list
36       
37
38def get_res(context, ig_site):
39    '''Get resource of a ig site, and print to a file '''
40    try:
41        ad = ig_site.listresources(context)
42        f = open("%s-ad.xml" % (ig_site.name), "w+")
43        f.write(ad.text)
44        f.close()
45        print "[%s] Done" % (ig_site.name)
46    except:
47        print "[%s] OFFLINE" % (ig_site.name)
48
49def two_nodes_rspec(context):
50    ''' Create an RSpec for a simple two-node link'''
51    # provide the slicename
52    slicename = 'xuan-genilib'
53
54    # delete previous sliver
55    #ig.UtahDDC.deletesliver(context, "xuan-genilib")   
56   
57    # create RSpec object
58    rspec = pg.Request()
59   
60    # create node1
61    vm1 = pg.XenVM("node1")
62    vm1_iface1 = vm1.addInterface("if0")
63    vm1_iface1.component_id = "eth1"
64    vm1.disk_image = 'https://www.emulab.net/image_metadata.php?uuid=bea6f9b2-f59b-11e3-8053-001143e453fe'
65    rspec.addResource(vm1)
66   
67    # create node2
68    vm2 = pg.XenVM("node2")
69    vm2_iface1 = vm2.addInterface("if0")
70    vm2_iface1.component_id = "eth1"
71    vm2.disk_image = 'https://www.emulab.net/image_metadata.php?uuid=bea6f9b2-f59b-11e3-8053-001143e453fe'
72    rspec.addResource(vm2)
73   
74    # create link node2 <--> node1
75    link = pg.Link()
76    link.addInterface(vm1_iface1)
77    link.addInterface(vm2_iface1)
78    rspec.addResource(link)
79   
80    # create RSpec xml file
81    rspec.write("test.xml")
82   
83    # create sliver
84    manifest = ig.UtahDDC.createsliver(context, slicename, rspec)
85    print manifest.text
86   
87
88def deletesliver(context, am, slicename):
89    '''Delete slivers from a AM in a Slice'''
90    am.deletesliver(context, slicename)
91   
92def custom_image_rspec(context):
93    '''Create RSpec with custom disk image'''
94    slicename = 'xuan-genilib'
95    am = ig.Clemson
96   
97    rspec = pg.Request()
98    vm1 = pg.XenVM("node1")
99    vm1_iface1 = vm1.addInterface("if0")
100    vm1_iface1.component_id = "eth1"
101    vm1_iface1.addAddress(pg.IPv4Address('192.168.1.1', '255.255.255.0'))
102    vm1.disk_image = 'https://www.emulab.net/image_metadata.php?uuid=bea6f9b2-f59b-11e3-8053-001143e453fe'
103    vm1.addService(pg.Install(url="http://geni-myvini.umkc.gpeni.net/resources/experiments/xorp_autostart.tar.gz", path="/local"))
104    vm1.addService(pg.Install(url="http://geni-myvini.umkc.gpeni.net/resources/experiments/xorp_run.tar.gz", path="/local"))
105    vm1.addService(pg.Execute(shell="bash", command="/bin/bash /local/xorp_autostart/start-xorp.sh"))
106    rspec.addResource(vm1)
107   
108    # create node2
109    vm2 = pg.XenVM("node2")
110    vm2_iface1 = vm2.addInterface("if0")
111    vm2_iface1.component_id = "eth1"
112    vm2_iface1.addAddress(pg.IPv4Address('192.168.1.2', '255.255.255.0'))
113    vm2.disk_image = 'https://www.emulab.net/image_metadata.php?uuid=bea6f9b2-f59b-11e3-8053-001143e453fe'
114    vm2.addService(pg.Install(url="http://geni-myvini.umkc.gpeni.net/resources/experiments/xorp_autostart.tar.gz", path="/local"))
115    vm2.addService(pg.Install(url="http://geni-myvini.umkc.gpeni.net/resources/experiments/xorp_run.tar.gz", path="/local"))
116    vm2.addService(pg.Execute(shell="bash", command="/bin/bash /local/xorp_autostart/start-xorp.sh"))   
117    rspec.addResource(vm2)
118
119    # create link node2 <--> node1
120    link = pg.LAN('lan0')
121    link.addInterface(vm1_iface1)
122    link.addInterface(vm2_iface1)
123    rspec.addResource(link)
124
125    rspec.write("test.xml")   
126   
127    # create slivers
128    manifest = am.createsliver(context, slicename, rspec)
129   
130    print manifest.text
131   
132
133   
134       
135   
136   
137