Esempio n. 1
0
    def setup_rundb(self):
        """
        Configures the run database parameters, sets them into extra_vars
        """

        rundb_conn_default = '~/.config/linchpin/rundb-::mac::.json'
        rundb_conn = self.get_cfg(section='lp',
                                  key='rundb_conn',
                                  default=rundb_conn_default)
        rundb_type = self.get_cfg(section='lp',
                                  key='rundb_type',
                                  default='TinyRunDB')
        rundb_conn_type = self.get_cfg(section='lp',
                                       key='rundb_conn_type',
                                       default='file')
        self.rundb_hash = self.get_cfg(section='lp',
                                       key='rundb_hash',
                                       default='sha256')

        if rundb_conn_type == 'file':
            rundb_conn_int = rundb_conn.replace('::mac::', str(get_mac()))
            rundb_conn_int = os.path.expanduser(rundb_conn_int)
            rundb_conn_dir = os.path.dirname(rundb_conn_int)

            if not os.path.exists(rundb_conn_dir):
                os.mkdir(rundb_conn_dir)

        self.set_evar('rundb_type', rundb_type)
        self.set_evar('rundb_conn', rundb_conn_int)
        self.set_evar('rundb_hash', self.rundb_hash)

        return BaseDB(DB_DRIVERS[rundb_type], rundb_conn_int)
Esempio n. 2
0
    def setup_rundb(self):
        """
        Configures the run database parameters, sets them into extra_vars
        """

        rundb_conn = self.get_cfg(section='lp',
                                  key='rundb_conn')
        rundb_type = self.get_cfg(section='lp',
                                  key='rundb_type',
                                  default='TinyRunDB')
        rundb_conn_type = self.get_cfg(section='lp',
                                       key='rundb_conn_type',
                                       default='file')
        self.rundb_hash = self.get_cfg(section='lp',
                                       key='rundb_hash',
                                       default='sha256')

        if rundb_conn_type == 'file':

            rundb_conn_f = rundb_conn.replace('::mac::', str(get_mac()))
            rundb_conn_f = rundb_conn_f.replace('{{ workspace }}',
                                                self.workspace)
            rundb_conn_f = os.path.realpath(os.path.expanduser(rundb_conn_f))
            rundb_conn_dir = os.path.dirname(rundb_conn_f)

            if not os.path.exists(rundb_conn_dir):
                try:
                    os.makedirs(rundb_conn_dir)
                except OSError as exc:
                    if (exc.errno == errno.EEXIST and
                            os.path.isdir(rundb_conn_dir)):
                        pass
                    else:
                        raise
            rundb_conn = rundb_conn_f

        self.set_evar('rundb_type', rundb_type)
        self.set_evar('rundb_conn', rundb_conn)
        self.set_evar('rundb_hash', self.rundb_hash)

        return BaseDB(DB_DRIVERS[rundb_type], rundb_conn)
Esempio n. 3
0
def main():

    module = AnsibleModule(
        argument_spec=dict(
            db_type=dict(type='str', required=False, default='TinyRunDB'),
            conn_str=dict(type='str', required=True),
            operation=dict(choices=['init',
                                    'update',
                                    'purge',
                                    'get'],
                           default='update'),
            run_id=dict(type='int', required=False),
            table=dict(type='str', required=True),
            action=dict(type='str', required=False, default='up'),
            key=dict(type='str', required=False),
            value=dict(type='str', required=False),
        ),
    )
    db_type = module.params['db_type']
    conn_str = os.path.expanduser(module.params['conn_str'])
    table = module.params['table']
    action = module.params['action']
    op = module.params['operation']
    key = module.params['key']
    value = module.params['value']
    run_id = module.params['run_id']

    is_changed = False
    output = None
    try:
        rundb = BaseDB(DB_DRIVERS[db_type], conn_str=conn_str)

        val = value
        # if value looks like a dict it is a dict!
        if value and (value.startswith("{") or value.startswith("[")):
            try:
                val = json.loads(value)
            except Exception as e:
                try:
                    val = ast.literal_eval(value)
                except Exception as e:
                    module.fail_json(msg=e)

        if op in ['init', 'purge']:
            if op == "init":
                if val:
                    rundb.schema = val
                    output = rundb.init_table(table)
                else:
                    msg = ("'table' and 'value' required for init operation")
                    module.fail_json(msg=msg)
            if op == "purge":
                output = rundb.purge(table=table)

        elif op in ['update', 'get']:
            if op == "update":
                # idempotent update
                if run_id and key and val:
                    # get the record first
                    runid = int(run_id)
                    out = rundb.get_record(table,
                                           action=action,
                                           run_id=runid)
                    existing_val = out[0].get(key, "")
                    if existing_val == value:
                        is_changed = False
                    else:
                        output = rundb.update_record(table, runid, key, val)[0]
                else:
                    msg = ("'table', 'run_id, 'key', and 'value' required"
                           " for update operation")
                    module.fail_json(msg=msg)

            if op == "get":
                if key:
                    runid = None
                    if run_id:
                        runid = int(run_id)
                    if key == 'run_id':
                        output = rundb.get_record(table,
                                                  action=action,
                                                  run_id=runid)[1]

                    else:
                        record = rundb.get_record(table,
                                                  action=action,
                                                  run_id=runid)[0]

                        if key in record:
                            output = record.get(key)
                        else:
                            msg = "key '{0}' was not found in"
                            " record".format(key)
                            module.fail_json(msg=msg)
                else:
                    msg = "The 'key' value must be passed"
                    module.fail_json(msg=msg)

        else:
            msg = "Module 'action' required"
            module.fail_json(msg=msg)

        if output:
            is_changed = True

        module.exit_json(output=output, changed=is_changed)

    except Exception as e:
        module.fail_json(msg=str(e))
Esempio n. 4
0
def main():

    module = AnsibleModule(argument_spec=dict(
        db_type=dict(type='str', required=False, default='TinyRunDB'),
        conn_str=dict(type='str', required=True),
        operation=dict(choices=['init', 'update', 'purge', 'get'],
                       default='update'),
        run_id=dict(type='int', required=False),
        table=dict(type='str', required=True),
        action=dict(type='str', required=False, default='up'),
        key=dict(type='str', required=False),
        value=dict(type='str', required=False),
    ), )
    db_type = module.params['db_type']
    conn_str = os.path.expanduser(module.params['conn_str'])
    table = module.params['table']
    action = module.params['action']
    op = module.params['operation']
    key = module.params['key']
    value = module.params['value']
    run_id = module.params['run_id']

    is_changed = False
    output = None
    try:
        rundb = BaseDB(DB_DRIVERS[db_type], conn_str=conn_str)

        val = value
        # if value looks like a dict it is a dict!
        if value and (value.startswith("{") or value.startswith("[")):
            try:
                val = json.loads(value)
            except Exception as e:
                try:
                    val = ast.literal_eval(value)
                except Exception as e:
                    module.fail_json(msg=e)

        if op in ['init', 'purge']:
            if op == "init":
                if val:
                    rundb.schema = val
                    output = rundb.init_table(table)
                else:
                    msg = ("'table' and 'value' required for init operation")
                    module.fail_json(msg=msg)
            if op == "purge":
                output = rundb.purge(table=table)

        elif op in ['update', 'get']:
            if op == "update":
                # idempotent update
                if run_id and key and val:
                    # get the record first
                    runid = int(run_id)
                    out = rundb.get_record(table, action=action, run_id=runid)
                    existing_val = out[0].get(key, "")
                    if existing_val == value:
                        is_changed = False
                    else:
                        output = rundb.update_record(table, runid, key, val)[0]
                else:
                    msg = ("'table', 'run_id, 'key', and 'value' required"
                           " for update operation")
                    module.fail_json(msg=msg)

            if op == "get":
                if key:
                    runid = None
                    if run_id:
                        runid = int(run_id)
                    if key == 'run_id':
                        output = rundb.get_record(table,
                                                  action=action,
                                                  run_id=runid)[1]

                    else:
                        record = rundb.get_record(table,
                                                  action=action,
                                                  run_id=runid)[0]

                        if key in record:
                            output = record.get(key)
                        else:
                            msg = "key '{0}' was not found in"
                            " record".format(key)
                            module.fail_json(msg=msg)
                else:
                    msg = "The 'key' value must be passed"
                    module.fail_json(msg=msg)

        else:
            msg = "Module 'action' required"
            module.fail_json(msg=msg)

        if output:
            is_changed = True

        module.exit_json(output=output, changed=is_changed)

    except Exception as e:
        module.fail_json(msg=str(e))
Esempio n. 5
0
def main():


    module = AnsibleModule(
        argument_spec=dict(
            db_type=dict(type='str', required=False, default='TinyRunDB'),
            db_schema=dict(type='dict', required=True),
            conn_str=dict(type='str', required=True),
            operation=dict(choices=['init',
                                    'update',
                                    'purge',
                                    'get'],
                           default='update'),
            run_id=dict(type='int', required=False),
            table=dict(type='str', required=True),
            action=dict(type='str', required=False, default='up'),
            key=dict(type='str', required=False),
            value=dict(type='str', required=False),
        ),
    )
    db_type = module.params['db_type']
    db_schema = module.params['db_schema']
    conn_str = os.path.expanduser(module.params['conn_str'])
    op = module.params['operation']
    value = module.params['value']

    is_changed = False
    output = None
    try:
        rundb = BaseDB(DB_DRIVERS[db_type], conn_str=conn_str)
        rundb.schema = db_schema

        val = value
        # if value looks like a dict it is a dict!
        if value and (value.startswith("{") or value.startswith("[")):
            try:
                val = json.loads(value)
            except Exception:
                try:
                    val = ast.literal_eval(value)
                except Exception as e:
                    module.fail_json(msg=e)

        if op == "init":
            init_rundb(module, rundb, val)
        elif op == "purge":
            output = purge_rundb(rundb)
        elif op == "update":
            output = update_rundb(module, rundb, val)
        elif op == "get":
            output = get_item(module, rundb)
        else:
            msg = "Module 'action' required"
            module.fail_json(msg=msg)

        if output:
            is_changed = True

        module.exit_json(output=str(output), changed=is_changed)

    except Exception as e:
        module.fail_json(msg=str(e))