deployer/docs/hosts.md

143 lines
3.3 KiB
Markdown
Raw Normal View History

2021-04-18 18:54:32 +02:00
# Hosts
2021-10-11 22:07:35 +02:00
2021-11-07 21:30:02 +01:00
To define a new host use [host()](api.md#host) function. Deployer keeps list of
all defined tasks in the `Deployer::get()->hosts` collection.
```php
host('example.org');
```
Each host contains own configuration key-value pairs. The [host()](api.md#host)
call defines two important configs: **alias** and **hostname**.
- **hostname** - used then connecting to remote host.
2021-11-07 21:56:43 +01:00
- **alias** - used as a key in `Deployer::get()->hosts` collection.
2021-11-07 21:30:02 +01:00
```php
task('test', function () {
writeln('The {{alias}} is {{hostname}}');
});
```
2021-11-10 23:20:32 +01:00
```
2021-11-07 21:30:02 +01:00
$ dep test
[example.org] The example.org is example.org
```
We can override hostname via `set()` method:
```php
host('example.org')
->set('hostname', 'example.cloud.google.com');
```
Now new hostname will be used for ssh connect, and host will be referred in
Deployer via the alias.
2021-11-10 23:20:32 +01:00
```
2021-11-07 21:30:02 +01:00
$ dep test
[example.org] The example.org is example.cloud.google.com
```
Another important ssh connection parameter is `remote_user`.
```php
host('example.org')
->set('hostname', 'example.cloud.google.com');
->set('remote_user', 'deployer');
```
Now Deployer will be using something
like `ssh deployer@example.cloud.google.com`
for establishing connection.
Also, Deployer's `Host` class has special setter methods (for better IDE
autocompletion).
```php
host('example.org')
->setHostname('example.cloud.google.com');
->setRemoteUser('deployer');
```
:::info Config file
It is a good practice to keep connection parameters out of `deploy.php` file, as
they can change depending on where are deploy executed. Only specify `hostname`
and `remote_user` and other keep in `~/.ssh/config`:
```
Host *
IdentityFile ~/.ssh/id_rsa
```
:::
## Host config
|Method|Value|
|------|-----|
|`setHostname` | The `hostname` |
|`setRemoteUser` | The `remote_user` |
|`setPort` | The `port` |
|`setConfigFile` | For example, `~/.ssh/config`. |
|`setIdentityFile` | For example, `~/.ssh/id_rsa`. |
|`setForwardAgent` | Default: `true`. |
|`setSshMultiplexing` | Default: `true`. |
|`setShell` | Default: `bash -ls`. |
|`setDeployPath` | For example, `~/myapp`. |
|`setLabels` | Key-value pairs for host selector. |
|`setSshArguments` | For example, `['-o UserKnownHostsFile=/dev/null']` |
2021-11-07 22:15:17 +01:00
## Multiple hosts
You can pass multiple hosts to the host function:
```php
host('example.org', 'deployer.org', ...)
->setRemoteUser('anton');
```
## Host ranges
If you have a lot of hosts following similar patterns, you can describe them
like this rather than listing each hostname:
```php
host('www[01:50].example.org');
```
For numeric patterns, leading zeros can be included or removed, as desired.
Ranges are inclusive.
You can also define alphabetic ranges:
```php
host('db[a:f].example.org');
```
## Localhost
A special function [localhost()](api.md#localhost) defines a special local host.
Deployer will not connect to this host and will execute commands locally.
```php
localhost(); // Alias and hostname will be "localhost".
localhost('ci'); // Alias is "ci", hostname is "localhost".
```
2021-11-07 22:19:46 +01:00
## YAML Inventory
You can use [import()](api.md#import) function to keep host separately in, for
example, *inventory.yaml* file.
```php title="deploy.php"
import('inventory.yaml');
```
```yaml title="inventory.yaml"
hosts:
example.org:
remote_user: deployer
deployer.org:
remote_user: deployer
```