Commit ce5f3dd1 by Michael DeHaan

Port the file module over to the new common code infrastructure + cleanup some…

Port the file module over to the new common code infrastructure + cleanup some redundant imports since the module code already imports those things.
parent 8e60ad98
...@@ -65,7 +65,3 @@ def main(): ...@@ -65,7 +65,3 @@ def main():
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()
#!/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 sys
try:
import json
except ImportError:
import simplejson as json
print >>sys.stderr, "THIS IS A TEST FAILURE"
print json.dumps({
"failed" : True,
"msg" : "this module always fails"
})
...@@ -19,9 +19,7 @@ ...@@ -19,9 +19,7 @@
# #
# see examples/playbooks/get_url.yml # see examples/playbooks/get_url.yml
import os
import shutil import shutil
import syslog
import datetime import datetime
import tempfile import tempfile
...@@ -188,5 +186,4 @@ def main(): ...@@ -188,5 +186,4 @@ def main():
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()
...@@ -22,9 +22,7 @@ ...@@ -22,9 +22,7 @@
# tag. Latest is not supported, you should not be doing # tag. Latest is not supported, you should not be doing
# that. Contribs welcome! -- MPD # that. Contribs welcome! -- MPD
import os
import re import re
import subprocess
def get_version(dest): def get_version(dest):
''' samples the version of the git repo ''' ''' samples the version of the git repo '''
......
...@@ -17,9 +17,7 @@ ...@@ -17,9 +17,7 @@
# 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/>.
import os
import grp import grp
import subprocess
def get_bin_path(module, arg): def get_bin_path(module, arg):
if os.path.exists('/usr/sbin/%s' % arg): if os.path.exists('/usr/sbin/%s' % arg):
......
...@@ -19,13 +19,16 @@ ...@@ -19,13 +19,16 @@
# 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/>.
#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] #
# absent == remove from fstab and unmounted # mount name=mountpoint, src=device_to_be_mounted fstype=fstype
# present == add to fstab, do not change mount state # opts=mount_opts, dump=0 passno=0 state=[present|absent|mounted|unmounted]
# mounted == add to fstab if not there and make sure it is mounted #
# unmounted == do not change fstab state, but unmount # absent == remove from fstab and unmounted
# present == add to fstab, do not change mount state
# mounted == add to fstab if not there and make sure it is mounted
# unmounted == do not change fstab state, but unmount
def write_fstab(lines, dest): def write_fstab(lines, dest):
...@@ -37,14 +40,15 @@ def write_fstab(lines, dest): ...@@ -37,14 +40,15 @@ def write_fstab(lines, dest):
fs_w.close() fs_w.close()
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 = { args = dict(
'opts': 'defaults', opts = 'defaults',
'dump': '0', dump = '0',
'passno': '0', passno = '0',
'fstab': '/etc/fstab' 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'
...@@ -84,7 +88,6 @@ def set_mount(**kwargs): ...@@ -84,7 +88,6 @@ def set_mount(**kwargs):
else: else:
to_write.append(line) to_write.append(line)
if not exists: if not exists:
to_write.append(new_line % args) to_write.append(new_line % args)
changed = True changed = True
...@@ -96,14 +99,15 @@ def set_mount(**kwargs): ...@@ -96,14 +99,15 @@ 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 = { args = dict(
'opts': 'default', opts = 'default',
'dump': '0', dump = '0',
'passno': '0', passno = '0',
'fstab': '/etc/fstab' fstab = '/etc/fstab'
} )
args.update(kwargs) args.update(kwargs)
to_write = [] to_write = []
...@@ -138,7 +142,8 @@ def unset_mount(**kwargs): ...@@ -138,7 +142,8 @@ def unset_mount(**kwargs):
def mount(**kwargs): def mount(**kwargs):
"mount up a path or remount if needed" """ mount up a path or remount if needed """
name = kwargs['name'] name = kwargs['name']
if os.path.ismount(name): if os.path.ismount(name):
cmd = ['/bin/mount', '-o', 'remount', name] cmd = ['/bin/mount', '-o', 'remount', name]
...@@ -153,7 +158,8 @@ def mount(**kwargs): ...@@ -153,7 +158,8 @@ def mount(**kwargs):
return call.returncode, out+err return call.returncode, out+err
def umount(**kwargs): def umount(**kwargs):
"unmount a path" """ unmount a path """
name = kwargs['name'] name = kwargs['name']
cmd = ['/bin/umount', name] cmd = ['/bin/umount', name]
...@@ -165,6 +171,7 @@ def umount(**kwargs): ...@@ -165,6 +171,7 @@ def umount(**kwargs):
return call.returncode, out+err return call.returncode, out+err
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
state = dict(required=True, choices=['present', 'absent', 'mounted', 'unmounted']), state = dict(required=True, choices=['present', 'absent', 'mounted', 'unmounted']),
...@@ -194,7 +201,6 @@ def main(): ...@@ -194,7 +201,6 @@ def main():
if module.params['fstab'] is not None: if module.params['fstab'] is not None:
args['fstab'] = module.params['fstab'] args['fstab'] = module.params['fstab']
# absent == remove from fstab and unmounted # absent == remove from fstab and unmounted
# unmounted == do not change fstab state, but unmount # unmounted == do not change fstab state, but unmount
# present == add to fstab, do not change mount state # present == add to fstab, do not change mount state
...@@ -218,7 +224,6 @@ def main(): ...@@ -218,7 +224,6 @@ def main():
module.exit_json(changed=changed, **args) module.exit_json(changed=changed, **args)
if state == 'unmounted': if state == 'unmounted':
if os.path.ismount(name): if os.path.ismount(name):
res,msg = umount(**args) res,msg = umount(**args)
...@@ -228,8 +233,6 @@ def main(): ...@@ -228,8 +233,6 @@ def main():
module.exit_json(changed=changed, **args) module.exit_json(changed=changed, **args)
if state in ['mounted', 'present']: if state in ['mounted', 'present']:
name, changed = set_mount(**args) name, changed = set_mount(**args)
if state == 'mounted': if state == 'mounted':
...@@ -258,5 +261,4 @@ def main(): ...@@ -258,5 +261,4 @@ def main():
# this is magic, see lib/ansible/module_common.py # this is magic, see lib/ansible/module_common.py
#<<INCLUDE_ANSIBLE_MODULE_COMMON>> #<<INCLUDE_ANSIBLE_MODULE_COMMON>>
main() main()
...@@ -18,21 +18,16 @@ ...@@ -18,21 +18,16 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
import subprocess
def get_facter_data(): def get_facter_data():
p = subprocess.Popen(["/usr/bin/env", "ohai"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(["/usr/bin/env", "ohai"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate() (out, err) = p.communicate()
rc = p.returncode rc = p.returncode
return rc, out, err return rc, out, err
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict() argument_spec = dict()
) )
rc, out, err = get_facter_data() rc, out, err = get_facter_data()
if rc != 0: if rc != 0:
module.fail_json(msg=err) module.fail_json(msg=err)
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,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/>.
import base64
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict() argument_spec = dict()
......
...@@ -26,19 +26,16 @@ else: ...@@ -26,19 +26,16 @@ else:
# PostgreSQL module specific support methods. # PostgreSQL module specific support methods.
# #
def db_exists(cursor, db): def db_exists(cursor, db):
query = "SELECT * FROM pg_database WHERE datname=%(db)s" query = "SELECT * FROM pg_database WHERE datname=%(db)s"
cursor.execute(query, {'db': db}) cursor.execute(query, {'db': db})
return cursor.rowcount == 1 return cursor.rowcount == 1
def db_delete(cursor, db): def db_delete(cursor, db):
query = "DROP DATABASE %s" % db query = "DROP DATABASE %s" % db
cursor.execute(query) cursor.execute(query)
return True return True
def db_create(cursor, db): def db_create(cursor, db):
query = "CREATE DATABASE %s" % db query = "CREATE DATABASE %s" % db
cursor.execute(query) cursor.execute(query)
...@@ -48,7 +45,6 @@ def db_create(cursor, db): ...@@ -48,7 +45,6 @@ def db_create(cursor, db):
# Module execution. # Module execution.
# #
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
......
...@@ -20,14 +20,10 @@ ...@@ -20,14 +20,10 @@
import array import array
import fcntl import fcntl
import glob import glob
import sys
import os
import platform import platform
import re import re
import socket import socket
import struct import struct
import subprocess
import traceback
try: try:
import selinux import selinux
......
...@@ -17,8 +17,6 @@ ...@@ -17,8 +17,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/>.
import base64
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
......
...@@ -17,10 +17,8 @@ ...@@ -17,10 +17,8 @@
# 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/>.
import os
import pwd import pwd
import grp import grp
import subprocess
try: try:
import spwd import spwd
HAVE_SPWD=True HAVE_SPWD=True
......
...@@ -17,11 +17,6 @@ VIRT_FAILED = 1 ...@@ -17,11 +17,6 @@ VIRT_FAILED = 1
VIRT_SUCCESS = 0 VIRT_SUCCESS = 0
VIRT_UNAVAILABLE=2 VIRT_UNAVAILABLE=2
# other modules
import os
import sys
import subprocess
try: try:
import libvirt import libvirt
except ImportError: except ImportError:
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
import yum import yum
import datetime import datetime
import traceback import traceback
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment