Skip to content

Commit

Permalink
Fixes #18086: Resources changes are not commited in git
Browse files Browse the repository at this point in the history
  • Loading branch information
fanf committed Aug 3, 2020
1 parent 375f8e4 commit e40be98
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,13 @@ class GitTechniqueReader(
allKnownTechniquePaths.find { case (techniquePath, _) =>
path.path.startsWith(techniquePath.path)
}.map { case (n, techniqueId) =>
(techniqueId, changeType)
// a change in the "resources" sub-directory is always an update
val change = if(path.path.contains("/resources/")) {
ChangeType.MODIFY
} else {
changeType
}
(techniqueId, change)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.normation.cfclerk.services._
import com.normation.errors._
import com.normation.eventlog.EventActor
import com.normation.eventlog.ModificationId
import com.normation.rudder.domain.logger.TechniqueReaderLoggerPure
import com.normation.utils.Control
import com.normation.utils.StringUuidGenerator
import net.liftweb.common._
Expand All @@ -56,12 +57,13 @@ class TechniqueRepositoryImpl(
techniqueReader: TechniqueReader
, refLibCallbacks: Seq[TechniquesLibraryUpdateNotification]
, uuidGen : StringUuidGenerator
) extends TechniqueRepository with UpdateTechniqueLibrary with Loggable {
) extends TechniqueRepository with UpdateTechniqueLibrary {

/**
* Callback to call on technique lib update
*/
private[this] var callbacks = refLibCallbacks.sortBy( _.order )
val logger = TechniqueReaderLoggerPure.logEffect


/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ class TechniqueWriter (
updateResources = technique.ressources.collect{case r if r.state != ResourceFileState.Deleted => r.copy(state = ResourceFileState.Untouched) }
techniqueWithResourceUpdated = technique.copy(ressources = updateResources)
json <- writeJson(techniqueWithResourceUpdated)
// Commit technique with old resources, will effectively delete and add resources
commit <- archiver.commitTechnique(technique,json +: metadata +: agentFiles, modId, committer, s"Committing technique ${technique.name}")
commit <- archiver.commitTechnique(technique, modId, committer, s"Committing technique ${technique.name}")
} yield {
techniqueWithResourceUpdated
}
Expand Down Expand Up @@ -718,7 +717,7 @@ class DSCTechniqueWriter(

trait TechniqueArchiver {
def deleteTechnique(techniqueName : String, techniqueVersion : String, category : String, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit]
def commitTechnique(technique : Technique, filesToAdd : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit]
def commitTechnique(technique : Technique, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit]
}

class TechniqueArchiverImpl (
Expand All @@ -730,8 +729,6 @@ class TechniqueArchiverImpl (
, personIdentservice : PersonIdentService
) extends GitArchiverUtils with TechniqueArchiver {

import ResourceFileState._

override val encoding : String = "UTF-8"

def deleteTechnique(techniqueName : String, techniqueVersion : String, category : String, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = {
Expand All @@ -745,27 +742,24 @@ class TechniqueArchiverImpl (
}).chainError(s"error when deleting and committing Technique '${techniqueName}/${techniqueVersion}").unit
}

def commitTechnique(technique : Technique, gitPath : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = {
def commitTechnique(technique : Technique, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = {

val filesToAdd =
gitPath ++
(technique.ressources.filter(f => f.state == New || f.state == Modified)).map(f => s"techniques/${technique.category}/${technique.bundleName.value}/${technique.version.value}/resources/${f.path}")
val techniqueGitPath = s"techniques/${technique.category}/${technique.bundleName.value}/${technique.version.value}"
// additionnal files to delete, for migration purpose.
val filesToDelete =
s"ncf/50_techniques/${technique.bundleName.value}" +:
s"dsc/ncf/50_techniques/${technique.bundleName.value}" +:
technique.ressources.filter(f => f.state == Deleted ).map(f => s"techniques/${technique.category}/${technique.bundleName.value}/${technique.version.value}/resources/${f.path}")
s"dsc/ncf/50_techniques/${technique.bundleName.value}" +: Nil
(for {
ident <- personIdentservice.getPersonIdentOrDefault(commiter.name)
added <- ZIO.foreach(filesToAdd) { f =>
IOResult.effect (gitRepo.git.add.addFilepattern(f).call())
}
// we want to add anything modified under technique path, because it's likelly the reality - if not, bug is
// elsewhere and we need to correct what caused the bad add/delete.
_ <- IOResult.effect (gitRepo.git.add.addFilepattern(techniqueGitPath).call())
_ <- IOResult.effect (gitRepo.git.add.setUpdate(true).addFilepattern(techniqueGitPath).call())
removed <- ZIO.foreach(filesToDelete) { f =>
IOResult.effect(gitRepo.git.rm.addFilepattern(f).call())
}
commit <- IOResult.effect(gitRepo.git.commit.setCommitter(ident).setMessage(msg).call())
} yield {
gitPath
}).chainError(s"error when committing Technique '${technique.name}/${technique.version}").unit
} yield ()).chainError(s"error when committing Technique '${technique.name}/${technique.version}").unit
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.normation.cfclerk.services._
import com.normation.errors._
import com.normation.eventlog.EventActor
import com.normation.eventlog.ModificationId
import com.normation.rudder.domain.logger.ApplicationLoggerPure
import com.normation.rudder.domain.policies.ActiveTechniqueCategory
import com.normation.rudder.domain.policies.ActiveTechniqueCategoryId
import com.normation.rudder.repository.FullActiveTechniqueCategory
Expand Down Expand Up @@ -240,6 +241,8 @@ class TechniqueAcceptationUpdater(

case (TechniqueDeleted(name, versions), Some(activeTechnique)) =>
//if an active technique still exists for that technique, disable it
ApplicationLoggerPure.warn(s"Technique '${name}' (${versions.map(_.toString).mkString(",")})' is deleted" +
s"but an active technique is still present in tree: disabling it.") *>
rwActiveTechniqueRepo.changeStatus(activeTechnique.id, false, modId, actor, reason)

case (TechniqueUpdated(name, mods), Some(activeTechnique)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class TestTechniqueWriter extends Specification with ContentMatchers with Loggab

val expectedPath = "src/test/resources/configuration-repository"
object TestTechniqueArchiver extends TechniqueArchiver {
def commitTechnique(technique : Technique, gitPath : Seq[String], modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = UIO.unit
def commitTechnique(technique : Technique, modId: ModificationId, commiter: EventActor, msg : String) : IOResult[Unit] = UIO.unit
def deleteTechnique(techniqueName: String, techniqueVersion: String, category : String, modId: ModificationId, commiter: EventActor, msg: String): IOResult[Unit] = UIO.unit
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import com.normation.box._
import com.normation.cfclerk.services.GitRepositoryProvider
import com.normation.cfclerk.services.TechniqueRepository
import com.normation.errors.IOResult
import com.normation.errors.effectUioUnit
import com.normation.rudder.ncf.ResourceFile
import com.normation.rudder.ncf.ResourceFileState
import net.liftweb.json.JsonAST.JArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ along with Rudder. If not, see <http://www.gnu.org/licenses/>.
have these information.
-->
<logger name="com.normation.rudder.services.policies.write" level="info" />
<!--
The following logger take care of information about reading techniques in repository
-->
<logger name="techniques.reader" level="info" />


<!--
Expand Down

0 comments on commit e40be98

Please sign in to comment.