gitlab-ci.py 5.14 KB
Newer Older
fuyanbin's avatar
fuyanbin committed
1
2
3
4
5
6
7
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import json
import subprocess
import argparse
import urllib.request


OPENAPI_LOGIN_URL = "https://openapi.linkfog.cn/sys/openapi/login"
OPENAPI_PACKAGE_RESULT_CALLBACK_URL = "https://open.dianxinai.com/api/v2/channelPkg/buildQueryResult"


def make_argument_parser():
    parser = argparse.ArgumentParser(prog='deploy', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        'json_argv',
        nargs='?',
        default=None,
        help='json format argv'
    )
    parser.add_argument(
        '--build-directory',
        default='/build',
        help='build directory'
    )
    parser.add_argument(
        '--vendor',
        default='AB',
        help='vendor'
    )
    parser.add_argument(
        '--orgcode',
        default='A03',
        help='organization code'
    )
    parser.add_argument(
        '--channel-id',
        default='CBV9UHAA0320220905143123262',
        help='channel id'
    )
    parser.add_argument(
        '--package-type',
        default='owner',
        help='package type: owner or other'
    )
    parser.add_argument(
        '--signal',
        default='package',
        help='signal: pakage, notify_failure, notify_success'
    )
    parser.add_argument(
        '--pipeline-id',
        default='',
        help='pipeline id'
    )
    parser.add_argument(
        '--project-id',
        default='',
        help='project id'
    )
    parser.add_argument(
        '--url-tag',
        default='',
        help='url tag'
    )
    parser.add_argument(
        '--event',
        default='',
        help='package event'
    )
    return parser


def package(options):
    argv = {}
    if options.json_argv:
        try:
            argv = json.loads(options.json_argv)
        except Exception:
            os._exit(1)

    argv['build_directory'] = argv.get('build_directory', options.build_directory)
    argv['vendor'] = argv.get('vendor', options.vendor)
    argv['orgcode'] = argv.get('orgcode', options.orgcode)
    argv['channel_id'] = argv.get('channel_id', options.channel_id)
    argv['package_type'] = argv.get('package_type', options.package_type)
    argv['project_id'] = argv.get('project_id', options.project_id)
    argv['pipeline_id'] = argv.get('pipeline_id', options.pipeline_id)
    argv['url_tag'] = argv.get('url_tag', options.url_tag)
fuyanbin's avatar
fuyanbin committed
92
93
    if options.event == 'deploy':
        package_script = './pack-deploy-ci.sh'
fuyanbin's avatar
typo    
fuyanbin committed
94
    elif options.event == 'system-part':
fuyanbin's avatar
fuyanbin committed
95
        package_script = './pack-system-part-ci.sh'
fuyanbin's avatar
fuyanbin committed
96
97
    else:
        package_script = './pack-ci.sh'
fuyanbin's avatar
fuyanbin committed
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    subprocess.run([
        package_script,
        '--build-directory', argv['build_directory'],
        '--vendor', argv['vendor'],
        '--orgcode', argv['orgcode'],
        '--channel-id', argv['channel_id'],
        '--package-type', argv['package_type'],
        '--project-id', argv['project_id'],
        '--pipeline-id', argv['pipeline_id'],
        '--url-tag', argv['url_tag'],
    ])


def __login_openapi():
    data = {
        "username": "open_admin",
        "password": "open_admin123"
    }
    encoded_data = json.dumps(data).encode('utf-8')
    req = urllib.request.Request(OPENAPI_LOGIN_URL, data=encoded_data, method='POST')
    req.add_header('Content-Type', 'application/json')
    try:
        with urllib.request.urlopen(req) as response:
            return json.loads(response.read())
    except urllib.error.URLError as e:
        print(e.reason)
        os._exit(3)


def notify_success(pipeline_id, project_id):
    data = {
        "pipelineId": pipeline_id,
        "projectId": project_id,
        "code": 0,
        "msg": "success",
    }
    with open('.result/{}-{}.json'.format(project_id, pipeline_id), 'r') as f:
        data.update(json.loads(f.read()))

    encoded_data = json.dumps(data).encode('utf-8')
    print(encoded_data)
    req = urllib.request.Request(OPENAPI_PACKAGE_RESULT_CALLBACK_URL, data=encoded_data, method='POST')
    req.add_header('Content-Type', 'application/json')
    try:
        with urllib.request.urlopen(req, timeout=10) as response:
            print(response.read())
    except urllib.error.URLError as e:
        print(e.reason)
        os._exit(2)


def notify_failure(pipeline_id, project_id):
    data = {
        "pipelineId": pipeline_id,
        "projectId": project_id,
        "code": 1,
        "msg": "failure"
    }

    encoded_data = json.dumps(data).encode('utf-8')
    print(encoded_data)
    req = urllib.request.Request(OPENAPI_PACKAGE_RESULT_CALLBACK_URL, data=encoded_data, method='POST')
    req.add_header('Content-Type', 'application/json')
    try:
        with urllib.request.urlopen(req, timeout=10) as response:
            print(response.read())
    except urllib.error.URLError as e:
        print(e.reason)
        os._exit(2)


def main():
    parser = make_argument_parser()
    options = parser.parse_args()

    if options.signal == 'package':
        package(options)
    elif options.signal == 'notify_success':
        notify_success(options.pipeline_id, options.project_id)
    elif options.signal == 'notify_failure':
        notify_failure(options.pipeline_id, options.project_id)


if __name__ == '__main__':
    main()