Skip to content

Commit

Permalink
Its a mess, but started refactoring the whole plugin (because its fun)
Browse files Browse the repository at this point in the history
  • Loading branch information
timperrett committed Dec 15, 2011
1 parent 31c2e44 commit c08608e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 110 deletions.
4 changes: 1 addition & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ organization := "eu.getintheloop"

name := "sbt-cloudbees-plugin"

version in Posterous := "0.3.3"

version <<= sbtVersion("0.3.3-"+_)
version := "0.4.0"

// maven repositories
resolvers ++= Seq(
Expand Down
159 changes: 52 additions & 107 deletions src/main/scala/Plugin.scala
Original file line number Diff line number Diff line change
@@ -1,116 +1,61 @@
package bees
package cloudbees

import sbt._
import sbt._, Project.Initialize
import com.cloudbees.api.{BeesClient,HashWriteProgress}
import com.github.siasia.WebPlugin._

object RunCloudPlugin extends Plugin {
private[bees] object Internals {

final class PromptableOption(val opt: Option[String]){
def orPromtFor(withText: String): Option[String] =
opt orElse SimpleReader.readLine("\n"+withText+": ").map(_.trim)
}

final case class UserSettings(key: String, secret: String)

import java.util.Properties
import java.io.{File,FileInputStream}

implicit def option2PromtableOption(in: Option[String]): PromptableOption =
new PromptableOption(in)

def targetAppId(username: String, appId: String) = appId.split("/").toList match {
case a :: Nil => username+"/"+a
case _ => appId
}

def configuration: Option[Properties] = {
val properties = new Properties
val config = new File(System.getProperty("user.home"), ".bees/bees.config")
object Plugin extends Plugin {
val CloudBees = config("cloudbees")

if(config.exists){
var fis: FileInputStream = null;
try {
fis = new FileInputStream(config)
properties.load(fis)
fis.close()
Some(properties)
} catch {
case _ => None
} finally {
if(fis != null) try { fis.close() } catch { case _ => }
}
} else None
}

def keyFor(what: String): Option[String] = (for {
config <- configuration
} yield config.getProperty(what)) match {
case s@Some(value:String) if !value.isEmpty => s
case Some(null) => None
case _ => None
}

}
import Internals._
import scala.collection.JavaConverters._
// actual plugin definition

private val log = ConsoleLogger()

// settings
private val beesApiHost = "api.cloudbees.com"
private val beesApiKey = keyFor("bees.api.key") orElse keyFor("bees.apikey")
private val beesSecret = keyFor("bees.api.secret") orElse keyFor("bees.secret")
val beesApplicationId = keyFor("bees.application.id")
val beesUsername = keyFor("bees-username")

val beesShouldDeltaWar = SettingKey[Boolean]("bees-should-delta-war", "Deploy only a delta-WAR to CloudBees (default: true)")

val useDeltaWar = SettingKey[Boolean]("use-delta-war", "Deploy only a delta-WAR to CloudBees (default: true)")
val host = SettingKey[String]("host", "Host URL of the CloudBees API")
val apiKey = SettingKey[Option[String]]("api-key", "Your CloudBees API key")
val apiSecret = SettingKey[Option[String]]("api-secrect", "Your CloudBees API secret")
val applicationId = SettingKey[Option[String]]("application-id", "The application identifier of the deploying project")
val username = SettingKey[Option[String]]("username", "Your CloudBees username")
// tasks
val beesAppList = TaskKey[Unit]("bees-app-list", "List the applications in your Run@Cloud account")

private def beesApplistTask {
log.info("Applications")
log.info("============")
client.foreach {
val applications = TaskKey[Unit]("applications")
val deploy = TaskKey[Unit]("deploy")

val cloudBeesSettings: Seq[Project.Setting[_]] =
inConfig(CloudBees)(Seq(
host := "api.cloudbees.com",
useDeltaWar := true,
username := None,
apiKey := None,
apiSecret := None,
applicationId := None,
applications <<= applicationsTask//,
//deploy <<= deployTask
))

import scala.collection.JavaConverters._

/***** tasks ******/
val applicationsTask: Initialize[Task[Unit]] = (host, apiKey, apiSecret) map { (h,k,s) =>
client(h,k,s).foreach {
_.applicationList.getApplications.asScala.foreach(
a => log.info("+ %s - %s".format(a.getTitle, a.getUrls.head)))
a => ConsoleLogger().info("+ %s - %s".format(a.getTitle, a.getUrls.head)))
}
}

// deploy task
val beesDeploy: Command = Command.command("bees-deploy")(beesDeployAction)

private def beesDeployAction(state: State): State = {
val warPath: File = Project.extract(state).evalTask(packageWar: ScopedTask[File], state)
val deltaWar: Boolean = Project.extract(state).get(beesShouldDeltaWar: SettingKey[Boolean])
app.map(info => client.foreach { c =>
if (warPath.exists) {
log.info("Deploying application '%s' to Run@Cloud".format(info))
c.applicationDeployWar(info, null, null, warPath.asFile.getAbsolutePath, null, deltaWar, new HashWriteProgress)
} else log.error("No WAR file exists to deploy to Run@Cloud")
})
state
}

// set defaults into the users build.sbt
val deploymentSettings: Seq[Project.Setting[_]] = webSettings ++ Seq(
beesShouldDeltaWar := true,
beesAppList := beesApplistTask,
Keys.commands ++= Seq(beesDeploy))

private def app: Option[String] = for {
uid <- beesUsername orPromtFor("CloudBees Username")
aid <- beesApplicationId orPromtFor("CloudBees Application ID")
} yield targetAppId(uid, aid)

private def client: Option[BeesClient] = machineSettings.map(s =>
new BeesClient("http://%s/api".format(beesApiHost), s.key, s.secret, "xml", "1.0"))

private def machineSettings = for {
key <- beesApiKey orPromtFor("CloudBees API Key")
secret <- beesSecret orPromtFor("CloudBees Secret")
} yield UserSettings(key,secret)
}


/***** internal *****/
private def client(host: String, key: Option[String], secret: Option[String]): Option[BeesClient] =
for {
k <- key
s <- secret
} yield new BeesClient("http://%s/api".format(host), k, s, "xml", "1.0")

// val deployTask =
// private def beesApplistTask {
// log.info("Applications")
// log.info("============")
// client.foreach {
// _.applicationList.getApplications.asScala.foreach(
// a => log.info("+ %s - %s".format(a.getTitle, a.getUrls.head)))
// }
// }


}

0 comments on commit c08608e

Please sign in to comment.