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