sfaclientlib improved
[sfa.git] / sfa / examples / miniclient.py
1 #!/usr/bin/env python
2
3 # this is designed to use a totally empty new directory
4 # so we demonstrate how to bootstrap the whole thing
5
6 # init logging on console
7 import logging
8 console = logging.StreamHandler()
9 logger=logging.getLogger('')
10 logger.addHandler(console)
11 logger.setLevel(logging.DEBUG)
12
13 import uuid
14 def unique_call_id(): return uuid.uuid4().urn
15
16 # use sys.argv to point to a completely fresh directory
17 import sys
18 args=sys.argv[1:]
19 if len(args)!=1:
20     print "Usage: %s directory"%sys.argv[0]
21     sys.exit(1)
22 dir=args[0]
23 logger.debug('sfaclientsample: Using directory %s'%dir)
24
25 ###
26
27 # this uses a test sfa deployment at openlab
28 registry_url="http://sfa1.pl.sophia.inria.fr:12345/"
29 aggregate_url="http://sfa1.pl.sophia.inria.fr:12347/"
30 # this is where the private key sits - would be ~/.ssh/id_rsa in most cases
31 # but in this context, create this local file
32 # the tests key pair can be found in
33 # http://git.onelab.eu/?p=tests.git;a=blob;f=system/config_default.py
34 # search for public_key / private_key
35 private_key="miniclient-private-key"
36 # user hrn
37 user_hrn="pla.inri.fake-pi1"
38
39 slice_hrn="pla.inri.slpl1"
40 # hrn_to_urn(slice_hrn,'slice')
41 slice_urn='urn:publicid:IDN+pla:inri+slice+slpl1'
42
43 from sfa.client.sfaclientlib import SfaClientBootstrap
44
45 bootstrap = SfaClientBootstrap (user_hrn, registry_url, dir=dir, logger=logger)
46 # install the private key in the client directory from 'private_key'
47 bootstrap.init_private_key_if_missing(private_key)
48
49 def truncate(content, length=20, suffix='...'):
50     if isinstance (content, (int) ): return content
51     if isinstance (content, list): return truncate ( "%s"%content, length, suffix)
52     if len(content) <= length:
53         return content
54     else:
55         return content[:length+1]+ ' '+suffix
56
57 def has_call_id (server_version):
58     return server_version.has_key('call_id_support')
59
60
61 ### issue a GetVersion call
62 ### this assumes we've already somehow initialized the certificate
63 def get_version (url):
64     # make sure we have a self-signed cert
65     bootstrap.self_signed_cert()
66     server_proxy = bootstrap.server_proxy_simple(url)
67     server_version = server_proxy.GetVersion()
68     print "miniclient: GetVersion at %s returned:"%(url)
69     for (k,v) in server_version.iteritems(): print "miniclient: \tversion[%s]=%s"%(k,truncate(v))
70     print "has-call-id=",has_call_id(server_version)
71
72 # version_dict = {'type': 'SFA', 'version': '1', }
73
74 version_dict = {'type':'ProtoGENI', 'version':'2'}
75
76
77 # ditto with list resources
78 def list_resources ():
79     bootstrap.bootstrap_my_gid()
80     credential = bootstrap.my_credential_string()
81     credentials = [ credential ]
82     options = {}
83     options [ 'geni_rspec_version' ] = version_dict
84 #    options [ 'call_id' ] = unique_call_id()
85     list_resources = bootstrap.server_proxy (aggregate_url).ListResources(credentials,options)
86     print "miniclient: ListResources at %s returned : %s"%(aggregate_url,truncate(list_resources))
87
88 def list_slice_resources ():
89     bootstrap.bootstrap_my_gid()
90     credential = bootstrap.slice_credential_string (slice_hrn)
91     credentials = [ credential ]
92     options = { }
93     options [ 'geni_rspec_version' ] = version_dict
94     options [ 'geni_slice_urn' ] = slice_urn
95 #    options [ 'call_id' ] = unique_call_id()
96     list_resources = bootstrap.server_proxy (aggregate_url).ListResources(credentials,options)
97     print "miniclient: ListResources at %s for slice %s returned : %s"%(aggregate_url,slice_urn,truncate(list_resources))
98     
99     
100     
101
102 def main ():
103     get_version(registry_url)
104     get_version(aggregate_url)
105 #    list_resources()
106 #    list_slice_resources()
107
108 main()