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 mac;
String ip; String ip;
String state; String state;
int resultCode;
public String getMac() { public String getMac() {
return mac; return mac;
@ -20,11 +21,16 @@ public class InvestigateDhcpEntryAnswer extends Answer {
return state; 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); super(cmd, success, details);
InvestigateDhcpEntryCommand icmd = (InvestigateDhcpEntryCommand)cmd; InvestigateDhcpEntryCommand icmd = (InvestigateDhcpEntryCommand)cmd;
mac = icmd.getMac(); mac = icmd.getMac();
ip = icmd.getIp(); ip = icmd.getIp();
state = icmd.getState(); 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" [ $? -ne 0 ] && exit_with_error "touch /var/log/dnsmasq.log failed"
touch /etc/dnsmasq-resolv.conf touch /etc/dnsmasq-resolv.conf
[ $? -ne 0 ] && exit_with_error "touch /etc/dnsmasq-resolv.conf failed" [ $? -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 #produce echoer.sh
cat > /usr/bin/echoer.sh<<'EOF' cat > /usr/bin/echoer.sh<<'EOF'
#!/bin/sh #!/bin/sh
sed -i /"$*"/d /var/lib/dnsmasq.trace
echo "$*" >> /var/lib/dnsmasq.trace echo "$*" >> /var/lib/dnsmasq.trace
EOF EOF
[ $? -ne 0 ] && exit_with_error "can't produce /usr/bin/echoer.sh" [ $? -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 chmod +x /usr/bin/echoer.sh
[ $? -ne 0 ] && exit_with_error "chmod +x /usr/bin/echoer.sh failed" [ $? -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 ResourceUnavailableException = Base | 0x1f;
public static final long ConnectionException = Base | 0x20; public static final long ConnectionException = Base | 0x20;
public static final long PermissionDeniedException = Base | 0x21; 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) { public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd, int nTimes) {
for (int i = 0; i < nTimes; i ++) { for (int i = 0; i < nTimes; i ++) {
if (sshExecuteCmdOneShot(sshConnection, cmd)) try {
return true; if (sshExecuteCmdOneShot(sshConnection, cmd))
return true;
} catch (sshException e) {
continue;
}
} }
return false; 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) { public static boolean sshExecuteCmd(com.trilead.ssh2.Connection sshConnection, String cmd) {
return sshExecuteCmd(sshConnection, cmd, 3); 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); s_logger.debug("Executing cmd: " + cmd);
Session sshSession = null; Session sshSession = null;
try { try {
@ -63,7 +82,7 @@ public class SSHCmdHelper {
Thread.sleep(1000); Thread.sleep(1000);
if (sshSession == null) { if (sshSession == null) {
return false; throw new sshException("Cannot open ssh session");
} }
sshSession.execCommand(cmd); sshSession.execCommand(cmd);
@ -75,7 +94,7 @@ public class SSHCmdHelper {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
while (true) { while (true) {
if (stdout == null || stderr == null) { 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)) { if ((stdout.available() == 0) && (stderr.available() == 0)) {
@ -108,21 +127,17 @@ public class SSHCmdHelper {
s_logger.debug(cmd + " output:" + new String(buffer)); s_logger.debug(cmd + " output:" + new String(buffer));
Thread.sleep(1000); Thread.sleep(1000);
if (sshSession.getExitStatus() != 0) { return sshSession.getExitStatus();
return false; } catch (Exception e) {
} s_logger.debug("Ssh executed failed", e);
throw new sshException("Ssh executed failed " + e.getMessage());
return true;
} catch (IOException e) {
s_logger.debug("Executing cmd: " + cmd + " failed, due to: " + e.toString());
return false;
} catch (InterruptedException e) {
return false;
} catch (Exception e) {
return false;
} finally { } finally {
if (sshSession != null) if (sshSession != null)
sshSession.close(); 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);
}
}