Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pattern option to service module #902

Merged
merged 1 commit into from
Aug 18, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions library/service
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

import platform

SERVICE = None
CHKCONFIG = None
INITCTL = None
PS_OPTIONS = 'auxww'

def _find_binaries(m):
# list of possible paths for service/chkconfig binaries
Expand Down Expand Up @@ -56,14 +59,33 @@ def _find_binaries(m):
else:
INITCTL = None

def _get_service_status(name):
def _get_service_status(name, pattern):
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))

# set the running state to None because we don't know it yet
running = None

# If pattern is provided, search for that
# before checking initctl, service output, and other tricks
if pattern is not None:
psbin = '/bin/ps'
if not os.path.exists(psbin):
if os.path.exists('/usr/bin/ps'):
psbin = '/usr/bin/ps'
else:
psbin = None
if psbin is not None:
(rc, psout, pserr) = _run('%s %s' % (psbin, PS_OPTIONS))
# If rc is 0, set running as appropriate
# If ps command fails, fall back to other means.
if rc == 0:
if pattern in psout:
running = True
else:
running = False

# Check if we got upstart on the system and then the job state
if INITCTL != None:
if INITCTL != None and running is None:
# check the job status by upstart response
initctl_rc, initctl_status_stdout, initctl_status_stderr = _run("%s status %s" % (INITCTL, name))
if initctl_status_stdout.find("stop/waiting") != -1:
Expand Down Expand Up @@ -134,21 +156,29 @@ def main():
argument_spec = dict(
name = dict(required=True),
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
pattern = dict(required=False, default=None),
enabled = dict(choices=BOOLEANS)
)
)

name = module.params['name']
state = module.params['state']
pattern = module.params['pattern']
enable = module.boolean(module.params.get('enabled', None))

# Set PS options here if 'ps auxww' will not work on
# target platform
if platform.system() == 'SunOS':
global PS_OPTIONS
PS_OPTIONS = '-ef'

# ===========================================
# find binaries locations on minion
_find_binaries(module)

# ===========================================
# get service status
running = _get_service_status(name)
running = _get_service_status(name, pattern)

# ===========================================
# Some common variables
Expand Down