cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
get_latest_dcp.py
Go to the documentation of this file.
1 # /*******************************************************************************
2 # * Copyright 2016 -- 2022 IBM Corporation
3 # *
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # *******************************************************************************/
16 
17 # *
18 # * cloudFPGA
19 # * =============================================
20 # * Created: Oct 2021
21 # * Authors: FAB, WEI, NGL, DID
22 # *
23 # * Description:
24 # * Python script to download latest dcp file to ./dcps/
25 # *
26 
27 import os
28 import json
29 import requests
30 
31 __cfp_json_path__ = "/../cFp.json"
32 __shell_type_key__ = 'cFpSRAtype'
33 __mod_type_key__ = 'cFpMOD'
34 __dcps_folder_name__ = '/dcps/'
35 
36 __credentials_file_name__ = "user.json"
37 
38 __openstack_user_template__ = {'credentials': {'username': "your user name", 'password': "your user password"},
39  'project': "default"}
40 
41 __cf_manager_url__ = "10.12.0.132:8080"
42 
43 
44 def load_user_credentials(filedir):
45  json_file = filedir + "/" + __credentials_file_name__
46  global __openstack_user__
47  global __openstack_pw__
48  global __openstack_project__
49 
50  try:
51  with open(json_file, 'r') as infile:
52  data = json.load(infile)
53  __openstack_user__ = data['credentials']['username']
54  __openstack_pw__ = data['credentials']['password']
55  if 'project' in data:
56  __openstack_project__ = data['project']
57  return 0
58  except Exception as e:
59  print(e)
60  print("Writing credentials template to {}\n".format(json_file))
61 
62  with open(json_file, 'w') as outfile:
63  json.dump(__openstack_user_template__, outfile)
64  return -1
65 
66 
67 def main():
68  me_abs = os.path.dirname(os.path.realpath(__file__))
69  cfp_json_file = me_abs + __cfp_json_path__
70  debugging_flow = os.environ.get('CFP_DEBUGGING')
71  if debugging_flow is not None:
72  cfp_json_file = me_abs + debugging_flow + '/cFp.json'
73  with open(cfp_json_file, 'r') as json_file:
74  cFp_data = json.load(json_file)
75 
76  # 1. check folders and file names
77  root_abs = os.path.realpath(me_abs+"/../")
78  if debugging_flow is not None:
79  root_abs = os.path.realpath(me_abs + debugging_flow + "/env/" + "/../")
80  cFp_data['abs_path'] = root_abs
81  dcps_folder = root_abs + __dcps_folder_name__
82  os.system("mkdir -p {}".format(dcps_folder)) # to be sure
83  dcp_file_name = "3_top{}_STATIC.dcp".format(cFp_data[__mod_type_key__])
84  target_file_name = os.path.abspath(dcps_folder + "/" + dcp_file_name)
85  meta_file_name = "3_top{}_STATIC.json".format(cFp_data[__mod_type_key__])
86  target_meta_name = os.path.abspath(dcps_folder + "/" + meta_file_name)
87 
88  shell_type = cFp_data[__shell_type_key__]
89 
90  # 2. check credentials
91  if load_user_credentials(root_abs) == -1:
92  print("Please save your (escaped) OpenStack credentials in {}/{}".format(root_abs, __credentials_file_name__))
93  exit(1)
94 
95  cfrm_url = __cf_manager_url__
96  if debugging_flow is not None:
97  cfrm_url = "localhost:8080"
98 
99  # 3. check if version and cert is still the same
100  current_id = -1
101  current_cert = '-1'
102  # check if both, dcp and meta exists
103  if os.path.isfile(target_meta_name) and os.path.isfile(target_file_name):
104  with open(target_meta_name, 'r') as meta_file:
105  cur_meta = json.load(meta_file)
106  current_cert = cur_meta['cert']
107  current_id = cur_meta['id']
108  # for Mantles
109  if 'pl_id' in cur_meta:
110  current_id = cur_meta['pl_id']
111 
112  requests_error = False
113  try:
114  r1 = requests.get("http://"+cfrm_url+"/composablelogic/by_shell/"+str(shell_type)
115  +"?username={0}&password={1}".format(__openstack_user__, __openstack_pw__))
116  except Exception as e:
117  print(str(e))
118  requests_error = True
119  if requests_error or r1.status_code != 200:
120  print("ERROR: Failed to connect to CFRM ({}). STOP.".format(r1.status_code))
121  exit(1)
122 
123  r1_list = json.loads(r1.text)
124  latest_shell_id = r1_list[-1]['id']
125 
126  # get meta
127  try:
128  r3 = requests.get("http://"+cfrm_url+"/composablelogic/"+str(latest_shell_id)
129  +"/meta?username={0}&password={1}".format(__openstack_user__, __openstack_pw__))
130  except Exception as e:
131  print(str(e))
132  requests_error = True
133  if requests_error or r3.status_code != 200:
134  print("ERROR: Failed to connect to CFRM ({}). STOP.".format(r1.status_code))
135  exit(1)
136 
137  dcp_meta = json.loads(r3.text)
138 
139  if latest_shell_id == current_id:
140  # check for cert
141  if dcp_meta['cert'] == current_cert:
142  print('[cFBuild] Current dcp (path: {}; id: {}) is up to date. DONE.'
143  .format(target_file_name, latest_shell_id))
144  exit(0)
145 
146  print('[cFBuild] Update detected, downloading newest version... (can take some minutes)\n')
147  # 4. download if new version available
148  download_url = "http://"+cfrm_url+"/composablelogic/"+str(latest_shell_id)+"/dcp" + \
149  "?username={0}&password={1}".format(__openstack_user__, __openstack_pw__)
150  err_msg = ""
151  with requests.get(download_url, stream=True) as r2:
152  r2.raise_for_status()
153  if r2.status_code != 200:
154  requests_error = True
155  err_msg = "{}".format(r2.status_code)
156  with open(target_file_name, 'wb') as f:
157  for chunk in r2.iter_content(chunk_size=8192):
158  f.write(chunk)
159  if requests_error:
160  print("ERROR: Failed to download latest dcp ({}). STOP.".format(err_msg))
161  exit(1)
162 
163  with open(target_meta_name, 'w') as outfile:
164  json.dump(dcp_meta, outfile)
165 
166  print("[cFBuild] Updated dcp of Shell '{}' to latest version ({}) successfully. DONE.\n\t(downloaded dcp to {})"
167  .format(shell_type, latest_shell_id, target_file_name))
168  return
169 
170 
171 if __name__ == '__main__':
172  main()
173  exit(0)
174 
175 
def load_user_credentials(filedir)