|
1 | | -# TagIt |
| 1 | +# TagIt |
| 2 | + |
2 | 3 | [](https://goreportcard.com/report/github.com/ncode/tagit) |
3 | 4 | [](https://opensource.org/licenses/Apache-2.0) |
4 | 5 | [](https://codecov.io/gh/ncode/tagit) |
5 | 6 |
|
| 7 | +TagIt is a tool that updates Consul service registration tags with outputs of a script. It copies the current service registration and appends the output of the script line by line as tags, while keeping the original tags. |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Why TagIt?](#why-tagit) |
| 12 | +- [Installation](#installation) |
| 13 | +- [Usage](#usage) |
| 14 | + - [Run Command](#run-command) |
| 15 | + - [Cleanup Command](#cleanup-command) |
| 16 | + - [Systemd Command](#systemd-command) |
| 17 | +- [How It Works](#how-it-works) |
| 18 | +- [Examples](#examples) |
| 19 | +- [Contributing](#contributing) |
| 20 | +- [License](#license) |
6 | 21 |
|
7 | | -Update consul registration tags with outputs of a script. |
8 | | -It copies the current service registration and appends the output of the script line by line as tags, while keeping the original tags. |
| 22 | +## Why TagIt? |
9 | 23 |
|
10 | | -## Why? |
| 24 | +TagIt addresses a feature that's currently missing from Consul. You can read more about the need for this functionality in [this Consul issue](https://github.com/hashicorp/consul/issues/1048). |
11 | 25 |
|
12 | | -Basically because it's a very useful feature that is missing from consul. Read more about it [here](https://github.com/hashicorp/consul/issues/1048). |
13 | | -A few scenarios where this can be useful: |
| 26 | +Here are some scenarios where TagIt can be useful: |
14 | 27 |
|
15 | | -1. Your databases are under mydb.service.consul, and you would like to ensure that all the writes go to the leader |
16 | | - 1. You run a script that checks the leader and updates the tag |
17 | | -2. You have a service that is not consul aware, but you would like to use consul for service discovery |
18 | | - 1. You run a script that checks the service and updates the tags |
19 | | -3. You have a load or a webserver, and you would like to have tags for all vhosts that are served by this server |
20 | | - 1. You run a script that checks the vhosts and updates the tags |
21 | | -4. Pretty much any services that are not consul aware, but you would like to use consul for service discovery |
22 | | - 1. You run a script that checks the service and updates the tags |
| 28 | +1. **Database Leader Tagging**: Ensure all writes go to the leader by tagging it appropriately. |
| 29 | +2. **Non-Consul-Aware Service Discovery**: Use Consul for service discovery with services that aren't Consul-aware. |
| 30 | +3. **Web Server VHost Tagging**: Tag all vhosts served by a web server or load balancer. |
| 31 | +4. **Generic Service Tagging**: Tag any services for Consul-based service discovery. |
23 | 32 |
|
24 | | -## How to test it? |
| 33 | +## Installation |
| 34 | + |
| 35 | +To install TagIt, use the following commands: |
25 | 36 |
|
26 | 37 | ```bash |
27 | | -$ git clone github.com/ncode/tagit |
28 | | -$ cd configs/development |
29 | | -$ make |
| 38 | +$ git clone https://github.com/ncode/tagit |
| 39 | +$ cd tagit |
| 40 | +$ go build |
30 | 41 | ``` |
31 | 42 |
|
| 43 | +## Usage |
| 44 | + |
| 45 | +TagIt provides three main commands: `run`, `cleanup`, and `systemd`. |
| 46 | + |
| 47 | +### Run Command |
| 48 | + |
| 49 | +The `run` command starts TagIt and continuously updates the tags based on the script output: |
| 50 | + |
| 51 | +```bash |
| 52 | +$ ./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit |
| 53 | +``` |
| 54 | + |
| 55 | +### Cleanup Command |
| 56 | + |
| 57 | +The `cleanup` command removes all tags with the specified prefix from the service: |
| 58 | + |
| 59 | +```bash |
| 60 | +$ ./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit |
| 61 | +``` |
| 62 | + |
| 63 | +### Systemd Command |
| 64 | + |
| 65 | +The `systemd` command generates a systemd service file for TagIt: |
| 66 | + |
| 67 | +```bash |
| 68 | +./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit |
| 69 | +``` |
| 70 | + |
| 71 | +This command will output a systemd service file that you can use to run TagIt as a system service. |
| 72 | + |
| 73 | +## How It Works |
| 74 | + |
| 75 | +TagIt interacts with Consul as follows: |
| 76 | + |
32 | 77 | ```mermaid |
33 | 78 | sequenceDiagram |
34 | 79 | participant tagit |
35 | 80 | participant consul |
36 | 81 | loop execute script on interval |
37 | | - tagit->>consul: Do you have a service with id my-service1? |
38 | | - consul->>tagit: Yes, here it is and that's the current registration |
39 | | - tagit->>consul: Update current registration adding or removing prefixed tags wiht the output of the script |
| 82 | + tagit->>consul: Query service with ID |
| 83 | + consul->>tagit: Return current service registration |
| 84 | + tagit->>tagit: Execute script and process output |
| 85 | + tagit->>consul: Update service registration with new tags |
40 | 86 | end |
41 | 87 | ``` |
42 | 88 |
|
43 | | -## Todo |
| 89 | +## Examples |
| 90 | + |
| 91 | +Here's an example of how to test TagIt: |
| 92 | + |
| 93 | +1. Start a Consul agent in development mode: |
| 94 | + ```bash |
| 95 | + consul agent -dev & |
| 96 | + ``` |
| 97 | + |
| 98 | +2. Register a service with Consul: |
| 99 | + ```bash |
| 100 | + curl --request PUT --data @examples/consul/my-service1.json http://127.0.0.1:8500/v1/agent/service/register |
| 101 | + ``` |
| 102 | + |
| 103 | +3. Run TagIt: |
| 104 | + ```bash |
| 105 | + ./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit |
| 106 | + ``` |
| 107 | + |
| 108 | +4. Generate a systemd service file: |
| 109 | + ```bash |
| 110 | + ./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit > /etc/systemd/system/tagit-my-service1.service |
| 111 | + ``` |
| 112 | + |
| 113 | +5. Clean up the tags: |
| 114 | + ```bash |
| 115 | + ./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit |
| 116 | + ``` |
| 117 | + |
| 118 | +## Contributing |
| 119 | + |
| 120 | +Contributions to TagIt are welcome! Please feel free to submit a Pull Request. |
| 121 | + |
| 122 | +## License |
44 | 123 |
|
45 | | -- [ ] Adds a systemd unit file generator |
| 124 | +TagIt is licensed under the Apache License, Version 2.0. See the [LICENSE](LICENSE) file for details. |
0 commit comments