cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
cfsp_image.py
Go to the documentation of this file.
1 # *****************************************************************************
2 # * cloudFPGA
3 # * Copyright 2016 -- 2022 IBM Corporation
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 Usage:
19  cfsp image (get | post | delete)
20 Commands:
21  get <id> Get all images of a user. Either <id> of image or no argument for all.
22  post Request an image.
23  delete id Delete an image with image_id=id.
24 """
25 from __future__ import absolute_import
26 
27 import sys,os
28 import json
29 python_api_client_path = os.getcwd()+"/cFSPlib/python_api_client/"
30 sys.path.append(python_api_client_path)
31 
32 import cfsp_globals
33 import swagger_client
34 from swagger_client.api.images_api import ImagesApi # noqa: E501
35 from swagger_client.rest import ApiException
36 from swagger_client.api_client import ApiClient
37 from swagger_client.configuration import Configuration
38 
39 
41  confirm = input("[c]Confirm or [v]Void: ")
42  if confirm != 'c' and confirm != 'v':
43  print("\n Invalid Option. Please Enter a Valid Option.")
44  return confirm_choice()
45  print (confirm)
46  return confirm
47 
48 def main(args):
49 
50  conf = Configuration()
51  conf.host = cfsp_globals.__cf_manager_url__
52  api_client = ApiClient(conf)
53  api_instance = ImagesApi(api_client=api_client)
54 
55  if ((len(args['<args>']) < 1) or (len(args['<args>']) > 2)):
56  print("ERROR: invalid arguments provided in 'cfsp image' command. Aborting...")
57  exit(print(__doc__))
58 
59  username = cfsp_globals.__cfsp_username__
60  password = cfsp_globals.__cfsp_password__
61  project_name = cfsp_globals.__cfsp_project__
62 
63  if args['<args>'][0] == 'get':
64  if (len(args['<args>']) == 2):
65  try:
66  api_response = api_instance.cf_manager_rest_api_get_image_single(username, password, args['<args>'][1])
67  except ApiException as e:
68  print("Exception when calling ImagesApi->cf_manager_rest_api_get_image_single: %s\n" % e)
69  exit(-1)
70  elif (len(args['<args>']) == 1):
71  try:
72  api_response = api_instance.cf_manager_rest_api_get_images(username, password, limit=args['--limit'])
73  except ApiException as e:
74  print("Exception when calling ImagesApi->cf_manager_rest_api_get_images: %s\n" % e)
75  exit(-1)
76  else:
77  exit(print("ERROR: invalid arguments provided in cfsp image get. Aborting..."))
78  return(api_response)
79  elif args['<args>'][0] == 'post':
80  try:
81  comment = args['--comment']
82  # Upload an image
83  image_details = '{ "breed": "SHELL", "fpga_board": "FMKU60", "shell_type": "Themisto", "comment" : "' + comment + '"}'
84  pr_verify_rpt = ""
85  image_file = args['--image_file']
86  if (image_file.find("pblock") != -1):
87  print("ERROR: the image file contains the keyword 'pblock' which is typically generated in pr flow. Do you wish to continue anyway ?")
88  if (confirm_choice() != 'c'):
89  exit(print("ERROR: Aborting upon user selection..."))
90  length_image_file = len(image_file)
91  image_file_type = image_file[length_image_file - 3 : length_image_file]
92  if (image_file_type == "bit"):
93  api_response = api_instance.cf_manager_rest_api_post_images(image_details, image_file, pr_verify_rpt, username, password)
94  this_id = api_response.id
95  try:
96  api_response = api_instance.cf_manager_rest_api_get_image_single(username, password, this_id)
97  except ApiException as e:
98  print("Exception when calling ImagesApi->cf_manager_rest_api_get_image_single: %s (nested in ImagesApi->cf_manager_rest_api_post_images)\n" % e)
99  return(api_response)
100  else:
101  exit(print("ERROR: invalid image file provided in cfsp post-app-logic. You should provide a .bit file instead. Aborting..."))
102  except ApiException as e:
103  print("Exception when calling ImagesApi->cf_manager_rest_api_post_images: %s\n" % e)
104  exit(-1)
105  elif args['<args>'][0] == 'post-app-logic':
106  try:
107  image_file = args['--image_file']
108  if (image_file.find("pblock") == -1):
109  print("WARNING: the image file does not contain the keyword 'pblock' which is typically generated in pr flow. Do you wish to continue anyway ?")
110  if (confirm_choice() != 'c'):
111  exit(print("ERROR: Aborting upon user selection..."))
112  image_dirname = os.path.dirname(image_file)
113  json_file = image_dirname+ "/3_topFMKU60_STATIC.json"
114  print("INFO: This json file will be used: " + json_file)
115  with open(json_file) as f:
116  data = json.load(f)
117  cl_id = str(data['id'])
118  comment = args['--comment']
119  #TODO: Parse also shell and fpga_board from the json file
120  image_details = '{"cl_id": "' + cl_id + '", "fpga_board": "FMKU60", "shell_type": "Themisto", "comment" : "' + comment + '"}'
121  length_image_file = len(image_file)
122  image_file_type = image_file[length_image_file - 3 : length_image_file]
123  image_file_name = image_file[0 : length_image_file - 4]
124  if (image_file_type == "bin"):
125  sig_file = args['--sig_file']
126  if (sig_file is None):
127  sig_file = image_file + ".sig"
128  print("INFO: No --sig_file provided. Assuming " + sig_file)
129  pr_verify_rpt = args['--pr_verify_rpt']
130  if (pr_verify_rpt is None):
131  pr_verify_rpt = image_file_name + ".rpt"
132  pr_verify_rpt_new = pr_verify_rpt.replace("4_","5_") # FIXME: This is a bug if the path, apart from the filename, contains the characters '4_'.
133  pr_verify_rpt = pr_verify_rpt_new
134  print("INFO: No --pr_verify_rpt provided. Assuming " + pr_verify_rpt)
135  # Upload an image
136  api_response = api_instance.cf_manager_rest_api_post_app_logic(image_details, image_file, sig_file, pr_verify_rpt, username, password)
137  this_id = api_response.id
138  try:
139  api_response = api_instance.cf_manager_rest_api_get_image_single(username, password, this_id)
140  except ApiException as e:
141  print("Exception when calling ImagesApi->cf_manager_rest_api_get_image_single: %s (nested in ImagesApi->cf_manager_rest_api_post_app_logic)\n" % e)
142  return(api_response)
143  else:
144  exit(print("ERROR: invalid image file provided in cfsp post-app-logic. You should provide a .bin file instead. Aborting..."))
145  except ApiException as e:
146  print("Exception when calling ImagesApi->cf_manager_rest_api_post_app_logic: %s\n" % e)
147  exit(-1)
148  elif args['<args>'][0] == 'delete':
149  if (len(args['<args>']) == 2):
150  try:
151  # Delete an image
152  image_id = args['<args>'][1]
153  api_instance.cf_manager_rest_api_delete_image(username, password, image_id)
154  except ApiException as e:
155  print("Exception when calling ImagesApi->cf_manager_rest_api_delete_image: %s\n" % e)
156  else:
157  exit(print("ERROR: invalid arguments provided in cfsp image delete. Aborting..."))
158  else:
159  exit(print("ERROR: invalid command provided in cfsp image. Type 'cfsp help image' to get a list of supported commands. Aborting..."))
160 
161 if __name__ == '__main__':
162  main(args)
def main(args)
Definition: cfsp_image.py:48
string input
Definition: test.py:9