Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
ea296e9e
Commit
ea296e9e
authored
Apr 26, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'devel' of
https://github.com/jkleint/ansible
into jkleint-devel
parents
47ec93b3
c6db4e8f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
17 deletions
+36
-17
library/service
+36
-17
No files found.
library/service
View file @
ea296e9e
...
...
@@ -21,13 +21,22 @@ try:
import
json
except
ImportError
:
import
simplejson
as
json
import
os
import
sys
import
shlex
import
subprocess
# ===========================================
SERVICE
=
'/sbin/service'
def
_run
(
cmd
):
''' :Return: A tuple of ``(returncode, stdout, stderr)`` resulting from executing
`cmd` with the shell. '''
process
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
shell
=
True
)
stdout
,
stderr
=
process
.
communicate
()
return
(
process
.
returncode
,
stdout
,
stderr
)
argfile
=
sys
.
argv
[
1
]
args
=
open
(
argfile
,
'r'
)
.
read
()
items
=
shlex
.
split
(
args
)
...
...
@@ -46,26 +55,31 @@ for arg in items:
name
=
params
[
'name'
]
state
=
params
.
get
(
'state'
,
'unknown'
)
list_
=
params
.
get
(
'list'
)
# running and started are the same
if
state
not
in
[
'running'
,
'started'
,
'stopped'
,
'restarted'
]:
print
json
.
dumps
(
dict
(
failed
=
True
,
msg
=
'invalid state'
))
sys
.
exit
(
1
)
if
list_
and
list_
not
in
(
'status'
,):
print
json
.
dumps
(
dict
(
failed
=
True
,
msg
=
'invalid argument to list'
))
sys
.
exit
(
1
)
# ===========================================
# get service status
status
=
os
.
popen
(
"service
%
s status"
%
name
)
.
read
(
)
rc
,
stdout
,
stderr
=
_run
(
"
%
s
%
s status"
%
(
SERVICE
,
name
)
)
# ===========================================
# determine if we are going to change anything
running
=
False
if
st
atus
.
find
(
"not running"
)
!=
-
1
:
if
st
dout
.
find
(
"not running"
)
!=
-
1
:
running
=
False
elif
st
atus
.
find
(
"running"
)
!=
-
1
:
elif
st
dout
.
find
(
"running"
)
!=
-
1
:
running
=
True
elif
name
==
'iptables'
and
st
atus
.
find
(
"ACCEPT"
)
!=
-
1
:
elif
name
==
'iptables'
and
st
dout
.
find
(
"ACCEPT"
)
!=
-
1
:
# iptables status command output is lame
# TODO: lookup if we can use a return code for this instead?
running
=
True
...
...
@@ -81,32 +95,37 @@ elif state == "restarted":
# ===========================================
# run change commands if we need to
def
_run
(
cmd
):
return
subprocess
.
call
(
cmd
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
shell
=
True
)
rc
=
0
if
changed
:
if
state
in
(
'started'
,
'running'
):
rc
=
_run
(
"service
%
s start"
%
name
)
rc
,
stdout
,
stderr
=
_run
(
"
%
s
%
s start"
%
(
SERVICE
,
name
)
)
elif
state
==
'stopped'
:
rc
=
_run
(
"service
%
s stop"
%
name
)
rc
,
stdout
,
stderr
=
_run
(
"
%
s
%
s stop"
%
(
SERVICE
,
name
)
)
elif
state
==
'restarted'
:
rc1
=
_run
(
"service
%
s stop"
%
name
)
rc2
=
_run
(
"service
%
s start"
%
name
)
rc
=
rc1
and
rc2
rc1
,
stdout1
,
stderr1
=
_run
(
"
%
s
%
s stop"
%
(
SERVICE
,
name
))
rc2
,
stdout2
,
stderr2
=
_run
(
"
%
s
%
s start"
%
(
SERVICE
,
name
))
rc
=
rc1
and
rc2
stdout
=
stdout1
+
stdout2
stderr
=
stderr1
+
stderr2
if
rc
!=
0
:
# yeah, should probably include output of failure...
print
json
.
dumps
({
"failed"
:
1
,
"rc"
:
rc
"rc"
:
rc
,
})
print
>>
sys
.
stderr
,
stdout
+
stderr
sys
.
exit
(
1
)
# ===============================================
# success
print
json
.
dumps
({
"changed"
:
changed
})
result
=
{
"changed"
:
changed
}
if
list_
==
'status'
:
rc
,
stdout
,
stderr
=
_run
(
"
%
s
%
s status"
%
(
SERVICE
,
name
))
result
[
'status'
]
=
stdout
print
json
.
dumps
(
result
)
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