Skip to content

Commit

Permalink
Correct event log and history. Need the same datetime of event
Browse files Browse the repository at this point in the history
  • Loading branch information
fanf committed Nov 16, 2023
1 parent 771945b commit 3ac5db8
Show file tree
Hide file tree
Showing 20 changed files with 208 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1281,11 +1281,17 @@ object NodeFactChangeEvent {
}
}

final case class ChangeContext(modId: ModificationId, actor: EventActor, message: Option[String], actorIp: Option[String])
final case class ChangeContext(
modId: ModificationId,
actor: EventActor,
eventDate: DateTime,
message: Option[String],
actorIp: Option[String]
)

object ChangeContext {
def newForRudder(msg: Option[String] = None, actorIp: Option[String] = None) =
ChangeContext(ModificationId(java.util.UUID.randomUUID.toString), eventlog.RudderEventActor, msg, actorIp)
ChangeContext(ModificationId(java.util.UUID.randomUUID.toString), eventlog.RudderEventActor, DateTime.now(), msg, actorIp)
}

final case class NodeFactChangeEventCC[+A <: MinimalNodeFactInterface](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ package com.normation.rudder.facts.nodes
import com.normation.errors.IOResult
import com.normation.eventlog.ModificationId
import com.normation.inventory.domain.AcceptedInventory
import com.normation.inventory.domain.InventoryStatus
import com.normation.inventory.domain.NodeId
import com.normation.inventory.domain.PendingInventory
import com.normation.inventory.domain.RemovedInventory
import com.normation.rudder.batch.AsyncDeploymentActor
import com.normation.rudder.batch.AutomaticStartDeployment
Expand Down Expand Up @@ -218,7 +220,7 @@ class EventLogsNodeFactChangeEventCallback(
next: MinimalNodeFactInterface
): IOResult[Unit] = {
val diff = ModifyNodeDiff(toNode(old), toNode(next))
eventLogRepository.saveModifyNode(cc.modId, cc.actor, diff, cc.message).unit
eventLogRepository.saveModifyNode(cc.modId, cc.actor, diff, cc.message, cc.eventDate).unit
}

change.event match {
Expand All @@ -228,6 +230,7 @@ class EventLogsNodeFactChangeEventCallback(
case NodeFactChangeEvent.Accepted(node) =>
val log = AcceptNodeEventLog.fromInventoryLogDetails(
principal = change.cc.actor,
creationDate = change.cc.eventDate,
inventoryDetails = InventoryLogDetails(
nodeId = node.id,
inventoryVersion = node.lastInventoryDate.getOrElse(node.factProcessedDate),
Expand All @@ -248,6 +251,7 @@ class EventLogsNodeFactChangeEventCallback(
case NodeFactChangeEvent.Refused(node) =>
val log = RefuseNodeEventLog.fromInventoryLogDetails(
principal = change.cc.actor,
creationDate = change.cc.eventDate,
inventoryDetails = InventoryLogDetails(
nodeId = node.id,
inventoryVersion = node.lastInventoryDate.getOrElse(node.factProcessedDate),
Expand All @@ -268,8 +272,9 @@ class EventLogsNodeFactChangeEventCallback(
case NodeFactChangeEvent.Deleted(node) =>
val log = DeleteNodeEventLog.fromInventoryLogDetails(
None,
change.cc.actor,
InventoryLogDetails(
principal = change.cc.actor,
creationDate = change.cc.eventDate,
inventoryDetails = InventoryLogDetails(
node.id,
node.lastInventoryDate.getOrElse(node.factProcessedDate),
node.fqdn,
Expand Down Expand Up @@ -307,53 +312,74 @@ class HistorizeNodeState(

override def run(change: NodeFactChangeEventCC[MinimalNodeFactInterface]): IOResult[Unit] = {

def save(nodeId: NodeId, alsoJDBC: Boolean) = {
def save(node: MinimalNodeFactInterface, eventDate: DateTime, alsoJDBC: Boolean, status: InventoryStatus): IOResult[Unit] = {
// we want to save the fact with everything
implicit val attrs = SelectFacts.all
if (gitFactStorage == NoopFactStorage && !alsoJDBC) ZIO.unit
else {
sourceFactStorage.getAccepted(nodeId).flatMap {
case None => ZIO.unit
case Some(full) =>
for {
_ <- ZIO.when(alsoJDBC)(historyRepos.save(nodeId, FactLogData(full, change.cc.actor, AcceptedInventory)))
_ <- gitFactStorage.save(full)
} yield ()
(if (status == PendingInventory) sourceFactStorage.getPending(node.id)
else sourceFactStorage.getAccepted(node.id)).flatMap { res =>
val nf = res match {
case Some(x) => x
case None => // in case of refuse event, node is already deleted
NodeFact.fromMinimal(node)
}
for {
_ <- ZIO.when(alsoJDBC)(historyRepos.save(node.id, FactLogData(nf, change.cc.actor, AcceptedInventory), eventDate))
_ <- gitFactStorage.save(nf)
} yield ()
}
}

}

change.event match {
case NodeFactChangeEvent.NewPending(node) => ZIO.unit
case NodeFactChangeEvent.UpdatedPending(oldNode, newNode) => ZIO.unit
case NodeFactChangeEvent.Accepted(node) => save(node.id, true)
case NodeFactChangeEvent.Refused(node) => save(node.id, true)
case NodeFactChangeEvent.Updated(oldNode, newNode) => save(newNode.id, false)
case NodeFactChangeEvent.Deleted(node) =>
/*
* This hook registers the deletion events into postgresql `nodefacts` table so that the inventory accept/refuse
* fact can be latter cleaned-up.
*/
((
def delete(nodeId: NodeId): IOResult[Unit] = {
/*
* This hook registers the deletion events into postgresql `nodefacts` table so that the inventory accept/refuse
* fact can be latter cleaned-up.
*/
(
(
if (cleanUpImmediately) {
historyRepos.delete(node.id)
historyRepos.delete(nodeId)
} else { // save delete event, clean-up will be automatically done by script
historyRepos.saveDeleteEvent(node.id, DateTime.now(), change.cc.actor)
historyRepos.saveDeleteEvent(nodeId, change.cc.eventDate, change.cc.actor)
}
).catchAll(err => {
NodeLoggerPure
.warn(s"Error when updating node '${node.id.value}' historical inventory information in base: ${err.fullMsg}")
})) *>
NodeLoggerPure.Delete.debug(s" - delete fact about node '${node.id.value}'") *>
gitFactStorage
.changeStatus(node.id, RemovedInventory)
.catchAll(err =>
NodeLoggerPure.info(s"Error when trying to update fact when deleting node '${node.id.value}': ${err.fullMsg}")
)
.unit
.warn(s"Error when updating node '${nodeId.value}' historical inventory information in base: ${err.fullMsg}")
})
) *>
NodeLoggerPure.Delete.debug(s" - delete fact about node '${nodeId.value}'") *>
gitFactStorage
.changeStatus(nodeId, RemovedInventory)
.catchAll(err =>
NodeLoggerPure.info(s"Error when trying to update fact when deleting node '${nodeId.value}': ${err.fullMsg}")
)
.unit
}

case NodeFactChangeEvent.Noop(nodeId) => ZIO.unit
change.event match {
case NodeFactChangeEvent.NewPending(node) =>
NodeLoggerPure.debug(s"Save new in node fact fs") *>
save(node, change.cc.eventDate, false, PendingInventory)
case NodeFactChangeEvent.UpdatedPending(oldNode, newNode) =>
NodeLoggerPure.debug(s"Update pending in node fact fs") *>
save(newNode, change.cc.eventDate, false, PendingInventory)
case NodeFactChangeEvent.Accepted(node) =>
NodeLoggerPure.debug(s"Accept in node fact fs and postgres") *>
save(node, change.cc.eventDate, true, AcceptedInventory) // callback done post accept
case NodeFactChangeEvent.Refused(node) =>
NodeLoggerPure.debug(s"Refused in node fact fs and postgres") *>
save(node, change.cc.eventDate, true, AcceptedInventory)
case NodeFactChangeEvent.Updated(oldNode, newNode) =>
NodeLoggerPure.debug(s"Update in node fact fs") *>
save(newNode, change.cc.eventDate, false, AcceptedInventory)
case NodeFactChangeEvent.Deleted(node) =>
NodeLoggerPure.debug(s"Delete in node fact fs") *>
delete(node.id)
case NodeFactChangeEvent.Noop(nodeId) =>
NodeLoggerPure.debug(s"noop") *>
ZIO.unit
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.normation.rudder.domain.Constants
import com.normation.rudder.domain.logger.NodeLoggerPure
import com.normation.rudder.domain.nodes.NodeState
import com.softwaremill.quicklens._
import scala.annotation.nowarn
import zio._
import zio.concurrent.ReentrantLock
import zio.stream.ZStream
Expand Down Expand Up @@ -359,8 +358,6 @@ class CoreNodeFactRepository(
* - do we want to fork and timeout each callbacks ? likely so
* - do we want to parallel exec them ? likely so, the user can build his own callback sequencer callback if he wants
*/
// TODO: perhaps we only want to accept post hooks on core node facts
@nowarn("msg=abstract type A in type pattern .+ is unchecked.")
private[nodes] def runCallbacks[A <: MinimalNodeFactInterface](e: NodeFactChangeEventCC[A]): IOResult[Unit] = {
for {
cs <- callbacks.get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ import com.normation.rudder.domain.nodes.NodeInfo
import com.normation.rudder.domain.nodes.NodeKind
import com.normation.rudder.repository.WoNodeRepository
import com.normation.rudder.services.nodes.NodeInfoService

import com.softwaremill.quicklens._
import org.joda.time.DateTime

import zio._
import zio.stream.ZSink
import zio.syntax._
Expand Down Expand Up @@ -232,7 +235,7 @@ class WoFactNodeRepositoryProxy(backend: NodeFactRepository) extends WoNodeRepos
case None => Inconsistency(s"Node with id '${node.id.value}' was not found").fail
case Some(fact) => CoreNodeFact.updateNode(fact, node).succeed
}
_ <- backend.save(NodeFact.fromMinimal(fact))(ChangeContext(modId, actor, reason, None), SelectFacts.none)
_ <- backend.save(NodeFact.fromMinimal(fact))(ChangeContext(modId, actor, DateTime.now(), reason, None), SelectFacts.none)
} yield fact.toNode
}

Expand Down Expand Up @@ -261,7 +264,7 @@ class WoFactNodeRepositoryProxy(backend: NodeFactRepository) extends WoNodeRepos
.setToIfDefined(agentKey)
.modify(_.rudderSettings.keyStatus)
.setToIfDefined(agentKeyStatus)
_ <- backend.save(NodeFact.fromMinimal(newNode))(ChangeContext(modId, actor, reason, None), SelectFacts.none)
_ <- backend.save(NodeFact.fromMinimal(newNode))(ChangeContext(modId, actor, DateTime.now(), reason, None), SelectFacts.none)
} yield ()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import com.normation.rudder.domain.workflows.ChangeRequestId
import com.normation.rudder.domain.workflows.WorkflowStepChange
import com.normation.rudder.services.eventlog.EventLogFactory
import doobie._
import org.joda.time.DateTime

trait EventLogRepository {
def eventLogFactory: EventLogFactory
Expand Down Expand Up @@ -396,14 +397,16 @@ trait EventLogRepository {
modId: ModificationId,
principal: EventActor,
modifyDiff: ModifyNodeDiff,
reason: Option[String]
reason: Option[String],
eventDate: DateTime
) = {
saveEventLog(
modId,
eventLogFactory.getModifyNodeFromDiff(
principal = principal,
modifyDiff = modifyDiff,
reason = reason
reason = reason,
creationDate = eventDate
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
package com.normation.rudder.repository.ldap

import com.normation.NamedZioLogger

import com.normation.errors._
import com.normation.eventlog.EventActor
import com.normation.eventlog.ModificationId
Expand All @@ -57,6 +58,9 @@ import com.normation.rudder.repository.WoNodeRepository
import com.normation.rudder.services.reports.CacheComplianceQueueAction
import com.normation.rudder.services.reports.CacheExpectedReportAction
import com.normation.rudder.services.reports.InvalidateCache

import org.joda.time.DateTime

import zio._
import zio.syntax._

Expand Down Expand Up @@ -97,7 +101,7 @@ class WoLDAPNodeRepository(
case LDIFNoopChangeRecord(_) => ZIO.unit
case _ =>
val diff = ModifyNodeDiff(oldNode, node)
actionLogger.saveModifyNode(modId, actor, diff, reason)
actionLogger.saveModifyNode(modId, actor, diff, reason, DateTime.now())
}
} yield {
node
Expand Down Expand Up @@ -164,7 +168,7 @@ class WoLDAPNodeRepository(
case _ =>
val diff =
ModifyNodeDiff.keyInfo(nodeId, agentsInfo._1.map(_.securityToken), agentsInfo._2, agentKey, agentKeyStatus)
actionLogger.saveModifyNode(modId, actor, diff, reason)
actionLogger.saveModifyNode(modId, actor, diff, reason, DateTime.now())
}
} yield ())
}
Expand Down
Loading

0 comments on commit 3ac5db8

Please sign in to comment.