Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
fanf committed Oct 31, 2017
1 parent 2759b1e commit 8dac39b
Show file tree
Hide file tree
Showing 44 changed files with 218 additions and 440 deletions.
4 changes: 2 additions & 2 deletions rudder-core/src/main/resources/ldap/rudder.schema
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ objectclass ( RudderObjectClasses:30
DESC 'A rule'
SUP top
STRUCTURAL
MUST ( ruleId $ serial )
MUST ( ruleId )
MAY ( cn $ description $ longDescription $
isEnabled $ isSystem $
ruleTarget $
ruleTarget $ serial $
directiveId $ tag $ serializedTags ) )

objectclass ( RudderObjectClasses:31
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,42 @@ final case class AgentConfig(
, bundlesequence : Seq[BundleName]
)


/**
* A type that tells if the Technique supports directive by directive
* generation or not.
*/
sealed trait TechniqueGenerationMode {
def name: String
}

final object TechniqueGenerationMode {

/*
* This technique does not support mutiple directives on the same node
* but if the directive parameters are merged.
* This is the historical way of working for Rudder techniques.
*/
final case object MergeDirectives extends TechniqueGenerationMode {
override val name = "merge_directives"
}

/*
* The technique supports several independant directives (and so,
* several technique version or modes).
*/
final case object MultipleDirectives extends TechniqueGenerationMode {
override val name = "multi_directives"
}

def allValues = ca.mrvisser.sealerate.values[TechniqueGenerationMode]

def parse(value: String): Option[TechniqueGenerationMode] = {
val v = value.toLowerCase
allValues.find( _.name == v)
}
}

/**
* A structure containing all informations about a technique deprecation
*/
Expand Down Expand Up @@ -104,7 +140,7 @@ case class Technique(
, longDescription : String = ""
, isSystem : Boolean = false
, providesExpectedReports: Boolean = false //does that Technique comes with a template file (csv) of expected reports ?

, generationMode : TechniqueGenerationMode = TechniqueGenerationMode.MergeDirectives
) extends HashcodeCaching {

require(null != id && !isEmpty(id.name.value), "ID is required in policy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import com.normation.cfclerk.exceptions._
import java.io.File
import net.liftweb.common._
import scala.collection.mutable.{ Map => MutMap }
import com.normation.utils.Utils
import scala.collection.immutable.SortedMap
import java.io.InputStream
import org.eclipse.jgit.treewalk.TreeWalk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ object CfclerkXmlConstants {
val TECHNIQUE_LONG_DESCRIPTION = "LONG_DESCRIPTION"
val TECHNIQUE_IS_SYSTEM = "SYSTEM"
val TECHNIQUE_DEPRECATION_INFO = "DEPRECATED"
val TECHNIQUE_GENERATION_MODE = "GENERATION"

//bundles
val BUNDLES_ROOT = "BUNDLES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import CfclerkXmlConstants._
import com.normation.cfclerk.domain._
import com.normation.cfclerk.services.SystemVariableSpecService
import com.normation.cfclerk.exceptions.ParsingException
import com.normation.utils.Utils._
import scala.xml._
import net.liftweb.common._
import com.normation.inventory.domain.AgentType
Expand Down Expand Up @@ -101,6 +100,9 @@ class TechniqueParser(
}
)

// 4.3: does the technique support generation without directive merge (i.e mutli directive)
val generationMode = nonEmpty((xml \ TECHNIQUE_GENERATION_MODE).text).flatMap(TechniqueGenerationMode.parse).getOrElse(TechniqueGenerationMode.MergeDirectives)

val technique = Technique(
id
, name
Expand All @@ -115,6 +117,7 @@ class TechniqueParser(
, longDescription
, isSystem
, providesExpectedReports
, generationMode
)

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
package com.normation.eventlog

import org.joda.time.DateTime
import org.joda.time.format._
import scala.collection._
import scala.xml._
import java.security.Principal
import com.normation.utils.HashcodeCaching


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ package com.normation.rudder.api

import net.liftweb.common.Box
import net.liftweb.common.Full
import org.joda.time.DateTime
import com.normation.ldap.sdk.LDAPConnectionProvider
import com.normation.rudder.repository.ldap.LDAPEntityMapper
import com.normation.rudder.domain.RudderDit
import com.normation.ldap.sdk.RoLDAPConnection
import com.normation.ldap.sdk.RwLDAPConnection
import com.normation.rudder.services.queries.LDAPFilter
import com.normation.ldap.sdk.BuildFilter
import com.normation.rudder.domain.RudderLDAPConstants
import net.liftweb.common.Loggable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ package com.normation.rudder.batch
import net.liftweb.actor.LiftActor
import net.liftweb.actor.LAPinger
import net.liftweb.common._
import net.liftweb.util.Helpers
import org.joda.time.DateTime
import com.normation.rudder.domain.logger.ApplicationLogger

Expand Down Expand Up @@ -211,4 +210,4 @@ trait AbstractScheduler extends Loggable {
}
}
}
}
}
6 changes: 0 additions & 6 deletions rudder-core/src/main/scala/com/normation/rudder/db/DB.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import com.normation.rudder.domain.policies.Rule
import com.normation.rudder.domain.policies.RuleId
import com.normation.rudder.domain.policies.RuleTarget
import com.normation.rudder.domain.reports.NodeConfigId
import com.normation.rudder.domain.reports.Reports
import com.normation.rudder.reports.execution.{ AgentRun => RudderAgentRun }
import com.normation.rudder.reports.execution.AgentRunId
import com.normation.rudder.repository.GitCommitId
Expand All @@ -59,11 +58,9 @@ import org.joda.time.DateTime

import doobie.imports._
import scalaz._, Scalaz._
import scalaz.concurrent.Task
import com.normation.rudder.db.Doobie._


import doobie.postgres.pgtypes._



Expand Down Expand Up @@ -212,7 +209,6 @@ final object DB {
case class SerializedRules[T](
id : T
, ruleId : String
, serial : Int
, categoryId : Option[String]
, name : String
, shortDescription : Option[String]
Expand Down Expand Up @@ -311,7 +307,6 @@ final object DB {
Rule (
RuleId(rule.ruleId)
, rule.name
, rule.serial
, RuleCategoryId(rule.categoryId.getOrElse("rootRuleCategory")) // this is not really useful as RuleCategory are not really serialized
, ruleTargets.flatMap(x => RuleTarget.unser(x.targetSerialisation)).toSet
, directives.map(x => DirectiveId(x.directiveId)).toSet
Expand All @@ -327,7 +322,6 @@ final object DB {
SerializedRules (
()
, rule.id.value
, rule.serial
, Opt(rule.categoryId.value)
, rule.name
, Opt(rule.shortDescription)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.normation.rudder.services.reports.RunAndConfigInfo
import org.slf4j.LoggerFactory
import doobie.util.log.ExecFailure
import doobie.util.log.ProcessingFailure
import scala.language.implicitConversions

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ object RudderLDAPConstants extends Loggable {

val A_PRIORITY = "directivePriority"
val A_LONG_DESCRIPTION = "longDescription"
val A_SERIAL = "serial"
//val A_SERIAL = "serial" - removed in 4.3 but kept in schema for compatibility reason

val A_NODE_CONFIGURATION_SYSTEM_VARIABLE = "systemVariable"
val A_NODE_CONFIGURATION_TARGET_SYSTEM_VARIABLE = "targetSystemVariable"
Expand Down Expand Up @@ -151,8 +151,6 @@ object RudderLDAPConstants extends Loggable {
val OC_RULE = "rule"
val OC_ACTIVE_TECHNIQUE_LIB_VERSION = "activeTechniqueLibraryVersion"
val OC_ABSTRACT_RULE_WITH_CF3POLICYDRAFT = "abstractDirectiveNodeConfiguration"
val OC_RULE_WITH_CF3POLICYDRAFT = "directiveNodeConfiguration"
val OC_TARGET_RULE_WITH_CF3POLICYDRAFT = "targetDirectiveNodeConfiguration"
val OC_NODE_CONFIGURATION = "nodeConfiguration" //actually a node configuration, not a "rudder server"
val OC_PARAMETER = "parameter"
val OC_PROPERTY = "property"
Expand All @@ -166,7 +164,7 @@ object RudderLDAPConstants extends Loggable {

OC += (OC_RULE,
must = Set(A_RULE_UUID),
may = Set(A_NAME, A_DESCRIPTION, A_LONG_DESCRIPTION, A_IS_ENABLED, A_IS_SYSTEM, A_RULE_TARGET, A_DIRECTIVE_UUID, A_SERIAL, A_RULE_CATEGORY, A_SERIALIZED_TAGS))
may = Set(A_NAME, A_DESCRIPTION, A_LONG_DESCRIPTION, A_IS_ENABLED, A_IS_SYSTEM, A_RULE_TARGET, A_DIRECTIVE_UUID, "serial", A_RULE_CATEGORY, A_SERIALIZED_TAGS))

OC += (OC_RUDDER_NODE,
must = Set(A_NODE_UUID, A_NAME, A_IS_BROKEN, A_IS_SYSTEM),
Expand Down Expand Up @@ -218,12 +216,6 @@ object RudderLDAPConstants extends Loggable {
may = Set(A_DESCRIPTION, A_RULE_TARGET, A_DIRECTIVE_VARIABLES,
A_IS_SYSTEM, A_IS_ENABLED))

OC += (OC_RULE_WITH_CF3POLICYDRAFT, sup = OC(OC_ABSTRACT_RULE_WITH_CF3POLICYDRAFT),
must = Set(A_DIRECTIVE_UUID, A_RULE_UUID, A_PRIORITY, A_SERIAL))

OC += (OC_TARGET_RULE_WITH_CF3POLICYDRAFT, sup = OC(OC_ABSTRACT_RULE_WITH_CF3POLICYDRAFT),
must = Set(A_TARGET_DIRECTIVE_UUID, A_RULE_UUID, A_PRIORITY, A_SERIAL))

OC += (OC_ACTIVE_TECHNIQUE_LIB_VERSION,
may = Set(A_INIT_DATETIME))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ import com.normation.rudder.rule.category.RuleCategoryId

case class RuleId(value:String) extends HashcodeCaching

case class SerialedRuleId(ruleId : RuleId, serial : Int) extends HashcodeCaching

/**
* A rule is a binding between a set of directives
* and some target (group of node, etc) on which applying
Expand All @@ -56,7 +54,6 @@ case class SerialedRuleId(ruleId : RuleId, serial : Int) extends HashcodeCaching
case class Rule(
id : RuleId
, name : String
, serial : Int
, categoryId : RuleCategoryId
//is not mandatory, but if not present, rule is disabled
, targets : Set[RuleTarget] = Set()
Expand All @@ -66,7 +63,7 @@ case class Rule(
, longDescription : String = ""
, isEnabledStatus : Boolean = false
, isSystem : Boolean = false
/**
/*
* Optionally, Rule can have Tags
*/
, tags : Tags = Tags(Set())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,20 @@ import com.normation.utils.HashcodeCaching


import org.joda.time.DateTime
import org.joda.time.Interval
import com.normation.rudder.domain.policies.GlobalPolicyMode
import com.normation.rudder.domain.policies.PolicyModeOverrides.Unoverridable
import com.normation.rudder.domain.policies.PolicyModeOverrides.Always
import com.normation.rudder.domain.policies.PolicyMode.Enforce
import net.liftweb.common.Full
import net.liftweb.common.Failure
import net.liftweb.common.Box
import com.normation.utils.Control.sequence
import com.normation.rudder.reports.ComplianceMode
import com.normation.rudder.reports.AgentRunInterval
import com.normation.rudder.reports.ComplianceModeName
import com.normation.rudder.reports.FullCompliance
import com.normation.rudder.reports.ChangesOnly
import com.normation.rudder.reports.ReportsDisabled
import com.normation.rudder.reports.GlobalComplianceMode
import com.normation.rudder.reports.ResolvedAgentRunInterval
import org.joda.time.Duration
import com.normation.rudder.domain.Constants
import com.sun.xml.internal.txw2.EndDocument

final case class NodeModeConfig(
globalComplianceMode: ComplianceMode
Expand Down Expand Up @@ -110,7 +104,6 @@ final case class NodeExpectedReports(

final case class RuleExpectedReports(
ruleId : RuleId
, serial : Int
, directives: List[DirectiveExpectedReports]
)

Expand Down Expand Up @@ -234,7 +227,6 @@ object ExpectedReportsSerialisation {
~ ("rules" -> (n.ruleExpectedReports.map { r =>
(
("ruleId" -> r.ruleId.value)
~ ("serial" -> r.serial)
~ ("directives" -> (r.directives.map { d =>
(
("directiveId" -> d.directiveId.value)
Expand Down Expand Up @@ -341,18 +333,16 @@ object ExpectedReportsSerialisation {
def rule(json: JValue): Box[RuleExpectedReports] = {
(
(json \ "ruleId" )
, (json \ "serial")
, (json \ "directives")
) match {
case (JString(id), JInt(bigSerial), jsonDirectives) =>
case (JString(id), jsonDirectives) =>
for {
serial <- tryo(bigSerial.toValidInt)
directives <- jsonDirectives match {
case JArray(directives) => sequence(directives)(directive)
case x => Failure(s"Error when parsing the list of directives from expected rule report: '${compactRender(x)}'")
}
} yield {
RuleExpectedReports(RuleId(id), serial, directives.toList)
RuleExpectedReports(RuleId(id), directives.toList)
}
case _ =>
Failure(s"Error when parsing rule expected reports from json: '${compactRender(json)}'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ object AggregatedStatusReport {
final case class RuleNodeStatusReport(
nodeId : NodeId
, ruleId : RuleId
, serial : Int
, agentRunTime : Option[DateTime]
, configId : Option[NodeConfigId]
//only one DirectiveStatusReport by directiveId
Expand All @@ -194,7 +193,7 @@ final case class RuleNodeStatusReport(

override lazy val compliance = ComplianceLevel.sum(directives.map(_._2.compliance) )

override def toString() = s"""[[${nodeId.value}: ${ruleId.value}/${serial}; run: ${agentRunTime.getOrElse("no time")};${configId.map(_.value).getOrElse("no config id")}->${expirationDate}]
override def toString() = s"""[[${nodeId.value}: ${ruleId.value}; run: ${agentRunTime.getOrElse("no time")};${configId.map(_.value).getOrElse("no config id")}->${expirationDate}]
| compliance:${compliance}
| ${directives.values.toSeq.sortBy( _.directiveId.value ).map { x => s"${x}" }.mkString("\n ")}]
|""".stripMargin('|')
Expand All @@ -220,13 +219,13 @@ final case class RuleNodeStatusReport(
}

object RuleNodeStatusReport {
def merge(reports: Iterable[RuleNodeStatusReport]): Map[(NodeId, RuleId, Int, Option[DateTime], Option[NodeConfigId]), RuleNodeStatusReport] = {
reports.groupBy(r => (r.nodeId, r.ruleId, r.serial, r.agentRunTime, r.configId)).map { case (id, reports) =>
def merge(reports: Iterable[RuleNodeStatusReport]): Map[(NodeId, RuleId, Option[DateTime], Option[NodeConfigId]), RuleNodeStatusReport] = {
reports.groupBy(r => (r.nodeId, r.ruleId, r.agentRunTime, r.configId)).map { case (id, reports) =>
val newDirectives = DirectiveStatusReport.merge(reports.flatMap( _.directives.values))

//the merge of two reports expire when the first one expire
val expire = new DateTime( reports.map( _.expirationDate.getMillis).min )
(id, RuleNodeStatusReport(id._1, id._2, id._3, id._4, id._5, newDirectives, expire))
(id, RuleNodeStatusReport(id._1, id._2, id._3, id._4, newDirectives, expire))
}.toMap
}
}
Expand Down Expand Up @@ -470,7 +469,6 @@ object NodeStatusReportSerialization {
("rules" -> (x.reports.map { r =>
(
("ruleId" -> r.ruleId.value)
~ ("serial" -> r.serial)
~ ("compliance" -> r.compliance.pc.toJson)
~ ("numberReports" -> r.compliance.total)
~ ("directives" -> (r.directives.values.map { d =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,6 @@ trait WoRuleRepository {
*/
def updateSystem(rule:Rule, modId: ModificationId, actor:EventActor, reason:Option[String]) : Box[Option[ModifyRuleDiff]]

/**
* Increment the serial of rules with given ID by one.
* Return the new serial value.
* The method fails if no rule has such ID.
*/
def incrementSerial(id:RuleId) : Box[Int]

/**
* Delete the rule with the given ID.
* If no rule with such ID exists, it is an error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,10 @@ class HistorizationJdbcRepository(db: Doobie) extends HistorizationRepository wi
*/
def insertRule(now: DateTime)(r: Rule) = {
(for {
//as of 4.3, serial is always 0 before being totally deleted in future version
pk <- sql"""
insert into rules (ruleid, serial, categoryid, name, shortdescription, longdescription, isenabled, starttime)
values (${r.id}, ${r.serial}, ${r.categoryId}, ${r.name}, ${r.shortDescription}, ${r.longDescription}, ${r.isEnabled}, ${now})
values (${r.id}, 0, ${r.categoryId}, ${r.name}, ${r.shortDescription}, ${r.longDescription}, ${r.isEnabled}, ${now})
""".update.withUniqueGeneratedKeys[Long]("rulepkeyid")
_ <- Update[DB.SerializedRuleDirectives]("""
insert into rulesdirectivesjoin (rulepkeyid, directiveid)
Expand Down
Loading

0 comments on commit 8dac39b

Please sign in to comment.