-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathfunctions.php
More file actions
1259 lines (1157 loc) · 33.8 KB
/
functions.php
File metadata and controls
1259 lines (1157 loc) · 33.8 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
use Deployer\Exception\Exception;
use Deployer\Exception\GracefulShutdownException;
use Deployer\Exception\RunException;
use Deployer\Exception\TimeoutException;
use Deployer\Exception\WillAskUser;
use Deployer\Host\Host;
use Deployer\Host\Localhost;
use Deployer\Host\Range;
use Deployer\Import\Import;
use Deployer\Ssh\RunParams;
use Deployer\Support\ObjectProxy;
use Deployer\Task\Context;
use Deployer\Task\GroupTask;
use Deployer\Task\Task;
use Deployer\Utility\Httpie;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use function Deployer\Support\array_merge_alternate;
use function Deployer\Support\is_closure;
/**
* Define one or more hosts for deployment.
*
* ```php
* host('example.org');
* host('prod.example.org', 'staging.example.org');
* ```
*
* Inside a task, returns the `Host` instance for the given alias:
* ```php
* task('test', function () {
* $port = host('example.org')->get('port');
* });
* ```
*/
function host(string ...$hostname): Host|ObjectProxy
{
$deployer = Deployer::get();
if (count($hostname) === 1 && $deployer->hosts->has($hostname[0])) {
return $deployer->hosts->get($hostname[0]);
}
$aliases = Range::expand($hostname);
foreach ($aliases as $alias) {
if ($deployer->hosts->has($alias)) {
$host = $deployer->hosts->get($alias);
throw new \InvalidArgumentException("Host \"$host\" already exists.");
}
}
if (count($aliases) === 1) {
$host = new Host($aliases[0]);
$deployer->hosts->set($aliases[0], $host);
return $host;
} else {
$hosts = array_map(function ($hostname) use ($deployer): Host {
$host = new Host($hostname);
$deployer->hosts->set($hostname, $host);
return $host;
}, $aliases);
return new ObjectProxy($hosts);
}
}
/**
* Define a local host. Commands run on the local machine instead of over SSH.
*
* ```php
* localhost('ci'); // Alias and hostname will be "ci".
* ```
*/
function localhost(string ...$hostnames): Localhost|ObjectProxy
{
$deployer = Deployer::get();
$hostnames = Range::expand($hostnames);
if (count($hostnames) <= 1) {
$host = count($hostnames) === 1 ? new Localhost($hostnames[0]) : new Localhost();
$deployer->hosts->set($host->getAlias(), $host);
return $host;
} else {
$hosts = array_map(function ($hostname) use ($deployer): Localhost {
$host = new Localhost($hostname);
$deployer->hosts->set($host->getAlias(), $host);
return $host;
}, $hostnames);
return new ObjectProxy($hosts);
}
}
/**
* Return the host the current task is running on.
*
* ```php
* task('whoami', function () {
* writeln(currentHost()->getAlias());
* });
* ```
*/
function currentHost(): Host
{
return Context::get()->getHost();
}
/**
* Return hosts matching a selector expression.
*
* ```php
* on(select('stage=prod, role=db'), function (Host $host) {
* // Runs on hosts tagged stage=prod AND role=db.
* });
* ```
*
* @return Host[]
*/
function select(string $selector): array
{
return Deployer::get()->selector->select($selector);
}
/**
* Return the hosts the user picked on the command line.
*
* @return Host[]
*/
function selectedHosts(): array
{
$hosts = [];
foreach (get('selected_hosts', []) as $alias) {
$hosts[] = Deployer::get()->hosts->get($alias);
}
return $hosts;
}
/**
* Import another recipe (PHP or MAML).
*
* Built-in `recipe/*` and `contrib/*` paths are already on PHP's include path,
* so they can be imported by relative path. Use `__DIR__` for files inside
* your own project.
*
* ```php
* import('recipe/common.php'); // built-in recipe
* import('contrib/rsync.php'); // contrib recipe
* import(__DIR__ . '/config/hosts.maml'); // local file
* ```
*/
function import(string $file): void
{
Import::import($file);
}
/**
* Set the description for the next task defined with `task()`.
*
* ```php
* desc('Restart php-fpm');
* task('restart', function () {
* run('sudo systemctl restart php-fpm');
* });
* ```
*
* Calling `desc()` with no argument returns the pending description.
*/
function desc(?string $title = null): ?string
{
static $store = null;
if ($title === null) {
return $store;
} else {
return $store = $title;
}
}
/**
* Define a task, or return an already defined one.
*
* Pass a callback to define a single task:
* ```php
* task('deploy:run', function () {
* run('echo deploying');
* });
* ```
*
* Pass an array of task names to define a group task:
* ```php
* task('deploy', ['deploy:update_code', 'deploy:vendors', 'deploy:symlink']);
* ```
*
* Pass only the name to fetch an existing task:
* ```php
* task('deploy')->desc('Run full deploy');
* ```
*
* @param string $name Task name.
* @param callable|array|null $body Callback, list of task names, or null to fetch an existing task.
* @return Task
*/
function task(string $name, callable|array|null $body = null): Task
{
$deployer = Deployer::get();
if (empty($body)) {
return $deployer->tasks->get($name);
}
if (is_callable($body)) {
$task = new Task($name, $body);
} elseif (is_array($body)) {
$task = new GroupTask($name, $body);
} else {
throw new \InvalidArgumentException('Task body should be a function or an array.');
}
if ($deployer->tasks->has($name)) {
// If task already exists, try to replace.
$existingTask = $deployer->tasks->get($name);
if (get_class($existingTask) !== get_class($task)) {
// There is no "up" or "down"casting in PHP.
throw new \Exception('Tried to replace Task \'' . $name . '\' with a GroupTask or vice-versa. This is not supported. If you are sure you want to do that, remove the old task `Deployer::get()->tasks->remove(<taskname>)` and then re-add the task.');
}
if ($existingTask instanceof GroupTask) {
$existingTask->setGroup($body);
} elseif ($existingTask instanceof Task) {
$existingTask->setCallback($body);
}
$task = $existingTask;
} else {
// If task does not exist, add it to the Collection.
$deployer->tasks->set($name, $task);
}
$task->saveSourceLocation();
if (!empty(desc())) {
$task->desc(desc());
desc(''); // Clear title.
}
return $task;
}
/**
* Run a task (or callback) before another task.
*
* ```php
* before('deploy:symlink', 'deploy:cache:warmup');
*
* before('deploy:symlink', function () {
* run('echo about to symlink');
* });
* ```
*
* @param string $task Name of the task to attach the hook to.
* @param string|callable $do Task name or callback to run.
*
* @return ?Task
*/
function before(string $task, string|callable $do): ?Task
{
if (is_closure($do)) {
$newTask = task("before:$task", $do);
before($task, "before:$task");
return $newTask;
}
task($task)->addBefore($do);
return null;
}
/**
* Run a task (or callback) after another task.
*
* ```php
* after('deploy:symlink', 'deploy:cleanup');
*
* after('deploy:failed', function () {
* run('echo something went wrong');
* });
* ```
*
* @param string $task Name of the task to attach the hook to.
* @param string|callable $do Task name or callback to run.
*
* @return ?Task
*/
function after(string $task, string|callable $do): ?Task
{
if (is_closure($do)) {
$newTask = task("after:$task", $do);
after($task, "after:$task");
return $newTask;
}
task($task)->addAfter($do);
return null;
}
/**
* Run a task (or callback) when another task fails.
*
* Calling `fail()` again for the same task replaces the previous handler.
*
* ```php
* fail('deploy', 'deploy:unlock');
*
* fail('deploy', function () {
* run('echo rollback triggered');
* });
* ```
*
* @param string $task Name of the task whose failure triggers `$do`.
* @param string|callable $do Task name or callback to run on failure.
*
* @return ?Task
*/
function fail(string $task, string|callable $do): ?Task
{
if (is_callable($do)) {
$newTask = task("fail:$task", $do);
fail($task, "fail:$task");
return $newTask;
}
$deployer = Deployer::get();
$deployer->fail->set($task, $do);
return null;
}
/**
* Add a CLI option to the `dep` binary.
*
* ```php
* use Symfony\Component\Console\Input\InputOption;
*
* option('tag', null, InputOption::VALUE_REQUIRED, 'Release tag');
*
* task('deploy', function () {
* $tag = input()->getOption('tag');
* });
* ```
*
* @param string $name Option name (long form, no leading dashes).
* @param string|array|null $shortcut Single-letter shortcut, `|`-separated list, or array of shortcuts.
* @param int|null $mode One of the `InputOption::VALUE_*` constants.
* @param string $description Help text shown in `dep --help`.
* @param string|string[]|int|bool|null $default Default value (must be null for `VALUE_NONE`).
*/
function option(string $name, $shortcut = null, ?int $mode = null, string $description = '', $default = null): void
{
Deployer::get()->inputDefinition->addOption(
new InputOption($name, $shortcut, $mode, $description, $default),
);
}
/**
* Change the current working directory.
*
* Both `cd()` and the `cwd:` argument of `run()` change the working directory
* for executed commands. The difference: `cd()` changes it for the rest of the
* current task, while `cwd:` overrides it for a single `run()` call only.
*
* ```php
* set('deploy_path', '~/deployer.org');
*
* task('task1', function () {
* cd('{{deploy_path}}');
*
* run('pwd');
* // output: /home/deployer/deployer.org
*
* run('pwd', cwd: '/usr'); // Override working dir for this run only.
* // output: /usr
*
* run('pwd');
* // output: /home/deployer/deployer.org
* });
* ```
*
* Note that `cd()` only changes the working directory within a single task.
* The next task starts fresh.
*
* ```php
* task('task2', function () {
* run('pwd'); // cd from previous task is not used.
* // output: /home/deployer
* });
*
* task('all', [
* 'task1',
* 'task2',
* ]);
* ```
*/
function cd(string $path): void
{
set('working_path', parse($path));
}
/**
* Switch the user that `run()` uses for subsequent commands.
*
* Returns a closure that restores the previous user when called.
*
* ```php
* $restore = become('deployer');
* run('whoami'); // deployer
* $restore();
* ```
*
* @return \Closure Restores the previous user when invoked.
*/
function become(string $user): \Closure
{
$currentBecome = get('become');
set('become', $user);
return function () use ($currentBecome) {
set('become', $currentBecome);
};
}
/**
* Run a callback inside a working directory, then restore the previous one.
*
* Use this when you need a scoped `cd()` that does not leak to the rest of the task.
*
* ```php
* within('{{release_path}}', function () {
* run('composer install');
* });
* ```
*
* @return mixed Whatever `$callback` returns, or null.
* @throws Exception
*/
function within(string $path, callable $callback): mixed
{
$lastWorkingPath = get('working_path', '');
try {
set('working_path', parse($path));
return $callback();
} finally {
set('working_path', $lastWorkingPath);
}
}
/**
* Run a command on the current remote host and return its trimmed stdout.
*
* ```php
* run('echo hello world');
* run('cd {{deploy_path}} && git status');
* run('curl medv.io', timeout: 5);
*
* $path = run('readlink {{deploy_path}}/current');
* run("echo $path");
* ```
*
* Pass secrets via placeholders (e.g. `%token%`) so they are redacted in logs:
* ```php
* run('curl -u admin:%token% https://api.example', secrets: ['token' => getenv('TOKEN')]);
* ```
*
* Use the `| quote` filter to escape config values as shell arguments:
* ```php
* run('echo {{ message | quote }}');
* run('grep -r {{ pattern | quote }} {{release_path}}');
* ```
*
* To emit a literal `{{`, escape it with a backslash:
* ```php
* run('echo \{{not_replaced}}'); // outputs: {{not_replaced}}
* ```
*
* @param string $command Command to run on the remote host.
* @param string|null $cwd Working directory for this run. Defaults to `{{working_path}}` (set by `cd()`).
* @param int|null $timeout Max runtime in seconds (default: `{{default_timeout}}`, 300; `null` disables).
* @param int|null $idleTimeout Max seconds without output before aborting.
* @param array<string, scalar>|null $secrets Map of `%name%` placeholders to redacted values.
* @param array<string, scalar>|null $env Environment variables: `run('echo $KEY', env: ['KEY' => 'value']);`
* @param bool|null $forceOutput Print command output in real time.
* @param bool|null $nothrow Return output instead of throwing on non-zero exit.
* @throws RunException
* @throws TimeoutException
* @throws WillAskUser
*/
function run(
string $command,
?string $cwd = null,
?array $env = null,
#[\SensitiveParameter]
?array $secrets = null,
?bool $nothrow = false,
?bool $forceOutput = false,
?int $timeout = null,
?int $idleTimeout = null,
): string {
$runParams = new RunParams(
shell: currentHost()->getShell(),
cwd: $cwd ?? (has('working_path') ? get('working_path') : null),
env: array_merge_alternate(get('env', []), $env ?? []),
nothrow: $nothrow,
timeout: $timeout ?? get('default_timeout', 300),
idleTimeout: $idleTimeout,
forceOutput: $forceOutput,
secrets: $secrets,
);
$dotenv = get('dotenv', false);
if (!empty($dotenv)) {
$runParams->dotenv = $dotenv;
}
$run = function (string $command, ?RunParams $params = null) use ($runParams): string {
$params = $params ?? $runParams;
$host = currentHost();
$command = parse($command);
if ($host instanceof Localhost) {
$process = Deployer::get()->processRunner;
$output = $process->run($host, $command, $params);
} else {
$client = Deployer::get()->sshClient;
$output = $client->run($host, $command, $params);
}
return rtrim($output);
};
if (preg_match('/^sudo\b/', $command)) {
try {
return $run($command);
} catch (RunException) {
$askpass = get('sudo_askpass', '/tmp/dep_sudo_pass');
$password = get('sudo_pass', false);
if ($password === false) {
writeln("<fg=green;options=bold>run</> $command");
$password = askHiddenResponse(" [sudo] password for {{remote_user}}: ");
}
$run("echo -e '#!/bin/sh\necho \"\$PASSWORD\"' > $askpass");
$run("chmod a+x $askpass");
$command = preg_replace('/^sudo\b/', 'sudo -A', $command);
$output = $run(" SUDO_ASKPASS=$askpass PASSWORD=%sudo_pass% $command", $runParams->with(
secrets: ['sudo_pass' => quote($password)],
));
$run("rm $askpass");
return $output;
}
} else {
return $run($command);
}
}
/**
* Run a command on the local machine and return its trimmed stdout.
*
* ```php
* $branch = runLocally('git rev-parse --abbrev-ref HEAD');
* runLocally('npm run build', timeout: 600);
* ```
*
* @param string $command Command to run locally.
* @param string|null $cwd Working directory for this run. Defaults to `{{working_path}}`.
* @param int|null $timeout Max runtime in seconds (default 300, `null` disables).
* @param int|null $idleTimeout Max seconds without output before aborting.
* @param array<string, scalar>|null $secrets Map of `%name%` placeholders to redacted values.
* @param array<string, scalar>|null $env Environment variables: `runLocally('echo $KEY', env: ['KEY' => 'value']);`
* @param bool|null $forceOutput Print command output in real time.
* @param bool|null $nothrow Return output instead of throwing on non-zero exit.
* @param string|null $shell Shell to run in. Default `bash -s`.
*
* @throws RunException
* @throws TimeoutException
*/
function runLocally(
string $command,
?string $cwd = null,
?int $timeout = null,
?int $idleTimeout = null,
#[\SensitiveParameter]
?array $secrets = null,
?array $env = null,
?bool $forceOutput = false,
?bool $nothrow = false,
?string $shell = null,
): string {
$runParams = new RunParams(
shell: $shell ?? 'bash -s',
cwd: $cwd,
env: $env,
nothrow: $nothrow,
timeout: $timeout,
idleTimeout: $idleTimeout,
forceOutput: $forceOutput,
secrets: $secrets,
);
$process = Deployer::get()->processRunner;
$command = parse($command);
$output = $process->run(new Localhost(), $command, $runParams);
return rtrim($output);
}
/**
* Return whether a shell test command succeeds on the remote host.
*
* ```php
* if (test('[ -d {{release_path}} ]')) {
* run('rm -rf {{release_path}}');
* }
* ```
*/
function test(string $command): bool
{
$true = '+' . array_rand(array_flip(['accurate', 'appropriate', 'correct', 'legitimate', 'precise', 'right', 'true', 'yes', 'indeed']));
return trim(run("if $command; then echo $true; fi")) === $true;
}
/**
* Return whether a shell test command succeeds on the local machine.
*
* ```php
* if (testLocally('[ -f .env ]')) {
* upload('.env', '{{release_path}}/.env');
* }
* ```
*/
function testLocally(string $command): bool
{
return runLocally("if $command; then echo +true; fi") === '+true';
}
/**
* Run a callback on each given host.
*
* ```php
* on(select('stage=prod, role=db'), function (Host $host) {
* run('mysqldump app > /tmp/backup.sql');
* });
*
* on(host('example.org'), function (Host $host) {
* run('uptime');
* });
*
* on(Deployer::get()->hosts, function (Host $host) {
* run('uname -a');
* });
* ```
*
* @param Host|Host[] $hosts
*/
function on($hosts, callable $callback): void
{
if (!is_array($hosts) && !($hosts instanceof \Traversable)) {
$hosts = [$hosts];
}
foreach ($hosts as $host) {
if ($host instanceof Host) {
$host->config()->load();
Context::push(new Context($host));
try {
$callback($host);
$host->config()->save();
} catch (GracefulShutdownException $e) {
Deployer::get()->logger->renderException($e, $host);
} finally {
Context::pop();
}
} else {
throw new \InvalidArgumentException("Function on can iterate only on Host instances.");
}
}
}
/**
* Run another task by name from inside the current task.
*
* ```php
* task('deploy', function () {
* invoke('deploy:update_code');
* invoke('deploy:symlink');
* });
* ```
*
* @throws Exception
*/
function invoke(string $taskName): void
{
$task = Deployer::get()->tasks->get($taskName);
Deployer::get()->logger->startTask($task);
$task->run(Context::get());
Deployer::get()->logger->endTask($task);
}
/**
* Upload files or directories to the current host via rsync.
*
* To copy the *contents* of a directory, end the source with `/`:
* ```php
* upload('build/', '{{release_path}}/public'); // copies contents of build/
* upload('build', '{{release_path}}/public'); // copies the build/ dir itself
* ```
*
* `$config` keys:
* - `flags`: replaces the default `-azP` flags
* - `options`: extra flags appended to the rsync command
* - `timeout`: process timeout in seconds (`null` = no limit)
* - `progress_bar`: show transfer progress
* - `display_stats`: show rsync statistics
*
* Note: PHP shell-escaping breaks rsync's `--exclude={'a','b'}` brace list.
* Pass each exclude as its own `--exclude=...` option, or use `--exclude-from=<file>`.
*
* @param string|string[] $source
* @param array $config
* @phpstan-param array{flags?: string, options?: array, timeout?: int|null, progress_bar?: bool, display_stats?: bool} $config
*
* @throws RunException
*/
function upload($source, string $destination, array $config = []): void
{
$rsync = Deployer::get()->rsync;
$host = currentHost();
$source = is_array($source) ? array_map('Deployer\parse', $source) : parse($source);
$destination = parse($destination);
if ($host instanceof Localhost) {
$rsync->call($host, $source, $destination, $config);
} else {
$rsync->call($host, $source, "{$host->connectionString()}:$destination", $config);
}
}
/**
* Download a file or directory from the current host via rsync.
*
* ```php
* download('{{deploy_path}}/.dep/database.sql', 'backup/database.sql');
* ```
*
* `$config` accepts the same keys as `upload()`.
*
* @param array $config
*
* @throws RunException
*/
function download(string $source, string $destination, array $config = []): void
{
$rsync = Deployer::get()->rsync;
$host = currentHost();
$source = parse($source);
$destination = parse($destination);
if ($host instanceof Localhost) {
$rsync->call($host, $source, $destination, $config);
} else {
$rsync->call($host, "{$host->connectionString()}:$source", $destination, $config);
}
}
/**
* Print an info message, prefixed with `info` and the host alias.
*
* ```php
* info('Deployed release {{release_name}}');
* ```
*/
function info(string $message): void
{
writeln("<fg=green;options=bold>info</> " . parse($message));
}
/**
* Print a warning message.
*/
function warning(string $message): void
{
$message = "<fg=yellow;options=bold>warning</> <comment>$message</comment>";
if (Context::has()) {
writeln($message);
} else {
Deployer::get()->output->writeln($message);
}
}
/**
* Write a line to the output, prefixed with the current host alias.
*
* The message is parsed for `{{config}}` placeholders before printing.
*/
function writeln(string $message, int $options = 0): void
{
$host = currentHost();
output()->writeln("[$host] " . parse($message), $options);
}
/**
* Replace `{{name}}` placeholders in a string with values from the current config.
*
* ```php
* $current = parse('{{deploy_path}}/current');
* ```
*/
function parse(string $value): string
{
return Context::get()->getConfig()->parse($value);
}
/**
* Set a [global config](https://deployer.org/docs/8.x/basics#global-configurations) value.
*
* ```php
* set('keep_releases', 5);
* set('shared_files', ['.env']);
* ```
*
* @param mixed $value
* @throws Exception
*/
function set(string $name, $value): void
{
if (!Context::has()) {
Deployer::get()->config->set($name, $value);
} else {
Context::get()->getConfig()->set($name, $value);
}
}
/**
* Append values to an array config option.
*
* ```php
* add('shared_files', ['.env']);
* add('shared_dirs', ['storage/logs']);
* ```
*/
function add(string $name, array $array): void
{
if (!Context::has()) {
Deployer::get()->config->add($name, $array);
} else {
Context::get()->getConfig()->add($name, $array);
}
}
/**
* Return a config value, or `$default` if it isn't set.
*
* Callable values are resolved on first access and cached.
*
* ```php
* $branch = get('branch', 'main');
* ```
*
* @param mixed|null $default
*
* @return mixed
*/
function get(string $name, $default = null)
{
if (!Context::has()) {
return Deployer::get()->config->get($name, $default);
} else {
return Context::get()->getConfig()->get($name, $default);
}
}
/**
* Return whether a config option is set.
*/
function has(string $name): bool
{
if (!Context::has()) {
return Deployer::get()->config->has($name);
} else {
return Context::get()->getConfig()->has($name);
}
}
/**
* Prompt the user for a string, on the current host.
*
* Returns `$default` in quiet mode (`-q`).
*
* ```php
* $branch = ask('Branch to deploy?', 'main');
* $tag = ask('Tag?', null, ['v1.0', 'v1.1', 'v1.2']);
* ```
*/
function ask(string $message, ?string $default = null, ?array $autocomplete = null): ?string
{
if (WillAskUser::$noAsk) {
throw new WillAskUser($message);
}
Context::required(__FUNCTION__);
if (output()->isQuiet()) {
return $default;
}
if (Deployer::isWorker()) {
return Deployer::masterCall(currentHost(), __FUNCTION__, ...func_get_args());
}
/** @var QuestionHelper */
$helper = Deployer::get()->getHelper('question');
$tag = currentHost()->getTag();
$message = parse($message);
$message = "[$tag] <question>$message</question> " . (($default === null) ? "" : "(default: $default) ");
$question = new Question($message, $default);
if (!empty($autocomplete)) {
$question->setAutocompleterValues($autocomplete);
}
return $helper->ask(input(), output(), $question);
}
/**
* Prompt the user to pick one or more values from a list.
*
* ```php
* $color = askChoice('Pick a color', ['red', 'green', 'blue'], 0);
* ```
*
* @param mixed $default Key into `$availableChoices`, or null for no default.
* @return mixed Selected value (or array of values when `$multiselect` is true).
* @throws Exception
*/
function askChoice(string $message, array $availableChoices, $default = null, bool $multiselect = false)
{
if (WillAskUser::$noAsk) {
throw new WillAskUser($message);
}
Context::required(__FUNCTION__);
if (empty($availableChoices)) {
throw new \InvalidArgumentException('Available choices should not be empty');
}
if ($default !== null && !array_key_exists($default, $availableChoices)) {
throw new \InvalidArgumentException('Default choice is not available');
}
if (output()->isQuiet()) {
if ($default === null) {
$default = key($availableChoices);
}
return [$default => $availableChoices[$default]];
}
if (Deployer::isWorker()) {
return Deployer::masterCall(currentHost(), __FUNCTION__, ...func_get_args());
}
/** @var QuestionHelper */
$helper = Deployer::get()->getHelper('question');
$tag = currentHost()->getTag();
$message = parse($message);
$message = "[$tag] <question>$message</question> " . (($default === null) ? "" : "(default: $default) ");
$question = new ChoiceQuestion($message, $availableChoices, $default);
$question->setMultiselect($multiselect);
return $helper->ask(input(), output(), $question);
}
/**
* Prompt the user with a yes/no question. Returns `$default` in quiet mode.
*
* ```php
* if (askConfirmation('Drop the database?')) {
* run('mysql -e "DROP DATABASE app"');
* }
* ```
*/
function askConfirmation(string $message, bool $default = false): bool