コード例 #1
0
ファイル: waptdevutils.py プロジェクト: Aguay-val/WAPT
def add_ads_groups(waptconfigfile,
                   hosts_list,
                   wapt_server_user,
                   wapt_server_passwd,
                   key_password=None):
    # initialise wapt api with local config file
    wapt = Wapt(config_filename=waptconfigfile)
    wapt.dbpath = ':memory:'

    # get current packages status from repositories
    wapt.update(register=False, filter_on_host_cap=False)

    hosts_list = ensure_list(hosts_list)

    # get the collection of hosts from waptserver inventory
    all_hosts = wapt.waptserver.get(
        'api/v1/hosts?columns=uuid,computer_fqdn,depends',
        auth=(wapt_server_user, wapt_server_passwd))['result']
    if hosts_list:
        hosts = [h for h in all_hosts if h['computer_fqdn'] in hosts_list]
    else:
        hosts = hosts_list

    result = []

    for h in hosts:
        try:
            hostname = h['computer_fqdn']
            print('Computer %s... \r' % hostname, end='')

            groups = get_computer_groups(h['computer_name'])
            wapt_groups = h['depends']
            additional = [
                group for group in groups
                if not group in wapt_groups and wapt.is_available(group)
            ]

            if additional:
                # now update the host package : download and append missing packages
                tmpdir = mkdtemp()
                try:
                    package = wapt.edit_host(hostname, target_directory=tmpdir)
                    control = package['package']
                    depends = ensure_list(control.depends)

                    control.depends = ','.join(depends + additional)
                    control.save_control_to_wapt(package.sourcespath)
                    buid_res = wapt.build_upload(
                        package.sourcespath,
                        private_key_passwd=key_password,
                        wapt_server_user=wapt_server_user,
                        wapt_server_passwd=wapt_server_passwd,
                        inc_package_release=True)[0]
                    print("  done, new packages: %s" % (','.join(additional)))
                    if os.path.isfile(buid_res):
                        os.remove(buid_res)
                    result.append(hostname)
                finally:
                    # cleanup of temporary
                    if os.path.isdir(tmpdir):
                        rmtree(tmpdir)
        except Exception as e:
            print(" error %s" % e)
            raise

    return result