I am attempting an API script that changes an openvpn client's remote IP and restarts the instance.
This always produces the intended result but on occasion leaves behind an ovpn_status.py process that is consuming 100% cpu, which I then have to kill.
The script retrieves the instance, updates the remote endpoint, restarts the instance, checks the status.
What might I be doing wrong here to cause the runaway process and how better to handle it?
I'm guessing it is related to potential repeated calls to /api/openvpn/service/search_sessions below.
client_name = sys.argv[1]
remote_ip = sys.argv[2]
r = requests.get(
"{}/api/openvpn/instances/search".format(remote_uri),
auth=(api_key, api_secret),
verify=False
)
if r.status_code != 200:
print("error : %s" % r.text)
sys.exit(1)
instances = json.loads(r.text)
for row in instances['rows']:
if row['description'] == client_name and row['role'] == 'client':
uuid = row['uuid']
instance = {
"instance": {
"remote": remote_ip,
}
}
print("Changing remote from {} to {}".format(row['remote'], remote_ip))
r = requests.post(
"{}/api/openvpn/instances/set/{}".format(remote_uri, uuid),
auth=(api_key, api_secret),
verify=False,
json=instance
)
if r.status_code != 200:
print("error setting new remote: %s" % r.text)
sys.exit(1)
if row['enabled'] == '0':
print("instance {} disabled. Skipping restart".format(client_name))
sys.exit(0)
print("restarting {}...".format(client_name))
r = requests.post(
"{}/api/openvpn/service/restart_service/{}".format(remote_uri, uuid),
auth=(api_key, api_secret),
verify=False
)
if r.status_code != 200:
print("error restarting: %s" % r.text)
sys.exit(1)
sleep_time = 5
num_checks = 3
while num_checks > 0:
time.sleep(sleep_time)
num_checks -= 1
r = requests.post(
"{}/api/openvpn/service/search_sessions".format(remote_uri),
auth=(api_key, api_secret),
verify=False
)
if r.status_code != 200:
print("error checking status after restart: %s" % r.text)
sys.exit(1)
statuses = json.loads(r.text)
for s in statuses['rows']:
if s['id'] == uuid and s['status'] == 'connected':
client_status_str = "{} {} {} {} {} {}".format(
s['description'],
s['status'],
s['real_address'],
s['virtual_address'],
s['bytes_sent'],
s['bytes_received']
)
print("Success {}".format(client_status_str))
sys.exit(0)
print("{} failed to (re)start".format(client_name))
sys.exit(1)
print("No matching vpn client instance found")
sys.exit(1)