BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 1 | # ============LICENSE_START=============================================== |
| 2 | # Copyright (C) 2020 Nordix Foundation. All rights reserved. |
| 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 | # ============LICENSE_END================================================= |
| 16 | # |
| 17 | |
| 18 | # This script delete policies spread over a number rics |
| 19 | # Intended for parallel processing |
| 20 | # Returns a string with result, either "0" for ok, or "1<fault description>" |
| 21 | |
| 22 | import os |
| 23 | import json |
| 24 | import sys |
| 25 | import requests |
BjornMagnussonXA | 048aaa1 | 2020-06-04 07:48:37 +0200 | [diff] [blame] | 26 | import traceback |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 27 | |
| 28 | # disable warning about unverified https requests |
| 29 | from requests.packages import urllib3 |
| 30 | |
| 31 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 32 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame^] | 33 | #arg responsecode baseurl num_rics uuid startid count pids pid_id proxy |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 34 | |
| 35 | try: |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame^] | 36 | if len(sys.argv) != 10: |
| 37 | print("1Expected 9 args, got "+str(len(sys.argv)-1)+ ". Args: responsecode baseurl num_rics uuid startid count pids pid_id proxy") |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 38 | sys.exit() |
| 39 | |
| 40 | responsecode=int(sys.argv[1]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 41 | baseurl=str(sys.argv[2]) |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 42 | num_rics=int(sys.argv[3]) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 43 | uuid=str(sys.argv[4]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 44 | start=int(sys.argv[5]) |
| 45 | count=int(sys.argv[6]) |
| 46 | pids=int(sys.argv[7]) |
| 47 | pid_id=int(sys.argv[8]) |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame^] | 48 | httpproxy=str(sys.argv[9]) |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 49 | |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame^] | 50 | proxydict=None |
| 51 | if httpproxy != "NOPROXY": |
| 52 | proxydict = { |
| 53 | "http" : httpproxy, |
| 54 | "https" : httpproxy |
| 55 | } |
BjornMagnussonXA | ad04778 | 2020-06-08 15:54:11 +0200 | [diff] [blame] | 56 | if uuid == "NOUUID": |
| 57 | uuid="" |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 58 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 59 | total_retry_count=0 |
| 60 | |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 61 | stop=count*num_rics+start |
| 62 | for i in range(start,stop): |
| 63 | if (i%pids == (pid_id-1)): |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 64 | retry_cnt=5 |
| 65 | while(retry_cnt>0): |
| 66 | if ("/v2/policies/" in baseurl): |
| 67 | url=str(baseurl+uuid+str(i)) |
| 68 | else: |
| 69 | url=str(baseurl+"?id="+uuid+str(i)) |
| 70 | try: |
BjornMagnussonXA | be9a07f | 2021-02-25 10:51:46 +0100 | [diff] [blame^] | 71 | if proxydict is None: |
| 72 | resp=requests.delete(url, verify=False, timeout=90) |
| 73 | else: |
| 74 | resp=requests.delete(url, verify=False, timeout=90, proxies=proxydict) |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 75 | except Exception as e1: |
| 76 | print("1Delete failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc()) |
| 77 | sys.exit() |
| 78 | if (resp.status_code == None): |
| 79 | print("1Delete failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None") |
| 80 | sys.exit() |
| 81 | if (resp.status_code != responsecode): |
| 82 | if (resp.status_code == 503 ) and (retry_cnt > 1): |
| 83 | sleep(0.1) |
| 84 | retry_cnt -= 1 |
| 85 | total_retry_count += 1 |
| 86 | else: |
| 87 | print("1Delete failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code)) |
| 88 | sys.exit() |
| 89 | else: |
| 90 | retry_cnt=-1 |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 91 | |
BjornMagnussonXA | 49f0e5a | 2020-11-08 22:41:39 +0100 | [diff] [blame] | 92 | if (total_retry_count > 0): |
| 93 | print("0 retries:"+str(total_retry_count)) |
| 94 | else: |
| 95 | print("0") |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 96 | sys.exit() |
| 97 | |
| 98 | except Exception as e: |
| 99 | print("1"+str(e)) |
BjornMagnussonXA | 4207b83 | 2020-11-03 09:52:49 +0100 | [diff] [blame] | 100 | traceback.print_exc() |
BjornMagnussonXA | bbd2e9d | 2020-05-27 21:24:06 +0200 | [diff] [blame] | 101 | sys.exit() |