Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing day 1 #6013

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,15 @@ object GitNodeFactStorageImpl {
* a special operation)
*/
class GitNodeFactStorageImpl(
override val gitRepo: GitRepositoryProvider,
groupOwner: Option[String],
actuallyCommit: Boolean
) extends NodeFactStorage with GitItemRepository with SerializeFacts[(NodeId, InventoryStatus), NodeFact] {
gitRepo: GitRepositoryProvider,
groupOwner: Option[String],
actuallyCommit: Boolean
) extends NodeFactStorage with SerializeFacts[(NodeId, InventoryStatus), NodeFact] {

private val relativePath = "nodes"

private val gitItemRepository: GitItemRepository = new GitItemRepository(gitRepo, relativePath)

override val relativePath = "nodes"
override val entity: String = "node"
override val fileFormat: String = "10"
val committer = new PersonIdent("rudder-fact", "email not set")
Expand Down Expand Up @@ -489,9 +492,9 @@ class GitNodeFactStorageImpl(
case Some(go) => IOResult.attempt(file.setGroup(go))
}
_ <- ZIO.when(actuallyCommit) {
commitAddFile(
gitItemRepository.commitAddFile(
committer,
toGitPath(file.toJava),
gitItemRepository.toGitPath(file.toJava),
s"Save inventory facts for ${merged.rudderSettings.status.name} node '${merged.fqdn}' (${merged.id.value})"
)
}
Expand Down Expand Up @@ -519,7 +522,11 @@ class GitNodeFactStorageImpl(
}
def delete(file: File) = {
(if (actuallyCommit) {
commitRmFile(committer, toGitPath(file.toJava), s"Updating facts for node '${nodeId.value}': deleted")
gitItemRepository.commitRmFile(
committer,
gitItemRepository.toGitPath(file.toJava),
s"Updating facts for node '${nodeId.value}': deleted"
)
} else {
IOResult.attempt(file.delete())
}).flatMap(_ => fileToNode(file)(attrs).map(Some(_)).catchAll(_ => None.succeed)).map {
Expand Down Expand Up @@ -553,10 +560,10 @@ class GitNodeFactStorageImpl(
// we need to overwrite
IOResult.attempt(fromFile.moveTo(toFile)(File.CopyOptions(overwrite = true))) *>
ZIO.when(actuallyCommit) {
commitMvDirectory(
gitItemRepository.commitMvDirectory(
committer,
toGitPath(fromFile.toJava),
toGitPath(toFile.toJava),
gitItemRepository.toGitPath(fromFile.toJava),
gitItemRepository.toGitPath(toFile.toJava),
s"Updating facts for node '${nodeId.value}' to status: ${to.name}"
)
} *> fileToNode(toFile)(SelectFacts.none).map(Some(_)).catchAll(_ => None.succeed).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ import zio.*
/**
* Utility trait that factor out file commits.
*/
trait GitItemRepository {
class GitItemRepository(
// the underlying git repository where items are saved
gitRepo: GitRepositoryProvider,
// relative path from git root directory where items for that implementation
// are saved. For example, rules are saved in `/var/rudder/config-repo/rules`,
// where `/var/rudder/config-repos` is the root directory provided by gitRepos,
// the relativePath is `rules`
relativePath: String
) {

// the underlying git repository where items are saved
def gitRepo: GitRepositoryProvider
// relative path from git root directory where items for that implementation
// are saved. For example, rules are saved in `/var/rudder/config-repo/rules`,
// where `/var/rudder/config-repos` is the root directory provided by gitRepos,
// the relativePath is `rules`
def relativePath: String

lazy val getItemDirectory: File = {
final lazy val getItemDirectory: File = {

/**
* Create directory given in argument if does not exist, checking
Expand Down Expand Up @@ -109,13 +109,13 @@ trait GitItemRepository {

// better.files.File.pathAsString is normalized without an ending slash, an Git path are relative to "/"
// *without* the leading slash.
def toGitPath(fsPath: File): String = fsPath.getPath.replace(gitRepo.rootDirectory.pathAsString + "/", "")
final def toGitPath(fsPath: File): String = fsPath.getPath.replace(gitRepo.rootDirectory.pathAsString + "/", "")

/**
* Files in gitPath are added.
* commitMessage is used for the message of the commit.
*/
def commitAddFile(commiter: PersonIdent, gitPath: String, commitMessage: String): IOResult[GitCommitId] = {
final def commitAddFile(commiter: PersonIdent, gitPath: String, commitMessage: String): IOResult[GitCommitId] = {
gitRepo.semaphore.withPermit(
for {
_ <- GitArchiveLoggerPure.debug(s"Add file '${gitPath}' from configuration repository")
Expand All @@ -140,7 +140,7 @@ trait GitItemRepository {
* Files in gitPath are removed.
* commitMessage is used for the message of the commit.
*/
def commitRmFile(commiter: PersonIdent, gitPath: String, commitMessage: String): IOResult[GitCommitId] = {
final def commitRmFile(commiter: PersonIdent, gitPath: String, commitMessage: String): IOResult[GitCommitId] = {
gitRepo.semaphore.withPermit(
for {
_ <- GitArchiveLoggerPure.debug(s"remove file '${gitPath}' from configuration repository")
Expand All @@ -167,7 +167,7 @@ trait GitItemRepository {
* 'gitRepo.git added' (with and without the 'update' mode).
* commitMessage is used for the message of the commit.
*/
def commitMvDirectory(
final def commitMvDirectory(
commiter: PersonIdent,
oldGitPath: String,
newGitPath: String,
Expand Down Expand Up @@ -198,26 +198,35 @@ trait GitItemRepository {

}

object GitItemRepository {}

/*
* An extension of simple GitItemRepositoty that in addition knows how to link commitId and modId together.
* Used for all configuration objects, but not for facts.
*/
trait GitConfigItemRepository extends GitItemRepository {
class GitConfigItemRepository(
gitRepo: GitRepositoryProvider,
relativePath: String,
gitModificationRepository: GitModificationRepository
) {

def gitModificationRepository: GitModificationRepository
private val gitItemRepository: GitItemRepository = new GitItemRepository(gitRepo, relativePath)

final def toGitPath(fsPath: File): String = gitItemRepository.toGitPath(fsPath)
final def getItemDirectory: File = gitItemRepository.getItemDirectory

/**
* Files in gitPath are added.
* commitMessage is used for the message of the commit.
*/
def commitAddFileWithModId(
final def commitAddFileWithModId(
modId: ModificationId,
commiter: PersonIdent,
gitPath: String,
commitMessage: String
): IOResult[GitCommitId] = {
for {
commit <- commitAddFile(commiter, gitPath, commitMessage)
commit <- gitItemRepository.commitAddFile(commiter, gitPath, commitMessage)
mod <- gitModificationRepository.addCommit(commit, modId)
} yield {
commit
Expand All @@ -228,14 +237,14 @@ trait GitConfigItemRepository extends GitItemRepository {
* Files in gitPath are removed.
* commitMessage is used for the message of the commit.
*/
def commitRmFileWithModId(
final def commitRmFileWithModId(
modId: ModificationId,
commiter: PersonIdent,
gitPath: String,
commitMessage: String
): IOResult[GitCommitId] = {
for {
commit <- commitRmFile(commiter, gitPath, commitMessage)
commit <- gitItemRepository.commitRmFile(commiter, gitPath, commitMessage)
mod <- gitModificationRepository.addCommit(commit, modId)
} yield {
commit
Expand All @@ -249,15 +258,15 @@ trait GitConfigItemRepository extends GitItemRepository {
* 'gitRepo.git added' (with and without the 'update' mode).
* commitMessage is used for the message of the commit.
*/
def commitMvDirectoryWithModId(
final def commitMvDirectoryWithModId(
modId: ModificationId,
commiter: PersonIdent,
oldGitPath: String,
newGitPath: String,
commitMessage: String
): IOResult[GitCommitId] = {
for {
commit <- commitMvDirectory(commiter, oldGitPath, newGitPath, commitMessage)
commit <- gitItemRepository.commitMvDirectory(commiter, oldGitPath, newGitPath, commitMessage)
mod <- gitModificationRepository.addCommit(commit, modId)
} yield {
commit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,25 @@ trait EditorTechniqueReader {
}

class EditorTechniqueReaderImpl(
uuidGen: StringUuidGenerator,
personIdentService: PersonIdentService,
override val gitRepo: GitRepositoryProvider,
override val xmlPrettyPrinter: RudderPrettyPrinter,
override val gitModificationRepository: GitModificationRepository,
override val encoding: String,
override val groupOwner: String,
yamlTechniqueSerializer: YamlTechniqueSerializer,
parameterTypeService: ParameterTypeService,
ruddercCmd: String,
methodsSystemLib: String,
methodsLocalLib: String
) extends EditorTechniqueReader with GitConfigItemRepository with XmlArchiverUtils {
override val relativePath: String = "ncf"
uuidGen: StringUuidGenerator,
personIdentService: PersonIdentService,
gitRepo: GitRepositoryProvider,
override val xmlPrettyPrinter: RudderPrettyPrinter,
gitModificationRepository: GitModificationRepository,
override val encoding: String,
override val groupOwner: String,
yamlTechniqueSerializer: YamlTechniqueSerializer,
parameterTypeService: ParameterTypeService,
ruddercCmd: String,
methodsSystemLib: String,
methodsLocalLib: String
) extends EditorTechniqueReader with XmlArchiverUtils {

private val relativePath: String = "ncf"

private val gitConfigItemRepository: GitConfigItemRepository =
new GitConfigItemRepository(gitRepo, relativePath, gitModificationRepository)

val configuration_repository = gitRepo.rootDirectory
val ncfRootDir: File = configuration_repository / relativePath
val methodsFile: File = ncfRootDir / "generic_methods.json"
Expand Down Expand Up @@ -165,7 +170,12 @@ class EditorTechniqueReaderImpl(
// commit file
modId = ModificationId(uuidGen.newUuid)
ident <- personIdentService.getPersonIdentOrDefault(RudderEventActor.name)
_ <- commitAddFileWithModId(modId, ident, toGitPath(methodsFile.toJava), "Saving updated generic methods definition")
_ <- gitConfigItemRepository.commitAddFileWithModId(
modId = modId,
commiter = ident,
gitPath = gitConfigItemRepository.toGitPath(methodsFile.toJava),
commitMessage = "Saving updated generic methods definition"
)
} yield {
res
}
Expand Down
Loading