/src/main/scala/Diagnostic.scala

https://github.com/sshark/resource_manager · Scala · 113 lines · 86 code · 11 blank · 16 comment · 1 complexity · 4dd9e53a792f3e70394bcc529bae731a MD5 · raw file

  1. // JDK refs.
  2. import java.util._
  3. import java.io._
  4. import java.util.logging._
  5. import javax.xml.parsers._
  6. import javax.xml.xpath._
  7. import javax.xml.transform._
  8. import javax.xml.transform.dom._
  9. import javax.xml.transform.stream._
  10. import org.xml.sax._
  11. import org.xml.sax.helpers._
  12. // Scala 2.9 refs.
  13. import scala.xml._
  14. import scala.actors._
  15. import scala.actors.Actor._
  16. import scala.actors.Futures._
  17. // Third party refs.
  18. import org.custommonkey.xmlunit._
  19. import org.restlet._
  20. import org.restlet.data._
  21. import org.restlet.representation._
  22. import org.restlet.resource._
  23. import org.restlet.service._
  24. import org.restlet.ext.xml._
  25. import com.mongodb._
  26. import com.mongodb.casbah._
  27. import com.mongodb.casbah.commons._
  28. /**
  29. * A Restlet that allows the users to query the state
  30. * of their provision
  31. * @see ResourceManager for the URI mappings
  32. * @author Raymond Tay
  33. * @version 1.0
  34. */
  35. class Diagnostic extends ServerResource {
  36. /**
  37. * Retrieves the status of the VPC's
  38. * web, app, db provisioning
  39. * @param project_id - project's id passed in via http request
  40. * @return A XML document
  41. */
  42. @Get("xml")
  43. def getVPCStatus() = {
  44. val id = getRequestAttributes().get("project_id")
  45. val configCol = MongoConnection()("vpc")("configuration")
  46. val record = MongoDBObject("project_id" -> id)
  47. configCol.findOne(record).foreach { rec =>
  48. val webIds = rec.get("moab_web_rsrv_id").asInstanceOf[BasicDBList]
  49. val appIds = rec.get("moab_app_rsrv_id").asInstanceOf[BasicDBList]
  50. val dbIds = rec.get("moab_db_rsrv_id").asInstanceOf[BasicDBList]
  51. import scala.collection.mutable._
  52. var xmlList = new ListBuffer[Future[Status]]
  53. val docList = new ListBuffer[Elem]
  54. webIds.toArray.foreach {
  55. id => xmlList += checkStatus(id.asInstanceOf[String])
  56. }
  57. xmlList.foreach {
  58. x => docList += x().toXML
  59. }
  60. xmlList = new ListBuffer[Future[Status]]
  61. appIds.toArray.foreach {
  62. id => xmlList += checkStatus(id.asInstanceOf[String])
  63. }
  64. xmlList.foreach {
  65. x => docList += x().toXML
  66. }
  67. xmlList = new ListBuffer[Future[Status]]
  68. dbIds.toArray.foreach { id => xmlList += checkStatus(id.asInstanceOf[String]) }
  69. xmlList.foreach {
  70. x => docList += x().toXML
  71. }
  72. buildJDom(docList.toList)
  73. }
  74. }
  75. def checkStatus(projectId: String) : Future[Status] = {
  76. val f = future {
  77. val proc: Process = new ProcessBuilder("./getvmstatus.sh", "-n" + projectId).start
  78. val input = new Array[Byte](256)
  79. withBufferedInput(proc.getInputStream)(reader => reader.read(input,0,255))
  80. val xmlString = (new String(input)).trim
  81. new Status {
  82. val status = ((xml.XML.loadString(xmlString) \\ "request") \ "status").text
  83. val vncPort = ((xml.XML.loadString(xmlString) \\ "request") \ "vncport").text
  84. val vpc = projectId
  85. }
  86. }
  87. f
  88. }
  89. def withBufferedInput(source: InputStream)(op: InputStream => Unit) {
  90. val bufferedInput = new BufferedInputStream(source)
  91. try {
  92. op(bufferedInput)
  93. } finally {
  94. bufferedInput.close()
  95. }
  96. }
  97. def buildJDom(list: scala.List[Elem]) : org.w3c.dom.Document = {
  98. def toXML =
  99. <system>{ for(ele <- list) yield ele }</system>
  100. Elem2JDom.toDom(toXML)
  101. }
  102. }