Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Dec 3, 2023
1 parent f52e2e9 commit be7ff4e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions rpmbuild/bin/copr-builder
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Allow users to control their Copr builder instance
https://github.com/fedora-copr/debate/tree/main/user-ssh-builders
"""

import time
import argparse
from datetime import datetime, timedelta
from contextlib import suppress
Expand Down Expand Up @@ -72,11 +71,17 @@ class CMDShow:
- The user for which the instance was spawned
"""
def run(self):
"""
Print the information
"""
print("Remaining time for the machine: {0}".format(self.remaining_time))
print("Current copr-rpmbuild PID: {0}".format(self.copr_rpmbuild_pid))

@property
def copr_rpmbuild_pid(self):
"""
PID of the current/last `copr-rpmbuild` process
"""
with suppress(OSError), open(RPMBUILD_PID_PATH, "r") as fp:
pid = fp.read()
if pid.isdecimal():
Expand All @@ -85,14 +90,16 @@ class CMDShow:

@property
def expiration(self):
"""
The user preference of when the VM should expire
"""
try:
with open(EXPIRATION_PATH, "r") as fp:
timestamp = float(fp.read())
return datetime.fromtimestamp(timestamp)
except (OSError, ValueError):
return None


@property
def remaining_time(self):
"""
Expand All @@ -105,6 +112,7 @@ class CMDShow:
if delta.total_seconds() < 0:
return "expired"

# TODO Print reasonable value when `copr-builder prolong --hours 666`
days, hours, minutes, seconds = timedelta_to_dhms(delta)
return ("{0} days, {1} hours, {2} minutes, {3} seconds"
.format(days, hours, minutes, seconds))
Expand All @@ -114,6 +122,7 @@ def cmd_prolong(args):
"""
Prolong the VM expiration time
"""
# TODO Validate against `copr-builder prolong --hours 666`
expiration = CMDShow().expiration + timedelta(hours=args.hours)
with open(EXPIRATION_PATH, "w+") as fp:
fp.write(str(expiration.timestamp()))
Expand All @@ -131,6 +140,9 @@ def cmd_release():


def get_parser():
"""
Return argument parser
"""
parser = argparse.ArgumentParser(
"copr-builder",
description="Control a Copr builder",
Expand Down Expand Up @@ -180,6 +192,9 @@ def get_parser():


def main():
"""
Main
"""
parser = get_parser()
args = parser.parse_args()

Expand All @@ -190,7 +205,6 @@ def main():
cmd_help()

elif args.command == "show":
# cmd_show()
cmd = CMDShow()
cmd.run()

Expand Down

0 comments on commit be7ff4e

Please sign in to comment.