implement gitlab-runner verify --delete

This commit is contained in:
Antoine 2020-09-16 19:49:19 +02:00
parent 2ec8fb5411
commit 70fb42a677
Signed by: antoine
GPG Key ID: 098FB66FC0475E70
11 changed files with 148 additions and 116 deletions

View File

@ -1,103 +0,0 @@
# gitlab runner
command used to register new runner
```shell script
$ docker run -d --name gitlab-runner --restart always -v /var/run/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest
$ gitlab-runner register -non-interactive --description "manualy registered gitlab runner" --url "http://172.17.0.1:8080/" --registration-token "3b79eb1f-32f3-4db2-ad1b-6
702e476d839" --tag-list "docker,manual" --executor shell
```
same with curl
```shell script
$ curl --request POST -H 'Content-Type: application/json' "http://localhost:8080/api/v4/runners" -d '{"description":"manualy registered gitlab runner","tag_list":"docker,manual","run_untagged":false,"locked":true,"active":true,"info":{"name":"gitlab-runner","version":"13.3.1","revision":"738bbe5a","platform":"linux","architecture":"amd64","features":{"variables":false,"image":false,"services":false,"artifacts":false,"cache":false,"shared":false,"upload_multiple_artifacts":false,"upload_raw_artifacts":false,"session":false,"terminal":false,"refspecs":false,"masking":false,"proxy":false,"raw_variables":false,"artifacts_exclude":false,"multi_build_steps":false}},"token":"registration_token"}'
```
---
```json
{
"description":"manualy registered gitlab runner",
"tag_list":"docker,manual",
"run_untagged":false,
"locked":true,
"active":true,
"info":{
"name":"gitlab-runner",
"version":"13.3.1",
"revision":"738bbe5a",
"platform":"linux",
"architecture":"amd64",
"features":{
"variables":false,
"image":false,
"services":false,
"artifacts":false,
"cache":false,
"shared":false,
"upload_multiple_artifacts":false,
"upload_raw_artifacts":false,
"session":false,
"terminal":false,
"refspecs":false,
"masking":false,
"proxy":false,
"raw_variables":false,
"artifacts_exclude":false,
"multi_build_steps":false
}
},
"token":"registration_token"
}
```
## http call use when job is run
```
[POST /api/v4/jobs/request HTTP/1.1
Host: 172.17.0.1:8080
User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64)
Content-Length: 510
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip
```
```json
{
"info":{
"name":"gitlab-runner",
"version":"13.3.1",
"revision":"738bbe5a",
"platform":"linux",
"architecture":"amd64",
"executor":"shell",
"shell":"bash",
"features":{
"variables":true,
"image":false,
"services":false,
"artifacts":true,
"cache":true,
"shared":true,
"upload_multiple_artifacts":true,
"upload_raw_artifacts":true,
"session":true,
"terminal":true,
"refspecs":true,
"masking":true,
"proxy":false,
"raw_variables":true,
"artifacts_exclude":true,
"multi_build_steps":true
}
},
"token":"76a79b73-b211-48c6-a3da-6b99fb8b0612"
}
```
...

35
misc/doc-gitlab-runner.md Normal file
View File

@ -0,0 +1,35 @@
# gitlab runner
## start gitlab runner
```shell script
$ docker run -d --name gitlab-runner --restart always -v /var/run/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest
```
## register new runner
```shell script
$ gitlab-runner register -non-interactive --description "manualy registered gitlab runner" --url "http://172.17.0.1:8080/" --registration-token "3b79eb1f-32f3-4db2-ad1b-6
702e476d839" --tag-list "docker,manual" --executor shell
```
with curl
```shell script
$ curl --request POST -H 'Content-Type: application/json' "http://localhost:8080/api/v4/runners" -d '{"description":"manualy registered gitlab runner","tag_list":"docker,manual","run_untagged":false,"locked":true,"active":true,"info":{"name":"gitlab-runner","version":"13.3.1","revision":"738bbe5a","platform":"linux","architecture":"amd64","features":{"variables":false,"image":false,"services":false,"artifacts":false,"cache":false,"shared":false,"upload_multiple_artifacts":false,"upload_raw_artifacts":false,"session":false,"terminal":false,"refspecs":false,"masking":false,"proxy":false,"raw_variables":false,"artifacts_exclude":false,"multi_build_steps":false}},"token":"registration_token"}'
```
## list runners
```shell script
$ gitlab-runner list
```
local listing :
cf => /etc/gitlab-runner/config.toml
## verify which runner is always alive
```shell script
$ gitlab-runner verify --delete
```

View File

@ -0,0 +1,37 @@
[POST /api/v4/jobs/request HTTP/1.1
Host: 172.17.0.1:8080
User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64)
Content-Length: 510
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip]
{
"info":{
"name":"gitlab-runner",
"version":"13.3.1",
"revision":"738bbe5a",
"platform":"linux",
"architecture":"amd64",
"executor":"shell",
"shell":"bash",
"features":{
"variables":true,
"image":false,
"services":false,
"artifacts":true,
"cache":true,
"shared":true,
"upload_multiple_artifacts":true,
"upload_raw_artifacts":true,
"session":true,
"terminal":true,
"refspecs":true,
"masking":true,
"proxy":false,
"raw_variables":true,
"artifacts_exclude":true,
"multi_build_steps":true
}
},
"token":"76a79b73-b211-48c6-a3da-6b99fb8b0612"
}

View File

@ -0,0 +1,40 @@
[POST /api/v4/runners HTTP/1.1
Host: 172.17.0.1:8080
User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64)
Content-Length: 510
Accept: application/json
Content-Type: application/json
Accept-Encoding: gzip]
{
"description": "manualy registered gitlab runner",
"tag_list": "docker,manual",
"run_untagged": false,
"locked": true,
"active": true,
"info": {
"name": "gitlab-runner",
"version": "13.3.1",
"revision": "738bbe5a",
"platform": "linux",
"architecture": "amd64",
"features": {
"variables": false,
"image": false,
"services": false,
"artifacts": false,
"cache": false,
"shared": false,
"upload_multiple_artifacts": false,
"upload_raw_artifacts": false,
"session": false,
"terminal": false,
"refspecs": false,
"masking": false,
"proxy": false,
"raw_variables": false,
"artifacts_exclude": false,
"multi_build_steps": false
}
},
"token": "registration_token"
}

View File

@ -0,0 +1,8 @@
[POST /api/v4/runners/verify HTTP/1.1
Host: 172.17.0.1:8080
User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64)
Content-Length: 48
Content-Type: application/json
Accept-Encoding: gzip]
{"token":"19b280e8-9c56-4566-8234-7a172d717be3"}

View File

@ -11,8 +11,8 @@ import tk.antoine_roux.wiki.annotation.ApiVersion;
import tk.antoine_roux.wiki.configuration.Exception.DeleteRunnerException;
import tk.antoine_roux.wiki.model.Runner;
import tk.antoine_roux.wiki.model.request.AddRunner;
import tk.antoine_roux.wiki.model.request.DeleteRunner;
import tk.antoine_roux.wiki.model.response.RegisterResponse;
import tk.antoine_roux.wiki.model.request.TokenRunner;
import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse;
import tk.antoine_roux.wiki.utilitary.Boolean;
import java.lang.invoke.MethodHandles;
@ -62,22 +62,33 @@ public class ControllerHandlers {
@ResponseBody
@ApiVersion({API_VERSION})
@PostMapping("/runners")
public ResponseEntity<RegisterResponse> addRunner(@RequestBody AddRunner body) {
public ResponseEntity<RegisterRunnerResponse> addRunner(@RequestBody AddRunner body) {
logger.debug("Receive register runner request " + body);
Runner createdRunner = this.runnerRegistrar.addRunner(body);
RegisterResponse response = createdRunner.toRegisterResponse();
RegisterRunnerResponse response = createdRunner.toRegisterResponse();
logger.debug("Response register runner " + response);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
@ApiVersion(API_VERSION)
@DeleteMapping("/runners")
public ResponseEntity<Void> deleteRunner(@RequestBody DeleteRunner body) throws Exception {
public ResponseEntity<Void> deleteRunner(@RequestBody TokenRunner body) throws Exception {
logger.debug("Receive delete runner request " + body);
Boolean.trueOrElseThrow(this.runnerRegistrar.removeRunnerByRegistrationToken(body), DeleteRunnerException::new);
logger.debug("Successfully delete runner ");
return ResponseEntity.noContent().build();
}
@ApiVersion(API_VERSION)
@PostMapping("/runners/verify")
public ResponseEntity<Void> runnerExist(@RequestBody TokenRunner body) {
boolean isValid = this.runnerRegistrar.checkRunner(body);
if (isValid) {
return ResponseEntity.ok().build();
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
}
}

View File

@ -3,7 +3,7 @@ package tk.antoine_roux.wiki;
import org.springframework.stereotype.Service;
import tk.antoine_roux.wiki.model.Runner;
import tk.antoine_roux.wiki.model.request.AddRunner;
import tk.antoine_roux.wiki.model.request.DeleteRunner;
import tk.antoine_roux.wiki.model.request.TokenRunner;
import java.util.ArrayList;
import java.util.List;
@ -34,7 +34,11 @@ public class RunnerRegistrar {
return r;
}
public boolean removeRunnerByRegistrationToken(DeleteRunner r) {
public boolean removeRunnerByRegistrationToken(TokenRunner r) {
return this.runners.removeIf(runner -> runner.authenticationToken.equals(r.token));
}
public boolean checkRunner(TokenRunner body) {
return this.runners.stream().anyMatch(runner -> runner.authenticationToken.equals(body.token));
}
}

View File

@ -1,6 +1,6 @@
package tk.antoine_roux.wiki.model;
import tk.antoine_roux.wiki.model.response.RegisterResponse;
import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse;
import java.util.List;
import java.util.UUID;
@ -15,7 +15,7 @@ public class Runner {
public UUID authenticationToken;
public Integer id;
public RegisterResponse toRegisterResponse() {
return new RegisterResponse(String.valueOf(this.id), this.authenticationToken.toString());
public RegisterRunnerResponse toRegisterResponse() {
return new RegisterRunnerResponse(String.valueOf(this.id), this.authenticationToken.toString());
}
}

View File

@ -5,6 +5,6 @@ import java.util.UUID;
/**
* Model use to remove runner instance
*/
public class DeleteRunner {
public class TokenRunner {
public UUID token;
}

View File

@ -5,13 +5,13 @@ import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Json response object for register endpoint
*/
public class RegisterResponse {
public class RegisterRunnerResponse {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
String id;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
String token;
public RegisterResponse(String id, String token) {
public RegisterRunnerResponse(String id, String token) {
this.id = id;
this.token = token;
}