-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julien Duchesne
committed
Jun 4, 2019
1 parent
567696c
commit 2a8de07
Showing
9 changed files
with
120 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.vscode | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,95 @@ | ||
package sync | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/coveo/credentials-sync/credentials" | ||
"github.com/coveo/credentials-sync/targets" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Configuration struct { | ||
Sources *credentials.SourcesConfiguration | ||
StopOnError bool `mapstructure:"stop_on_error"` | ||
Targets *targets.Configuration | ||
Sources *credentials.SourcesConfiguration | ||
StopOnError bool `mapstructure:"stop_on_error"` | ||
TargetParallelism int `mapstructure:"target_parallelism"` | ||
Targets *targets.Configuration | ||
} | ||
|
||
func NewConfiguration() *Configuration { | ||
return &Configuration{ | ||
StopOnError: false, | ||
StopOnError: false, | ||
TargetParallelism: 4, | ||
} | ||
} | ||
|
||
func (config *Configuration) Sync() { | ||
validTargets := []targets.Target{} | ||
// Start reading credentials | ||
creds, err := config.Sources.Credentials() | ||
if err != nil { | ||
log.Fatalf("Caught an error while fetching credentials") | ||
} | ||
|
||
// Initialize targets | ||
validTargets := []targets.Target{} | ||
allTargets := config.Targets.AllTargets() | ||
initChannel := make(chan targets.Target) | ||
for _, target := range allTargets { | ||
go initTarget(target, initChannel) | ||
go initTarget(target, creds, initChannel, config.StopOnError) | ||
} | ||
for i := 0; i < len(allTargets); i++ { | ||
initTarget := <-initChannel | ||
if initTarget != nil { | ||
validTargets = append(validTargets, initTarget) | ||
} else if config.StopOnError { | ||
return | ||
} | ||
} | ||
|
||
syncChannel := make(chan bool, config.TargetParallelism) | ||
for _, target := range validTargets { | ||
syncChannel <- true | ||
go syncCredentials(target, creds, syncChannel, config.StopOnError) | ||
} | ||
|
||
for i := 0; i < cap(syncChannel); i++ { | ||
syncChannel <- true | ||
} | ||
|
||
} | ||
|
||
func initTarget(target targets.Target, channel chan targets.Target) { | ||
err := target.Initialize([]credentials.Credentials{}) | ||
func initTarget(target targets.Target, creds []credentials.Credentials, channel chan targets.Target, panicOnError bool) { | ||
err := target.Initialize(creds) | ||
if err == nil { | ||
log.Infof("Connected to %s", target.ToString()) | ||
channel <- target | ||
} else { | ||
log.Errorf("Target `%s` has failed initialization. Ignoring it.", target.GetName()) | ||
message := fmt.Sprintf("Target `%s` has failed initialization: %v", target.GetName(), err) | ||
if panicOnError { | ||
log.Fatal(message) | ||
} | ||
log.Warning(message) | ||
channel <- nil | ||
} | ||
} | ||
|
||
func syncCredentials(target targets.Target, credentialsList []credentials.Credentials, channel chan bool, panicOnError bool) { | ||
defer func() { <-channel }() | ||
|
||
credChannel := make(chan bool, 1) | ||
for _, cred := range credentialsList { | ||
credChannel <- true | ||
go func(cred credentials.Credentials) { | ||
defer func() { <-credChannel }() | ||
if err := target.UpdateCredentials(cred); err != nil { | ||
message := fmt.Sprintf("Failed to send credential %s to %s: %v", cred.GetID(), target.GetName(), err) | ||
if panicOnError { | ||
log.Fatal(message) | ||
} | ||
log.Error(message) | ||
} | ||
}(cred) | ||
} | ||
for i := 0; i < cap(credChannel); i++ { | ||
credChannel <- true | ||
} | ||
|
||
log.Infof("Finished sync to %s", target.GetName()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters