blob: 530e877679b7156a9bfe48ced1866afe7a100c18 [file] [log] [blame]
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +02001# ============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
22import os
23import json
24import sys
25import requests
BjornMagnussonXA048aaa12020-06-04 07:48:37 +020026import traceback
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020027
28# disable warning about unverified https requests
29from requests.packages import urllib3
30
31urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
32
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010033#arg responsecode baseurl num_rics uuid startid count pids pid_id proxy
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020034
35try:
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010036 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")
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020038 sys.exit()
39
40 responsecode=int(sys.argv[1])
BjornMagnussonXA4207b832020-11-03 09:52:49 +010041 baseurl=str(sys.argv[2])
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020042 num_rics=int(sys.argv[3])
BjornMagnussonXA4207b832020-11-03 09:52:49 +010043 uuid=str(sys.argv[4])
BjornMagnussonXAad047782020-06-08 15:54:11 +020044 start=int(sys.argv[5])
45 count=int(sys.argv[6])
46 pids=int(sys.argv[7])
47 pid_id=int(sys.argv[8])
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010048 httpproxy=str(sys.argv[9])
BjornMagnussonXAad047782020-06-08 15:54:11 +020049
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010050 proxydict=None
51 if httpproxy != "NOPROXY":
52 proxydict = {
53 "http" : httpproxy,
54 "https" : httpproxy
55 }
BjornMagnussonXAad047782020-06-08 15:54:11 +020056 if uuid == "NOUUID":
57 uuid=""
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020058
BjornMagnussonXAa5491572021-05-04 09:21:24 +020059 http_retry_count=0
60 connect_retry_count=0
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010061
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020062 stop=count*num_rics+start
63 for i in range(start,stop):
64 if (i%pids == (pid_id-1)):
BjornMagnussonXAa5491572021-05-04 09:21:24 +020065 connect_ok=False
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010066 retry_cnt=5
67 while(retry_cnt>0):
68 if ("/v2/policies/" in baseurl):
69 url=str(baseurl+uuid+str(i))
70 else:
71 url=str(baseurl+"?id="+uuid+str(i))
72 try:
BjornMagnussonXAbe9a07f2021-02-25 10:51:46 +010073 if proxydict is None:
74 resp=requests.delete(url, verify=False, timeout=90)
75 else:
76 resp=requests.delete(url, verify=False, timeout=90, proxies=proxydict)
BjornMagnussonXAa5491572021-05-04 09:21:24 +020077 cconnect_ok=True
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010078 except Exception as e1:
BjornMagnussonXAa5491572021-05-04 09:21:24 +020079 if (retry_cnt > 1):
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010080 sleep(0.1)
81 retry_cnt -= 1
BjornMagnussonXAa5491572021-05-04 09:21:24 +020082 connect_retry_count += 1
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010083 else:
BjornMagnussonXAa5491572021-05-04 09:21:24 +020084 print("1Delete failed for id:"+uuid+str(i)+ ", "+str(e1) + " "+traceback.format_exc())
BjornMagnussonXA49f0e5a2020-11-08 22:41:39 +010085 sys.exit()
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +020086
BjornMagnussonXAa5491572021-05-04 09:21:24 +020087 if (cconnect_ok == True):
88 if (resp.status_code == None):
89 print("1Delete failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: None")
90 sys.exit()
91 if (resp.status_code != responsecode):
92 if (resp.status_code >= 500) and (http_retry_count < 600 ) and (retry_cnt > 1):
93 sleep(0.1)
94 retry_cnt -= 1
95 http_retry_count += 1
96 else:
97 print("1Delete failed for id:"+uuid+str(i)+ ", expected response code: "+str(responsecode)+", got: "+str(resp.status_code)+str(resp.raw))
98 sys.exit()
99 else:
100 retry_cnt=-1
101
102 print("0 http retries:"+str(http_retry_count) + ", connect retries: "+str(connect_retry_count))
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200103 sys.exit()
104
105except Exception as e:
106 print("1"+str(e))
BjornMagnussonXA4207b832020-11-03 09:52:49 +0100107 traceback.print_exc()
BjornMagnussonXAbbd2e9d2020-05-27 21:24:06 +0200108sys.exit()