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
fa63e9ce
Commit
fa63e9ce
authored
Nov 26, 2012
by
Michael DeHaan
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1680 from eest/fix_fbsd_enable
Rework FreeBSD "enabled" code
parents
0c70abfa
d4af9e4c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
18 deletions
+65
-18
library/service
+65
-18
No files found.
library/service
View file @
fa63e9ce
...
@@ -72,7 +72,8 @@ examples:
...
@@ -72,7 +72,8 @@ examples:
import
platform
import
platform
import
os
import
os
import
re
import
tempfile
import
shlex
class
Service
(
object
):
class
Service
(
object
):
"""
"""
...
@@ -108,6 +109,9 @@ class Service(object):
...
@@ -108,6 +109,9 @@ class Service(object):
self
.
svc_initctl
=
None
self
.
svc_initctl
=
None
self
.
enable_cmd
=
None
self
.
enable_cmd
=
None
self
.
arguments
=
module
.
params
.
get
(
'arguments'
,
''
)
self
.
arguments
=
module
.
params
.
get
(
'arguments'
,
''
)
self
.
rcconf_file
=
None
self
.
rcconf_key
=
None
self
.
rcconf_value
=
None
# select whether we dump additional debug info through syslog
# select whether we dump additional debug info through syslog
self
.
syslogging
=
False
self
.
syslogging
=
False
...
@@ -194,6 +198,59 @@ class Service(object):
...
@@ -194,6 +198,59 @@ class Service(object):
out
=
''
out
=
''
return
rc
,
out
,
err
return
rc
,
out
,
err
def
service_enable_rcconf
(
self
):
if
self
.
rcconf_file
is
None
or
self
.
rcconf_key
is
None
or
self
.
rcconf_value
is
None
:
self
.
module
.
fail_json
(
msg
=
"service_enable_rcconf() requires rcconf_file, rcconf_key and rcconf_value"
)
changed
=
None
entry
=
'
%
s="
%
s"
\n
'
%
(
self
.
rcconf_key
,
self
.
rcconf_value
)
RCFILE
=
open
(
self
.
rcconf_file
,
"r"
)
new_rc_conf
=
[]
# Build a list containing the possibly modified file.
for
rcline
in
RCFILE
:
# Parse line removing whitespaces, quotes, etc.
rcarray
=
shlex
.
split
(
rcline
,
comments
=
True
)
if
len
(
rcarray
)
>=
1
and
'='
in
rcarray
[
0
]:
(
key
,
value
)
=
rcarray
[
0
]
.
split
(
"="
,
1
)
if
key
==
self
.
rcconf_key
:
if
value
==
self
.
rcconf_value
:
# Since the proper entry already exists we can stop iterating.
changed
=
False
break
else
:
# We found the key but the value is wrong, replace with new entry.
rcline
=
entry
changed
=
True
# Add line to the list.
new_rc_conf
.
append
(
rcline
)
# We are done with reading the current rc.conf, close it.
RCFILE
.
close
()
# If we did not see any trace of our entry we need to add it.
if
changed
is
None
:
new_rc_conf
.
append
(
entry
)
changed
=
True
if
changed
is
True
:
# Create a temporary file next to the current rc.conf (so we stay on the same filesystem).
# This way the replacement operation is atomic.
rcconf_dir
=
os
.
path
.
dirname
(
self
.
rcconf_file
)
rcconf_base
=
os
.
path
.
basename
(
self
.
rcconf_file
)
(
TMP_RCCONF
,
tmp_rcconf_file
)
=
tempfile
.
mkstemp
(
dir
=
rcconf_dir
,
prefix
=
"
%
s-"
%
rcconf_base
)
# Write out the contents of the list into our temporary file.
for
rcline
in
new_rc_conf
:
os
.
write
(
TMP_RCCONF
,
rcline
)
# Close temporary file.
os
.
close
(
TMP_RCCONF
)
# Replace previous rc.conf.
self
.
module
.
atomic_replace
(
tmp_rcconf_file
,
self
.
rcconf_file
)
# ===========================================
# ===========================================
# Subclass: Linux
# Subclass: Linux
...
@@ -368,28 +425,18 @@ class FreeBsdService(Service):
...
@@ -368,28 +425,18 @@ class FreeBsdService(Service):
def
service_enable
(
self
):
def
service_enable
(
self
):
if
self
.
enable
:
if
self
.
enable
:
rc
=
"YES"
self
.
rcconf_value
=
"YES"
else
:
else
:
rc
=
"NO"
self
.
rcconf_value
=
"NO"
rcfiles
=
[
'/etc/rc.conf'
,
'/usr/local/etc/rc.conf'
]
rcfiles
=
[
'/etc/rc.conf'
,
'/usr/local/etc/rc.conf'
]
for
rcfile
in
rcfiles
:
for
rcfile
in
rcfiles
:
if
os
.
path
.
isfile
(
rcfile
):
if
os
.
path
.
isfile
(
rcfile
):
rcconf
=
rcfile
self
.
rcconf_file
=
rcfile
entry
=
"
%
s_enable"
%
self
.
name
self
.
rcconf_key
=
"
%
s_enable"
%
self
.
name
full_entry
=
'
%
s="
%
s"'
%
(
entry
,
rc
)
rc
=
open
(
rcconf
,
"r+"
)
return
self
.
service_enable_rcconf
()
rctext
=
rc
.
read
()
if
re
.
search
(
"^
%
s"
%
full_entry
,
rctext
,
re
.
M
)
is
None
:
if
re
.
search
(
"^
%
s"
%
entry
,
rctext
,
re
.
M
)
is
None
:
rctext
+=
"
\n
%
s"
%
full_entry
else
:
rctext
=
re
.
sub
(
"^
%
s.*"
%
entry
,
full_entry
,
rctext
,
1
,
re
.
M
)
rc
.
truncate
(
0
)
rc
.
seek
(
0
)
rc
.
write
(
rctext
)
rc
.
close
()
def
service_control
(
self
):
def
service_control
(
self
):
if
self
.
action
is
"start"
:
if
self
.
action
is
"start"
:
...
...
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