18
18
use PunktDe \Elastic \Sync \Exception \ConfigurationException ;
19
19
use PunktDe \Elastic \Sync \Exception \SynchronizationException ;
20
20
use PunktDe \Elastic \Sync \Service \ElasticsearchService ;
21
+ use PunktDe \Elastic \Sync \Service \ShellCommandService ;
21
22
22
23
class Synchronizer
23
24
{
@@ -50,6 +51,12 @@ class Synchronizer
50
51
*/
51
52
protected $ elasticSeacrhService ;
52
53
54
+ /**
55
+ * @Flow\Inject
56
+ * @var ShellCommandService
57
+ */
58
+ protected $ shellCommandService ;
59
+
53
60
public function __construct ()
54
61
{
55
62
$ this ->consoleOutput = new ConsoleOutput ();
@@ -65,43 +72,48 @@ public function sync(string $presetName): void
65
72
$ remoteConfiguration = $ this ->configurationService ->getRemoteConfiguration ($ presetName );
66
73
$ remoteInstanceConfiguration = $ this ->presetConfigurationFactory ->getRemoteInstanceConfiguration ($ presetName );
67
74
68
- $ this ->compileAndRunCloneScript ($ remoteConfiguration , $ localConfiguration , $ remoteInstanceConfiguration );
69
- $ this ->createAliases ($ localConfiguration );
75
+ $ sshTunnelPid = $ this ->shellCommandService ->openSshTunnelToRemoteElasticsearchServer ($ remoteConfiguration , $ remoteInstanceConfiguration );
76
+
77
+ try {
78
+ $ this ->compileAndRunCloneScript ($ remoteConfiguration , $ localConfiguration , $ remoteInstanceConfiguration );
79
+ $ this ->createAdditionalAliases ($ localConfiguration );
80
+ } catch (\Exception $ exception ) {
81
+ $ this ->consoleOutput ->output ('<error>%s</error> ' , [$ exception ->getMessage ()]);
82
+ } finally {
83
+ $ this ->shellCommandService ->closeSshTunnelToRemoteElasticsearchServer ($ sshTunnelPid );
84
+ }
70
85
}
71
86
72
87
73
88
/**
74
89
* @param PresetConfiguration $remoteConfiguration
75
90
* @param PresetConfiguration $localConfiguration
76
91
* @param RemoteInstanceConfiguration $remoteInstanceConfiguration
92
+ * @throws SynchronizationException
93
+ * @throws \Neos\Flow\Http\Client\CurlEngineException
94
+ * @throws \Neos\Flow\Http\Exception
77
95
*/
78
96
private function compileAndRunCloneScript (PresetConfiguration $ remoteConfiguration , PresetConfiguration $ localConfiguration , RemoteInstanceConfiguration $ remoteInstanceConfiguration ): void
79
97
{
98
+ $ tunneledRemoteConfiguration = $ remoteConfiguration ->withTunneledConnection ();
80
99
81
- $ indexConfiguration = [];
100
+ $ indexConfigurations = [];
82
101
foreach ($ remoteConfiguration ->getIndices () as $ key => $ index ) {
83
- $ indexConfiguration [$ key ] = [
84
- 'remote ' => $ index ,
85
- 'local ' => $ localConfiguration ->getIndices ()[$ key ]
86
- ];
102
+ $ indexConfigurations [$ key ] = $ this ->checkAndExpandRemoteIndices ($ tunneledRemoteConfiguration , $ index ['indexName ' ]);
87
103
}
88
104
89
- try {
90
- $ view = new StandaloneView ();
91
- $ view ->setTemplatePathAndFilename ('resource://PunktDe.Elastic.Sync/Private/Template/CopyElastic.sh.template ' );
92
- $ view ->assignMultiple ([
93
- 'localConfiguration ' => $ localConfiguration ,
94
- 'remoteConfiguration ' => $ remoteConfiguration ,
95
- 'remoteInstance ' => $ remoteInstanceConfiguration ,
96
- 'indices ' => $ indexConfiguration ,
97
- 'elasticDumpPath ' => $ this ->elasticDumpPath ,
98
- ]);
99
-
100
- $ script = $ view ->render ();
101
- passthru ($ script );
102
- } catch (\Neos \FluidAdaptor \Exception $ exception ) {
103
- $ this ->consoleOutput ->output ('<error>%s</error> ' , [$ exception ->getMessage ()]);
104
- }
105
+ $ view = new StandaloneView ();
106
+ $ view ->setTemplatePathAndFilename ('resource://PunktDe.Elastic.Sync/Private/Template/CopyElastic.sh.template ' );
107
+ $ view ->assignMultiple ([
108
+ 'localConfiguration ' => $ localConfiguration ,
109
+ 'remoteConfiguration ' => $ remoteConfiguration ,
110
+ 'remoteInstance ' => $ remoteInstanceConfiguration ,
111
+ 'indexConfigurations ' => $ indexConfigurations ,
112
+ 'elasticDumpPath ' => $ this ->elasticDumpPath ,
113
+ ]);
114
+
115
+ $ script = $ view ->render ();
116
+ passthru ($ script );
105
117
}
106
118
107
119
/**
@@ -110,7 +122,7 @@ private function compileAndRunCloneScript(PresetConfiguration $remoteConfigurati
110
122
* @throws \Neos\Flow\Http\Client\CurlEngineException
111
123
* @throws \Neos\Flow\Http\Exception
112
124
*/
113
- private function createAliases (PresetConfiguration $ localConfiguration ): void
125
+ private function createAdditionalAliases (PresetConfiguration $ localConfiguration ): void
114
126
{
115
127
$ definedAliases = $ localConfiguration ->getPostCloneConfiguration ('createAliases ' );
116
128
@@ -125,4 +137,23 @@ private function createAliases(PresetConfiguration $localConfiguration): void
125
137
$ this ->consoleOutput ->outputLine ('%s -> %s ' , [$ alias , $ index ]);
126
138
}
127
139
}
140
+
141
+ /**
142
+ * @param PresetConfiguration $remoteConfiguration
143
+ * @param string $indexTarget
144
+ * @return array
145
+ * @throws SynchronizationException
146
+ * @throws \Neos\Flow\Http\Client\CurlEngineException
147
+ * @throws \Neos\Flow\Http\Exception
148
+ */
149
+ private function checkAndExpandRemoteIndices (PresetConfiguration $ remoteConfiguration , string $ indexTarget ): array
150
+ {
151
+ $ indices = $ this ->elasticSeacrhService ->getIndices ($ remoteConfiguration , $ indexTarget );
152
+
153
+ if (empty ($ indices )) {
154
+ throw new SynchronizationException (sprintf ('No index was found with the pattern "%s" on the remote server. ' , $ indices ), 1602999099 );
155
+ }
156
+
157
+ return array_column ($ indices , 'index ' );
158
+ }
128
159
}
0 commit comments