Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move merged PR job directories to trash_bin_dir #271

Merged
merged 61 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
afc2f67
Preliminary work on PR cleanup task
Neves-P Apr 16, 2024
5e17612
WIP disk cleanup
Neves-P May 7, 2024
901b8cc
Merge branch 'develop' into feature/disk_cleanup
Neves-P May 7, 2024
5d8f0a1
cleanup script WIP
Neves-P May 7, 2024
568e4c5
Make date subdiretory
Neves-P May 7, 2024
7e91ce6
Have subdirectory with date and comment
Neves-P May 7, 2024
648232d
Typo
Neves-P May 7, 2024
8e2082d
Better read from config and logging
Neves-P Jun 4, 2024
d5f1d39
Scripting & cron job follow later. Closes #1
Neves-P Jun 4, 2024
e0910f6
Treat hound to some white spaces
Neves-P Jun 4, 2024
fec556b
Merge branch 'Neves-Bot:develop' into feature/disk_cleanup
Neves-P Jun 6, 2024
1d87ea2
Merge pull request #2 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 6, 2024
d645202
Update eessi_bot_event_handler.py
Neves-P Jun 10, 2024
d75bb11
Update tasks/clean_up.py
Neves-P Jun 10, 2024
435bcae
Handle based on `merged` not `action` from `request_body`
Neves-P Jun 10, 2024
6b38f37
Add docstring and use python routines (not shell commands)
Neves-P Jun 10, 2024
c4ee159
Merge pull request #3 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 10, 2024
769daec
Actions are 'closed', not 'merged'
Neves-P Jun 10, 2024
f321fe5
Merge pull request #4 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 10, 2024
1c47315
Read from config properly
Neves-P Jun 10, 2024
f0c0082
Merge branch 'Neves-Bot:feature/disk_cleanup' into feature/disk_cleanup
Neves-P Jun 10, 2024
7b98722
Merge pull request #5 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 10, 2024
1a6eab3
Fix typo in `merged`.
Neves-P Jun 10, 2024
b1dfd1d
Merge branch 'feature/disk_cleanup' of https://github.com/Neves-P/ees…
Neves-P Jun 10, 2024
fd3912e
Merge pull request #6 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 11, 2024
0b91343
merged is a Bool (?)
Neves-P Jun 11, 2024
cee6416
Merge pull request #7 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 11, 2024
784c9b9
Better formatting for merge conflict errors
Neves-P Jun 11, 2024
1f2000b
Merge pull request #8 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 11, 2024
4160bd1
Fix bug in getting repository name
Neves-P Jun 11, 2024
744e29e
Merge pull request #9 from Neves-P/feature/disk_cleanup
Neves-Bot Jun 12, 2024
29ab450
Same formatting for directory timestamp
Neves-P Jun 12, 2024
2497993
Merge branch 'Neves-Bot:feature/disk_cleanup' into feature/disk_cleanup
Neves-P Jun 25, 2024
087d6c5
Best to not read comment from config
Neves-P Jul 1, 2024
5dbd132
Remove local variable
Neves-P Jul 1, 2024
0235586
Merge pull request #10 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 1, 2024
b782889
Restore cleanup_pr.sh
Neves-P Jul 1, 2024
f4114b3
Restore cleanup_pr.sh
Neves-P Jul 1, 2024
283f602
Also move pr event dirs
Neves-P Jul 1, 2024
c025746
Unnecessary library
Neves-P Jul 1, 2024
b018037
Merge pull request #11 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 1, 2024
e56e9ed
Use shutil.copy2 and os.remove instead
Neves-P Jul 1, 2024
fbb16c3
Merge pull request #12 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 1, 2024
00eeb89
Move whole directory(ies) to trash
Neves-P Jul 2, 2024
4b360b3
lint
Neves-P Jul 2, 2024
e3052e7
Merge pull request #13 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 2, 2024
b8f6d57
Better formatting error messages
Neves-P Jul 2, 2024
59a337c
Need copytree instead
Neves-P Jul 2, 2024
7a07249
Merge branch 'Neves-Bot:feature/disk_cleanup' into feature/disk_cleanup
Neves-P Jul 2, 2024
c4dc9b5
Merge pull request #14 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 2, 2024
f80ab2b
Use rmtree
Neves-P Jul 2, 2024
f0e0255
Merge branches 'feature/disk_cleanup' and 'feature/disk_cleanup' of h…
Neves-P Jul 2, 2024
1c08013
Merge pull request #15 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 3, 2024
da970e7
copytree with dirs_exist_ok True
Neves-P Jul 5, 2024
3673540
Merge pull request #16 from Neves-P/feature/disk_cleanup
Neves-Bot Jul 5, 2024
71e7b31
tweak clean up method by
truib Aug 5, 2024
d1a0278
add information to README.md and app.cfg.example
truib Aug 5, 2024
1bc2bf4
Merge pull request #2 from trz42/tweak_cleanup
Neves-P Aug 5, 2024
d8d79b6
Lint
Neves-P Aug 5, 2024
f69c49c
add empty line
truib Aug 6, 2024
f9332fe
Merge pull request #3 from trz42/feature/disk_cleanup
Neves-P Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions eessi_bot_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

# Standard library imports
import sys
from datetime import datetime, timezone

# Third party imports (anything installed into the local Python environment)
from pyghee.lib import create_app, get_event_info, PyGHee, read_event_from_json
Expand All @@ -28,8 +29,9 @@
from connections import github
from tasks.build import check_build_permission, get_architecture_targets, get_repo_cfg, \
request_bot_build_issue_comments, submit_build_jobs
from tasks.deploy import deploy_built_artefacts
from tools import config
from tasks.deploy import deploy_built_artefacts, determine_job_dirs
from tasks.clean_up import move_to_trash_bin
from tools import config, cvmfs_repository
from tools.args import event_handler_parse
from tools.commands import EESSIBotCommand, EESSIBotCommandError, \
contains_any_bot_command, get_bot_command
Expand Down Expand Up @@ -599,6 +601,61 @@ def start(self, app, port=3000):
self.log(log_file_info)
waitress.serve(app, listen='*:%s' % port)

def handle_pull_request_merged_event(self, event_info, pr):
"""
Handle events of type pull_request with the action merged. Main action
is to scan directories used and move them to the trash_bin.

Args:
event_info (dict): event received by event_handler
pr (github.PullRequest.PullRequest): instance representing the pull request

Returns:
github.IssueComment.IssueComment instance or None (note, github refers to
PyGithub, not the github from the internal connections module)
"""

# Detect event and only act if PR is merged
request_body = event_info['raw_request_body']
action = request_body['action']

if action == 'merged':
self.log("PR merged: scanning directories used by PR")
self.log(f"merge '{action}' action is handled")
else:
self.log(f"merge action '{action}' not handled")
return
# at this point we know that we are handling a new merge
# NOTE: Permissions to merge are already handled through GitHub, we
# don't need to check here

clean_up_comments_cfg = self.cfg[config.SECTION_CLEAN_UP_COMMENTS]
# 1) determine the jobs that have been run for the PR
job_dirs = determine_job_dirs(pr.number)

# 2) Get trash_bin_dir from configs
trash_bin_root_dir = self.cfg[config.SECTION_MERGED_PR][config.MERGED_PR_SETTING_TRASH_BIN_ROOT_DIR]
repo_cfg = get_repo_cfg(self.cfg)
repo_name = repo_cfg[cvmfs_repository.REPOS_CFG_REPO_NAME]
dt = datetime.now(timezone.utc)
trash_bin_dir = "/".join([trash_bin_root_dir, repo_name, dt.strftime('%Y%m%d')])

# Subdirectory with date of move. Also with repository name. Handle symbolic links (later?)
# cron job deletes symlinks?

# 3) move the directories to the trash_bin
self.log("Moving directories to trash_bin")
move_to_trash_bin(trash_bin_dir, job_dirs)

# 4) report move to pull request?
repo_name = pr.base.repo.full_name
gh = github.get_instance()
repo = gh.get_repo(repo_name)
pull_request = repo.get_pull(pr.number)
moved_comment = clean_up_comments_cfg[config.CLEAN_UP_COMMENTS_SETTING_MOVED_COMMENT]
issue_comment = pull_request.create_issue_comment(moved_comment)
return issue_comment


def main():
"""
Expand Down
158 changes: 0 additions & 158 deletions scripts/cleanup_pr.sh

This file was deleted.

39 changes: 39 additions & 0 deletions tasks/clean_up.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file is part of the EESSI build-and-deploy bot,
# see https://github.com/EESSI/eessi-bot-software-layer
#
# The bot helps with requests to add software installations to the
# EESSI software layer, see https://github.com/EESSI/software-layer
#
# author: Bob Droege (@bedroge)
# author: Kenneth Hoste (@boegel)
# author: Hafsa Naeem (@hafsa-naeem)
# author: Jacob Ziemke (@jacobz137)
# author: Jonas Qvigstad (@jonas-lq)
# author: Lara Ramona Peeters (@laraPPr)
# author: Pedro Santos Neves (@Neves-P)
# author: Thomas Roeblitz (@trz42)
#
# license: GPLv2
#

# Standard library imports
import sys

# Third party imports (anything installed into the local Python environment)
from pyghee.utils import log

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from tools import run_cmd


def move_to_trash_bin(trash_bin_dir, job_dirs):
funcname = sys._getframe().f_code.co_name
log(f"{funcname}(): trash_bin_dir = {trash_bin_dir}")

move_cmd = ["mkdir -p trash_bin_dir && mv -t", trash_bin_dir]
for job_dir in job_dirs:
move_cmd.append(job_dir)
' '.join(move_cmd)
out, err, ec = run_cmd(move_cmd, 'Move job directories to trash_bin', raise_on_error=False)

return
6 changes: 6 additions & 0 deletions tools/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
SUBMITTED_JOB_COMMENTS_SETTING_INITIAL_COMMENT = 'initial_comment'
SUBMITTED_JOB_COMMENTS_SETTING_AWAITS_RELEASE = 'awaits_release'

SECTION_MERGED_PR = 'merged_pr'
MERGED_PR_SETTING_TRASH_BIN_ROOT_DIR = 'trash_bin_dir'

SECTION_CLEAN_UP_COMMENTS = 'clean_up_comments'
CLEAN_UP_COMMENTS_SETTING_MOVED_COMMENT = 'moved_comment'


def read_config(path='app.cfg'):
"""
Expand Down
Loading