1c625e0795ccf54b7a235f8c453d906128acd568
[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
58 ### issue a GetVersion call
59 ### this assumes we've already somehow initialized the certificate
60 def get_version (url):
61     # make sure we have a self-signed cert
62     bootstrap.self_signed_cert()
63     server_proxy = bootstrap.server_proxy_simple(url)
64     server_version = server_proxy.GetVersion()
65     print "miniclient: GetVersion at %s returned:"%(url)
66     for (k,v) in server_version.iteritems(): print "miniclient: \tversion[%s]=%s"%(k,truncate(v))
67
68 # version_dict = {'type': 'SFA', 'version': '1', }
69
70 version_dict = {'type':'ProtoGENI', 'version':'2'}
71
72
73 # ditto with list resources
74 def list_resources ():
75     bootstrap.bootstrap_my_gid()
76     credential = bootstrap.my_credential_string()
77     credentials = [ credential ]
78     options = {}
79     options [ 'geni_rspec_version' ] = version_dict
80     options [ 'call_id' ] = unique_call_id()
81     list_resources = bootstrap.server_proxy (aggregate_url).ListResources(credentials,options)
82     print "miniclient: ListResources at %s returned : %s"%(aggregate_url,truncate(list_resources))
83
84 def list_slice_resources ():
85     bootstrap.bootstrap_my_gid()
86     credential = bootstrap.slice_credential_string (slice_hrn)
87     credentials = [ credential ]
88     options = { }
89     options [ 'geni_rspec_version' ] = version_dict
90     options [ 'geni_slice_urn' ] = slice_urn
91     options [ 'call_id' ] = unique_call_id()
92     list_resources = bootstrap.server_proxy (aggregate_url).ListResources(credentials,options)
93     print "miniclient: ListResources at %s for slice %s returned : %s"%(aggregate_url,slice_urn,truncate(list_resources))
94     
95     
96
97 def main ():
98     get_version(registry_url)
99     get_version(aggregate_url)
100     list_resources()
101     list_slice_resources()
102
103 main()