Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / repository / management / PhabricatorRepositoryManagementClusterizeWorkflow.php
blobc3de833a1f3ac6f60155a9e11f2805610c6ab7c6
1 <?php
3 final class PhabricatorRepositoryManagementClusterizeWorkflow
4 extends PhabricatorRepositoryManagementWorkflow {
6 protected function didConstruct() {
7 $this
8 ->setName('clusterize')
9 ->setExamples('**clusterize** [options] __repository__ ...')
10 ->setSynopsis(
11 pht('Convert existing repositories into cluster repositories.'))
12 ->setArguments(
13 array(
14 array(
15 'name' => 'service',
16 'param' => 'service',
17 'help' => pht(
18 'Cluster repository service in Almanac to move repositories '.
19 'into.'),
21 array(
22 'name' => 'remove-service',
23 'help' => pht('Take repositories out of a cluster.'),
25 array(
26 'name' => 'repositories',
27 'wildcard' => true,
29 ));
32 public function execute(PhutilArgumentParser $args) {
33 $viewer = $this->getViewer();
35 $repositories = $this->loadRepositories($args, 'repositories');
36 if (!$repositories) {
37 throw new PhutilArgumentUsageException(
38 pht('Specify one or more repositories to clusterize.'));
41 $service_name = $args->getArg('service');
42 $remove_service = $args->getArg('remove-service');
44 if ($remove_service && $service_name) {
45 throw new PhutilArgumentUsageException(
46 pht('Specify --service or --remove-service, but not both.'));
49 if (!$service_name && !$remove_service) {
50 throw new PhutilArgumentUsageException(
51 pht('Specify --service or --remove-service.'));
54 if ($remove_service) {
55 $service = null;
56 } else {
57 $service = id(new AlmanacServiceQuery())
58 ->setViewer($viewer)
59 ->withNames(array($service_name))
60 ->withServiceTypes(
61 array(
62 AlmanacClusterRepositoryServiceType::SERVICETYPE,
64 ->needActiveBindings(true)
65 ->executeOne();
66 if (!$service) {
67 throw new PhutilArgumentUsageException(
68 pht(
69 'No repository service "%s" exists.',
70 $service_name));
74 if ($service) {
75 $service_phid = $service->getPHID();
77 $bindings = $service->getActiveBindings();
79 $unique_devices = array();
80 foreach ($bindings as $binding) {
81 $unique_devices[$binding->getDevicePHID()] = $binding->getDevice();
84 if (count($unique_devices) > 1) {
85 $device_names = mpull($unique_devices, 'getName');
87 echo id(new PhutilConsoleBlock())
88 ->addParagraph(
89 pht(
90 'Service "%s" is actively bound to more than one device (%s).',
91 $service_name,
92 implode(', ', $device_names)))
93 ->addParagraph(
94 pht(
95 'If you clusterize a repository onto this service it may be '.
96 'unclear which devices have up-to-date copies of the '.
97 'repository. If so, leader/follower ambiguity will freeze the '.
98 'repository. You may need to manually promote a device to '.
99 'unfreeze it. See "Ambiguous Leaders" in the documentation '.
100 'for discussion.'))
101 ->drawConsoleString();
103 $prompt = pht('Continue anyway?');
104 if (!phutil_console_confirm($prompt)) {
105 throw new PhutilArgumentUsageException(
106 pht('User aborted the workflow.'));
109 } else {
110 $service_phid = null;
113 $content_source = $this->newContentSource();
114 $diffusion_phid = id(new PhabricatorDiffusionApplication())->getPHID();
116 foreach ($repositories as $repository) {
117 $xactions = array();
119 $xactions[] = id(new PhabricatorRepositoryTransaction())
120 ->setTransactionType(
121 PhabricatorRepositoryServiceTransaction::TRANSACTIONTYPE)
122 ->setNewValue($service_phid);
124 id(new PhabricatorRepositoryEditor())
125 ->setActor($viewer)
126 ->setActingAsPHID($diffusion_phid)
127 ->setContentSource($content_source)
128 ->setContinueOnNoEffect(true)
129 ->setContinueOnMissingFields(true)
130 ->applyTransactions($repository, $xactions);
132 if ($service) {
133 echo tsprintf(
134 "%s\n",
135 pht(
136 'Moved repository "%s" to cluster service "%s".',
137 $repository->getDisplayName(),
138 $service->getName()));
139 } else {
140 echo tsprintf(
141 "%s\n",
142 pht(
143 'Removed repository "%s" from cluster service.',
144 $repository->getDisplayName()));
148 return 0;