2021-04-18 18:54:32 +02:00
|
|
|
# Hosts
|
2021-10-11 22:07:35 +02:00
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
To define a new host use the [host()](api.md#host) function. Deployer keeps a list of
|
|
|
|
all defined hosts in the `Deployer::get()->hosts` collection.
|
2021-11-07 21:30:02 +01:00
|
|
|
|
|
|
|
```php
|
|
|
|
host('example.org');
|
|
|
|
```
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
Each host contains it's own configuration key-value pairs. The [host()](api.md#host)
|
2021-11-07 21:30:02 +01:00
|
|
|
call defines two important configs: **alias** and **hostname**.
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
- **hostname** - used when 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');
|
|
|
|
```
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
The hostname will be used for the ssh connection, but the host will be referred
|
|
|
|
by its alias when running Deployer.
|
2021-11-07 21:30:02 +01:00
|
|
|
|
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')
|
2022-02-11 15:11:32 +07:00
|
|
|
->set('hostname', 'example.cloud.google.com')
|
2021-11-07 21:30:02 +01:00
|
|
|
->set('remote_user', 'deployer');
|
|
|
|
```
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
Now Deployer will connect using something like
|
|
|
|
`ssh deployer@example.cloud.google.com` to establishing connection.
|
2021-11-07 21:30:02 +01:00
|
|
|
|
|
|
|
Also, Deployer's `Host` class has special setter methods (for better IDE
|
|
|
|
autocompletion).
|
|
|
|
|
|
|
|
```php
|
|
|
|
host('example.org')
|
2022-02-11 15:11:32 +07:00
|
|
|
->setHostname('example.cloud.google.com')
|
2021-11-07 21:30:02 +01:00
|
|
|
->setRemoteUser('deployer');
|
|
|
|
```
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
## Host config
|
2021-11-07 21:30:02 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `alias`
|
2021-11-07 21:30:02 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
The identifier used to identify a host.
|
|
|
|
You can use actual hostname or something like `prod` or `staging`.
|
|
|
|
|
|
|
|
### `hostname`
|
2021-11-07 21:30:02 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Deployer uses this config for actual ssh connection.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `remote_user`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Deployer uses this config for actual ssh connection. If not specified,
|
|
|
|
Deployer will be using `RemoteUser` from **~/.ssh/config** file, or current
|
|
|
|
OS username.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `port`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Port of remote ssh server to connect to. Default is `22`.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `config_file`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Default is `~/.ssh/config`.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
:::info Config file
|
|
|
|
It is a good practice to keep connection parameters out of `deploy.php` file, as
|
|
|
|
they can change depending on where the deploy is executed from. Only specify
|
|
|
|
`hostname` and `remote_user` and other keep in `~/.ssh/config`:
|
|
|
|
|
|
|
|
```
|
|
|
|
Host *
|
|
|
|
IdentityFile ~/.ssh/id_rsa
|
|
|
|
```
|
|
|
|
:::
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `identity_file`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
|
|
|
For example, `~/.ssh/id_rsa`.
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `forward_agent`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Default is `true`.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `ssh_multiplexing`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Default is `true`.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `shell`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
Default is `bash -ls`.
|
2022-01-30 12:08:03 +01:00
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `deploy_path`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
|
|
|
For example, `~/myapp`.
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `labels`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
|
|
|
Key-value pairs for host selector.
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `ssh_arguments`
|
2022-01-30 12:08:03 +01:00
|
|
|
|
|
|
|
For example, `['-o UserKnownHostsFile=/dev/null']`
|
|
|
|
|
2022-03-22 15:07:23 +01:00
|
|
|
### `ssh_control_path`
|
|
|
|
|
|
|
|
Default is `~/.ssh/%C`.
|
|
|
|
|
|
|
|
If **CI** env is present, default value is `/dev/shm/%C`.
|
2021-11-07 21:30:02 +01:00
|
|
|
|
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
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
If you have a lot of hosts following similar patterns, you can describe them
|
2021-11-07 22:15:17 +01:00
|
|
|
like this rather than listing each hostname:
|
|
|
|
|
|
|
|
```php
|
|
|
|
host('www[01:50].example.org');
|
|
|
|
```
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
For numeric patterns, leading zeros can be included or removed, as desired.
|
2021-11-07 22:15:17 +01:00
|
|
|
Ranges are inclusive.
|
|
|
|
|
|
|
|
You can also define alphabetic ranges:
|
|
|
|
|
|
|
|
```php
|
|
|
|
host('db[a:f].example.org');
|
|
|
|
```
|
|
|
|
|
|
|
|
## Localhost
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
The [localhost()](api.md#localhost) function defines a special local host.
|
|
|
|
Deployer will not connect to this host, but will execute commands locally instead.
|
2021-11-07 22:15:17 +01:00
|
|
|
|
|
|
|
```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
|
|
|
|
|
2021-11-22 03:23:57 -05:00
|
|
|
You can use the [import()](api.md#import) function to keep host definitions in a
|
|
|
|
separate file. For example, *inventory.yaml*.
|
2021-11-07 22:19:46 +01:00
|
|
|
|
|
|
|
```php title="deploy.php"
|
|
|
|
import('inventory.yaml');
|
|
|
|
```
|
|
|
|
|
|
|
|
```yaml title="inventory.yaml"
|
|
|
|
hosts:
|
|
|
|
example.org:
|
|
|
|
remote_user: deployer
|
|
|
|
deployer.org:
|
|
|
|
remote_user: deployer
|
|
|
|
```
|