我正在使用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>
但这不起作用。任何人都可以帮忙!!
答案 0 :(得分: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的操作。