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
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
Gabriel Monnerat
slapos.toolbox
Commits
8128ac04
Commit
8128ac04
authored
Sep 02, 2016
by
Nicolas Wavrant
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
notifier.py: if given correct arguments, actions run by the notifier can be reported in files
parent
a77c935c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
53 additions
and
4 deletions
+53
-4
slapos/pubsub/notifier.py
slapos/pubsub/notifier.py
+53
-4
No files found.
slapos/pubsub/notifier.py
View file @
8128ac04
...
@@ -3,7 +3,10 @@
...
@@ -3,7 +3,10 @@
import
argparse
import
argparse
import
csv
import
csv
import
datetime
import
json
import
httplib
import
httplib
import
os
import
socket
import
socket
import
subprocess
import
subprocess
import
sys
import
sys
...
@@ -13,6 +16,20 @@ import urllib2
...
@@ -13,6 +16,20 @@ import urllib2
import
urlparse
import
urlparse
import
uuid
import
uuid
def
createStatusItem
(
item_directory
,
instance_name
,
callback
,
date
,
link
,
status
):
global
app
callback_short_name
=
os
.
path
.
basename
(
callback
)
content
=
json
.
dumps
({
'title'
:
'%s-PBS %s : %s'
%
(
instance_name
,
callback_short_name
,
status
),
'description'
:
'%s run at %s'
%
(
callback_short_name
,
datetime
.
datetime
.
fromtimestamp
(
date
).
isoformat
()),
'pubDate'
:
date
,
'link'
:
link
,
})
item_path
=
os
.
path
.
join
(
item_directory
,
"status_%s"
%
time
.
time
())
with
open
(
item_path
,
'w'
)
as
file
:
file
.
write
(
content
)
def
main
():
def
main
():
parser
=
argparse
.
ArgumentParser
()
parser
=
argparse
.
ArgumentParser
()
...
@@ -32,8 +49,31 @@ def main():
...
@@ -32,8 +49,31 @@ def main():
type
=
int
,
required
=
False
,
type
=
int
,
required
=
False
,
help
=
"Additional parameter for notification-url"
)
help
=
"Additional parameter for notification-url"
)
# Verbose mode
parser
.
add_argument
(
'--instance-root-name'
,
dest
=
'instance_root_name'
,
type
=
str
,
required
=
False
,
help
=
"Path to config file containing info on instance"
)
parser
.
add_argument
(
'--log-url'
,
required
=
False
,
dest
=
'log_url'
,
help
=
"URL where the log file will be accessible"
)
parser
.
add_argument
(
'--status-item-directory'
,
dest
=
'status_item_directory'
,
required
=
False
,
default
=
''
,
type
=
str
,
help
=
"Directory containing PBS status to publish as feed."
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
if
args
.
instance_root_name
and
args
.
log_url
and
args
.
status_item_directory
:
# Verbose mode
saveStatus
=
lambda
status
:
createStatusItem
(
args
.
status_item_directory
,
args
.
instance_root_name
,
args
.
executable
[
0
],
time
.
time
(),
args
.
log_url
,
status
)
else
:
saveStatus
=
lambda
status
:
None
saveStatus
(
'STARTED'
)
try
:
try
:
content
=
subprocess
.
check_output
(
content
=
subprocess
.
check_output
(
args
.
executable
[
0
],
args
.
executable
[
0
],
...
@@ -45,7 +85,9 @@ def main():
...
@@ -45,7 +85,9 @@ def main():
args
.
executable
[
0
],
args
.
executable
[
0
],
content
.
replace
(
'&'
,
'&'
).
replace
(
'<'
,
'<'
).
replace
(
'>'
,
'>'
)
content
.
replace
(
'&'
,
'&'
).
replace
(
'<'
,
'<'
).
replace
(
'>'
,
'>'
)
))
))
saveStatus
(
'FINISHED'
)
except
subprocess
.
CalledProcessError
as
e
:
except
subprocess
.
CalledProcessError
as
e
:
saveStatus
(
'ERROR'
)
content
=
e
.
output
content
=
e
.
output
exit_code
=
e
.
returncode
exit_code
=
e
.
returncode
content
=
(
"FAILURE</br><p>%s Failed with returncode <em>%d</em>.</p>"
content
=
(
"FAILURE</br><p>%s Failed with returncode <em>%d</em>.</p>"
...
@@ -91,23 +133,30 @@ def main():
...
@@ -91,23 +133,30 @@ def main():
notification_path
+=
str
(
transaction_id
)
notification_path
+=
str
(
transaction_id
)
headers
=
{
'Content-Type'
:
feed
.
info
().
getheader
(
'Content-Type'
)}
headers
=
{
'Content-Type'
:
feed
.
info
().
getheader
(
'Content-Type'
)}
error_message
=
""
try
:
try
:
notification
=
httplib
.
HTTPConnection
(
notification_url
.
hostname
,
notification
=
httplib
.
HTTPConnection
(
notification_url
.
hostname
,
notification_port
)
notification_port
)
notification
.
request
(
'POST'
,
notification_path
,
body
,
headers
)
notification
.
request
(
'POST'
,
notification_path
,
body
,
headers
)
response
=
notification
.
getresponse
()
response
=
notification
.
getresponse
()
if
not
(
200
<=
response
.
status
<
300
):
if
not
(
200
<=
response
.
status
<
300
):
sys
.
stderr
.
write
(
"The remote server at %s didn't send a successful reponse.
\
n
"
%
notif_url
)
error_message
=
(
"The remote server at %s didn't send a successful reponse.
\
n
"
sys
.
stderr
.
write
(
"Its response was %r
\
n
"
%
response
.
reason
)
"Its response was %r
\
n
"
)
%
(
notif_url
,
response
.
reason
)
some_notification_failed
=
True
some_notification_failed
=
True
except
socket
.
error
as
exc
:
except
socket
.
error
as
exc
:
sys
.
stderr
.
write
(
"Connection with remote server at %s failed:
\
n
"
%
notif_url
)
error_message
=
"Connection with remote server at %s failed:
\
n
"
%
notif_url
sys
.
stderr
.
write
(
traceback
.
format_exc
(
exc
))
error_message
.
append
(
traceback
.
format_exc
(
exc
))
some_notification_failed
=
True
some_notification_failed
=
True
finally
:
if
error_message
:
sys
.
stderr
.
write
(
error_message
)
saveStatus
(
saveStatus
(
'ERROR ON NOTIFYING : %s'
)
%
error_message
)
if
some_notification_failed
:
if
some_notification_failed
:
sys
.
exit
(
1
)
sys
.
exit
(
1
)
saveStatus
(
'OK'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
main
()
main
()
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