Merge pull request #669 from nix85/fix_mount_mod
Standardizing the mount module
This commit is contained in:
commit
432dae769e
1 changed files with 105 additions and 136 deletions
241
library/mount
241
library/mount
|
@ -19,18 +19,6 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
import os
|
|
||||||
import os.path
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
import sys
|
|
||||||
import syslog
|
|
||||||
|
|
||||||
|
|
||||||
#mount module - mount fs and define in fstab
|
#mount module - mount fs and define in fstab
|
||||||
#usage:
|
#usage:
|
||||||
# mount name=mountpoint, src=device_to_be_mounted fstype=fstype opts=mount_opts, dump=0 passno=0 state=[present|absent|mounted|unmounted]
|
# mount name=mountpoint, src=device_to_be_mounted fstype=fstype opts=mount_opts, dump=0 passno=0 state=[present|absent|mounted|unmounted]
|
||||||
|
@ -39,14 +27,6 @@ import syslog
|
||||||
# mounted == add to fstab if not there and make sure it is mounted
|
# mounted == add to fstab if not there and make sure it is mounted
|
||||||
# unmounted == do not change fstab state, but unmount
|
# unmounted == do not change fstab state, but unmount
|
||||||
|
|
||||||
def exit_json(rc=0, **kwargs):
|
|
||||||
print json.dumps(kwargs)
|
|
||||||
sys.exit(rc)
|
|
||||||
|
|
||||||
def fail_json(**kwargs):
|
|
||||||
kwargs['failed'] = True
|
|
||||||
exit_json(rc=1, **kwargs)
|
|
||||||
|
|
||||||
def write_fstab(lines, dest):
|
def write_fstab(lines, dest):
|
||||||
|
|
||||||
fs_w = open(dest, 'w')
|
fs_w = open(dest, 'w')
|
||||||
|
@ -59,10 +39,12 @@ def write_fstab(lines, dest):
|
||||||
def set_mount(**kwargs):
|
def set_mount(**kwargs):
|
||||||
"set/change a mount point location in fstab"
|
"set/change a mount point location in fstab"
|
||||||
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
||||||
args = { 'opts':'defaults',
|
args = {
|
||||||
'dump':'0',
|
'opts': 'defaults',
|
||||||
'passno':'0',
|
'dump': '0',
|
||||||
'fstab':'/etc/fstab' }
|
'passno': '0',
|
||||||
|
'fstab': '/etc/fstab'
|
||||||
|
}
|
||||||
args.update(kwargs)
|
args.update(kwargs)
|
||||||
|
|
||||||
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
|
new_line = '%(src)s %(name)s %(fstype)s %(opts)s %(dump)s %(passno)s\n'
|
||||||
|
@ -116,10 +98,12 @@ def set_mount(**kwargs):
|
||||||
def unset_mount(**kwargs):
|
def unset_mount(**kwargs):
|
||||||
"remove a mount point from fstab"
|
"remove a mount point from fstab"
|
||||||
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
# kwargs: name, src, fstype, opts, dump, passno, state, fstab=/etc/fstab
|
||||||
args = { 'opts':'default',
|
args = {
|
||||||
'dump':'0',
|
'opts': 'default',
|
||||||
'passno':'0',
|
'dump': '0',
|
||||||
'fstab':'/etc/fstab' }
|
'passno': '0',
|
||||||
|
'fstab': '/etc/fstab'
|
||||||
|
}
|
||||||
args.update(kwargs)
|
args.update(kwargs)
|
||||||
|
|
||||||
to_write = []
|
to_write = []
|
||||||
|
@ -166,10 +150,11 @@ def mount(**kwargs):
|
||||||
if call.returncode == 0:
|
if call.returncode == 0:
|
||||||
return 0, ''
|
return 0, ''
|
||||||
else:
|
else:
|
||||||
return call.rc, out+err
|
return call.returncode, out+err
|
||||||
|
|
||||||
def umount(**kwargs):
|
def umount(**kwargs):
|
||||||
"unmount a path"
|
"unmount a path"
|
||||||
|
name = kwargs['name']
|
||||||
cmd = ['/bin/umount', name]
|
cmd = ['/bin/umount', name]
|
||||||
|
|
||||||
call = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
call = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
|
@ -177,117 +162,101 @@ def umount(**kwargs):
|
||||||
if call.returncode == 0:
|
if call.returncode == 0:
|
||||||
return 0, ''
|
return 0, ''
|
||||||
else:
|
else:
|
||||||
return call.rc, out+err
|
return call.returncode, out+err
|
||||||
|
|
||||||
argfile = sys.argv[1]
|
def main():
|
||||||
args = open(argfile, 'r').read()
|
module = AnsibleModule(
|
||||||
items = shlex.split(args)
|
argument_spec = dict(
|
||||||
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
state = dict(required=True, choices=['present', 'absent', 'mounted', 'unmounted']),
|
||||||
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % args)
|
name = dict(required=True),
|
||||||
|
opts = dict(default=None),
|
||||||
if not len(items):
|
passno = dict(default=None),
|
||||||
fail_json(msg='the module requires arguments -a')
|
dump = dict(default=None),
|
||||||
sys.exit(1)
|
src = dict(required=True),
|
||||||
|
fstype = dict(required=True),
|
||||||
params = {}
|
fstab = dict(default=None)
|
||||||
for x in items:
|
)
|
||||||
(k, v) = x.split("=",1)
|
)
|
||||||
params[k] = v
|
|
||||||
|
changed = False
|
||||||
state = params.get('state',None)
|
rc = 0
|
||||||
name = params.get('name', None)
|
args = {
|
||||||
opts = params.get('opts', None)
|
'name': module.params['name'],
|
||||||
passno = params.get('passno', None)
|
'src': module.params['src'],
|
||||||
dump = params.get('dump', None)
|
'fstype': module.params['fstype']
|
||||||
src = params.get('src', None)
|
}
|
||||||
fstype = params.get('fstype', None)
|
if module.params['passno'] is not None:
|
||||||
fstab = params.get('fstab', None)
|
args['passno'] = module.params['passno']
|
||||||
|
if module.params['opts'] is not None:
|
||||||
|
args['opts'] = module.params['opts']
|
||||||
if state not in [ 'present', 'absent', 'mounted', 'unmounted' ]:
|
if module.params['dump'] is not None:
|
||||||
fail_json(msg='invalid state')
|
args['dump'] = module.params['dump']
|
||||||
|
if module.params['fstab'] is not None:
|
||||||
if not name:
|
args['fstab'] = module.params['fstab']
|
||||||
fail_json(msg='no name option given')
|
|
||||||
|
|
||||||
if not src:
|
# absent == remove from fstab and unmounted
|
||||||
fail_json(msg='no src option given')
|
# unmounted == do not change fstab state, but unmount
|
||||||
|
# present == add to fstab, do not change mount state
|
||||||
if not fstype:
|
# mounted == add to fstab if not there and make sure it is mounted, if it has changed in fstab then remount it
|
||||||
fail_json(msg='no fstype option given')
|
|
||||||
|
state = module.params['state']
|
||||||
|
name = module.params['name']
|
||||||
changed = False
|
if state == 'absent':
|
||||||
rc = 0
|
name, changed = unset_mount(**args)
|
||||||
args = {'name':name,
|
if changed:
|
||||||
'src':src,
|
if os.path.ismount(name):
|
||||||
'fstype':fstype }
|
res,msg = umount(**args)
|
||||||
if passno is not None:
|
if res:
|
||||||
args['passno'] = passno
|
fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
||||||
if opts is not None:
|
|
||||||
args['opts'] = opts
|
if os.path.exists(name):
|
||||||
if dump is not None:
|
try:
|
||||||
args['dump'] = dump
|
os.rmdir(name)
|
||||||
if fstab is not None:
|
except (OSError, IOError), e:
|
||||||
args['fstab'] = fstab
|
fail_json(msg="Error rmdir %s: %s" % (name, str(e)))
|
||||||
|
|
||||||
|
module.exit_json(changed=changed, **args)
|
||||||
# absent == remove from fstab and unmounted
|
|
||||||
# unmounted == do not change fstab state, but unmount
|
|
||||||
# present == add to fstab, do not change mount state
|
if state == 'unmounted':
|
||||||
# mounted == add to fstab if not there and make sure it is mounted, if it has changed in fstab then remount it
|
|
||||||
|
|
||||||
|
|
||||||
if state == 'absent':
|
|
||||||
name, changed = unset_mount(**args)
|
|
||||||
if changed:
|
|
||||||
if os.path.ismount(name):
|
if os.path.ismount(name):
|
||||||
res,msg = umount(**args)
|
res,msg = umount(**args)
|
||||||
if res:
|
if res:
|
||||||
fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
||||||
|
|
||||||
if os.path.exists(name):
|
|
||||||
try:
|
|
||||||
os.rmdir(name)
|
|
||||||
except (OSError, IOError), e:
|
|
||||||
fail_json(msg="Error rmdir %s: %s" % (name, str(e)))
|
|
||||||
|
|
||||||
exit_json(changed=changed, **args)
|
|
||||||
|
|
||||||
|
|
||||||
if state == 'unmounted':
|
|
||||||
if os.path.ismount(name):
|
|
||||||
res,msg = umount(**args)
|
|
||||||
if res:
|
|
||||||
fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
|
||||||
changed = True
|
|
||||||
|
|
||||||
exit_json(changed=changed, **args)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if state in ['mounted', 'present']:
|
|
||||||
name, changed = set_mount(**args)
|
|
||||||
if state == 'mounted':
|
|
||||||
if not os.path.exists(name):
|
|
||||||
try:
|
|
||||||
os.makedirs(name)
|
|
||||||
except (OSError, IOError), e:
|
|
||||||
fail_json(msg="Error making dir %s: %s" % (name, str(e)))
|
|
||||||
|
|
||||||
res = 0
|
|
||||||
if os.path.ismount(name):
|
|
||||||
if changed:
|
|
||||||
res,msg = mount(**args)
|
|
||||||
else:
|
|
||||||
changed = True
|
changed = True
|
||||||
res,msg = mount(**args)
|
|
||||||
|
module.exit_json(changed=changed, **args)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if state in ['mounted', 'present']:
|
||||||
|
name, changed = set_mount(**args)
|
||||||
|
if state == 'mounted':
|
||||||
|
if not os.path.exists(name):
|
||||||
|
try:
|
||||||
|
os.makedirs(name)
|
||||||
|
except (OSError, IOError), e:
|
||||||
|
fail_json(msg="Error making dir %s: %s" % (name, str(e)))
|
||||||
|
|
||||||
|
res = 0
|
||||||
|
if os.path.ismount(name):
|
||||||
|
if changed:
|
||||||
|
res,msg = mount(**args)
|
||||||
|
else:
|
||||||
|
changed = True
|
||||||
|
res,msg = mount(**args)
|
||||||
|
|
||||||
|
if res:
|
||||||
|
fail_json(msg="Error mounting %s: %s" % (name, msg))
|
||||||
|
|
||||||
|
|
||||||
|
module.exit_json(changed=changed, **args)
|
||||||
|
|
||||||
|
module.fail_json(msg='Unexpected position reached')
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
# this is magic, see lib/ansible/module_common.py
|
||||||
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
||||||
|
|
||||||
if res:
|
main()
|
||||||
fail_json(msg="Error mounting %s: %s" % (name, msg))
|
|
||||||
|
|
||||||
|
|
||||||
exit_json(changed=changed, **args)
|
|
||||||
|
|
||||||
fail_json(msg='Unexpected position reached')
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
Loading…
Reference in a new issue