1313import os
1414import re
1515import sys
16- import signal
1716import uuid
1817import boto3
1918
2019import openstack
2120
22- # Globals
21+
2322TESTCONTNAME = "scs-test-container"
24- EC2CRED = None
25- OSCONN = None
2623
2724logger = logging .getLogger (__name__ )
2825mandatory_services = ["compute" , "identity" , "image" , "network" ,
3128
3229
3330def check_presence_of_mandatory_services (conn : openstack .connection .Connection , s3_credentials = None ):
34- "Go over list of mandatory services and ensure they are all in the catalog"
3531 services = conn .service_catalog
3632
37- # We don't require swift if S3 can be found otherwise
3833 if s3_credentials :
3934 mandatory_services .remove ("object-store" )
4035 for svc in services :
@@ -118,36 +113,10 @@ def s3_from_env(creds, fieldnm, env, prefix=""):
118113 logger .warning (f"s3_creds[{ fieldnm } ] not set" )
119114
120115
121- def cleanup_ec2_cred (conn ):
122- "Remove ec2_cred if created"
123- if conn and EC2CRED :
124- conn .identity .delete_credential (EC2CRED )
125-
126-
127- def breakhandler (sig , frame ):
128- "Clean up created ec2 credential is any and reraise signal"
129- # global OSCONN
130- # print(f"Handle signal {signal.strsignal(sig)}: Conn {os_conn}, clean cred {ec2_cred}", file=sys.stderr)
131- cleanup_ec2_cred (OSCONN )
132- signal .signal (sig , signal .SIG_DFL )
133- signal .raise_signal (sig )
134-
135-
136- def install_sighandler (conn ):
137- "Set OpenStack connection and install signal handler"
138- global OSCONN
139- OSCONN = conn
140- signal .signal (signal .SIGINT , breakhandler )
141- signal .signal (signal .SIGTERM , breakhandler )
142- signal .signal (signal .SIGHUP , breakhandler )
143- signal .signal (signal .SIGPIPE , breakhandler )
144-
145-
146116def s3_from_ostack (creds , conn , endpoint ):
147- """Fill in creds from openstack swift/keystone
148- Sets global ec2_cred *if* an ec2 credential was created,
149- """
150- global EC2CRED
117+ """Set creds from openstack swift/keystone
118+ Returns credential ID *if* an ec2 credential was created,
119+ None otherwise."""
151120 rgx = re .compile (r"^(https*://[^/]*)/" )
152121 match = rgx .match (endpoint )
153122 if match :
@@ -161,27 +130,25 @@ def s3_from_ostack(creds, conn, endpoint):
161130 ec2_dict = eval (ec2_creds [0 ].blob , {"null" : None })
162131 creds ["AK" ] = ec2_dict ["access" ]
163132 creds ["SK" ] = ec2_dict ["secret" ]
164- return
133+ return None
165134 # Generate keyid and secret
166135 ak = uuid .uuid4 ().hex
167136 sk = uuid .uuid4 ().hex
168137 blob = f'{{"access": "{ ak } ", "secret": "{ sk } "}}'
169138 try :
170- install_sighandler (conn )
171139 crd = conn .identity .create_credential (type = "ec2" , blob = blob ,
172140 user_id = conn .current_user_id ,
173141 project_id = conn .current_project_id )
174142 creds ["AK" ] = ak
175143 creds ["SK" ] = sk
176- EC2CRED = crd .id
144+ return crd .id
177145 except BaseException as excn :
178146 logger .warning (f"ec2 creds creation failed: { excn !s} " )
179147 # pass
180- return
148+ return None
181149
182150
183151def check_for_s3_and_swift (conn : openstack .connection .Connection , s3_credentials = None ):
184- "Check S3 presence; either from S3_ env or swift."
185152 # If we get credentials, we assume that there is no Swift and only test s3
186153 if s3_credentials :
187154 try :
@@ -209,7 +176,7 @@ def check_for_s3_and_swift(conn: openstack.connection.Connection, s3_credentials
209176 )
210177 return 1
211178 # Get S3 endpoint (swift) and ec2 creds from OpenStack (keystone)
212- s3_from_ostack (s3_creds , conn , endpoint )
179+ ec2_cred = s3_from_ostack (s3_creds , conn , endpoint )
213180 # Overrides (var names are from libs3, in case you wonder)
214181 s3_from_env (s3_creds , "HOST" , "S3_HOSTNAME" , "https://" )
215182 s3_from_env (s3_creds , "AK" , "S3_ACCESS_KEY_ID" )
@@ -244,12 +211,12 @@ def check_for_s3_and_swift(conn: openstack.connection.Connection, s3_credentials
244211 if s3_buckets == [TESTCONTNAME ]:
245212 del_bucket (s3 , TESTCONTNAME )
246213 # Clean up ec2 cred IF we created one
247- cleanup_ec2_cred (conn )
214+ if ec2_cred :
215+ conn .identity .delete_credential (ec2_cred )
248216 return result
249217
250218
251219def main ():
252- "Main function"
253220 parser = argparse .ArgumentParser (
254221 description = "SCS Mandatory IaaS Service Checker" )
255222 parser .add_argument (
0 commit comments