1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cviebrock\LaravelElasticsearch\Console\Command; |
6
|
|
|
|
7
|
|
|
use Elasticsearch\Client; |
8
|
|
|
use Illuminate\Console\Command; |
9
|
|
|
use Throwable; |
10
|
|
|
|
11
|
|
|
final class AliasSwitchIndexCommand extends Command |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'laravel-elasticsearch:utils:alias-switch-index |
17
|
|
|
{new-index-name : The new index name} |
18
|
|
|
{old-index-name : The old index name} |
19
|
|
|
{alias-name : The alias name}'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Client |
23
|
|
|
*/ |
24
|
|
|
private $client; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
Client $client |
28
|
|
|
) { |
29
|
|
|
$this->client = $client; |
30
|
|
|
|
31
|
|
|
parent::__construct(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function handle(): int |
35
|
|
|
{ |
36
|
|
|
$newIndexName = $this->argument('new-index-name'); |
37
|
|
|
$oldIndexName = $this->argument('old-index-name'); |
38
|
|
|
$aliasName = $this->argument('alias-name'); |
39
|
|
|
|
40
|
|
|
if (!$this->argumentsAreValid( |
41
|
|
|
$newIndexName, |
42
|
|
|
$oldIndexName, |
43
|
|
|
$aliasName |
44
|
|
|
)) { |
45
|
|
|
return self::FAILURE; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (!$this->client->indices()->exists([ |
49
|
|
|
'index' => $newIndexName, |
50
|
|
|
])) { |
51
|
|
|
$this->output->writeln( |
52
|
|
|
sprintf( |
53
|
|
|
'<error>Index %s cannot be linked to alias because doesn\'t exists.</error>', |
54
|
|
|
$newIndexName |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
return self::FAILURE; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$this->client->indices()->putAlias([ |
63
|
|
|
'index' => $newIndexName, |
64
|
|
|
'name' => $aliasName, |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->client->indices()->deleteAlias([ |
68
|
|
|
'index' => $oldIndexName, |
69
|
|
|
'name' => $aliasName, |
70
|
|
|
]); |
71
|
|
|
} catch (Throwable $exception) { |
72
|
|
|
$this->output->writeln( |
73
|
|
|
sprintf( |
74
|
|
|
'<error>Error switching indexes - new index: %s, old index: %s in alias %s, exception message: %s.</error>', |
75
|
|
|
$newIndexName, |
76
|
|
|
$oldIndexName, |
77
|
|
|
$aliasName, |
78
|
|
|
$exception->getMessage() |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return self::FAILURE; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->output->writeln( |
86
|
|
|
sprintf( |
87
|
|
|
'<info>New index %s linked and old index %s removed from alias %s.</info>', |
88
|
|
|
$newIndexName, |
89
|
|
|
$oldIndexName, |
90
|
|
|
$aliasName |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return self::SUCCESS; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function argumentsAreValid($newIndexName, $oldIndexName, $aliasName): bool |
98
|
|
|
{ |
99
|
|
|
if ($newIndexName === null || |
100
|
|
|
!is_string($newIndexName) || |
101
|
|
|
mb_strlen($newIndexName) === 0 |
102
|
|
|
) { |
103
|
|
|
$this->output->writeln( |
104
|
|
|
'<error>Argument new-index-name must be a non empty string.</error>' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($oldIndexName === null || |
111
|
|
|
!is_string($oldIndexName) || |
112
|
|
|
mb_strlen($oldIndexName) === 0 |
113
|
|
|
) { |
114
|
|
|
$this->output->writeln( |
115
|
|
|
'<error>Argument old-index-name must be a non empty string.</error>' |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($aliasName === null || |
122
|
|
|
!is_string($aliasName) || |
123
|
|
|
mb_strlen($aliasName) === 0 |
124
|
|
|
) { |
125
|
|
|
$this->output->writeln( |
126
|
|
|
'<error>Argument alias-name must be a non empty string.</error>' |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|