cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
cfsp_instance.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 instance (get | post | restart | delete)
20 Commands:
21  get <id> Get all instances of a user. Either <id> of instance or no argument for all.
22  post Request an instance.
23  restart Triggers application restart of this instance.
24  delete id Delete an instance with instance_id=id. If no id is provided then all instances are deleted (after confirmation dialog with user).
25 """
26 from __future__ import absolute_import
27 
28 import sys,os
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.rest import ApiException
35 from swagger_client.api_client import ApiClient
36 from swagger_client.configuration import Configuration
37 from tqdm import tqdm
38 
40  confirm = input("[c]Confirm or [v]Void: ")
41  if confirm != 'c' and confirm != 'v':
42  print("\n Invalid Option. Please Enter a Valid Option.")
43  return confirm_choice()
44  print (confirm)
45  return confirm
46 
47 
48 def main(args):
49  conf = Configuration()
50  conf.host = cfsp_globals.__cf_manager_url__
51  api_client = ApiClient(conf)
52  api_instance = swagger_client.InstancesApi(api_client=api_client)
53 
54  if ((len(args['<args>']) < 1) or (len(args['<args>']) > 2)):
55  print("ERROR: invalid arguments provided in 'cfsp instance' command. Aborting...")
56  exit(print(__doc__))
57 
58  username = cfsp_globals.__cfsp_username__
59  password = cfsp_globals.__cfsp_password__
60  project_name = cfsp_globals.__cfsp_project__
61 
62  if args['<args>'][0] == 'get':
63  if (len(args['<args>']) == 2):
64  try:
65  api_response = api_instance.cf_manager_rest_api_get_instance(username, password, args['<args>'][1])
66  except ApiException as e:
67  print("Exception when calling InstancesApi->cf_manager_rest_api_get_instance: %s\n" % e)
68  exit(-1)
69  elif (len(args['<args>']) == 1):
70  try:
71  api_response = api_instance.cf_manager_rest_api_get_instances(username, password, limit=args['--limit'])
72  except ApiException as e:
73  print("Exception when calling InstancesApi->cf_manager_rest_api_get_instances: %s\n" % e)
74  exit(-1)
75  else:
76  exit(print("ERROR: invalid arguments provided in cfsp instance get. Aborting..."))
77  return(api_response)
78  elif args['<args>'][0] == 'post':
79  # create an instance of the API class
80  if len(args['--image_id']) != 1:
81  exit(print("ERROR: instance post supports only one image (--image_id). Aborting..."))
82  image_id = args['--image_id'][0]
83  try:
84  # Request an instance
85  api_response = api_instance.cf_manager_rest_api_post_instances(image_id, username, password, project_name=project_name, dont_verify_memory=args['--dont_verify_memory'])
86  # post_instance dows not return the role_ip, thus we call get_instance afterwards.
87  instance_id = api_response.instance_id
88  try:
89  api_response = api_instance.cf_manager_rest_api_get_instance(username, password, instance_id)
90  except ApiException as e:
91  print("Exception when calling InstancesApi->cf_manager_rest_api_get_instance: %s\n" % e)
92  exit(-1)
93  return(api_response)
94  except ApiException as e:
95  print("Exception when calling InstancesApi->cf_manager_rest_api_post_instances: %s\n" % e)
96  exit(-1)
97  elif args['<args>'][0] == 'restart':
98  if (len(args['<args>']) == 2):
99  instance_id = args['<args>'][1]
100  # Restart an instance
101  print("INFO: Restarting instance " + str(instance_id) + " ... ")
102  try:
103  api_response = api_instance.cf_manager_rest_api_app_restart_instance(username, password, instance_id)
104  except ApiException as e:
105  print("Exception when calling InstancesApi->cf_manager_rest_api_app_restart_instance: %s\n" % e)
106  exit(-1)
107  return(api_response)
108  else:
109  exit(print("ERROR: invalid arguments provided in cfsp instance restart. Aborting..."))
110  elif args['<args>'][0] == 'delete':
111  if (len(args['<args>']) == 1):
112  print("INFO: Really deleting all instances ?")
113  if confirm_choice() == 'c':
114  print("INFO: Confirmed deleting all instances")
115  try:
116  api_response_get_in_delete = api_instance.cf_manager_rest_api_get_instances(username, password, limit=args['--limit'])
117  if(len(api_response_get_in_delete) > 0):
118  for this_instance in tqdm(api_response_get_in_delete):
119  # Delete an instance
120  print("INFO: Deleting instance " + str(this_instance.instance_id) + " ... ")
121  try:
122  api_instance.cf_manager_rest_api_delete_instance(username, password, this_instance.instance_id)
123  except ApiException as e_in_delete:
124  print("Exception when calling InstancesApi->cf_manager_rest_api_delete_instance: %s\n" % e_in_delete)
125  else:
126  print("INFO: No instances to delete.")
127  except ApiException as e_in_get:
128  print("Exception when calling InstancesApi->cf_manager_rest_api_get_instances: %s\n" % e_in_get)
129  exit(-1)
130  else:
131  print ("INFO: Canceling deleting all instances")
132  elif (len(args['<args>']) == 2):
133  instance_id = args['<args>'][1]
134  # Delete an instance
135  print("INFO: Deleting instance " + str(instance_id) + " ... ")
136  try:
137  api_response = api_instance.cf_manager_rest_api_delete_instance(username, password, instance_id)
138  except ApiException as e:
139  print("Exception when calling InstancesApi->cf_manager_rest_api_delete_instance: %s\n" % e)
140  exit(-1)
141  return(api_response)
142  else:
143  exit(print("ERROR: invalid arguments provided in cfsp instance delete. Aborting..."))
144  else:
145  exit(print("ERROR: invalid command provided in cfsp instance. Type 'cfsp help instance' to get a list of supported commands. Aborting..."))
146 if __name__ == '__main__':
147  main(args)
string input
Definition: test.py:9