Merge ~lihuiguo/charm-logrotated:bug/1864598 into charm-logrotated:master

Proposed by Linda Guo
Status: Work in progress
Proposed branch: ~lihuiguo/charm-logrotated:bug/1864598
Merge into: charm-logrotated:master
Diff against target: 76 lines (+24/-21)
1 file modified
src/lib/lib_cron.py (+24/-21)
Reviewer Review Type Date Requested Status
🤖 prod-jenkaas-bootstack (community) continuous-integration Needs Fixing
Gabriel Cocenza Needs Information
Eric Chen Approve
BootStack Reviewers Pending
Review via email: mp+428357@code.launchpad.net

Commit message

Clear up previous cron job file when cron job config is changed

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Eric Chen (eric-chen) wrote :

It's dangerous for the logic when we want to modify it in the future.
The hidden logic here is.. we only put the cronjob file with one frequency ["hourly", "daily", "weekly", "monthly"]. That is the design limitation in charm-logrotate, not a limitation in linux cronjob. But I don't have good idea to make this design limitation explict in the cleanup_cronjob function right now.

I have other ideas to remove some duplicated code and avoid additional parameter in the cleanup_cronjob.

1. There could be a cronjob filepath generator to be used in install_cronjob and cleanup_cronjob
2. We can use frequency index to generate the path and it's easy to know which one should be reserved in the cleanup_cronjob. We also can change the parameter as an array in the future if need.

/* Pseudo Code */
def cleanup_cronjob(self, reserve=-1):
    '''
    if reserve = -1, delete cronjob file in all frequencies
    else: only reserve the cronjob file in corresponding folder.
    '''

    for i in range(len(self.cronjob_check_paths)):
        if reserve == -1 or i != reserve:
            path = cronjob_path_generator(i)
            os.remove(path)

Revision history for this message
Eric Chen (eric-chen) wrote :

Mark it as WIP until Linda provide the 2nd patch or continue the discussion

Revision history for this message
Linda Guo (lihuiguo) wrote :

Thanks Eric, I refactored the code to make it more easy for maintenance. Can you please take another look?

Revision history for this message
Eric Chen (eric-chen) wrote :

LGTM

review: Approve
Revision history for this message
Eric Chen (eric-chen) :
review: Needs Information
Revision history for this message
Eric Chen (eric-chen) wrote :

I forget to request the log of unit&func test. Please prove the testing are pass. Thanks!

review: Needs Information
Revision history for this message
Linda Guo (lihuiguo) wrote :
Revision history for this message
Linda Guo (lihuiguo) wrote (last edit ):

functional test is broken at MASTER branch, it's stuck at test_deploy[xenial] https://pastebin.canonical.com/p/Yj63xsbSvM/

Unit Workload Agent Machine Public address Ports Message
ubuntu-xenial/0* error idle 0 10.5.3.224 hook failed: "install"

juju log: https://pastebin.canonical.com/p/g7t3Svt9ZM/

Revision history for this message
Eric Chen (eric-chen) wrote :

> functional test is broken at MASTER branch, it's stuck at test_deploy[xenial]
> https://pastebin.canonical.com/p/Yj63xsbSvM/
>
> Unit Workload Agent Machine Public address Ports Message
> ubuntu-xenial/0* error idle 0 10.5.3.224 hook
> failed: "install"
>
> juju log: https://pastebin.canonical.com/p/g7t3Svt9ZM/

That is a known issue according to this MP.

https://code.launchpad.net/~txiao/charm-logrotated/+git/charm-logrotated/+merge/427993

But could you pass the func test on bionic, focal and jammy ? Thanks!

Revision history for this message
Linda Guo (lihuiguo) wrote :

yes. func test on bionic, focal and jammy passed. sorry, forgot to post the result https://pastebin.canonical.com/p/BkDTykKV2R/

Revision history for this message
Eric Chen (eric-chen) :
review: Approve
Revision history for this message
Eric Chen (eric-chen) wrote :

Wait for one more engineer review it , then we can merge it.
I will let Garbriel to help it, but you can also call for help in MM.

Revision history for this message
Gabriel Cocenza (gabrielcocenza) wrote :

Hi Linda.

Thanks for this patch. I just have a small observation about the "cleanup_cronjob" that I don't think has a good readability.

review: Needs Information
Revision history for this message
Eric Chen (eric-chen) wrote :

Mark it as "WIP" until Linda have time to work it. Or please call for help from DOC. thanks

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Needs Fixing (continuous-integration)

Unmerged commits

ee2ae00... by Linda Guo

Clear up previous cron job file

Clear up previous cron job file when cron job config is changed.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/lib/lib_cron.py b/src/lib/lib_cron.py
2index 5c17738..7114084 100644
3--- a/src/lib/lib_cron.py
4+++ b/src/lib/lib_cron.py
5@@ -39,17 +39,12 @@ class CronHelper:
6 If logrotate-cronjob config option is set to True install cronjob,
7 otherwise cleanup.
8 """
9- clean_up_file = self.cronjob_frequency if self.cronjob_enabled else -1
10+ cronjob_freq = self.cronjob_frequency if self.cronjob_enabled else -1
11
12 if self.cronjob_enabled is True:
13 cronjob_path = os.path.realpath(__file__)
14- cron_file_path = (
15- self.cronjob_base_path
16- + self.cronjob_check_paths[clean_up_file]
17- + "/"
18- + self.cronjob_logrotate_cron_file
19- )
20-
21+ cron_file_path = self.cronjob_path_generator(
22+ self.cronjob_check_paths[cronjob_freq])
23 logrotate_unit = hookenv.local_unit()
24 python_venv_path = os.getcwd().replace("charm", "") + ".venv/bin/python3"
25 # upgrade to template if logic increases
26@@ -63,22 +58,20 @@ class CronHelper:
27 cron_file.close()
28 os.chmod(cron_file_path, 700)
29
30- self.cleanup_cronjob(clean_up_file)
31+ self.cleanup_cronjob(cronjob_freq)
32
33 def cleanup_cronjob(self, frequency=-1):
34- """Cleanup previous config."""
35- if frequency == -1:
36- for check_path in self.cronjob_check_paths:
37- path = (
38- self.cronjob_base_path
39- + check_path
40- + "/"
41- + self.cronjob_logrotate_cron_file
42- )
43- if os.path.exists(path):
44+ """Cleanup previous config.
45+
46+ If frequency=-1, clear all cron jobs
47+ """
48+ for check_path in self.cronjob_check_paths:
49+ path = self.cronjob_path_generator(check_path)
50+ if os.path.exists(path):
51+ if frequency == -1 or self.cronjob_check_paths[frequency] != check_path:
52 os.remove(path)
53- if os.path.exists(self.cronjob_etc_config):
54- os.remove(self.cronjob_etc_config)
55+ if frequency == -1 and os.path.exists(self.cronjob_etc_config):
56+ os.remove(self.cronjob_etc_config)
57
58 def update_logrotate_etc(self):
59 """Run logrotate update config."""
60@@ -86,6 +79,16 @@ class CronHelper:
61 logrotate.read_config()
62 logrotate.modify_configs()
63
64+ def cronjob_path_generator(self, check_path):
65+ """Generate the cron job file path."""
66+ path = (
67+ self.cronjob_base_path
68+ + check_path
69+ + "/"
70+ + self.cronjob_logrotate_cron_file
71+ )
72+ return path
73+
74
75 def main():
76 """Ran by cron."""

Subscribers

People subscribed via source and target branches

to all changes: