Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
OpenXG UE
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenXG
OpenXG UE
Commits
6ce8bac8
Commit
6ce8bac8
authored
Feb 28, 2022
by
hardy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change log rotation concept for module UE
parent
bda95cfb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
39 deletions
+26
-39
ci-scripts/cls_log_mgt.py
ci-scripts/cls_log_mgt.py
+26
-39
No files found.
ci-scripts/cls_log_mgt.py
View file @
6ce8bac8
...
@@ -30,11 +30,15 @@
...
@@ -30,11 +30,15 @@
import
logging
import
re
import
re
import
subprocess
import
subprocess
import
logging
import
sshconnection
import
math
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
format
=
"[%(asctime)s] %(name)s:%(levelname)s: %(message)s"
)
class
Log_Mgt
:
class
Log_Mgt
:
...
@@ -49,40 +53,25 @@ class Log_Mgt:
...
@@ -49,40 +53,25 @@ class Log_Mgt:
#-----------------$
#-----------------$
def
__Check
Avail
Space
(
self
):
def
__Check
Used
Space
(
self
):
HOST
=
self
.
Username
+
'@'
+
self
.
IPAddress
HOST
=
self
.
Username
+
'@'
+
self
.
IPAddress
COMMAND
=
"df "
+
self
.
path
COMMAND
=
"df "
+
self
.
path
ssh
=
subprocess
.
Popen
([
"ssh"
,
"%s"
%
HOST
,
COMMAND
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
ssh
=
subprocess
.
Popen
([
"ssh"
,
"%s"
%
HOST
,
COMMAND
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
result
=
ssh
.
stdout
.
readlines
()
result
=
ssh
.
stdout
.
readlines
()
s
=
result
[
1
].
decode
(
'utf-8'
).
rstrip
()
#result[1] is the second line with the results we are looking for
s
=
result
[
1
].
decode
(
'utf-8'
).
rstrip
()
#result[1] is the second line with the results we are looking for
tmp
=
s
.
split
()
used
=
s
.
split
()[
4
]
#get 4th field ex: 70%
return
tmp
[
3
]
#return avail space from the line
m
=
re
.
match
(
'^(\d+)\%'
,
used
)
if
m
is
not
None
:
return
int
(
m
.
group
(
1
))
def
__GetOldestFile
(
self
):
def
__RemoveOldest
(
self
):
HOST
=
self
.
Username
+
'@'
+
self
.
IPAddress
mySSH
=
sshconnection
.
SSHConnection
()
COMMAND
=
"ls -rtl "
+
self
.
path
#-rtl will bring oldest file on top
mySSH
.
open
(
self
.
IPAddress
,
self
.
Username
,
self
.
Password
)
ssh
=
subprocess
.
Popen
([
"ssh"
,
"%s"
%
HOST
,
COMMAND
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
COMMAND
=
'echo '
+
self
.
Password
+
' | sudo -S find '
+
self
.
path
+
' -type f -mtime +14 -delete'
result
=
ssh
.
stdout
.
readlines
()
mySSH
.
command
(
COMMAND
,
'\$'
,
20
)
s
=
result
[
1
].
decode
(
'utf-8'
).
rstrip
()
mySSH
.
close
()
tmp
=
s
.
split
()
return
tmp
[
8
]
#return filename from the line
def
__AvgSize
(
self
):
HOST
=
self
.
Username
+
'@'
+
self
.
IPAddress
COMMAND
=
"ls -rtl "
+
self
.
path
ssh
=
subprocess
.
Popen
([
"ssh"
,
"%s"
%
HOST
,
COMMAND
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
result
=
ssh
.
stdout
.
readlines
()
if
len
(
result
)
>
1
:
#at least 1 file present
total_size
=
0
for
i
in
range
(
1
,
len
(
result
)):
s
=
result
[
i
].
decode
(
'utf-8'
).
rstrip
()
tmp
=
s
.
split
()
total_size
+=
int
(
tmp
[
4
])
#get filesize
return
math
.
floor
(
total_size
/
(
len
(
result
)
-
1
))
#compute average file/artifact size
else
:
#empty,no files
return
0
#-----------------$
#-----------------$
#PUBLIC Methods$
#PUBLIC Methods$
...
@@ -90,17 +79,15 @@ class Log_Mgt:
...
@@ -90,17 +79,15 @@ class Log_Mgt:
def
LogRotation
(
self
):
def
LogRotation
(
self
):
avail_space
=
int
(
self
.
__CheckAvailSpace
())
*
1000
#avail space in target folder, initially displayed in Gb
used_space
=
self
.
__CheckUsedSpace
()
#avail space in target folder
avg_size
=
self
.
__AvgSize
()
#average size of artifacts in the target folder
if
used_space
>
80
:
logging
.
debug
(
"Avail Space : "
+
str
(
avail_space
)
+
" / Artifact Avg Size : "
+
str
(
avg_size
))
logging
.
debug
(
'
\u001B
[1;37;41m Used Disk > 80%, on '
+
self
.
Username
+
'@'
+
self
.
IPAddress
+
'
\u001B
[0m'
)
if
avail_space
<
50
*
avg_size
:
#reserved space is 50x artifact file ; oldest file will be deleted
logging
.
debug
(
'
\u001B
[1;37;41m Removing Artifacts older than 14 days
\u001B
[0m'
)
oldestfile
=
self
.
__GetOldestFile
()
self
.
__RemoveOldest
()
HOST
=
self
.
Username
+
'@'
+
self
.
IPAddress
COMMAND
=
"echo "
+
self
.
Password
+
" | sudo -S rm "
+
self
.
path
+
"/"
+
oldestfile
logging
.
debug
(
COMMAND
)
ssh
=
subprocess
.
Popen
([
"ssh"
,
"%s"
%
HOST
,
COMMAND
],
shell
=
False
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
else
:
else
:
logging
.
debug
(
"Still some space left for artifacts storage"
)
logging
.
debug
(
'Used Disk < 80%, on '
+
self
.
Username
+
'@'
+
self
.
IPAddress
+
', no cleaning required'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment