示例#1
0
def test_apps_catalog_update_nominal(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    # Cache is empty
    assert not glob.glob(APPS_CATALOG_CACHE + "/*")

    # Update
    with requests_mock.Mocker() as m:

        _actual_apps_catalog_api_url,
        # Mock the server response with a dummy apps catalog
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG)

        mocker.spy(m18n, "n")
        _update_apps_catalog()
        m18n.n.assert_any_call("apps_catalog_updating")
        m18n.n.assert_any_call("apps_catalog_update_success")

    # Cache shouldn't be empty anymore empty
    assert glob.glob(APPS_CATALOG_CACHE + "/*")

    # And if we load the catalog, we sould find
    # - foo and bar as apps (unordered),
    # - yolo and swag as categories (ordered)
    catalog = app_catalog(with_categories=True)

    assert "apps" in catalog
    assert set(catalog["apps"].keys()) == set(["foo", "bar"])

    assert "categories" in catalog
    assert [c["id"] for c in catalog["categories"]] == ["yolo", "swag"]
示例#2
0
def test_apps_catalog_init(mocker):

    # Cache is empty
    assert not glob.glob(APPS_CATALOG_CACHE + "/*")
    # Conf doesn't exist yet
    assert not os.path.exists(APPS_CATALOG_CONF)
    # Conf doesn't exist yet
    assert not os.path.exists(APPS_CATALOG_CRON_PATH)

    # Initialize ...
    mocker.spy(m18n, "n")
    _initialize_apps_catalog_system()
    m18n.n.assert_any_call('apps_catalog_init_success')

    # Then there's a cron enabled
    assert cron_job_is_there()

    # And a conf with at least one list
    assert os.path.exists(APPS_CATALOG_CONF)
    apps_catalog_list = _read_apps_catalog_list()
    assert len(apps_catalog_list)

    # Cache is expected to still be empty though
    # (if we did update the apps_catalog during init,
    # we couldn't differentiate easily exceptions
    # related to lack of network connectivity)
    assert not glob.glob(APPS_CATALOG_CACHE + "/*")
示例#3
0
def test_apps_catalog_load_with_empty_cache(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    # Cache is empty
    assert not glob.glob(APPS_CATALOG_CACHE + "/*")

    # Update
    with requests_mock.Mocker() as m:

        # Mock the server response with a dummy apps catalog
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG)

        # Try to load the apps catalog
        # This should implicitly trigger an update in the background
        mocker.spy(m18n, "n")
        app_dict = _load_apps_catalog()["apps"]
        m18n.n.assert_any_call("apps_catalog_obsolete_cache")
        m18n.n.assert_any_call("apps_catalog_update_success")

    # Cache shouldn't be empty anymore empty
    assert glob.glob(APPS_CATALOG_CACHE + "/*")

    assert "foo" in app_dict.keys()
    assert "bar" in app_dict.keys()
示例#4
0
def test_apps_catalog_emptylist():

    # Initialize ...
    _initialize_apps_catalog_system()

    # Let's imagine somebody removed the default apps catalog because uh idk they dont want to use our default apps catalog
    os.system("rm %s" % APPS_CATALOG_CONF)
    os.system("touch %s" % APPS_CATALOG_CONF)

    apps_catalog_list = _read_apps_catalog_list()
    assert not len(apps_catalog_list)
示例#5
0
def test_apps_catalog_update_404(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    with requests_mock.Mocker() as m:

        # 404 error
        m.register_uri("GET", APPS_CATALOG_DEFAULT_URL_FULL, status_code=404)

        with pytest.raises(YunohostError):
            mocker.spy(m18n, "n")
            _update_apps_catalog()
            m18n.n.assert_any_call("apps_catalog_failed_to_download")
示例#6
0
def test_apps_catalog_update_corrupted(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    with requests_mock.Mocker() as m:

        # Corrupted json
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG[:-2])

        with pytest.raises(YunohostError):
            mocker.spy(m18n, "n")
            _update_apps_catalog()
            m18n.n.assert_any_call("apps_catalog_failed_to_download")
示例#7
0
def test_apps_catalog_update_sslerror(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    with requests_mock.Mocker() as m:

        # SSL error
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       exc=requests.exceptions.SSLError)

        with pytest.raises(YunohostError):
            mocker.spy(m18n, "n")
            _update_apps_catalog()
            m18n.n.assert_any_call("apps_catalog_failed_to_download")
示例#8
0
def test_apps_catalog_load_with_conflicts_between_lists(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    conf = [
        {
            "id": "default",
            "url": APPS_CATALOG_DEFAULT_URL
        },
        {
            "id":
            "default2",
            "url":
            APPS_CATALOG_DEFAULT_URL.replace("yunohost.org", "yolohost.org"),
        },
    ]

    write_to_yaml(APPS_CATALOG_CONF, conf)

    # Update
    with requests_mock.Mocker() as m:

        # Mock the server response with a dummy apps catalog
        # + the same apps catalog for the second list
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG)
        m.register_uri(
            "GET",
            APPS_CATALOG_DEFAULT_URL_FULL.replace("yunohost.org",
                                                  "yolohost.org"),
            text=DUMMY_APP_CATALOG,
        )

        # Try to load the apps catalog
        # This should implicitly trigger an update in the background
        mocker.spy(logger, "warning")
        app_dict = _load_apps_catalog()["apps"]
        logger.warning.assert_any_call(AnyStringWith("Duplicate"))

    # Cache shouldn't be empty anymore empty
    assert glob.glob(APPS_CATALOG_CACHE + "/*")

    assert "foo" in app_dict.keys()
    assert "bar" in app_dict.keys()
示例#9
0
def test_apps_catalog_load_with_oudated_api_version(mocker):

    # Initialize ...
    _initialize_apps_catalog_system()

    # Update
    with requests_mock.Mocker() as m:

        mocker.spy(m18n, "n")
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG)
        _update_apps_catalog()

    # Cache shouldn't be empty anymore empty
    assert glob.glob(APPS_CATALOG_CACHE + "/*")

    # Tweak the cache to replace the from_api_version with a different one
    for cache_file in glob.glob(APPS_CATALOG_CACHE + "/*"):
        cache_json = read_json(cache_file)
        assert cache_json["from_api_version"] == APPS_CATALOG_API_VERSION
        cache_json["from_api_version"] = 0
        write_to_json(cache_file, cache_json)

    # Update
    with requests_mock.Mocker() as m:

        # Mock the server response with a dummy apps catalog
        m.register_uri("GET",
                       APPS_CATALOG_DEFAULT_URL_FULL,
                       text=DUMMY_APP_CATALOG)

        mocker.spy(m18n, "n")
        app_dict = _load_apps_catalog()["apps"]
        m18n.n.assert_any_call("apps_catalog_update_success")

    assert "foo" in app_dict.keys()
    assert "bar" in app_dict.keys()

    # Check that we indeed have the new api number in cache
    for cache_file in glob.glob(APPS_CATALOG_CACHE + "/*"):
        cache_json = read_json(cache_file)
        assert cache_json["from_api_version"] == APPS_CATALOG_API_VERSION
示例#10
0
文件: tools.py 项目: trogeat/yunohost
def tools_postinstall(
    operation_logger,
    domain,
    password,
    ignore_dyndns=False,
    force_password=False,
    force_diskspace=False,
):
    """
    YunoHost post-install

    Keyword argument:
        domain -- YunoHost main domain
        ignore_dyndns -- Do not subscribe domain to a DynDNS service (only
        needed for nohost.me, noho.st domains)
        password -- YunoHost admin password

    """
    from yunohost.utils.password import assert_password_is_strong_enough
    from yunohost.domain import domain_main_domain
    import psutil

    dyndns_provider = "dyndns.yunohost.org"

    # Do some checks at first
    if os.path.isfile("/etc/yunohost/installed"):
        raise YunohostValidationError("yunohost_already_installed")

    if os.path.isdir(
            "/etc/yunohost/apps") and os.listdir("/etc/yunohost/apps") != []:
        raise YunohostValidationError(
            "It looks like you're trying to re-postinstall a system that was already working previously ... If you recently had some bug or issues with your installation, please first discuss with the team on how to fix the situation instead of savagely re-running the postinstall ...",
            raw_msg=True,
        )

    # Check there's at least 10 GB on the rootfs...
    disk_partitions = sorted(psutil.disk_partitions(),
                             key=lambda k: k.mountpoint)
    main_disk_partitions = [
        d for d in disk_partitions if d.mountpoint in ["/", "/var"]
    ]
    main_space = sum(
        [psutil.disk_usage(d.mountpoint).total for d in main_disk_partitions])
    GB = 1024**3
    if not force_diskspace and main_space < 10 * GB:
        raise YunohostValidationError("postinstall_low_rootfsspace")

    # Check password
    if not force_password:
        assert_password_is_strong_enough("admin", password)

    if not ignore_dyndns:
        # Check if yunohost dyndns can handle the given domain
        # (i.e. is it a .nohost.me ? a .noho.st ?)
        try:
            is_nohostme_or_nohost = _dyndns_provides(dyndns_provider, domain)
        # If an exception is thrown, most likely we don't have internet
        # connectivity or something. Assume that this domain isn't manageable
        # and inform the user that we could not contact the dyndns host server.
        except Exception:
            logger.warning(
                m18n.n("dyndns_provider_unreachable",
                       provider=dyndns_provider))
            is_nohostme_or_nohost = False

        # If this is a nohost.me/noho.st, actually check for availability
        if is_nohostme_or_nohost:
            # (Except if the user explicitly said he/she doesn't care about dyndns)
            if ignore_dyndns:
                dyndns = False
            # Check if the domain is available...
            elif _dyndns_available(dyndns_provider, domain):
                dyndns = True
            # If not, abort the postinstall
            else:
                raise YunohostValidationError("dyndns_unavailable",
                                              domain=domain)
        else:
            dyndns = False
    else:
        dyndns = False

    if os.system("iptables -V >/dev/null 2>/dev/null") != 0:
        raise YunohostValidationError(
            "iptables/nftables does not seems to be working on your setup. You may be in a container or your kernel does have the proper modules loaded. Sometimes, rebooting the machine may solve the issue.",
            raw_msg=True,
        )

    operation_logger.start()
    logger.info(m18n.n("yunohost_installing"))

    # New domain config
    domain_add(domain, dyndns)
    domain_main_domain(domain)

    # Change LDAP admin password
    tools_adminpw(password, check_strength=not force_password)

    # Enable UPnP silently and reload firewall
    firewall_upnp("enable", no_refresh=True)

    # Initialize the apps catalog system
    _initialize_apps_catalog_system()

    # Try to update the apps catalog ...
    # we don't fail miserably if this fails,
    # because that could be for example an offline installation...
    try:
        _update_apps_catalog()
    except Exception as e:
        logger.warning(str(e))

    # Init migrations (skip them, no need to run them on a fresh system)
    _skip_all_migrations()

    os.system("touch /etc/yunohost/installed")

    # Enable and start YunoHost firewall at boot time
    service_enable("yunohost-firewall")
    service_start("yunohost-firewall")

    regen_conf(names=["ssh"], force=True)

    # Restore original ssh conf, as chosen by the
    # admin during the initial install
    #
    # c.f. the install script and in particular
    # https://github.com/YunoHost/install_script/pull/50
    # The user can now choose during the install to keep
    # the initial, existing sshd configuration
    # instead of YunoHost's recommended conf
    #
    original_sshd_conf = "/etc/ssh/sshd_config.before_yunohost"
    if os.path.exists(original_sshd_conf):
        os.rename(original_sshd_conf, "/etc/ssh/sshd_config")

    regen_conf(force=True)

    logger.success(m18n.n("yunohost_configured"))

    logger.warning(m18n.n("yunohost_postinstall_end_tip"))
示例#11
0
def tools_postinstall(operation_logger,
                      domain,
                      password,
                      ignore_dyndns=False,
                      force_password=False):
    """
    YunoHost post-install

    Keyword argument:
        domain -- YunoHost main domain
        ignore_dyndns -- Do not subscribe domain to a DynDNS service (only
        needed for nohost.me, noho.st domains)
        password -- YunoHost admin password

    """
    from yunohost.utils.password import assert_password_is_strong_enough
    from yunohost.domain import domain_main_domain

    dyndns_provider = "dyndns.yunohost.org"

    # Do some checks at first
    if os.path.isfile('/etc/yunohost/installed'):
        raise YunohostError('yunohost_already_installed')

    if os.path.isdir(
            "/etc/yunohost/apps") and os.listdir("/etc/yunohost/apps") != []:
        raise YunohostError(
            "It looks like you're trying to re-postinstall a system that was already working previously ... If you recently had some bug or issues with your installation, please first discuss with the team on how to fix the situation instead of savagely re-running the postinstall ...",
            raw_msg=True)

    # Check password
    if not force_password:
        assert_password_is_strong_enough("admin", password)

    if not ignore_dyndns:
        # Check if yunohost dyndns can handle the given domain
        # (i.e. is it a .nohost.me ? a .noho.st ?)
        try:
            is_nohostme_or_nohost = _dyndns_provides(dyndns_provider, domain)
        # If an exception is thrown, most likely we don't have internet
        # connectivity or something. Assume that this domain isn't manageable
        # and inform the user that we could not contact the dyndns host server.
        except:
            logger.warning(
                m18n.n('dyndns_provider_unreachable',
                       provider=dyndns_provider))
            is_nohostme_or_nohost = False

        # If this is a nohost.me/noho.st, actually check for availability
        if is_nohostme_or_nohost:
            # (Except if the user explicitly said he/she doesn't care about dyndns)
            if ignore_dyndns:
                dyndns = False
            # Check if the domain is available...
            elif _dyndns_available(dyndns_provider, domain):
                dyndns = True
            # If not, abort the postinstall
            else:
                raise YunohostError('dyndns_unavailable', domain=domain)
        else:
            dyndns = False
    else:
        dyndns = False

    if os.system("iptables -V >/dev/null 2>/dev/null") != 0:
        raise YunohostError(
            "iptables/nftables does not seems to be working on your setup. You may be in a container or your kernel does have the proper modules loaded. Sometimes, rebooting the machine may solve the issue.",
            raw_msg=True)

    operation_logger.start()
    logger.info(m18n.n('yunohost_installing'))

    regen_conf(['nslcd', 'nsswitch'], force=True)

    # Initialize LDAP for YunoHost
    # TODO: Improve this part by integrate ldapinit into conf_regen hook
    tools_ldapinit()

    # Create required folders
    folders_to_create = [
        '/etc/yunohost/apps', '/etc/yunohost/certs',
        '/var/cache/yunohost/repo', '/home/yunohost.backup',
        '/home/yunohost.app'
    ]

    for folder in [x for x in folders_to_create if not os.path.exists(x)]:
        os.makedirs(folder)

    # Change folders permissions
    os.system('chmod 755 /home/yunohost.app')

    # Init ssowat's conf.json.persistent
    if not os.path.exists('/etc/ssowat/conf.json.persistent'):
        write_to_json('/etc/ssowat/conf.json.persistent', {})

    os.system('chmod 644 /etc/ssowat/conf.json.persistent')

    # Create SSL CA
    regen_conf(['ssl'], force=True)
    ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA'
    # (Update the serial so that it's specific to this very instance)
    os.system("openssl rand -hex 19 > %s/serial" % ssl_dir)
    commands = [
        'rm %s/index.txt' % ssl_dir,
        'touch %s/index.txt' % ssl_dir,
        'cp %s/openssl.cnf %s/openssl.ca.cnf' % (ssl_dir, ssl_dir),
        'sed -i s/yunohost.org/%s/g %s/openssl.ca.cnf ' % (domain, ssl_dir),
        'openssl req -x509 -new -config %s/openssl.ca.cnf -days 3650 -out %s/ca/cacert.pem -keyout %s/ca/cakey.pem -nodes -batch -subj /CN=%s/O=%s'
        % (ssl_dir, ssl_dir, ssl_dir, domain, os.path.splitext(domain)[0]),
        'cp %s/ca/cacert.pem /etc/ssl/certs/ca-yunohost_crt.pem' % ssl_dir,
        'update-ca-certificates'
    ]

    for command in commands:
        p = subprocess.Popen(command.split(),
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT)

        out, _ = p.communicate()

        if p.returncode != 0:
            logger.warning(out)
            raise YunohostError('yunohost_ca_creation_failed')
        else:
            logger.debug(out)

    logger.success(m18n.n('yunohost_ca_creation_success'))

    # New domain config
    regen_conf(['nsswitch'], force=True)
    domain_add(domain, dyndns)
    domain_main_domain(domain)

    # Change LDAP admin password
    tools_adminpw(password, check_strength=not force_password)

    # Enable UPnP silently and reload firewall
    firewall_upnp('enable', no_refresh=True)

    # Initialize the apps catalog system
    _initialize_apps_catalog_system()

    # Try to update the apps catalog ...
    # we don't fail miserably if this fails,
    # because that could be for example an offline installation...
    try:
        _update_apps_catalog()
    except Exception as e:
        logger.warning(str(e))

    # Create the archive directory (makes it easier for people to upload backup
    # archives, otherwise it's only created after running `yunohost backup
    # create` once.
    from yunohost.backup import _create_archive_dir
    _create_archive_dir()

    # Init migrations (skip them, no need to run them on a fresh system)
    _skip_all_migrations()

    os.system('touch /etc/yunohost/installed')

    # Enable and start YunoHost firewall at boot time
    service_enable("yunohost-firewall")
    service_start("yunohost-firewall")

    regen_conf(names=["ssh"], force=True)

    # Restore original ssh conf, as chosen by the
    # admin during the initial install
    #
    # c.f. the install script and in particular
    # https://github.com/YunoHost/install_script/pull/50
    # The user can now choose during the install to keep
    # the initial, existing sshd configuration
    # instead of YunoHost's recommended conf
    #
    original_sshd_conf = '/etc/ssh/sshd_config.before_yunohost'
    if os.path.exists(original_sshd_conf):
        os.rename(original_sshd_conf, '/etc/ssh/sshd_config')

    regen_conf(force=True)

    logger.success(m18n.n('yunohost_configured'))

    logger.warning(m18n.n('yunohost_postinstall_end_tip'))