deployer/docs/selector.md

179 lines
4.1 KiB
Markdown
Raw Normal View History

2022-09-07 22:25:12 +02:00
# Selector
Deployer uses the selector to choose hosts. Each host can have a set of labels.
2022-09-09 19:34:05 +02:00
Labels are key-value pairs.
For example, `stage: production` or `role: web`.
2022-09-07 22:25:12 +02:00
You can use labels to select hosts. For example, `dep deploy stage=production`
will deploy to all hosts with `stage: production` label.
Let's define two labels, **type** and **env**, to our hosts:
2022-09-07 22:25:12 +02:00
```php
host('web.example.com')
->setLabels([
'type' => 'web',
'env' => 'prod',
]);
host('db.example.com')
->setLabels([
'type' => 'db',
'env' => 'prod',
]);
```
Now let's define a task to check labels:
```php
task('info', function () {
writeln('type:' . get('labels')['type'] . ' env:' . get('labels')['env']);
});
```
Now we can run this task with a selector:
2022-09-07 22:25:12 +02:00
```bash
$ dep info env=prod
task info
[web.example.com] type:web env:prod
[db.example.com] type:db env:prod
```
As you can see, Deployer will run this task on all hosts with the `env: prod` label.
And if we define only the `type` label, Deployer will run this task on the specified host.
2022-09-07 22:25:12 +02:00
```bash
dep info type=web
task info
[web.example.com] type:web env:prod
```
## Selector syntax
Label syntax is represented by [disjunctive normal form](https://en.wikipedia.org/wiki/Disjunctive_normal_form)
2022-09-09 19:34:05 +02:00
(**OR of ANDs**).
2022-09-07 22:25:12 +02:00
2022-09-09 19:34:05 +02:00
For example, `type=web,env=prod` is a selector of: `type=web` **OR** `env=prod`.
2022-09-07 22:25:12 +02:00
```bash
$ dep info 'type=web,env=prod'
task info
[web.example.com] type:web env:prod
[db.example.com] type:db env:prod
```
As you can see, both hosts are selected (as both of them have the `env: prod` label).
2022-09-07 22:25:12 +02:00
We can use `&` to define **AND**. For example, `type=web & env=prod` is a selector
2022-09-09 19:34:05 +02:00
for hosts with `type: web` **AND** `env: prod` labels.
2022-09-07 22:25:12 +02:00
```bash
$ dep info 'type=web & env=prod'
task info
[web.example.com] type:web env:prod
```
We can also use `!=` to negate a label. For example, `type!=web` is a selector for
all hosts which do not have a `type: web` label.
2022-09-07 22:25:12 +02:00
```bash
$ dep info 'type!=web'
task info
[db.example.com] type:db env:prod
```
:::note
Deployer CLI can take a few selectors as arguments. For example,
`dep info type=web env=prod` is the same as `dep info 'type=web,env=prod'`.
2022-09-07 22:25:12 +02:00
You can install bash autocompletion for Deployer CLI, which will help you to
write selectors. See [installation](installation.md) for more.
:::
Deployer also has a few special selectors:
- `all` - select all hosts
- `alias=...` - select host by alias
If a selector does not contain an `=` sign, Deployer will assume that it is an alias.
2022-09-07 22:25:12 +02:00
For example `dep info web.example.com` is the same as `dep info alias=web.example.com`.
2022-09-07 22:25:12 +02:00
```bash
$ dep info web.example.com
task info
[web.example.com] type:web env:prod
```
2022-09-09 19:34:05 +02:00
```bash
$ dep info 'web.example.com' 'db.example.com'
$ # Same as:
$ dep info 'alias=web.example.com,alias=db.example.com'
````
## Using the select() function
2022-09-07 22:25:12 +02:00
You can use the [select()](api.md#select) function to select hosts by selector in your PHP code.
2022-09-09 19:34:05 +02:00
```php
task('info', function () {
$hosts = select('type=web,env=prod');
foreach ($hosts as $host) {
writeln('type:' . $host->get('labels')['type'] . ' env:' . $host->get('labels')['env']);
}
});
```
Or you can use the [on()](api.md#on) function to run a task on selected hosts.
2022-09-09 19:34:05 +02:00
```php
task('info', function () {
on(select('all'), function () {
writeln('type:' . get('labels')['type'] . ' env:' . get('labels')['env']);
});
});
```
## Task selectors
To restrict a task to run only on selected hosts, you can use the [select()](tasks.md#select) method.
2022-09-09 19:34:05 +02:00
```php
task('info', function () {
// ...
})->select('type=web,env=prod');
```
## Labels in YAML
2022-09-07 22:25:12 +02:00
You can also define labels in a YAML recipe. For example:
2022-09-07 22:25:12 +02:00
```yaml
hosts:
web.example.com:
remote_user: deployer
env:
environment: production
2022-09-07 22:25:12 +02:00
labels:
env: prod
```
But make sure to distinguish between the `env` and `labels.env` keys.
2022-09-07 22:25:12 +02:00
`env` is a configuration key, and `labels.env` is a label.
```php
task('info', function () {
writeln('env:' . get('env')['environment'] . ' labels.env:' . get('labels')['env']);
2022-09-07 22:25:12 +02:00
});
```
Will print:
```bash
$ dep info env=prod
task info
[web.example.com] env:production labels.env:prod
```