Arguments: fichiers de mot de passe

This commit is contained in:
nyanloutre 2018-11-04 18:22:16 +01:00
parent 1ea5329cd0
commit 31b7171656
1 changed files with 16 additions and 10 deletions

View File

@ -10,14 +10,12 @@ import tarfile
from tempfile import TemporaryDirectory
from multiprocessing import Pool
from gitlab import Gitlab
import argparse
whitelist = ['nyanloutre/site-musique']
secret = open('secret', 'r').readline().splitlines()[0]
gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0]
out_dir = './run'
def build(payload, gl):
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running'})
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running', 'name': 'Python CI'})
print("push from " + payload['user_name'])
print("repo: " + payload['project']['path_with_namespace'])
print("commit: " + payload['checkout_sha'])
@ -30,7 +28,7 @@ def build(payload, gl):
gitlab_repo_files.extractall(path=temp_dir.name)
check_call(['ls', '-lha', repo_dir])
try:
check_call(['nix-build', '-o', out_dir + '/' + payload['project']['path_with_namespace'], repo_dir])
check_call(['nix-build', '-o', args.output + '/' + payload['project']['path_with_namespace'], repo_dir])
except CalledProcessError:
return {'payload': payload, 'gitlab': gl, 'status': 'failed'}
@ -39,24 +37,26 @@ def build(payload, gl):
def build_success(result):
gl = result['gitlab']
payload = result['payload']
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': result['status']})
gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': result['status'], 'name': 'Python CI'})
print("build: " + result['status'])
@view_defaults(
route_name="gitlab_payload", renderer="json", request_method="POST"
)
class PayloadView(object):
class GitlabBuild(object):
def __init__(self, request):
self.request = request
self.payload = self.request.json
self.gl = Gitlab('https://gitlab.com', private_token=gitlab_token)
self.secret = open('secret', 'r').readline().splitlines()[0]
self.gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0]
self.gl = Gitlab('https://gitlab.com', private_token=self.gitlab_token)
@view_config(header="X-Gitlab-Event:Push Hook")
def payload_push(self):
if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == secret:
self.gl.projects.get(self.payload['project']['path_with_namespace']).commits.get(self.payload['checkout_sha']).statuses.create({'state': 'pending'})
if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == self.secret:
self.gl.projects.get(self.payload['project']['path_with_namespace']).commits.get(self.payload['checkout_sha']).statuses.create({'state': 'pending', 'name': 'Python CI'})
pool.apply_async(build, (self.payload, self.gl), callback=build_success)
return "build started"
else:
@ -64,6 +64,12 @@ class PayloadView(object):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='CI server')
parser.add_argument('--output', help='output directory')
parser.add_argument('--secret', help='repo secret file')
parser.add_argument('--gitlab-token', help='gitlab token file')
args = parser.parse_args()
pool = Pool(1)
config = Configurator()