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