Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
slapos
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
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Douglas
slapos
Commits
c0e1444d
Commit
c0e1444d
authored
Sep 21, 2015
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
monitor: add promise executor
parent
47103c5c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
stack/monitor/run-promise.py
stack/monitor/run-promise.py
+61
-0
No files found.
stack/monitor/run-promise.py
0 → 100644
View file @
c0e1444d
#!{{ python_executable }}
# -*- coding: utf-8 -*-
import
sys
import
os
import
subprocess
import
json
from
cStringIO
import
StringIO
def
main
():
if
len
(
sys
.
argv
)
<
4
:
print
(
"Usage: %s <pid_path> <output_path> <command...>"
%
sys
.
argv
[
0
])
return
2
pid_path
=
sys
.
argv
[
1
]
output_path
=
sys
.
argv
[
2
]
if
os
.
path
.
exists
(
pid_path
):
with
open
(
pid_path
,
"r"
)
as
pidfile
:
try
:
pid
=
int
(
pidfile
.
read
(
6
))
except
ValueError
:
pid
=
None
if
pid
and
os
.
path
.
exists
(
"/proc/"
+
str
(
pid
)):
print
(
"A process is already running with pid "
+
str
(
pid
))
return
1
with
open
(
pid_path
,
"w"
)
as
pidfile
:
process
=
executeCommand
(
sys
.
argv
[
3
:])
pidfile
.
write
(
str
(
process
.
pid
))
status_json
=
generateStatusJsonFromProcess
(
process
)
with
open
(
output_path
,
"w"
)
as
outputfile
:
json
.
dump
(
status_json
,
outputfile
)
os
.
remove
(
pid_path
)
def
generateStatusJsonFromProcess
(
process
):
stdout
,
stderr
=
process
.
communicate
()
try
:
status_json
=
json
.
loads
(
stdout
)
except
ValueError
:
status_json
=
{}
if
process
.
returncode
!=
0
:
status_json
[
"status"
]
=
"error"
elif
not
status_json
.
get
(
"status"
):
status_json
[
"status"
]
=
"OK"
if
stderr
:
status_json
[
"error"
]
=
stderr
return
status_json
def
executeCommand
(
args
):
return
subprocess
.
Popen
(
args
,
#cwd=instance_path,
#env=None if sys.platform == 'cygwin' else {},
stdin
=
None
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
if
__name__
==
"__main__"
:
sys
.
exit
(
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