12
12
INTERACTIVE_SERVICES_PREFIX = 'simcore/services/dynamic/'
13
13
COMPUTATIONAL_SERVICES_PREFIX = 'simcore/services/comp/'
14
14
_SESSION = Session ()
15
- log = logging .getLogger (__name__ )
15
+ _logger = logging .getLogger (__name__ )
16
16
17
17
def setup_registry_connection ():
18
- log .debug ("Setup registry connection started...%s" , config .REGISTRY_AUTH )
18
+ _logger .debug ("Setup registry connection started...%s" , config .REGISTRY_AUTH )
19
19
20
20
# get authentication state or set default value
21
21
if config .REGISTRY_AUTH :
22
- log .debug ("Authentifying registry..." )
22
+ _logger .debug ("Authentifying registry..." )
23
23
if not config .REGISTRY_USER :
24
24
raise exceptions .DirectorException ("User to access to registry is not defined" )
25
25
if not config .REGISTRY_PW :
26
26
raise exceptions .DirectorException ("PW to access to registry is not defined" )
27
27
_SESSION .auth = (config .REGISTRY_USER , config .REGISTRY_PW )
28
- log .debug ("Session authorization complete" )
28
+ _logger .debug ("Session authorization complete" )
29
29
30
30
def list_computational_services ():
31
31
return __list_services (COMPUTATIONAL_SERVICES_PREFIX )
@@ -34,12 +34,12 @@ def list_interactive_services():
34
34
return __list_services (INTERACTIVE_SERVICES_PREFIX )
35
35
36
36
def get_service_details (service_key , service_version ):
37
- return _get_repo_version_details (service_key , service_version )
37
+ return __get_repo_version_details (service_key , service_version )
38
38
39
39
def retrieve_list_of_images_in_repo (repository_name ):
40
40
request_result = __registry_request (repository_name + '/tags/list' )
41
41
result_json = request_result .json ()
42
- log .info ("retrieved list of images in %s: %s" ,repository_name , result_json )
42
+ _logger .info ("retrieved list of images in %s: %s" ,repository_name , result_json )
43
43
return result_json
44
44
45
45
def list_interactive_service_dependencies (service_key ):
@@ -54,9 +54,9 @@ def list_interactive_service_dependencies(service_key):
54
54
# ok let's get the dependencies
55
55
dependencies = []
56
56
repos = __retrieve_list_of_repositories ()
57
- service_name = get_service_name (service_key , prefix )
57
+ service_name = get_service_first_name (service_key )
58
58
for repo in repos :
59
- if get_service_name (repo , prefix ) == service_name :
59
+ if get_service_first_name (repo ) == service_name :
60
60
if repo == service_key :
61
61
continue
62
62
dependencies .append (repo )
@@ -67,64 +67,64 @@ def retrieve_labels_of_image(image, tag):
67
67
result_json = request_result .json ()
68
68
labels = json .loads (result_json ["history" ][0 ]["v1Compatibility" ])[
69
69
"container_config" ]["Labels" ]
70
- log .info ("retrieved labels of image %s:%s: %s" , image , tag , result_json )
70
+ _logger .info ("retrieved labels of image %s:%s: %s" , image , tag , result_json )
71
71
return labels
72
72
73
- def get_service_name (repository_name , service_prefix ):
74
- service_name_suffixes = str (repository_name )[len (service_prefix ):]
75
- log .info ("retrieved service name from repo %s : %s" , repository_name , service_name_suffixes )
73
+ def get_service_first_name (repository_name ):
74
+ if str (repository_name ).startswith (INTERACTIVE_SERVICES_PREFIX ):
75
+ service_name_suffixes = str (repository_name )[len (INTERACTIVE_SERVICES_PREFIX ):]
76
+ elif str (repository_name ).startswith (COMPUTATIONAL_SERVICES_PREFIX ):
77
+ service_name_suffixes = str (repository_name )[len (COMPUTATIONAL_SERVICES_PREFIX ):]
78
+ else :
79
+ return "invalid service"
80
+
81
+ _logger .info ("retrieved service name from repo %s : %s" , repository_name , service_name_suffixes )
76
82
return service_name_suffixes .split ('/' )[0 ]
77
83
78
- def get_interactive_service_sub_name (repository_name ):
79
- return __get_service_sub_name (repository_name , INTERACTIVE_SERVICES_PREFIX )
84
+ def get_service_last_names (repository_name ):
85
+ if str (repository_name ).startswith (INTERACTIVE_SERVICES_PREFIX ):
86
+ service_name_suffixes = str (repository_name )[len (INTERACTIVE_SERVICES_PREFIX ):]
87
+ elif str (repository_name ).startswith (COMPUTATIONAL_SERVICES_PREFIX ):
88
+ service_name_suffixes = str (repository_name )[len (COMPUTATIONAL_SERVICES_PREFIX ):]
89
+ else :
90
+ return "invalid service"
91
+ service_last_name = str (service_name_suffixes ).replace ("/" , "_" )
92
+ _logger .info ("retrieved service last name from repo %s : %s" , repository_name , service_last_name )
93
+ return service_last_name
80
94
81
95
def __registry_request (path , method = "GET" ):
82
96
if not config .REGISTRY_URL :
83
97
raise exceptions .DirectorException ("URL to registry is not defined" )
84
- # TODO: is is always ssh?
85
- api_url = 'https://' + config .REGISTRY_URL + '/v2/' + path
98
+
99
+ if config .REGISTRY_SSL :
100
+ api_url = 'https://' + config .REGISTRY_URL + '/v2/' + path
101
+ else :
102
+ api_url = 'http://' + config .REGISTRY_URL + '/v2/' + path
86
103
87
104
try :
88
105
# r = s.get(api_url, verify=False) #getattr(s, method.lower())(api_url)
89
106
request_result = getattr (_SESSION , method .lower ())(api_url )
90
- log .info ("Request status: %s" ,request_result .status_code )
91
- if request_result .status_code > 399 :
107
+ _logger .info ("Request status: %s" ,request_result .status_code )
108
+ if request_result .status_code > 399 :
92
109
request_result .raise_for_status ()
93
110
94
111
return request_result
95
112
except HTTPError as err :
96
- log .exception ("HTTP error returned while accessing registry" )
113
+ _logger .exception ("HTTP error returned while accessing registry" )
97
114
if err .response .status_code == 404 :
98
115
raise exceptions .ServiceNotAvailableError (path , None ) from err
99
116
raise exceptions .RegistryConnectionError ("Error while accessing docker registry" ,err ) from err
100
117
except RequestException as err :
101
- log .exception ("Error while connecting to docker registry" )
118
+ _logger .exception ("Error while connecting to docker registry" )
102
119
raise exceptions .DirectorException (str (err )) from err
103
120
104
121
def __retrieve_list_of_repositories ():
105
122
request_result = __registry_request ('_catalog' )
106
123
result_json = request_result .json ()['repositories' ]
107
- log .info ("retrieved list of repos: %s" , result_json )
124
+ _logger .info ("retrieved list of repos: %s" , result_json )
108
125
return result_json
109
126
110
-
111
- def __has_service_sub_name (repository_name , service_prefix ):
112
- try :
113
- __get_service_sub_name (repository_name , service_prefix )
114
- return True
115
- except exceptions .ServiceNotAvailableError :
116
- return False
117
-
118
- def __get_service_sub_name (repository_name , service_prefix ):
119
- service_name_suffixes = str (repository_name )[len (service_prefix ):]
120
- list_of_suffixes = service_name_suffixes .split ('/' )
121
- last_suffix_index = len (list_of_suffixes ) - 1
122
- if last_suffix_index < 0 :
123
- raise exceptions .ServiceNotAvailableError (repository_name , None )
124
- log .info ("retrieved service sub name from repo %s : %s" , repository_name , list_of_suffixes )
125
- return list_of_suffixes [last_suffix_index ]
126
-
127
- def _get_repo_version_details (repo_key , repo_tag ):
127
+ def __get_repo_version_details (repo_key , repo_tag ):
128
128
image_tags = {}
129
129
label_request = __registry_request (repo_key + '/manifests/' + repo_tag )
130
130
label_data = label_request .json ()
@@ -137,31 +137,32 @@ def _get_repo_version_details(repo_key, repo_tag):
137
137
image_tags [label_key ] = label_data [label_key ]
138
138
return image_tags
139
139
140
- def _get_repo_details (repo ):
140
+ def __get_repo_details (repo ):
141
141
#pylint: disable=too-many-nested-blocks
142
142
current_repo = []
143
143
if "/comp/" in repo or "/dynamic/" in repo :
144
144
# get list of repo versions
145
145
req_images = __registry_request (repo + '/tags/list' )
146
146
im_data = req_images .json ()
147
147
tags = im_data ['tags' ]
148
- for tag in tags :
149
- image_tags = _get_repo_version_details (repo , tag )
150
- if image_tags :
151
- current_repo .append (image_tags )
148
+ if tags :
149
+ for tag in tags :
150
+ image_tags = __get_repo_version_details (repo , tag )
151
+ if image_tags :
152
+ current_repo .append (image_tags )
152
153
153
154
return current_repo
154
155
155
156
def __list_services (service_prefix ):
156
- log .info ("getting list of computational services" )
157
+ _logger .info ("getting list of computational services" )
157
158
list_all_repos = __retrieve_list_of_repositories ()
158
159
# get the services repos
159
160
list_of_specific_repos = [repo for repo in list_all_repos if str (repo ).startswith (service_prefix )]
160
- log .info ("retrieved list of computational repos : %s" , list_of_specific_repos )
161
+ _logger .info ("retrieved list of computational repos : %s" , list_of_specific_repos )
161
162
repositories = []
162
163
# or each repo get all tags details
163
164
for repo in list_of_specific_repos :
164
- details = _get_repo_details (repo )
165
+ details = __get_repo_details (repo )
165
166
for repo_detail in details :
166
167
repositories .append (repo_detail )
167
168
0 commit comments