register 2.66 KB
Newer Older
1
2
3
4
5
6
#!/bin/env python
import argparse
import subprocess
import os
import shlex

7
register_command_template = ("./gitlab-runner register "
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
                             "--non-interactive " 
                             "--locked "
                             "--executor shell "
                             "--config {config_path} "
                             "--url '{site_url}' "
                             "--tls-ca-file {cert_file} "
                             "--name '{hostname}-{project_name}' "
                             "--registration-token {token} " 
                             "--builds-dir {runner_dir}"
                             "--tag-list {user},{hostname} ")
def main():
    default_hostname = os.getenv('HOSTNAME').split('.')[-1]
    default_user = os.getenv('USER')
    default_cert_file_path = os.path.join(os.getenv('PWD'), "dci-gitlab.cines.fr.crt")
    parser = argparse.ArgumentParser(description='Register gitlab runners',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('token', type=str,
                        help='The gitlab specific project token')
    parser.add_argument('project_name', metavar='project-name', type=str,
                        help='the project name')
    parser.add_argument('--hostname', type=str, default=default_hostname,
			help='change hostname')
    parser.add_argument('--user', type=str, default=default_user,
			help='change user')
    parser.add_argument('--site-url', type=str, default='https://dci-gitlab.cines.fr/',
			help='change gitlabs site url')
    parser.add_argument('--cert-file', type=str, default=default_cert_file_path,
			help='use certificate file')
    args = parser.parse_args()

    runner_dir = os.path.join(os.getenv('PWD'), '-'.join([args.hostname, args.project_name]))

    register_command = register_command_template.format(project_name=args.project_name,
                               config_path=os.path.join(runner_dir, "config.toml"),
                               site_url=args.site_url,
                               hostname=args.hostname,
                               token=args.token,
                               cert_file=os.path.abspath(args.cert_file),
                               runner_dir=runner_dir,
                               user=args.user)

    if os.path.isdir(runner_dir):
        print("target dir already exists, registration not possible")
        exit()

    os.mkdir(runner_dir)
    subprocess.check_call(shlex.split(register_command))
    #toml_file = open(os.path.join(runner_dir, "config.toml"), "w")
    #toml_file.write(toml_configuration)
    #toml_file.close
    
    


if __name__ == '__main__':
    main()