Skip to content

Commit

Permalink
Add hook scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
janhybs committed Dec 11, 2018
1 parent ab2f838 commit d173a65
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 18 deletions.
20 changes: 20 additions & 0 deletions bin/cihpc-hook
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
# author: Jan Hybs

import sys


try:
import cihpc.git_tools
except ImportError as e:
print(sys.executable, sys.version)
print('sys.path:')
for i in range(len(sys.path)):
print('%2d) %s' % (i + 1, sys.path[i]))
raise


print('cihpc-hook v%s' % cihpc.__version__, sys.executable, str(sys.version).replace('\n', ' '))
sys.exit(
cihpc.git_tools.main()
)
9 changes: 9 additions & 0 deletions bin/triggers/local-trigger
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# args are following
REPO = $1
COMMIT = $2


$CIHPC_HOME/bin/cihpc -p $REPO --commit $COMMIT

9 changes: 9 additions & 0 deletions bin/triggers/remote-trigger
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# args are following
REPO = $1
COMMIT = $2


#$CIHPC_HOME/bin/cihpc -p $REPO --commit $COMMIT

38 changes: 20 additions & 18 deletions cihpc/git_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

logger = logging.getLogger(__name__)


from cihpc.git_tools.triggers.shell import ShellTrigger
from cihpc.common.processing.daemon import Daemon
from cihpc.common.utils.git.webhooks.push_hook import push_webhook_from_dict
from cihpc.common.utils.timer import Timer
Expand Down Expand Up @@ -123,7 +123,8 @@ class WebhookService(Daemon):
`https://github.com/`**`username`**`/`**`repository`**`/settings/hooks`
\n
Each time a webhook arrives it will run the following (assuming the new commit hash is `foobar`): \n
Each time a webhook arrives it will run the following (assuming the new commit hash is `foobar` and the
project is `foo`): \n
`{example_command}`
## Useful links
Expand Down Expand Up @@ -153,7 +154,7 @@ class WebhookService(Daemon):
</html>
'''.strip()

def __init__(self, name, args_constructor, flask_opts=None, **kwargs):
def __init__(self, name, trigger, flask_opts=None, **kwargs):
"""
Parameters
----------
Expand All @@ -164,9 +165,8 @@ def __init__(self, name, args_constructor, flask_opts=None, **kwargs):
flask_opts: dict or None
additional options for the flask server
args_constructor: cihpc.git_tools.utils.ArgConstructor
an argument constructor instance which will generate
arguments which will be passed to :class:`subprocess.Popen`
trigger: cihpc.git_tools.triggers.AbstractWebhookTrigger
instance with method 'process' accepting webhook payloads
kwargs: **dict
additional arguments passed to the :class:`daemon.DaemonContext` constructor
Expand All @@ -177,11 +177,11 @@ def __init__(self, name, args_constructor, flask_opts=None, **kwargs):
pid_file='/tmp/%s.pid' % name,
**kwargs
)
self.args_constructor = args_constructor
self.trigger = trigger

self.flask_opts = dict(
host='0.0.0.0',
port=5000,
port=5001,
debug=False,
)

Expand Down Expand Up @@ -218,17 +218,14 @@ def _process_payload(self, payload):
logger.info('%s starting' % payload.after)

with Timer(payload.after) as timer:
args = self.args_constructor.construct_arguments(payload.after)

try:
logger.info(str(args))
process = subprocess.Popen(args)
returncode = process.wait()
self.trigger.process(payload)
returncode = self.trigger.wait()

except Exception as e:
# no such binary
returncode = -1
logger.exception('Error while starting the process %s' % str(args))
logger.exception('Error while starting the process %s' % self.trigger)

logger.info('%s took %s [%d]' % (payload.after, timer.pretty_duration, returncode))

Expand All @@ -249,10 +246,10 @@ def index():
try:
import markdown

args = self.args_constructor.construct_arguments('foobar')
args = '$CIHPC_HOME/bin/cihpc -p foo --commit foobar'
return (self._welcome_html % markdown.markdown(self._welcome_message)).format(
self=self,
example_command=' '.join(args)
example_command=args
)
except Exception as e:
print(e)
Expand Down Expand Up @@ -286,11 +283,12 @@ def webhook():
return app


if __name__ == '__main__':
def main():
args = parse_args()
trigger = ShellTrigger()
ws = WebhookService(
'webhook-service',
None,
trigger,
working_directory=os.getcwd(),
flask_opts=dict(
debug=args.debug,
Expand All @@ -299,3 +297,7 @@ def webhook():
)
)
ws.do_action(args.action)


if __name__ == '__main__':
main()
22 changes: 22 additions & 0 deletions cihpc/git_tools/triggers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/python3
# author: Jan Hybs


class AbstractWebhookTrigger(object):

def process(self, payload):
"""
Parameters
----------
payload: cihpc.common.utils.git.webhooks.push_hook.PushWebhook
payload to process
Returns
-------
bool
True on success False otherwise
"""
raise NotImplementedError('Not implemented!')

def wait(self):
raise NotImplementedError('Not implemented!')
36 changes: 36 additions & 0 deletions cihpc/git_tools/triggers/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/python3
# author: Jan Hybs


import logging


logger = logging.getLogger(__name__)

import os
import subprocess

from cihpc.cfg.config import global_configuration
from cihpc.git_tools.triggers import AbstractWebhookTrigger


class ShellTrigger(AbstractWebhookTrigger):

def __init__(self, trigger_path=None):
self.trigger_path = trigger_path
self._process = None

def process(self, payload):
project = payload.repository.name
commit = payload.after

trigger_path = self.trigger_path or os.path.join(global_configuration.cwd, 'triggers', 'local-trigger')
args = ['bash', trigger_path, project, commit]

self._process = subprocess.Popen(args)

def wait(self):
self._process.wait()
logger.info('trigger ended with %d' % self._process.returncode)
return self._process.returncode

0 comments on commit d173a65

Please sign in to comment.