晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /usr/lib/python3.6/site-packages/cloudinit/cmd/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/lib/python3.6/site-packages/cloudinit/cmd/clean.py
#!/usr/bin/env python3

# Copyright (C) 2017 Canonical Ltd.
#
# This file is part of cloud-init. See LICENSE file for license information.

"""Define 'clean' utility and handler as part of cloud-init commandline."""

import argparse
import glob
import os
import sys

from cloudinit import settings
from cloudinit.distros import uses_systemd
from cloudinit.stages import Init
from cloudinit.subp import ProcessExecutionError, runparts, subp
from cloudinit.util import (
    del_dir,
    del_file,
    error,
    get_config_logfiles,
    is_link,
    write_file,
)

ETC_MACHINE_ID = "/etc/machine-id"
GEN_NET_CONFIG_FILES = [
    "/etc/netplan/50-cloud-init.yaml",
    "/etc/NetworkManager/conf.d/99-cloud-init.conf",
    "/etc/NetworkManager/conf.d/30-cloud-init-ip6-addr-gen-mode.conf",
    "/etc/NetworkManager/system-connections/cloud-init-*.nmconnection",
    "/etc/systemd/network/10-cloud-init-*.network",
    "/etc/network/interfaces.d/50-cloud-init.cfg",
]
GEN_SSH_CONFIG_FILES = [
    "/etc/ssh/sshd_config.d/50-cloud-init.conf",
]


def get_parser(parser=None):
    """Build or extend an arg parser for clean utility.

    @param parser: Optional existing ArgumentParser instance representing the
        clean subcommand which will be extended to support the args of
        this utility.

    @returns: ArgumentParser with proper argument configuration.
    """
    if not parser:
        parser = argparse.ArgumentParser(
            prog="clean",
            description=(
                "Remove logs, configs and artifacts so cloud-init re-runs "
                "on a clean system"
            ),
        )
    parser.add_argument(
        "-l",
        "--logs",
        action="store_true",
        default=False,
        dest="remove_logs",
        help="Remove cloud-init logs.",
    )
    parser.add_argument(
        "--machine-id",
        action="store_true",
        default=False,
        help=(
            "Set /etc/machine-id to 'uninitialized\n' for golden image"
            "creation. On next boot, systemd generates a new machine-id."
            " Remove /etc/machine-id on non-systemd environments."
        ),
    )
    parser.add_argument(
        "-r",
        "--reboot",
        action="store_true",
        default=False,
        help="Reboot system after logs are cleaned so cloud-init re-runs.",
    )
    parser.add_argument(
        "-s",
        "--seed",
        action="store_true",
        default=False,
        dest="remove_seed",
        help="Remove cloud-init seed directory /var/lib/cloud/seed.",
    )
    parser.add_argument(
        "-c",
        "--configs",
        choices=[
            "all",
            "ssh_config",
            "network",
        ],
        default=[],
        nargs="+",
        dest="remove_config",
        help="Remove cloud-init generated config files of a certain type."
        " Config types: all, ssh_config, network",
    )
    return parser


def remove_artifacts(remove_logs, remove_seed=False, remove_config=None):
    """Helper which removes artifacts dir and optionally log files.

    @param: remove_logs: Boolean. Set True to delete the cloud_dir path. False
        preserves them.
    @param: remove_seed: Boolean. Set True to also delete seed subdir in
        paths.cloud_dir.
    @param: remove_config: List of strings.
        Can be any of: all, network, ssh_config.
    @returns: 0 on success, 1 otherwise.
    """
    init = Init(ds_deps=[])
    init.read_cfg()
    if remove_logs:
        for log_file in get_config_logfiles(init.cfg):
            del_file(log_file)
    if remove_config and set(remove_config).intersection(["all", "network"]):
        for path in GEN_NET_CONFIG_FILES:
            for conf in glob.glob(path):
                del_file(conf)
    if remove_config and set(remove_config).intersection(
        ["all", "ssh_config"]
    ):
        for conf in GEN_SSH_CONFIG_FILES:
            del_file(conf)

    if not os.path.isdir(init.paths.cloud_dir):
        return 0  # Artifacts dir already cleaned
    seed_path = os.path.join(init.paths.cloud_dir, "seed")
    for path in glob.glob("%s/*" % init.paths.cloud_dir):
        if path == seed_path and not remove_seed:
            continue
        try:
            if os.path.isdir(path) and not is_link(path):
                del_dir(path)
            else:
                del_file(path)
        except OSError as e:
            error("Could not remove {0}: {1}".format(path, str(e)))
            return 1
    try:
        runparts(settings.CLEAN_RUNPARTS_DIR)
    except Exception as e:
        error(
            f"Failure during run-parts of {settings.CLEAN_RUNPARTS_DIR}: {e}"
        )
        return 1
    return 0


def handle_clean_args(name, args):
    """Handle calls to 'cloud-init clean' as a subcommand."""
    exit_code = remove_artifacts(
        args.remove_logs, args.remove_seed, args.remove_config
    )
    if args.machine_id:
        if uses_systemd():
            # Systemd v237 and later will create a new machine-id on next boot
            write_file(ETC_MACHINE_ID, "uninitialized\n", mode=0o444)
        else:
            # Non-systemd like FreeBSD regen machine-id when file is absent
            del_file(ETC_MACHINE_ID)
    if exit_code == 0 and args.reboot:
        cmd = ["shutdown", "-r", "now"]
        try:
            subp(cmd, capture=False)
        except ProcessExecutionError as e:
            error(
                'Could not reboot this system using "{0}": {1}'.format(
                    cmd, str(e)
                )
            )
            exit_code = 1
    return exit_code


def main():
    """Tool to collect and tar all cloud-init related logs."""
    parser = get_parser()
    sys.exit(handle_clean_args("clean", parser.parse_args()))


if __name__ == "__main__":
    main()

haha - 2025