passing the hrn of the initial caller instead of its credentail
[sfa.git] / sfa / managers / aggregate_manager_pl.py
1 ### $Id: slices.py 15842 2009-11-22 09:56:13Z anil $
2 ### $URL: https://svn.planet-lab.org/svn/sfa/trunk/sfa/plc/slices.py $
3
4 import datetime
5 import time
6 import traceback
7 import sys
8
9 from types import StringTypes
10 from sfa.util.misc import *
11 from sfa.util.rspec import *
12 from sfa.util.specdict import *
13 from sfa.util.faults import *
14 from sfa.util.record import GeniRecord
15 from sfa.util.policy import Policy
16 from sfa.util.record import *
17 from sfa.util.sfaticket import SfaTicket
18 from sfa.server.registry import Registries
19 from sfa.util.debug import log
20 import sfa.plc.peers as peers
21
22 def delete_slice(api, hrn):
23     slicename = hrn_to_pl_slicename(hrn)
24     slices = api.plshell.GetSlices(api.plauth, {'name': slicename})
25     if not slices:
26         return 1
27     slice = slices[0]
28
29     # determine if this is a peer slice
30     peer = peers.get_peer(api, hrn)
31     if peer:
32         api.plshell.UnBindObjectFromPeer(api.plauth, 'slice', slice['slice_id'], peer)
33     api.plshell.DeleteSliceFromNodes(api.plauth, slicename, slice['node_ids'])
34     if peer:
35         api.plshell.BindObjectToPeer(api.plauth, 'slice', slice['slice_id'], peer, slice['peer_slice_id'])
36     return 1
37
38 def create_slice(api, hrn, rspec):
39     # XX just import the legacy module and excute that until
40     # we transition the code to this module
41     from sfa.plc.slices import Slices
42     slices = Slices(api)
43     slices.create_slice(hrn, rspec)
44
45 def get_ticket(api, slice_hrn, rspec):
46     # the the slice record
47     registries = Registries(api)
48     registry = registries[api.hrn]
49     credential = self.api.getCredential()
50     records = registry.resolve(credential, slice_hrn)
51     
52     # make sure we get a local slice record
53     record = None  
54     for tmp_record in records:
55         if record['type'] == 'slice' and \
56            not record['peer_authority']:
57             record = SliceRecord(dict=tmp_record)
58     if not record:
59         raise RecordNotFound(slice_hrn)
60
61     # get sliver info
62     slivers = Slices(api).get_slivers(slice_hrn)
63     if not slivers:
64         raise SliverDoesNotExist(slice_hrn)
65     
66     # get initscripts
67     initscripts = None
68     data = {
69         'timestamp': int(time.time()),
70         'initscripts': initscripts,
71         'slivers': slivers
72     }
73     
74     # create the ticket
75     auth_hrn = record['authority'] 
76     auth_info = api.auth.get_auth_info(auth_hrn)
77     object_gid = record.get_gid_object()
78     new_ticket = SfaTicket(subject = object_gid.get_subject())
79     new_ticket.set_gid_caller(api.auth.client_gid)
80     new_ticket.set_gid_object(object_gid)
81     new_ticket.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
82     new_ticket.set_pubkey(object_gid.get_pubkey())
83     new_ticket.set_attributes(data)
84     new_ticket.set_rspec(rspec)
85     new_ticket.set_parent(api.auth.hierarchy.get_auth_ticket(auth_hrn))
86     new_ticket.encode()
87     new_ticket.sign()
88
89     return new_ticket.save_to_string(save_parents=True)
90
91 def start_slice(api, hrn):
92     slicename = hrn_to_pl_slicename(hrn)
93     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
94     if not slices:
95         raise RecordNotFound(hrn)
96     slice_id = slices[0]
97     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
98     attribute_id = attreibutes[0]['slice_attribute_id']
99     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "1" )
100
101     return 1
102  
103 def stop_slice(api, hrn):
104     slicename = hrn_to_pl_slicename(hrn)
105     slices = api.plshell.GetSlices(api.plauth, {'name': slicename}, ['slice_id'])
106     if not slices:
107         raise RecordNotFound(hrn)
108     slice_id = slices[0]['slice_id']
109     attributes = api.plshell.GetSliceTags(api.plauth, {'slice_id': slice_id, 'name': 'enabled'}, ['slice_attribute_id'])
110     attribute_id = attributes[0]['slice_attribute_id']
111     api.plshell.UpdateSliceTag(api.plauth, attribute_id, "0")
112     return 1
113
114 def reset_slice(api, hrn):
115     # XX not implemented at this interface
116     return 1
117
118 def get_slices(api):
119     # XX just import the legacy module and excute that until
120     # we transition the code to this module
121     from sfa.plc.slices import Slices
122     slices = Slices(api)
123     slices.refresh()
124     return slices['hrn']
125      
126  
127 def get_rspec(api, hrn=None, origin_hrn=None):
128     from sfa.plc.nodes import Nodes
129     nodes = Nodes(api, origin_hrn=origin_hrn)
130     if hrn:
131         rspec = nodes.get_rspec(hrn)
132     else:
133         nodes.refresh()
134         rspec = nodes['rspec'] 
135
136     return rspec
137
138 """
139 Returns the request context required by sfatables. At some point, this mechanism should be changed
140 to refer to "contexts", which is the information that sfatables is requesting. But for now, we just
141 return the basic information needed in a dict.
142 """
143 def fetch_context(slice_hrn, user_hrn, contexts):
144     base_context = {'sfa':{'user':{'hrn':user_hrn}}}
145     return base_context
146
147 def main():
148     r = RSpec()
149     r.parseFile(sys.argv[1])
150     rspec = r.toDict()
151     create_slice(None,'plc.princeton.tmacktestslice',rspec)
152
153 if __name__ == "__main__":
154     main()