From e581156dfc0b2835b1dc9dd0b23afd442052c1b3 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Wed, 9 Oct 2013 19:56:47 -0400 Subject: [PATCH 1/6] Added resp= option for svr4pkg to enable the -r option in pkgadd for response files --- library/packaging/svr4pkg | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/library/packaging/svr4pkg b/library/packaging/svr4pkg index 3f38d9db6a6..e3c25bfac3a 100644 --- a/library/packaging/svr4pkg +++ b/library/packaging/svr4pkg @@ -53,6 +53,9 @@ options: proxy: description: - HTTP[s] proxy to be used if C(src) is a URL. + resp: + description: + - Specifies the location of a response file to be used if package expects input on install. ''' EXAMPLES = ''' @@ -62,6 +65,9 @@ EXAMPLES = ''' # Install a package directly from an http site - svr4pkg: name=CSWpkgutil src=http://get.opencsw.org/now state=present +# Install a package with a response file +- svr4pkg: name=CSWggrep src=/tmp/third-party.pkg resp=/tmp/ggrep.response state=present + # Ensure that a package is not installed. - svr4pkg: name=SUNWgnome-sound-recorder state=absent ''' @@ -109,11 +115,13 @@ def run_command(module, cmd): cmd[0] = module.get_bin_path(progname, True) return module.run_command(cmd) -def package_install(module, name, src, proxy): +def package_install(module, name, src, proxy, resp): adminfile = create_admin_file() cmd = [ 'pkgadd', '-na', adminfile, '-d', src ] if proxy is not None: cmd += [ '-x', proxy ] + if resp is not None: + cmd += [ '-r', resp ] cmd.append(name) (rc, out, err) = run_command(module, cmd) os.unlink(adminfile) @@ -132,7 +140,8 @@ def main(): name = dict(required = True), state = dict(required = True, choices=['present', 'absent']), src = dict(default = None), - proxy = dict(default = None) + proxy = dict(default = None), + resp = dict(default = None) ), supports_check_mode=True ) @@ -140,6 +149,7 @@ def main(): name = module.params['name'] src = module.params['src'] proxy = module.params['proxy'] + resp = module.params['resp'] rc = None out = '' err = '' @@ -154,7 +164,7 @@ def main(): if not package_installed(module, name): if module.check_mode: module.exit_json(changed=True) - (rc, out, err) = package_install(module, name, src, proxy) + (rc, out, err) = package_install(module, name, src, proxy, resp) # Stdout is normally empty but for some packages can be # very long and is not often useful if len(out) > 75: From 9fdf4626eabeb68e8e4a3e5c12b78cf744abb8db Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Sat, 12 Oct 2013 13:28:48 -0400 Subject: [PATCH 2/6] Changed resp= to response_file= as per mpdehaan's request --- library/packaging/svr4pkg | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/library/packaging/svr4pkg b/library/packaging/svr4pkg index e3c25bfac3a..5abf6b56df1 100644 --- a/library/packaging/svr4pkg +++ b/library/packaging/svr4pkg @@ -53,7 +53,7 @@ options: proxy: description: - HTTP[s] proxy to be used if C(src) is a URL. - resp: + response_file: description: - Specifies the location of a response file to be used if package expects input on install. ''' @@ -66,7 +66,7 @@ EXAMPLES = ''' - svr4pkg: name=CSWpkgutil src=http://get.opencsw.org/now state=present # Install a package with a response file -- svr4pkg: name=CSWggrep src=/tmp/third-party.pkg resp=/tmp/ggrep.response state=present +- svr4pkg: name=CSWggrep src=/tmp/third-party.pkg response_file=/tmp/ggrep.response state=present # Ensure that a package is not installed. - svr4pkg: name=SUNWgnome-sound-recorder state=absent @@ -115,13 +115,13 @@ def run_command(module, cmd): cmd[0] = module.get_bin_path(progname, True) return module.run_command(cmd) -def package_install(module, name, src, proxy, resp): +def package_install(module, name, src, proxy, response_file): adminfile = create_admin_file() cmd = [ 'pkgadd', '-na', adminfile, '-d', src ] if proxy is not None: cmd += [ '-x', proxy ] - if resp is not None: - cmd += [ '-r', resp ] + if response_file is not None: + cmd += [ '-r', response_file ] cmd.append(name) (rc, out, err) = run_command(module, cmd) os.unlink(adminfile) @@ -141,7 +141,7 @@ def main(): state = dict(required = True, choices=['present', 'absent']), src = dict(default = None), proxy = dict(default = None), - resp = dict(default = None) + response_file = dict(default = None) ), supports_check_mode=True ) @@ -149,7 +149,7 @@ def main(): name = module.params['name'] src = module.params['src'] proxy = module.params['proxy'] - resp = module.params['resp'] + response_file = module.params['response_file'] rc = None out = '' err = '' @@ -164,7 +164,7 @@ def main(): if not package_installed(module, name): if module.check_mode: module.exit_json(changed=True) - (rc, out, err) = package_install(module, name, src, proxy, resp) + (rc, out, err) = package_install(module, name, src, proxy, response_file) # Stdout is normally empty but for some packages can be # very long and is not often useful if len(out) > 75: From df92881e26378bd766cb81672a79f949d30445b3 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Mon, 14 Oct 2013 20:08:44 -0400 Subject: [PATCH 3/6] Added note in description explaining this was added in Ansible 1.4 --- library/packaging/svr4pkg | 1 + 1 file changed, 1 insertion(+) diff --git a/library/packaging/svr4pkg b/library/packaging/svr4pkg index 5abf6b56df1..be44ce6e6ae 100644 --- a/library/packaging/svr4pkg +++ b/library/packaging/svr4pkg @@ -54,6 +54,7 @@ options: description: - HTTP[s] proxy to be used if C(src) is a URL. response_file: + version_added: "1.4" description: - Specifies the location of a response file to be used if package expects input on install. ''' From 101883e34a3aff223f64c37be97fe626911e70b1 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Sat, 19 Oct 2013 11:17:31 -0400 Subject: [PATCH 4/6] Added all_databases option which works like all-databases optin in mysqldump, and works in import by not naming a database to import as --- library/database/mysql_db | 43 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/library/database/mysql_db b/library/database/mysql_db index e33dba4caac..360bd1fcdd3 100644 --- a/library/database/mysql_db +++ b/library/database/mysql_db @@ -64,6 +64,15 @@ options: required: false default: present choices: [ "present", "absent", "dump", "import" ] + all_databases: + description: + - May only be provided if I(state) is C(dump) or C(import). (added in Ansible 1.4) + - Requires I(name) = a valid database name. + - Uses --all-databases option for mysqldump. + - When used for import, works similiar to mysql -u -p < target.sql + required: no + default: false + choices: [ "yes", "no", "true", "false", "0", "1" ] collation: description: - Collation mode @@ -92,6 +101,10 @@ author: Mark Theunissen EXAMPLES = ''' # Create a new database with name 'bobdata' - mysql_db: name=bobdata state=present +# Dumps all databases to hostname.sql +- mysql_db: name=bobdata state=dump all_databases=true target=/tmp/{{ inventory_hostname }}.sql +# Imports file.sql similiar to mysql -u -p < hostname.sql +- mysql_db: name=bobdata state=import all_databases=true target=/tmp/{{ inventory_hostname }}.sql ''' import ConfigParser @@ -116,16 +129,24 @@ def db_delete(cursor, db): cursor.execute(query) return True -def db_dump(host, user, password, db_name, target): - res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " - +db_name+" > " - +target) +def db_dump(host, user, password, db_name, target, all_databases): + if all_databases: + res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" --all-databases > " + +target) + else: + res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " + +db_name+" > " + +target) return (res == 0) -def db_import(host, user, password, db_name, target): - res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " - +db_name+" < " - +target) +def db_import(host, user, password, db_name, target, all_databases): + if all_databases: + res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" < " + +target) + else: + res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " + +db_name+" < " + +target) return (res == 0) def db_create(cursor, db, encoding, collation): @@ -207,6 +228,7 @@ def main(): db=dict(required=True, aliases=['name']), encoding=dict(default=""), collation=dict(default=""), + all_databases=dict(default="no", choices=BOOLEANS, type='bool'), target=dict(default=None), state=dict(default="present", choices=["absent", "present","dump", "import"]), ) @@ -216,6 +238,7 @@ def main(): module.fail_json(msg="the python mysqldb module is required") db = module.params["db"] + all_databases = module.params["all_databases"] encoding = module.params["encoding"] collation = module.params["collation"] state = module.params["state"] @@ -258,11 +281,11 @@ def main(): if state == "absent": changed = db_delete(cursor, db) elif state == "dump": - changed = db_dump(login_host, login_user, login_password, db, target) + changed = db_dump(login_host, login_user, login_password, db, target, all_databases) if not changed: module.fail_json(msg="dump failed!") elif state == "import": - changed = db_import(login_host, login_user, login_password, db, target) + changed = db_import(login_host, login_user, login_password, db, target, all_databases) if not changed: module.fail_json(msg="import failed!") else: From 1b0addeac93be104fe4c426850266656aa67ae24 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Sat, 19 Oct 2013 11:45:03 -0400 Subject: [PATCH 5/6] Removed mysql_db entry, that needs to be in a seperate commit --- library/database/mysql_db | 43 +++++++++------------------------------ 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/library/database/mysql_db b/library/database/mysql_db index 360bd1fcdd3..e33dba4caac 100644 --- a/library/database/mysql_db +++ b/library/database/mysql_db @@ -64,15 +64,6 @@ options: required: false default: present choices: [ "present", "absent", "dump", "import" ] - all_databases: - description: - - May only be provided if I(state) is C(dump) or C(import). (added in Ansible 1.4) - - Requires I(name) = a valid database name. - - Uses --all-databases option for mysqldump. - - When used for import, works similiar to mysql -u -p < target.sql - required: no - default: false - choices: [ "yes", "no", "true", "false", "0", "1" ] collation: description: - Collation mode @@ -101,10 +92,6 @@ author: Mark Theunissen EXAMPLES = ''' # Create a new database with name 'bobdata' - mysql_db: name=bobdata state=present -# Dumps all databases to hostname.sql -- mysql_db: name=bobdata state=dump all_databases=true target=/tmp/{{ inventory_hostname }}.sql -# Imports file.sql similiar to mysql -u -p < hostname.sql -- mysql_db: name=bobdata state=import all_databases=true target=/tmp/{{ inventory_hostname }}.sql ''' import ConfigParser @@ -129,24 +116,16 @@ def db_delete(cursor, db): cursor.execute(query) return True -def db_dump(host, user, password, db_name, target, all_databases): - if all_databases: - res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" --all-databases > " - +target) - else: - res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " - +db_name+" > " - +target) +def db_dump(host, user, password, db_name, target): + res = os.system("/usr/bin/mysqldump -q -h "+host+" -u "+user+ " --password="+password+" " + +db_name+" > " + +target) return (res == 0) -def db_import(host, user, password, db_name, target, all_databases): - if all_databases: - res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" < " - +target) - else: - res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " - +db_name+" < " - +target) +def db_import(host, user, password, db_name, target): + res = os.system("/usr/bin/mysql -h "+host+" -u "+user+" --password="+password+" " + +db_name+" < " + +target) return (res == 0) def db_create(cursor, db, encoding, collation): @@ -228,7 +207,6 @@ def main(): db=dict(required=True, aliases=['name']), encoding=dict(default=""), collation=dict(default=""), - all_databases=dict(default="no", choices=BOOLEANS, type='bool'), target=dict(default=None), state=dict(default="present", choices=["absent", "present","dump", "import"]), ) @@ -238,7 +216,6 @@ def main(): module.fail_json(msg="the python mysqldb module is required") db = module.params["db"] - all_databases = module.params["all_databases"] encoding = module.params["encoding"] collation = module.params["collation"] state = module.params["state"] @@ -281,11 +258,11 @@ def main(): if state == "absent": changed = db_delete(cursor, db) elif state == "dump": - changed = db_dump(login_host, login_user, login_password, db, target, all_databases) + changed = db_dump(login_host, login_user, login_password, db, target) if not changed: module.fail_json(msg="dump failed!") elif state == "import": - changed = db_import(login_host, login_user, login_password, db, target, all_databases) + changed = db_import(login_host, login_user, login_password, db, target) if not changed: module.fail_json(msg="import failed!") else: From d58f5a23ae1a0d257c8d55c96d27a193f6747a06 Mon Sep 17 00:00:00 2001 From: Jonathan Mainguy Date: Sat, 19 Oct 2013 11:54:36 -0400 Subject: [PATCH 6/6] Changed note describing this functionality was added in 1.4 to match what I have seen in other modules --- library/packaging/svr4pkg | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/library/packaging/svr4pkg b/library/packaging/svr4pkg index be44ce6e6ae..563952374d9 100644 --- a/library/packaging/svr4pkg +++ b/library/packaging/svr4pkg @@ -54,9 +54,8 @@ options: description: - HTTP[s] proxy to be used if C(src) is a URL. response_file: - version_added: "1.4" description: - - Specifies the location of a response file to be used if package expects input on install. + - Specifies the location of a response file to be used if package expects input on install. (added in Ansible 1.4) ''' EXAMPLES = '''