0

我正在使用 Izpack,并且我希望某个文件(Agent.service)只有在它是 RHEL7 服务器时才应该放在服务器中。我在包文件中有以下代码行。

<fileset
    dir="@/os/linux-rhel/"
    override="true" targetdir="${HOME}/">
    <os family="unix" name="Linux" version="7" />
    <include name="scripts/Agent.service" />
</fileset>  

但这不起作用。任何人都可以帮忙!

4

1 回答 1

1

我创建了一个新的 OSVersion 类,如下所示。此类具有变量“IS_REDHAT_LINUX_7”,如果服务器是 RHEL7,则返回 true。

注意:-作为 izpack 的一部分提供的 Osversion 类没有检查 RHEL 版本的逻辑,它只能用于确定服务器是否为 Rhel 或其他操作系统。

public class OsVersion {
    public final static String LINUX = "Linux";
    public final static String REDHAT = "RedHat";
    public final static String RED_HAT = "Red Hat";
    public final static String OS_NAME = System.getProperty("os.name");
    /**
     * True if this is Linux.
     */
    public static final boolean IS_LINUX = StringTool.startsWithIgnoreCase(OS_NAME, LINUX);
    /**
     * True if RedHat Linux was detected
     */
    public static final boolean IS_REDHAT_LINUX = IS_LINUX && ((FileUtil.fileContains(getReleaseFileNamesList(), REDHAT)
            || FileUtil.fileContains(getReleaseFileNamesList(), RED_HAT)));
    /**
     * True if RHEL7 server.
     */
    public static final boolean IS_REDHAT_LINUX_7 = IS_REDHAT_LINUX && ((getRedHatReleaseVersion() == 7));
    /**
     * True if RHEL6 server.
     */
    public static final boolean IS_REDHAT_LINUX_6 = IS_REDHAT_LINUX && ((getRedHatReleaseVersion() == 6));
    /**
     * Gets the etc Release Filename
     * 
     * @return name of the file the release info is stored in for Linux
     *         distributions
     */
    private static String getReleaseFileName() {
        String result = "";

        File[] etcList = new File("/etc").listFiles();

        if (etcList != null)
            for (int idx = 0; idx < etcList.length; idx++) {
                File etcEntry = etcList[idx];

                if (etcEntry.isFile()) {
                    if (etcEntry.getName().endsWith("-release")) {
                        // match :-)
                        return result = etcEntry.toString();
                    }
                }
            }

        return result;
    }
    /**
     * Gets the list of etc Release Filenames
     * 
     * @return name of the file the release info is stored in for Linux
     *         distributions
     */
    private static List<String> getReleaseFileNamesList() {
        List<String> result = new ArrayList<String>();

        File[] etcList = new File("/etc").listFiles();

        if (etcList != null)
            for (int idx = 0; idx < etcList.length; idx++) {
                File etcEntry = etcList[idx];

                if (etcEntry.isFile()) {
                    if (etcEntry.getName().endsWith("redhat-release")) {
                        result.add(etcEntry.toString());
                    }
                }
            }

        return result;
    }
    /**
     * Gets the RedHat Release Version
     * 
     * @return the major release version number
     */
    private static Integer getRedHatReleaseVersion() {
        String releaseDetails = "Red Hat Enterprise Linux Server release";
        String relaseFile = getReleaseFileName();
        BufferedReader br = null;
        FileReader fr = null;
        Integer redhatVersion = 0;
        try {
            fr = new FileReader(relaseFile);
            br = new BufferedReader(fr);
            String sCurrentLine;
            while ((sCurrentLine = br.readLine()) != null) {
                if (sCurrentLine.trim().startsWith(releaseDetails)) {
                    String s[] = sCurrentLine.split("release");
                    char version = s[1].trim().charAt(0);
                    redhatVersion = Character.getNumericValue(version);
                }
            }
        } catch (IOException e) {
            //Do Nothing.
        } finally {
            try {
                if (br != null)
                    br.close();
                if (fr != null)
                    fr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return redhatVersion;
    }

此外,现在您必须创建一个新条件,如下所示:-

<condition type="java" id="IS_REDHAT_LINUX_7">
        <java>
            <class>OsVersion</class>
            <field>IS_REDHAT_LINUX_7</field>
        </java>
        <returnvalue type="boolean">true</returnvalue>
    </condition>

现在您可以使用此 id="IS_REDHAT_LINUX_7" 来执行特定于 RHE7 的操作。

于 2016-12-16T12:50:43.733 回答