Скопируйте файл на основе версии RHEL с помощью Izpack

Я использую 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>  

но это не работает. Может кто-нибудь, пожалуйста, помогите!

1 ответ

Я создал новый класс OSVersion, как показано ниже. Этот класс имеет переменную "IS_REDHAT_LINUX_7", которая возвращает истину, если сервер RHEL7.

Примечание:- Класс Osversion, который предоставляется как часть izpack, не имеет логики для проверки версии 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.

Другие вопросы по тегам