forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorRepositoryManagementLockWorkflow.php
139 lines (114 loc) · 3.79 KB
/
PhabricatorRepositoryManagementLockWorkflow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
final class PhabricatorRepositoryManagementLockWorkflow
extends PhabricatorRepositoryManagementWorkflow {
protected function didConstruct() {
$this
->setName('lock')
->setExamples('**lock** [options] __repository__ ...')
->setSynopsis(
pht(
'Temporarily lock clustered repositories to perform maintenance.'))
->setArguments(
array(
array(
'name' => 'repositories',
'wildcard' => true,
),
));
}
public function execute(PhutilArgumentParser $args) {
$viewer = $this->getViewer();
$repositories = $this->loadRepositories($args, 'repositories');
if (!$repositories) {
throw new PhutilArgumentUsageException(
pht('Specify one or more repositories to lock.'));
}
foreach ($repositories as $repository) {
$display_name = $repository->getDisplayName();
if (!$repository->isHosted()) {
throw new PhutilArgumentUsageException(
pht(
'Unable to lock repository "%s": only hosted repositories may be '.
'locked.',
$display_name));
}
if (!$repository->supportsSynchronization()) {
throw new PhutilArgumentUsageException(
pht(
'Unable to lock repository "%s": only repositories that support '.
'clustering may be locked.',
$display_name));
}
if (!$repository->getAlmanacServicePHID()) {
throw new PhutilArgumentUsageException(
pht(
'Unable to lock repository "%s": only clustered repositories '.
'may be locked.',
$display_name));
}
}
$diffusion_phid = id(new PhabricatorDiffusionApplication())
->getPHID();
$locks = array();
foreach ($repositories as $repository) {
$engine = id(new DiffusionRepositoryClusterEngine())
->setViewer($viewer)
->setActingAsPHID($diffusion_phid)
->setRepository($repository);
$event = $engine->newMaintenanceEvent();
$logs = array();
$logs[] = $engine->newMaintenanceLog();
$locks[] = array(
'repository' => $repository,
'engine' => $engine,
'event' => $event,
'logs' => $logs,
);
}
$display_list = new PhutilConsoleList();
foreach ($repositories as $repository) {
$display_list->addItem(
pht(
'%s %s',
$repository->getMonogram(),
$repository->getName()));
}
echo tsprintf(
"%s\n\n%B\n",
pht('These repositories will be locked:'),
$display_list->drawConsoleString());
echo tsprintf(
"%s\n",
pht(
'While the lock is held: users will be unable to write to this '.
'repository, and you may safely perform working copy maintenance '.
'on this node in another terminal window.'));
$query = pht('Lock repositories and begin maintenance?');
if (!phutil_console_confirm($query)) {
throw new ArcanistUserAbortException();
}
foreach ($locks as $key => $lock) {
$engine = $lock['engine'];
$engine->synchronizeWorkingCopyBeforeWrite();
}
echo tsprintf(
"%s\n",
pht(
'Repositories are now locked. You may begin maintenance in '.
'another terminal window. Keep this process running until '.
'you complete the maintenance, then confirm that you are ready to '.
'release the locks.'));
while (!phutil_console_confirm('Ready to release the locks?')) {
// Wait for the user to confirm that they're ready.
}
foreach ($locks as $key => $lock) {
$lock['event']->saveWithLogs($lock['logs']);
$engine = $lock['engine'];
$engine->synchronizeWorkingCopyAfterWrite();
}
echo tsprintf(
"%s\n",
pht('Done.'));
return 0;
}
}