Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
import re
import traceback
import mysql.connector

from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = '''
---
module: create_workloadmgr_schema
short_description: creates workloadmgr schema in mariadb database
options:
  hostname:
    description:
        - hostname of the mysql server
    required: no
    default: 'localhost'

  username:
    description:
        - user name
    required: no
    default: 'root'

  password:
    description:
        - user password
    required: no
    default: 'root'

author: Murali Balcha
'''

EXAMPLES = '''
examples:
create_workloadmgr_schema:
    host

'''


def dbHandler(hostip, username, pwd):
    try:
        conn = mysql.connector.connect(
            user=username, password=pwd, host=hostip)
        return conn
    except mysql.connector.Error as err:
        print(err)


def exec_sql_file(cursor, sql_file):
    print("\n[INFO] Executing SQL script file: '%s'" % (sql_file))
    statement = ""
    for line in open(sql_file):
        if re.match(r'--', line):  # ignore sql comment lines
            continue

        if not re.search(r'[^-;]+;', line):
            # keep appending lines that don't end in ';'
            statement = statement + line
        else:
            # when you get a line ending in ';' then exec statement
            # and reset for next statement
            statement = statement + line
            # print("\n\n[DEBUG] Executing SQL statement:\n%s" % (statement))
            try:
                cursor.execute(statement)
            except Exception as e:
                print("\n[WARN] MySQLError during execute " + \
                      "statement \n\tArgs: '%s'" % (str(e.args)))
            statement = ""


def dispatch(hostip, username, password):
    dbname = 'workloadmgr'
    sqlscript = "/opt/stack/workloadmgr/workloadmgr/db/wlm_schema.sql"
    cmds = [#"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%s' " % hostip +
            #"IDENTIFIED BY '%s'; % password",
            "create database %s;" % dbname,
            "use workloadmgr;"]
    connStr = dbHandler(hostip, username, password)
    cursor = connStr.cursor()
    created = True
    for cmd in cmds:
        try:
            cursor.execute(cmd)
        except mysql.connector.errors.DatabaseError as ex:
            if ex.errno == 1007: 
                created = False
                pass
            else:
                raise
    if created:
        exec_sql_file(cursor, sqlscript)
    connStr.close()
    return dict(changed=True)


def main():
    module = AnsibleModule(
        argument_spec=dict(
            hostname=dict(required=False, default='localhost'),
            username=dict(required=False,
                          default="root"),
            password=dict(required=False, default='root', no_log=True),
        ),
    )

    hostname = module.params['hostname']
    username = module.params['username']
    password = module.params['password']

    try:
        d = dispatch(hostname, username, password)
    except Exception:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(**d)


# this is magic, see lib/ansible/module_common.py
# <<INCLUDE_ANSIBLE_MODULE_COMMON>>
if __name__ == '__main__':
    main()