Bug 8208 - bare metal provisioning

get DHCP entry state from DHCP server
This commit is contained in:
Frank 2011-03-11 14:07:47 -08:00
parent 4324efea26
commit e872996084
7 changed files with 131 additions and 18 deletions

View File

@ -7,6 +7,7 @@ public class InvestigateDhcpEntryAnswer extends Answer {
String mac;
String ip;
String state;
int resultCode;
public String getMac() {
return mac;
@ -20,11 +21,16 @@ public class InvestigateDhcpEntryAnswer extends Answer {
return state;
}
public InvestigateDhcpEntryAnswer(Command cmd, boolean success, String details) {
public int getResultCode() {
return resultCode;
}
public InvestigateDhcpEntryAnswer(Command cmd, boolean success, String details, int code) {
super(cmd, success, details);
InvestigateDhcpEntryCommand icmd = (InvestigateDhcpEntryCommand)cmd;
mac = icmd.getMac();
ip = icmd.getIp();
state = icmd.getState();
resultCode = code;
}
}

View File

@ -0,0 +1,27 @@
package com.cloud.agent.api.baremetal;
import com.cloud.agent.api.Command;
public class IpmISetBootDevCommand extends Command {
public enum BootDev {
pxe(),
disk(),
cdrom(),
}
BootDev bootDev;
public BootDev getBootDev() {
return bootDev;
}
public IpmISetBootDevCommand(BootDev dev) {
bootDev = dev;
}
@Override
public boolean executeInSequence() {
return true;
}
}

View File

@ -0,0 +1,21 @@
package com.cloud.agent.api.baremetal;
import com.cloud.agent.api.Command;
public class LinMinProvisionDoneCommand extends Command {
String mac;
public LinMinProvisionDoneCommand(String mac) {
this.mac = mac;
}
public String getMac() {
return mac;
}
@Override
public boolean executeInSequence() {
return true;
}
}

View File

@ -27,15 +27,48 @@ touch /var/log/dnsmasq.log
[ $? -ne 0 ] && exit_with_error "touch /var/log/dnsmasq.log failed"
touch /etc/dnsmasq-resolv.conf
[ $? -ne 0 ] && exit_with_error "touch /etc/dnsmasq-resolv.conf failed"
touch /var/lib/dnsmasq.trace
[ $? -ne 0 ] && exit_with_error "touch /var/lib/dnsmasq.trace failed"
#produce echoer.sh
cat > /usr/bin/echoer.sh<<'EOF'
#!/bin/sh
sed -i /"$*"/d /var/lib/dnsmasq.trace
echo "$*" >> /var/lib/dnsmasq.trace
EOF
[ $? -ne 0 ] && exit_with_error "can't produce /usr/bin/echoer.sh"
#produce lease_checker.sh
cat > /usr/bin/lease_checker.sh<<'EOF'
#!/bin/sh
# Usage: lease_checker dhcp_entry_state(add/old/del) mac ip
state=$1
mac=$2
ip=$3
exit_with_error() {
echo $1
exit $2
}
[ $# -ne 3 ] && exit_with_error "Wrong arguments.Usage: lease_checker dhcp_entry_state(add/old/del) mac ip" -3
[ -f /var/lib/dnsmasq.trace ] || exit_with_error "Cannot find /var/lib/dnsmasq" -1
pidof dnsmasq &>/dev/null
[ $? -ne 0 ] && exit_with_error "Dnsmasq is not running" -2
grep "$state $mac $ip" /var/lib/dnsmasq.trace
if [ $? -ne 0 ]; then
exit $?
else
sed -i /"$state $mac $ip"/d /var/lib/dnsmasq.trace
exit 0
fi
EOF
chmod +x /usr/bin/echoer.sh
[ $? -ne 0 ] && exit_with_error "chmod +x /usr/bin/echoer.sh failed"

View File

@ -59,4 +59,5 @@ public interface SerialVersionUID {
public static final long ResourceUnavailableException = Base | 0x1f;
public static final long ConnectionException = Base | 0x20;
public static final long PermissionDeniedException = Base | 0x21;
public static final long sshException = Base | 0x22;
}

View File

@ -43,17 +43,36 @@ public class SSHCmdHelper {
public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd, int nTimes) {
for (int i = 0; i < nTimes; i ++) {
try {
if (sshExecuteCmdOneShot(sshConnection, cmd))
return true;
} catch (sshException e) {
continue;
}
}
return false;
}
public static int sshExecuteCmdWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd) {
return sshExecuteCmdWithExitCode(sshConnection, cmd, 3);
}
public static int sshExecuteCmdWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd, int nTimes) {
for (int i = 0; i < nTimes; i ++) {
try {
return sshExecuteCmdOneShotWithExitCode(sshConnection, cmd);
} catch (sshException e) {
continue;
}
}
return -1;
}
public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd) {
return sshExecuteCmd(sshConnection, cmd, 3);
}
public static boolean sshExecuteCmdOneShot(com.trilead.ssh2.Connection sshConnection, String cmd) {
public static int sshExecuteCmdOneShotWithExitCode(com.trilead.ssh2.Connection sshConnection, String cmd) throws sshException {
s_logger.debug("Executing cmd: " + cmd);
Session sshSession = null;
try {
@ -63,7 +82,7 @@ public class SSHCmdHelper {
Thread.sleep(1000);
if (sshSession == null) {
return false;
throw new sshException("Cannot open ssh session");
}
sshSession.execCommand(cmd);
@ -75,7 +94,7 @@ public class SSHCmdHelper {
byte[] buffer = new byte[8192];
while (true) {
if (stdout == null || stderr == null) {
return false;
throw new sshException("stdout or stderr of ssh session is null");
}
if ((stdout.available() == 0) && (stderr.available() == 0)) {
@ -108,21 +127,17 @@ public class SSHCmdHelper {
s_logger.debug(cmd + " output:" + new String(buffer));
Thread.sleep(1000);
if (sshSession.getExitStatus() != 0) {
return false;
}
return true;
} catch (IOException e) {
s_logger.debug("Executing cmd: " + cmd + " failed, due to: " + e.toString());
return false;
} catch (InterruptedException e) {
return false;
return sshSession.getExitStatus();
} catch (Exception e) {
return false;
s_logger.debug("Ssh executed failed", e);
throw new sshException("Ssh executed failed " + e.getMessage());
} finally {
if (sshSession != null)
sshSession.close();
}
}
public static boolean sshExecuteCmdOneShot(com.trilead.ssh2.Connection sshConnection, String cmd) throws sshException {
return sshExecuteCmdOneShotWithExitCode(sshConnection, cmd) == 0;
}
}

View File

@ -0,0 +1,10 @@
package com.cloud.utils.ssh;
import com.cloud.utils.SerialVersionUID;
public class sshException extends Exception {
private static final long serialVersionUID = SerialVersionUID.sshException;
public sshException(String msg) {
super(msg);
}
}