add dryrun flag
This commit is contained in:
parent
3cb946056a
commit
34bab35b34
@ -33,6 +33,8 @@ class AgentCleanup:
|
||||
description=("Cleanup outdated and faulty drone agents"))
|
||||
parser.add_argument("-v", dest="log_level", action="append_const", const=-1,
|
||||
help="increase log level")
|
||||
parser.add_argument("-d", "--dry-run", dest="dry_run", action="store_true", default=False,
|
||||
help="dry run without modifications")
|
||||
parser.add_argument("-q", dest="log_level", action="append_const",
|
||||
const=1, help="decrease log level")
|
||||
parser.add_argument("--version", action="version", version="%(prog)s {}".format(__version__))
|
||||
@ -62,6 +64,8 @@ class AgentCleanup:
|
||||
if not os.path.exists(os.path.dirname(config["logfile"])):
|
||||
os.makedirs(os.path.dirname(config["logfile"]))
|
||||
|
||||
config["dry_run"] = self.args["dry_run"]
|
||||
|
||||
# Override correct log level from argparse
|
||||
levels = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
||||
log_level = levels.index(config["log_level"])
|
||||
@ -79,10 +83,16 @@ class AgentCleanup:
|
||||
def _clean(self):
|
||||
pending = self.config["pending"]
|
||||
control = defaultdict(dict)
|
||||
dry = self.config["dry_run"]
|
||||
dryrun_msg = ""
|
||||
|
||||
if dry:
|
||||
dryrun_msg = "[DRYRUN] "
|
||||
|
||||
for scaler in self.config["drone_scaler"]:
|
||||
error = []
|
||||
self.logger.info("Cleanup agents for scaler '{}'".format(scaler))
|
||||
self.logger.info("{dryrun_msg}Cleanup agents for scaler '{scaler}'".format(
|
||||
scaler=scaler, dryrun_msg=dryrun_msg))
|
||||
|
||||
res = run_command("drone -s {server} -t {token} --autoscaler {scaler} server ls --format '{{{{ . | jsonify }}}}'".format(
|
||||
server=self.config["drone_server"], token=self.config["drone_token"], scaler=scaler))
|
||||
@ -110,14 +120,17 @@ class AgentCleanup:
|
||||
|
||||
self.logger.info("Stopping '{agent}' ({triage}/3) {force}".format(
|
||||
agent=e, triage=control[e] or 3, force=force_msg))
|
||||
res = run_command("drone -s {server} -t {token} --autoscaler {scaler} server destroy {force} {agent}".format(
|
||||
server=self.config["drone_server"], token=self.config["drone_token"], scaler=scaler, agent=e, force=force))
|
||||
|
||||
if res.returncode > 0:
|
||||
self.log.sysexit_with_message("Command error:\n{}".format(humanize(res.stdout)))
|
||||
if not dry:
|
||||
res = run_command("drone -s {server} -t {token} --autoscaler {scaler} server destroy {force} {agent}".format(
|
||||
server=self.config["drone_server"], token=self.config["drone_token"], scaler=scaler, agent=e, force=force))
|
||||
|
||||
with open(self.config["statefile"], "wb") as json_file:
|
||||
pickle.dump(control, json_file) # nosec
|
||||
if res.returncode > 0:
|
||||
self.log.sysexit_with_message("Command error:\n{}".format(humanize(res.stdout)))
|
||||
|
||||
if not dry:
|
||||
with open(self.config["statefile"], "wb") as json_file:
|
||||
pickle.dump(control, json_file) # nosec
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
|
Reference in New Issue
Block a user