Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.toolbox
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
5
Merge Requests
5
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
slapos.toolbox
Commits
0ff082c1
Commit
0ff082c1
authored
Feb 07, 2019
by
Bryton Lacquement
🚪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix most ResourceWarning
Python 3 warns about unclosed files.
parent
5decb5a7
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
67 additions
and
40 deletions
+67
-40
slapos/promise/is_local_tcp_port_opened/__init__.py
slapos/promise/is_local_tcp_port_opened/__init__.py
+2
-1
slapos/promise/plugin/backupserver_check_backup.py
slapos/promise/plugin/backupserver_check_backup.py
+15
-14
slapos/runner/utils.py
slapos/runner/utils.py
+10
-5
slapos/test/monitor/test_config_document.py
slapos/test/monitor/test_config_document.py
+12
-6
slapos/test/monitor/testbootstrap.py
slapos/test/monitor/testbootstrap.py
+2
-1
slapos/test/monitor/testrunpromise.py
slapos/test/monitor/testrunpromise.py
+24
-12
slapos/test/test_agent.py
slapos/test/test_agent.py
+2
-1
No files found.
slapos/promise/is_local_tcp_port_opened/__init__.py
View file @
0ff082c1
...
...
@@ -28,7 +28,8 @@ def isLocalTcpPortOpened(ip_address, port):
ip_addr_hex
=
(
'%08X'
*
int_count
)
%
struct
.
unpack
(
'I'
*
int_count
,
socket
.
inet_pton
(
family
,
ip_address
))
full_addr_hex
=
ip_addr_hex
+
":%04X"
%
port
return
any
(
full_addr_hex
==
line
.
split
()[
1
]
for
line
in
open
(
tcp_path
).
readlines
())
with
open
(
tcp_path
)
as
f
:
return
any
(
full_addr_hex
==
line
.
split
()[
1
]
for
line
in
f
.
readlines
())
def
main
():
if
isLocalTcpPortOpened
(
sys
.
argv
[
1
],
int
(
sys
.
argv
[
2
])):
...
...
slapos/promise/plugin/backupserver_check_backup.py
View file @
0ff082c1
...
...
@@ -51,20 +51,21 @@ class RunPromise(GenericPromise):
# First, parse the log file
backup_started
=
False
backup_ended
=
False
for
line
in
open
(
status
,
'r'
):
m
=
re
.
match
(
r"(.*), (.*), (.*), backup (.*)$"
,
line
)
if
m
:
if
m
.
group
(
4
)
==
"running"
:
backup_started
=
True
backup_start
=
parse
(
m
.
group
(
1
))
elif
m
.
group
(
4
)
==
"failed"
:
backup_ended
=
True
backup_failed
=
True
backup_end
=
parse
(
m
.
group
(
1
))
elif
m
.
group
(
4
)
==
"success"
:
backup_ended
=
True
backup_failed
=
False
backup_end
=
parse
(
m
.
group
(
1
))
with
open
(
status
,
'r'
)
as
f
:
for
line
in
f
:
m
=
re
.
match
(
r"(.*), (.*), (.*), backup (.*)$"
,
line
)
if
m
:
if
m
.
group
(
4
)
==
"running"
:
backup_started
=
True
backup_start
=
parse
(
m
.
group
(
1
))
elif
m
.
group
(
4
)
==
"failed"
:
backup_ended
=
True
backup_failed
=
True
backup_end
=
parse
(
m
.
group
(
1
))
elif
m
.
group
(
4
)
==
"success"
:
backup_ended
=
True
backup_failed
=
False
backup_end
=
parse
(
m
.
group
(
1
))
# Then check result
if
backup_ended
and
backup_failed
:
...
...
slapos/runner/utils.py
View file @
0ff082c1
...
...
@@ -43,7 +43,8 @@ html_escape_table = {
def
getBuildAndRunParams
(
config
):
json_file
=
os
.
path
.
join
(
config
[
'etc_dir'
],
'config.json'
)
json_params
=
json
.
load
(
open
(
json_file
))
with
open
(
json_file
)
as
f
:
json_params
=
json
.
load
(
f
)
return
json_params
...
...
@@ -52,7 +53,8 @@ def saveBuildAndRunParams(config, params):
Works like that because this function do not care
about how you got the parameters"""
json_file
=
os
.
path
.
join
(
config
[
'etc_dir'
],
'config.json'
)
open
(
json_file
,
"w"
).
write
(
json
.
dumps
(
params
))
with
open
(
json_file
,
"w"
)
as
f
:
f
.
write
(
json
.
dumps
(
params
))
def
html_escape
(
text
):
...
...
@@ -899,7 +901,8 @@ def runSlapgridUntilSuccess(config, step):
else:
return -1
counter_file = os.path.join(config['
runner_workdir
'], '
.
turn
-
left
')
open(counter_file, '
w
+
').write(str(max_tries))
with open(counter_file, '
w
+
') as f:
f.write(str(max_tries))
counter = max_tries
slapgrid = True
# XXX-Nico runSoftwareWithLock can return 0 or False (0==False)
...
...
@@ -909,9 +912,11 @@ def runSlapgridUntilSuccess(config, step):
# slapgrid == 0 because EXIT_SUCCESS == 0
if slapgrid == 0:
break
times_left = int(open(counter_file).read()) - 1
with open(counter_file) as f:
times_left = int(f.read()) - 1
if times_left > 0 :
open(counter_file, '
w
+
').write(str(times_left))
with open(counter_file, '
w
+
') as f:
f.write(str(times_left))
counter = times_left
else :
counter = 0
...
...
slapos/test/monitor/test_config_document.py
View file @
0ff082c1
...
...
@@ -101,8 +101,10 @@ echo "htpasswd $@" > %s/monitor-htpasswd
def
check_config
(
self
):
config_parameter
=
os
.
path
.
join
(
self
.
config_dir
,
'config.parameters.json'
)
config_parameter_json
=
json
.
load
(
open
(
config_parameter
))
config_json
=
json
.
load
(
open
(
self
.
config_path
))
with
open
(
config_parameter
)
as
f
:
config_parameter_json
=
json
.
load
(
f
)
with
open
(
self
.
config_path
)
as
f
:
config_json
=
json
.
load
(
f
)
for
config
in
config_json
:
if
config
[
"key"
]:
...
...
@@ -112,20 +114,24 @@ echo "htpasswd $@" > %s/monitor-htpasswd
continue
if
config
[
"key"
]
==
'from-file'
:
self
.
assertTrue
(
os
.
path
.
exists
(
parameter
[
'file'
]))
self
.
assertEqual
(
config
[
"value"
],
open
(
parameter
[
'file'
]).
read
())
with
open
(
parameter
[
'file'
])
as
f
:
self
.
assertEqual
(
config
[
"value"
],
f
.
read
())
elif
config
[
"key"
]
==
'httpd-password'
:
http_passwd
=
"%s/monitor-htpasswd"
%
self
.
base_dir
#XXX where \n bellow come from ?
command
=
'htpasswd -cb %s admin %s%s'
%
(
http_passwd
,
config
[
"value"
],
'
\
n
'
)
self
.
assertTrue
(
os
.
path
.
exists
(
parameter
[
'file'
]))
self
.
assertTrue
(
os
.
path
.
exists
(
http_passwd
))
self
.
assertEqual
(
config
[
"value"
],
open
(
parameter
[
'file'
]).
read
())
self
.
assertEqual
(
open
(
http_passwd
).
read
(),
command
)
with
open
(
parameter
[
'file'
])
as
f
:
self
.
assertEqual
(
config
[
"value"
],
f
.
read
())
with
open
(
http_passwd
)
as
f
:
self
.
assertEqual
(
f
.
read
(),
command
)
elif
config
[
"key"
]
==
'cors-domain'
:
cors_file
=
"%s/test-httpd-cors.cfg"
%
self
.
base_dir
self
.
assertTrue
(
os
.
path
.
exists
(
cors_file
))
cors_string
=
self
.
generate_cors_string
(
config
[
"value"
].
split
())
self
.
assertEqual
(
cors_string
,
open
(
cors_file
).
read
())
with
open
(
cors_file
)
as
f
:
self
.
assertEqual
(
cors_string
,
f
.
read
())
def
check_cfg_config
(
self
,
config_list
):
cfg_output
=
os
.
path
.
join
(
self
.
config_dir
,
'config.cfg'
)
...
...
slapos/test/monitor/testbootstrap.py
View file @
0ff082c1
...
...
@@ -249,7 +249,8 @@ partition-folder = %(base_dir)s
instance_config
=
os
.
path
.
join
(
instance
.
config_folder
,
'.jio_documents'
,
'config.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
instance_config
))
config_content
=
json
.
loads
(
open
(
instance_config
).
read
())
with
open
(
instance_config
)
as
f
:
config_content
=
json
.
loads
(
f
.
read
())
self
.
assertEqual
(
len
(
config_content
),
4
)
key_list
=
[
''
,
'sample'
,
'monitor-password'
,
'cors-domain'
]
for
parameter
in
config_content
:
...
...
slapos/test/monitor/testrunpromise.py
View file @
0ff082c1
...
...
@@ -150,7 +150,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'my_promise.status.json'
)
os
.
system
(
'cat %s'
%
result_file
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
my_result
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
my_result
=
json
.
load
(
f
)
my_result
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
my_result
.
pop
(
u'execution-time'
))
expected_result
=
{
...
...
@@ -164,7 +165,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'my_second_promise.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
second_result
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
second_result
=
json
.
load
(
f
)
second_result
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
second_result
.
pop
(
u'execution-time'
))
...
...
@@ -185,7 +187,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'my_promise.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
my_result
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
my_result
=
json
.
load
(
f
)
my_result
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
my_result
.
pop
(
u'execution-time'
))
...
...
@@ -206,7 +209,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'my_promise.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
my_result
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
my_result
=
json
.
load
(
f
)
my_result
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
my_result
.
pop
(
u'execution-time'
))
...
...
@@ -225,7 +229,8 @@ class RunPromise(GenericPromise):
promise_runner2
=
MonitorPromiseLauncher
(
parser
)
promise_runner2
.
start
()
my_result
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
my_result
=
json
.
load
(
f
)
my_result
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
my_result
.
pop
(
u'execution-time'
))
...
...
@@ -286,7 +291,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'promise_1.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
result1
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result1
=
json
.
load
(
f
)
start_date
=
datetime
.
strptime
(
result1
[
'result'
].
pop
(
'date'
),
'%Y-%m-%dT%H:%M:%S+0000'
)
expected_result
=
{
...
...
@@ -301,7 +307,8 @@ class RunPromise(GenericPromise):
parser
=
self
.
getPromiseParser
(
force
=
True
)
promise_runner
=
MonitorPromiseLauncher
(
parser
)
promise_runner
.
start
()
result2
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result2
=
json
.
load
(
f
)
start_date2
=
datetime
.
strptime
(
result2
[
'result'
].
pop
(
'date'
),
'%Y-%m-%dT%H:%M:%S+0000'
)
self
.
assertTrue
(
result2
.
pop
(
u'execution-time'
))
self
.
assertEqual
(
expected_result
,
result2
)
...
...
@@ -318,7 +325,8 @@ class RunPromise(GenericPromise):
result2_file
=
os
.
path
.
join
(
self
.
output_dir
,
'promise_2.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
self
.
assertTrue
(
os
.
path
.
exists
(
result2_file
))
result1
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result1
=
json
.
load
(
f
)
start_date
=
datetime
.
strptime
(
result1
[
'result'
].
pop
(
'date'
),
'%Y-%m-%dT%H:%M:%S+0000'
)
self
.
assertTrue
(
result1
.
pop
(
u'execution-time'
))
...
...
@@ -352,7 +360,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'promise_1.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
result1
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result1
=
json
.
load
(
f
)
result1
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
result1
.
pop
(
u'execution-time'
))
expected_result
=
{
...
...
@@ -367,7 +376,8 @@ class RunPromise(GenericPromise):
# second run
promise_runner
=
MonitorPromiseLauncher
(
parser
)
promise_runner
.
start
()
result2
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result2
=
json
.
load
(
f
)
result2
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
result2
.
pop
(
u'execution-time'
))
self
.
assertEqual
(
expected_result
,
result2
)
...
...
@@ -380,7 +390,8 @@ class RunPromise(GenericPromise):
result_file
=
os
.
path
.
join
(
self
.
output_dir
,
'promise_1.status.json'
)
self
.
assertTrue
(
os
.
path
.
exists
(
result_file
))
result1
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result1
=
json
.
load
(
f
)
result1
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
result1
.
pop
(
u'execution-time'
))
expected_result
=
{
...
...
@@ -401,7 +412,8 @@ class RunPromise(GenericPromise):
promise_runner
=
MonitorPromiseLauncher
(
parser
)
promise_runner
.
start
()
result2
=
json
.
load
(
open
(
result_file
))
with
open
(
result_file
)
as
f
:
result2
=
json
.
load
(
f
)
result2
[
'result'
].
pop
(
'date'
)
self
.
assertTrue
(
result2
.
pop
(
u'execution-time'
))
self
.
assertEqual
(
expected_result
,
result2
)
...
...
slapos/test/test_agent.py
View file @
0ff082c1
...
...
@@ -159,7 +159,8 @@ class TestAutoSTemp(unittest.TestCase):
removes it when deleted.
"""
f
=
AutoSTemp
(
"foo"
)
self
.
assertEqual
(
open
(
f
.
name
,
"r"
).
read
(),
"foo"
)
with
open
(
f
.
name
,
"r"
)
as
file
:
self
.
assertEqual
(
file
.
read
(),
"foo"
)
fname
=
f
.
name
self
.
assertTrue
(
os
.
path
.
isfile
(
fname
))
del
f
...
...
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