Skip to content

Commit

Permalink
[python3] Migrated spreaper python file to python3 style
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregoire Bellon Gervais authored and adejanovski committed Mar 23, 2020
1 parent 896b72b commit d39b7a2
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/packaging/bin/spreaper
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#
# Copyright 2014-2017 Spotify AB
# Copyright 2016-2019 The Last Pickle Ltd
Expand All @@ -22,7 +22,8 @@ import logging
import os
import subprocess
import urllib
import urlparse
from urllib.parse import urlparse
from urllib.parse import urljoin

import sys
import requests
Expand Down Expand Up @@ -92,7 +93,7 @@ class ReaperCaller(object):
printq("Access to this operation seems to be restricted. Please login before making any call to the API:")
printq("spreaper login <username>")
if not str(r.status_code).startswith("2"):
print r.text
print(r.text)
r.raise_for_status()
if 'Location' in r.headers:
# any non 303/307 response with a 'Location' header, is a successful mutation pointing to the resource
Expand All @@ -103,23 +104,23 @@ class ReaperCaller(object):
return r.text

def get(self, endpoint):
the_url = urlparse.urljoin(self.base_url, endpoint)
the_url = urllib.parse.urljoin(self.base_url, endpoint)
return self._http_req("GET", the_url, cookies=COOKIES)

def post(self, endpoint, **params):
the_url = urlparse.urljoin(self.base_url, endpoint)
the_url = urllib.parse.urljoin(self.base_url, endpoint)
return self._http_req("POST", the_url, params=params)

def postFormData(self, endpoint, payload):
the_url = urlparse.urljoin(self.base_url, endpoint)
the_url = urllib.parse.urljoin(self.base_url, endpoint)
return self._http_req("POST_FORM", the_url, params=payload)

def put(self, endpoint, **params):
the_url = urlparse.urljoin(self.base_url, endpoint)
the_url = urllib.parse.urljoin(self.base_url, endpoint)
return self._http_req("PUT", the_url, params=params)

def delete(self, endpoint, **params):
the_url = urlparse.urljoin(self.base_url, endpoint)
the_url = urllib.parse.urljoin(self.base_url, endpoint)
return self._http_req("DELETE", the_url, params=params)


Expand Down Expand Up @@ -405,22 +406,22 @@ class ReaperCLI(object):

def __init__(self):
if len(sys.argv) < 2:
print REAPER_USAGE
print(REAPER_USAGE)
exit(1)
commands = [arg for arg in sys.argv[1:] if not arg[0].startswith('-')]
if len(commands) < 1:
print REAPER_USAGE
print(REAPER_USAGE)
exit(1)
command = commands[0].replace('-', '_')
if not hasattr(self, command):
print 'Unrecognized command: {0}'.format(command)
print REAPER_USAGE
print('Unrecognized command: {0}'.format(command))
print(REAPER_USAGE)
exit(1)
# use dispatch pattern to invoke method with same name as given command
try:
getattr(self, command)()
except requests.exceptions.HTTPError, err:
print ""
except requests.exceptions.HTTPError as err:
print("")
print("# HTTP request failed with err: {}".format(err))
exit(2)

Expand Down Expand Up @@ -464,12 +465,12 @@ class ReaperCLI(object):
def save_jwt(jwt):
try:
os.makedirs(os.path.expanduser('~/.reaper'))
os.chmod(os.path.expanduser('~/.reaper'),0700)
os.chmod(os.path.expanduser('~/.reaper'),0o700)
except OSError:
pass
with open(os.path.expanduser('~/.reaper/jwt'),'w+') as f:
f.write(jwt)
os.chmod(os.path.expanduser('~/.reaper/jwt'),0600)
os.chmod(os.path.expanduser('~/.reaper/jwt'),0o600)
printq("# JWT saved")

def login(self):
Expand Down Expand Up @@ -513,7 +514,7 @@ class ReaperCLI(object):
if cluster_names:
printq("# Found {0} clusters:".format(len(cluster_names)))
for cluster_name in cluster_names:
print cluster_name
print(cluster_name)
else:
printq("# No registered clusters found")

Expand All @@ -529,7 +530,7 @@ class ReaperCLI(object):
endpoint = '{0}?{1}'.format(endpoint, urllib.urlencode({'state': args.state, 'cluster_name': args.cluster, 'keyspace_name': args.keyspace}))
repair_runs = json.loads(reaper.get(endpoint))
printq("# Found {0} repair runs".format(len(repair_runs)))
print json.dumps(repair_runs, indent=2, sort_keys=True)
print(json.dumps(repair_runs, indent=2, sort_keys=True))

def list_segments(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -540,7 +541,7 @@ class ReaperCLI(object):
printq("# Listing segments for repair run '{0}'".format(args.run_id))
segments = json.loads(reaper.get("repair_run/{0}/segments".format(args.run_id)))
printq("# Found {0} segments".format(len(segments)))
print json.dumps(segments, indent=2, sort_keys=True)
print(json.dumps(segments, indent=2, sort_keys=True))

def list_schedules(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -555,7 +556,7 @@ class ReaperCLI(object):
printq("# Listing repair schedules for all clusters")
data = json.loads(reaper.get("repair_schedule"))
printq("# Found {0} schedules:".format(len(data)))
print json.dumps(data, indent=2, sort_keys=True)
print(json.dumps(data, indent=2, sort_keys=True))

def list_snapshots(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -567,12 +568,12 @@ class ReaperCLI(object):
printq("# Listing snapshots for cluster '{0}'".format(args.cluster))
snapshots = json.loads(reaper.get("snapshot/cluster/{0}".format(args.cluster)))
printq("# Found {0} snapshots".format(len(snapshots)))
print json.dumps(snapshots, indent=2, sort_keys=True)
print(json.dumps(snapshots, indent=2, sort_keys=True))
else:
printq("# Listing snapshots for cluster '{0}' and node '{1}'".format(args.cluster, args.node))
snapshots = json.loads(reaper.get("snapshot/{0}/{1}".format(args.cluster, args.node)))
printq("# Found {0} snapshots".format(len(snapshots)))
print json.dumps(snapshots, indent=2, sort_keys=True)
print(json.dumps(snapshots, indent=2, sort_keys=True))

def take_snapshot(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand Down Expand Up @@ -617,7 +618,7 @@ class ReaperCLI(object):

snapshot = json.loads(reply)
printq("# Snapshot taken")
print json.dumps(snapshot, indent=2, sort_keys=True)
print(json.dumps(snapshot, indent=2, sort_keys=True))

def delete_snapshot(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand Down Expand Up @@ -649,7 +650,7 @@ class ReaperCLI(object):
)
printq("# Cluster '{0}':".format(args.cluster_name))
cluster_data = reaper.get("cluster/{0}".format(args.cluster_name))
print json.dumps(json.loads(cluster_data), indent=2, sort_keys=True)
print(json.dumps(json.loads(cluster_data), indent=2, sort_keys=True))

def status_keyspace(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -659,7 +660,7 @@ class ReaperCLI(object):
)
printq("# Cluster '{0}', keyspace '{1}':".format(args.cluster_name, args.keyspace_name))
keyspace_data = reaper.get("cluster/{0}/{1}".format(args.cluster_name, args.keyspace_name))
print json.dumps(json.loads(keyspace_data), indent=2, sort_keys=True)
print(json.dumps(json.loads(keyspace_data), indent=2, sort_keys=True))

def status_repair(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -669,7 +670,7 @@ class ReaperCLI(object):
)
printq("# Repair run with id '{0}':".format(args.run_id))
repair_run_data = reaper.get("repair_run/{0}".format(args.run_id))
print json.dumps(json.loads(repair_run_data), indent=2, sort_keys=True)
print(json.dumps(json.loads(repair_run_data), indent=2, sort_keys=True))

def status_schedule(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -679,7 +680,7 @@ class ReaperCLI(object):
)
printq("# Repair schedule with id '{0}':".format(args.schedule_id))
repair_schedule_data = reaper.get("repair_schedule/{0}".format(args.schedule_id))
print json.dumps(json.loads(repair_schedule_data), indent=2, sort_keys=True)
print(json.dumps(json.loads(repair_schedule_data), indent=2, sort_keys=True))

def add_cluster(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand All @@ -692,7 +693,7 @@ class ReaperCLI(object):
cluster_data = reaper.postFormData("cluster/auth",
payload=payload)
printq("# Registration succeeded:")
print json.dumps(json.loads(cluster_data), indent=2, sort_keys=True)
print(json.dumps(json.loads(cluster_data), indent=2, sort_keys=True))

def delete_cluster(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand Down Expand Up @@ -755,7 +756,7 @@ class ReaperCLI(object):
printq("# Starting repair run with id={0}".format(repair_run.get('id')))
reply = reaper.put("repair_run/{0}/state/{1}".format(repair_run.get('id'), "RUNNING"))
repair_run = json.loads(reply)
print json.dumps(repair_run, indent=2, sort_keys=True)
print(json.dumps(repair_run, indent=2, sort_keys=True))

def schedule(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand Down Expand Up @@ -810,7 +811,7 @@ class ReaperCLI(object):
repairThreadCount=args.repair_threads)
repair_schedule = json.loads(reply)
printq("# Repair schedule with id={0} created:".format(repair_schedule.get('id')))
print json.dumps(repair_schedule, indent=2, sort_keys=True)
print(json.dumps(repair_schedule, indent=2, sort_keys=True))

def resume_repair(self):
reaper, args = ReaperCLI.prepare_reaper(
Expand Down Expand Up @@ -894,7 +895,7 @@ class ReaperCLI(object):

def printq(message):
if not quiet:
print message
print(message)

if __name__ == '__main__':
log.info("# ------------------------------------------------------------------------------------")
Expand Down

0 comments on commit d39b7a2

Please sign in to comment.