Exemple #1
0
def update_domain_name(machines, new_domain):
    """updates domain name of machines"""

    if not machines:
        exit_with_error('[ERROR] You did not specify any machines.')

    results = query_machines(machines)
    if not results:
        exit_with_error(f'[INFO] No matching machines found.')

    try:
        # create domain name if needed
        all_domains = session().Domains.read()
        names = [d['name'] for d in all_domains]

        if new_domain not in names:
            print(f'[INFO] Domain {new_domain} does not exist, creating...')
            session().Domains.create(name=new_domain, authoritative=True)

    except MaaSError as e:
        exit_with_error(f'[ERROR] MaaS: {e}')

    # update machines, one by one
    for r in results:
        try:
            session().Machine.update(system_id=r.system_id, domain=new_domain)
            print(f'[{r.system_id}] [{r.hostname}] [OK] Set to {new_domain}')

        except MaaSError as e:
            exit_with_error(
                f'[{r.system_id}] [{r.hostname}] [ERROR] MaaS: {e}')

    print('Done. Refresh machine list with "mjt_refresh".')
Exemple #2
0
def add_tags(new_tag, machines):
    """adds tags to machines"""

    if not machines:
        exit_with_error('[ERROR] You did not specify any machines.')

    results = query_machines(machines)
    if not results:
        exit_with_error(f'[INFO] No matching machines found.')

    try:
        # create tag name if needed
        all_tags = session().Tags.read()
        names = [t['name'] for t in all_tags]

        if new_tag not in names:
            print(f'[INFO] Tag {new_tag} does not exist, creating...')
            session().Tags.create(name=new_tag,
                                  description='Helper tag for nagios checks')

    except MaaSError as e:
        exit_with_error(f'[ERROR] MaaS: {e}')

    # updates machines, one by one
    for r in results:
        try:
            session().Tag.update_nodes(name=new_tag, system_id=r.system_id)
            print(f'[{r.system_id}] [{r.hostname}] [OK] Added tag {new_tag}')

        except MaaSError as e:
            exit_with_error(
                f'[{r.system_id}] [{r.hostname}] [ERROR] MaaS: {e}')

    print('Done. Refresh machine list with "mjt_refresh".')
Exemple #3
0
def update_hardware_info(machine, new_cpus, new_ram):
    """updates cpus and ram of machines"""

    results = query_machines(machine)
    if not results:
        exit_with_error(f'[INFO] No matching machines found.')

    # update machines, one by one
    for r in results:
        try:
            update = {}
            if new_cpus is not None:
                update['cpu_count'] = new_cpus

            if new_ram is not None:
                update['memory'] = new_ram * 1024

            session().Machine.update(system_id=r.system_id, **update)
            print(f'[{r.system_id}] [{r.hostname}]'
                  f' [OK] Updated hardware information: {update}')

        except MaaSError as e:
            exit_with_error(
                f'[{r.system_id}] [{r.hostname}] [ERROR] MaaS: {e}')

    print('Done. Refresh machine list with "jmt_refresh".')
Exemple #4
0
def get_ipmi_info(machines):
    """returns IPMI credentials for a list of machines"""
    return {
        row.fqdn: {
            'power_address': row.power_address,
            'power_pass': row.power_pass,
            'power_user': row.power_user,
            'system_id': row.system_id
        } for row in query_machines(machines)
    }
Exemple #5
0
def update_host_name(machine, new_hostname):
    """updates host name of machine"""

    results = query_machines([machine])
    if not results:
        exit_with_error(f'[INFO] No matching machines found.')

    # update machines, one by one
    r = results[0]
    try:
        session().Machine.update(system_id=r.system_id, hostname=new_hostname)
        print(f'[{r.system_id}] [{r.hostname}] [OK]'
              f' Updated hostname to {new_hostname}')

    except MaaSError as e:
        exit_with_error(f'[{r.system_id}] [{r.hostname}] [ERROR] MaaS: {e}')

    print('Done. Refresh machine list with "mjt_refresh".')
def ipmi_sel(cmd, machines):
    """lists or clear SEL of @machines"""

    results = query_machines(machines)
    if not results:
        exit_with_error(f'[INFO] No matching machines found.')

    # update machines, one by one
    for r in results:
        print(f'## [{r.system_id}] [{r.hostname}]')

        command_line = [
            'ipmi-sel', '-h', r.power_address, '-u', r.power_user, '-p',
            r.power_pass
        ]

        if cmd == 'clear':
            command_line.append('--clear')

        subprocess.run(command_line)
Exemple #7
0
def get_script_results(machine, skip):
    """returns all script group results, along with result status"""

    if isinstance(machine, str):
        query = [machine]
    else:
        query = machine

    machines = query_machines(query)
    if not machines:
        exit_with_error(f'UNKNOWN: No matching machine: {machine}', code=3)

    api = session()

    results = {}
    for m in machines:
        scripts = api.NodeScriptResults.read(system_id=m.system_id)
        for s in scripts:
            if s['type_name'] in skip or s['status_name'] in skip:
                continue

            if m.system_id not in results:
                results[m.system_id] = {}

            suppressed = [r for r in s['results'] if r['suppressed']]
            ids = ','.join([str(r['id']) for r in s['results']])
            results[m.system_id].update({
                s['id']: {
                    'type': s['type_name'],
                    'status': s['status_name'],
                    'total': len(s['results']),
                    'suppressed': len(suppressed),
                    'individual_ids': ids
                }
            })

    return results