2012-03-16 02:53:14 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import shutil
|
|
|
|
import stat
|
|
|
|
import grp
|
|
|
|
import pwd
|
2012-05-10 00:08:45 +02:00
|
|
|
import syslog
|
2012-04-12 19:33:10 +02:00
|
|
|
try:
|
|
|
|
import selinux
|
|
|
|
HAVE_SELINUX=True
|
|
|
|
except ImportError:
|
|
|
|
HAVE_SELINUX=False
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
def dump_kv(vars):
|
2012-06-01 04:14:33 +02:00
|
|
|
return " ".join("%s='%s'" % (k,v) for (k,v) in vars.items())
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
def exit_kv(rc=0, **kwargs):
|
2012-03-16 03:04:04 +01:00
|
|
|
if 'path' in kwargs:
|
|
|
|
add_path_info(kwargs)
|
2012-05-05 22:31:03 +02:00
|
|
|
print dump_kv(kwargs)
|
2012-03-16 03:04:04 +01:00
|
|
|
sys.exit(rc)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
def fail_kv(**kwargs):
|
2012-03-16 02:53:14 +01:00
|
|
|
kwargs['failed'] = True
|
2012-05-05 22:31:03 +02:00
|
|
|
exit_kv(rc=1, **kwargs)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-03-16 03:04:04 +01:00
|
|
|
def add_path_info(kwargs):
|
|
|
|
path = kwargs['path']
|
|
|
|
if os.path.exists(path):
|
|
|
|
(user, group) = user_and_group(path)
|
|
|
|
kwargs['user'] = user
|
|
|
|
kwargs['group'] = group
|
|
|
|
st = os.stat(path)
|
2012-04-26 14:55:31 +02:00
|
|
|
kwargs['mode'] = oct(stat.S_IMODE(st[stat.ST_MODE]))
|
2012-03-16 03:04:04 +01:00
|
|
|
# secontext not yet supported
|
2012-04-02 01:10:23 +02:00
|
|
|
if os.path.islink(path):
|
|
|
|
kwargs['state'] = 'link'
|
|
|
|
elif os.path.isfile(path):
|
2012-03-16 03:04:04 +01:00
|
|
|
kwargs['state'] = 'file'
|
|
|
|
else:
|
|
|
|
kwargs['state'] = 'directory'
|
2012-04-26 16:23:18 +02:00
|
|
|
if HAVE_SELINUX and selinux_enabled():
|
2012-04-12 19:33:10 +02:00
|
|
|
kwargs['secontext'] = ':'.join(selinux_context(path))
|
2012-03-16 03:04:04 +01:00
|
|
|
else:
|
|
|
|
kwargs['state'] = 'absent'
|
|
|
|
return kwargs
|
2012-04-26 16:23:18 +02:00
|
|
|
|
|
|
|
# Detect whether using selinux that is MLS-aware.
|
|
|
|
# While this means you can set the level/range with
|
|
|
|
# selinux.lsetfilecon(), it may or may not mean that you
|
|
|
|
# will get the selevel as part of the context returned
|
|
|
|
# by selinux.lgetfilecon().
|
|
|
|
def selinux_mls_enabled():
|
|
|
|
if not HAVE_SELINUX:
|
|
|
|
return False
|
|
|
|
if selinux.is_selinux_mls_enabled() == 1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def selinux_enabled():
|
|
|
|
if not HAVE_SELINUX:
|
|
|
|
return False
|
|
|
|
if selinux.is_selinux_enabled() == 1:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Determine whether we need a placeholder for selevel/mls
|
|
|
|
def selinux_initial_context():
|
|
|
|
context = [None, None, None]
|
|
|
|
if selinux_mls_enabled():
|
|
|
|
context.append(None)
|
|
|
|
return context
|
|
|
|
|
2012-04-22 08:27:34 +02:00
|
|
|
# If selinux fails to find a default, return an array of None
|
|
|
|
def selinux_default_context(path, mode=0):
|
2012-04-26 16:23:18 +02:00
|
|
|
context = selinux_initial_context()
|
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-22 08:27:34 +02:00
|
|
|
return context
|
|
|
|
try:
|
|
|
|
ret = selinux.matchpathcon(path, mode)
|
|
|
|
except OSError:
|
|
|
|
return context
|
|
|
|
if ret[0] == -1:
|
|
|
|
return context
|
|
|
|
context = ret[1].split(':')
|
|
|
|
return context
|
|
|
|
|
2012-04-25 18:39:11 +02:00
|
|
|
def selinux_context(path):
|
2012-04-26 16:23:18 +02:00
|
|
|
context = selinux_initial_context()
|
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-25 18:39:11 +02:00
|
|
|
return context
|
|
|
|
try:
|
|
|
|
ret = selinux.lgetfilecon(path)
|
|
|
|
except:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='failed to retrieve selinux context')
|
2012-04-25 18:39:11 +02:00
|
|
|
if ret[0] == -1:
|
|
|
|
return context
|
|
|
|
context = ret[1].split(':')
|
|
|
|
return context
|
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
# ===========================================
|
|
|
|
|
|
|
|
argfile = sys.argv[1]
|
|
|
|
args = open(argfile, 'r').read()
|
|
|
|
items = shlex.split(args)
|
2012-05-10 00:08:45 +02:00
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
if not len(items):
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(msg='the module requires arguments -a')
|
2012-03-16 02:53:14 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
for x in items:
|
2012-07-23 07:51:33 +02:00
|
|
|
(k, v) = x.split("=",1)
|
2012-03-16 02:53:14 +01:00
|
|
|
params[k] = v
|
|
|
|
|
|
|
|
state = params.get('state','file')
|
2012-04-20 14:02:12 +02:00
|
|
|
path = params.get('path', params.get('dest', params.get('name', None)))
|
|
|
|
if path:
|
|
|
|
path = os.path.expanduser(path)
|
|
|
|
src = params.get('src', None)
|
|
|
|
if src:
|
2012-04-20 14:09:43 +02:00
|
|
|
src = os.path.expanduser(src)
|
2012-04-02 01:10:23 +02:00
|
|
|
dest = params.get('dest', None)
|
2012-03-16 02:53:14 +01:00
|
|
|
mode = params.get('mode', None)
|
|
|
|
owner = params.get('owner', None)
|
|
|
|
group = params.get('group', None)
|
2012-03-16 03:37:37 +01:00
|
|
|
|
2012-04-12 19:33:10 +02:00
|
|
|
# selinux related options
|
|
|
|
seuser = params.get('seuser', None)
|
|
|
|
serole = params.get('serole', None)
|
|
|
|
setype = params.get('setype', None)
|
2012-04-18 02:12:09 +02:00
|
|
|
selevel = params.get('serange', 's0')
|
2012-04-25 18:39:11 +02:00
|
|
|
secontext = [seuser, serole, setype]
|
2012-04-26 16:23:18 +02:00
|
|
|
if selinux_mls_enabled():
|
2012-04-25 18:39:11 +02:00
|
|
|
secontext.append(selevel)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-05-02 20:38:44 +02:00
|
|
|
default_secontext = selinux_default_context(path)
|
|
|
|
for i in range(len(default_secontext)):
|
|
|
|
if i is not None and secontext[i] == '_default':
|
|
|
|
secontext[i] = default_secontext[i]
|
2012-04-22 08:27:34 +02:00
|
|
|
|
2012-04-02 01:10:23 +02:00
|
|
|
if state not in [ 'file', 'directory', 'link', 'absent']:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(msg='invalid state: %s' % state)
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-04-03 02:08:40 +02:00
|
|
|
if state == 'link' and (src is None or dest is None):
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(msg='src and dest are required for "link" state')
|
2012-04-02 01:10:23 +02:00
|
|
|
elif path is None:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(msg='path is required')
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# support functions
|
|
|
|
|
|
|
|
def user_and_group(filename):
|
|
|
|
st = os.stat(filename)
|
|
|
|
uid = st.st_uid
|
|
|
|
gid = st.st_gid
|
2012-06-30 02:05:23 +02:00
|
|
|
try:
|
|
|
|
user = pwd.getpwuid(uid)[0]
|
|
|
|
except KeyError:
|
2012-07-01 23:38:17 +02:00
|
|
|
user = str(uid)
|
2012-06-30 02:05:23 +02:00
|
|
|
try:
|
|
|
|
group = grp.getgrgid(gid)[0]
|
|
|
|
except KeyError:
|
2012-07-01 23:38:17 +02:00
|
|
|
group = str(gid)
|
2012-03-16 02:53:14 +01:00
|
|
|
return (user, group)
|
|
|
|
|
|
|
|
def set_context_if_different(path, context, changed):
|
2012-04-26 16:23:18 +02:00
|
|
|
if not HAVE_SELINUX or not selinux_enabled():
|
2012-04-12 19:33:10 +02:00
|
|
|
return changed
|
|
|
|
cur_context = selinux_context(path)
|
2012-04-22 08:27:34 +02:00
|
|
|
new_context = list(cur_context)
|
2012-04-26 16:23:18 +02:00
|
|
|
# Iterate over the current context instead of the
|
|
|
|
# argument context, which may have selevel.
|
|
|
|
for i in range(len(cur_context)):
|
2012-04-12 19:33:10 +02:00
|
|
|
if context[i] is not None and context[i] != cur_context[i]:
|
|
|
|
new_context[i] = context[i]
|
|
|
|
if cur_context != new_context:
|
|
|
|
try:
|
|
|
|
rc = selinux.lsetfilecon(path, ':'.join(new_context))
|
|
|
|
except OSError:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='invalid selinux context')
|
2012-04-12 19:33:10 +02:00
|
|
|
if rc != 0:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='set selinux context failed')
|
2012-04-12 19:33:10 +02:00
|
|
|
changed = True
|
|
|
|
return changed
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
def set_owner_if_different(path, owner, changed):
|
|
|
|
if owner is None:
|
|
|
|
return changed
|
|
|
|
user, group = user_and_group(path)
|
|
|
|
if owner != user:
|
2012-06-01 04:19:05 +02:00
|
|
|
rc = os.system("/bin/chown -R %s %s 2>/dev/null" % (owner, path))
|
2012-03-16 02:53:14 +01:00
|
|
|
if rc != 0:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='chown failed')
|
2012-03-16 02:53:14 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
def set_group_if_different(path, group, changed):
|
|
|
|
if group is None:
|
|
|
|
return changed
|
|
|
|
old_user, old_group = user_and_group(path)
|
|
|
|
if old_group != group:
|
|
|
|
rc = os.system("/bin/chgrp -R %s %s" % (group, path))
|
|
|
|
if rc != 0:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='chgrp failed')
|
2012-03-16 02:53:14 +01:00
|
|
|
return True
|
|
|
|
return changed
|
|
|
|
|
|
|
|
def set_mode_if_different(path, mode, changed):
|
|
|
|
if mode is None:
|
|
|
|
return changed
|
|
|
|
try:
|
|
|
|
# FIXME: support English modes
|
2012-04-02 01:10:23 +02:00
|
|
|
mode = int(mode, 8)
|
2012-03-16 02:53:14 +01:00
|
|
|
except Exception, e:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='mode needs to be something octalish', details=str(e))
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
st = os.stat(path)
|
2012-03-16 03:35:59 +01:00
|
|
|
prev_mode = stat.S_IMODE(st[stat.ST_MODE])
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-03-16 03:35:59 +01:00
|
|
|
if prev_mode != mode:
|
|
|
|
# FIXME: comparison against string above will cause this to be executed
|
|
|
|
# every time
|
2012-03-16 02:53:14 +01:00
|
|
|
try:
|
|
|
|
os.chmod(path, mode)
|
|
|
|
except Exception, e:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='chmod failed', details=str(e))
|
2012-03-16 03:35:59 +01:00
|
|
|
|
|
|
|
st = os.stat(path)
|
|
|
|
new_mode = stat.S_IMODE(st[stat.ST_MODE])
|
|
|
|
|
|
|
|
if new_mode != prev_mode:
|
|
|
|
return True
|
2012-03-16 02:53:14 +01:00
|
|
|
return changed
|
|
|
|
|
2012-04-02 01:10:23 +02:00
|
|
|
|
2012-03-16 02:53:14 +01:00
|
|
|
def rmtree_error(func, path, exc_info):
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='failed to remove directory')
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# go...
|
|
|
|
|
|
|
|
prev_state = 'absent'
|
2012-05-03 01:09:56 +02:00
|
|
|
if os.path.lexists(path):
|
2012-04-02 01:10:23 +02:00
|
|
|
if os.path.islink(path):
|
|
|
|
prev_state = 'link'
|
|
|
|
elif os.path.isfile(path):
|
2012-03-16 02:53:14 +01:00
|
|
|
prev_state = 'file'
|
|
|
|
else:
|
|
|
|
prev_state = 'directory'
|
|
|
|
|
|
|
|
if prev_state != 'absent' and state == 'absent':
|
|
|
|
try:
|
|
|
|
if prev_state == 'directory':
|
|
|
|
if os.path.islink(path):
|
|
|
|
os.unlink(path)
|
|
|
|
else:
|
|
|
|
shutil.rmtree(path, ignore_errors=False, onerror=rmtree_error)
|
|
|
|
else:
|
|
|
|
os.unlink(path)
|
|
|
|
except Exception, e:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg=str(e))
|
|
|
|
exit_kv(path=path, changed=True)
|
2012-03-16 02:53:14 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if prev_state != 'absent' and prev_state != state:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='refusing to convert between %s and %s' % (prev_state, state))
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
if prev_state == 'absent' and state == 'absent':
|
2012-05-05 22:31:03 +02:00
|
|
|
exit_kv(path=path, changed=False)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
if state == 'file':
|
|
|
|
|
|
|
|
if prev_state == 'absent':
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='file does not exist, use copy or template module to create')
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(path, secontext, changed)
|
|
|
|
changed = set_owner_if_different(path, owner, changed)
|
|
|
|
changed = set_group_if_different(path, group, changed)
|
|
|
|
changed = set_mode_if_different(path, mode, changed)
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
exit_kv(path=path, changed=changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
|
|
|
elif state == 'directory':
|
|
|
|
|
|
|
|
if prev_state == 'absent':
|
|
|
|
os.makedirs(path)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(path, secontext, changed)
|
|
|
|
changed = set_owner_if_different(path, owner, changed)
|
2012-04-02 01:44:08 +02:00
|
|
|
changed = set_group_if_different(path, group, changed)
|
2012-04-02 01:10:23 +02:00
|
|
|
changed = set_mode_if_different(path, mode, changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
exit_kv(path=path, changed=changed)
|
2012-03-16 02:53:14 +01:00
|
|
|
|
2012-04-02 01:10:23 +02:00
|
|
|
elif state == 'link':
|
|
|
|
|
|
|
|
if os.path.isabs(src):
|
|
|
|
abs_src = src
|
|
|
|
else:
|
2012-05-01 03:12:13 +02:00
|
|
|
abs_src = os.path.join(os.path.dirname(dest), src)
|
2012-04-24 14:06:32 +02:00
|
|
|
if not os.path.exists(abs_src):
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(dest=dest, src=src, msg='src file does not exist')
|
2012-04-02 01:10:23 +02:00
|
|
|
|
|
|
|
if prev_state == 'absent':
|
|
|
|
os.symlink(src, dest)
|
|
|
|
changed = True
|
2012-04-02 01:44:08 +02:00
|
|
|
elif prev_state == 'link':
|
2012-04-02 01:10:23 +02:00
|
|
|
old_src = os.readlink(dest)
|
|
|
|
if not os.path.isabs(old_src):
|
|
|
|
old_src = os.path.join(os.path.dirname(dest), old_src)
|
|
|
|
if old_src != src:
|
|
|
|
os.unlink(dest)
|
|
|
|
os.symlink(src, dest)
|
2012-04-02 01:44:08 +02:00
|
|
|
else:
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(dest=dest, src=src, msg='unexpected position reached')
|
2012-04-02 01:10:23 +02:00
|
|
|
|
|
|
|
# set modes owners and context as needed
|
|
|
|
changed = set_context_if_different(dest, secontext, changed)
|
|
|
|
changed = set_owner_if_different(dest, owner, changed)
|
2012-04-02 01:44:08 +02:00
|
|
|
changed = set_group_if_different(dest, group, changed)
|
2012-04-02 01:10:23 +02:00
|
|
|
changed = set_mode_if_different(dest, mode, changed)
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
exit_kv(dest=dest, src=src, changed=changed)
|
2012-04-02 01:10:23 +02:00
|
|
|
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
fail_kv(path=path, msg='unexpected position reached')
|
2012-03-16 02:53:14 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|