Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request tumblr#123 from byxorna/lldp-lshw-upload-status
Browse files Browse the repository at this point in the history
Lldp lshw upload status
  • Loading branch information
dallasmarlow committed Mar 4, 2014
2 parents eed0260 + 9980445 commit 3b2fc24
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
62 changes: 29 additions & 33 deletions app/models/AssetLifecycle.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,17 @@ object AssetLifecycle {
}

def updateAsset(asset: Asset, options: Map[String,String]): Status[Boolean] = asset.isServerNode match {
case true => updateServer(asset, options)
case false => updateOther(asset, options)
}

protected def updateOther(asset: Asset, options: Map[String,String]): Status[Boolean] = {
updateAssetAttributes(asset, options)
case true => updateServer(asset, options)
case false => updateAssetAttributes(asset, options)
}

protected def updateServer(asset: Asset, options: Map[String,String]): Status[Boolean] = {
if (asset.isIncomplete) {
updateIncompleteServer(asset, options)
} else if (asset.isNew) {
updateNewServer(asset, options)
} else if (asset.isMaintenance) {
updateMaintenanceServer(asset, options)
} else {
Left(new Exception("Only updates for Incomplete, New, and Maintenance servers are currently supported"))
updateServerHardwareMeta(asset, options)
}
}

Expand Down Expand Up @@ -155,6 +149,31 @@ object AssetLifecycle {
}.left.map(e => handleException(asset, "Error updating status/state for asset", e))
}

protected def updateServerHardwareMeta(asset: Asset, options: Map[String,String]): Status[Boolean] = {
// if asset's status is in the allowed statuses for updating, do it
if (Feature.allowedServerUpdateStatuses.contains(asset.getStatus())) {
// we will allow updates to lshw/lldp while the machine is in these statuses
allCatch[Boolean].either {
Asset.inTransaction {
options.get("lshw").foreach{lshw =>
parseLshw(asset, new LshwParser(lshw)).left.foreach{throw _}
InternalTattler.informational(asset, None, "Parsing and storing LSHW data succeeded")
}
options.get("lldp").foreach{lldp =>
parseLldp(asset, new LldpParser(lldp)).left.foreach{throw _}
InternalTattler.informational(asset, None, "Parsing and storing LLDP data succeeded")
}
options.get("CHASSIS_TAG").foreach{chassis_tag =>
MetaWrapper.createMeta(asset, Map(AssetMeta.Enum.ChassisTag.toString -> chassis_tag))
}
true
}
}.left.map(e => handleException(asset, "Exception updating asset", e))
} else {
Left(new Exception("Only updates for servers in statuses " + Feature.allowedServerUpdateStatuses.mkString(", ") + " are currently supported"))
}
}

protected def updateNewServer(asset: Asset, options: Map[String,String]): Status[Boolean] = {
val units = PowerUnits()
val requiredKeys = Set(RackPosition.toString) ++ PowerUnits.keys(units)
Expand Down Expand Up @@ -217,30 +236,7 @@ object AssetLifecycle {
MetaWrapper.createMeta(asset, filtered ++ Map(AssetMeta.Enum.ChassisTag.toString -> chassis_tag))
val newAsset = asset.copy(status = Status.New.map(_.id).getOrElse(0), updated = Some(new Date().asTimestamp))
Asset.partialUpdate(newAsset, newAsset.updated, Some(newAsset.status), State.New)
InternalTattler.informational(newAsset, None, "Parsing and storing LSHW data succeeded")
true
}
}.left.map(e => handleException(asset, "Exception updating asset", e))
}

protected def updateMaintenanceServer(asset: Asset, options: Map[String, String]): Status[Boolean] = {
//only lshw,lldp, and chasis tag can be updated in maintenance mode, at least one must be present
val allowedKeys = Set("lshw", "lldp", "CHASSIS_TAG")
if (allowedKeys.find{key => options.contains(key)} == None) {
return Left(new Exception("At least one of " + allowedKeys.mkString(",") + " required"))
}

allCatch[Boolean].either {
Asset.inTransaction {
options.get("lshw").foreach{lshw =>
parseLshw(asset, new LshwParser(lshw)).left.foreach{throw _}
}
options.get("lldp").foreach{lldp =>
parseLldp(asset, new LldpParser(lldp)).left.foreach{throw _}
}
options.get("CHASSIS_TAG").foreach{chassis_tag =>
MetaWrapper.createMeta(asset, Map(AssetMeta.Enum.ChassisTag.toString -> chassis_tag))
}
InternalTattler.informational(newAsset, None, "Parsing and storing LSHW/LLDP data succeeded")
true
}
}.left.map(e => handleException(asset, "Exception updating asset", e))
Expand Down
5 changes: 4 additions & 1 deletion app/util/config/Feature.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package util
package config

import models.{Asset, AssetMeta}
import models.{Asset, AssetMeta, Status}
import models.logs.LogMessageType

/**
Expand All @@ -17,6 +17,9 @@ object Feature extends Configurable {
override val referenceConfigFilename = "features_reference.conf"

def allowTagUpdates = getStringSet("allowTagUpdates")
def allowedServerUpdateStatuses = getStringSet("allowedServerUpdateStatuses").union(Set("MAINTENANCE"))
.map { m => Status.findByName(m) }
.filter(_.isDefined).map(_.get)
def defaultLogType = {
val lts = getString("defaultLogType", "Informational").toUpperCase
try {
Expand Down

0 comments on commit 3b2fc24

Please sign in to comment.