Skip to content
Snippets Groups Projects
Commit fda4d5c4 authored by Bo-Chun Chen's avatar Bo-Chun Chen Committed by Bo-Chun Chen
Browse files

Add git agent

parent dca91e02
No related branches found
No related tags found
6 merge requests!147Merge previous default branch feat-cod-rmq into main,!85kill nginx process running under user from login node,!51Fix acct create wait,!39WIP:Feat cod rmq,!38WIP: Feat cod rmq,!21Feat git commit agent
#!/usr/bin/env python
import sh
import sys
import json
from rc_rmq import RCRMQ
task = 'git_commit'
# Instantiate rabbitmq object
rc_rmq = RCRMQ({'exchange': 'RegUsr', 'exchange_type': 'topic'})
repo_location = '~/git/rc-users'
cheaha_ldif = repo_location + '/users/cheaha-openldap.ldif'
cheaha_ldapsearch_ldif = repo_location + '/users/cheaha-openldap-ldapsearch.ldif'
git = sh.git.bake(repo_location)
slapcat = sh.Command('/cm/local/apps/openldap/sbin/slapcat')
ldapsearch = sh.Command('ldapsearch')
def git_commit(ch, method, properties, body):
msg = json.loads(body)
username = msg['username']
ticketnum = msg['ticketnum']
success = False
try:
branch_name = 'issue-' + ticketnum
git.checkout('master')
git.pull()
git.checkout('-b', branch_name)
with open(cheaha_ldif, 'w') as ldif_f,\
open(cheaha_ldapsearch_ldif, 'w') as ldapsearch_ldif_f:
slapcat('-f', '/cm/local/apps/openldap/etc/slapd.conf', '-b', "'dc=cm,dc=cluster'", _out=ldif_f)
ldapsearch('-LLL', '-x', '-h', 'cheaha-master02', '-b', "'dc=cm,dc=cluster'", _out=ldapsearch_ldif_f)
git.diff()
git.add(cheaha_ldif)
git.add(cheaha_ldapsearch_ldif)
git.commit(m="Added new cheaha user: " + username)
git.push('origin', branch_name)
git.checkout('master')
time.sleep(60)
git.pull()
success = True
except:
e = sys.exc_info()[0]
print("[{}]: Error: {}".format(task, e))
# send confirm message
rc_rmq.publish_msg({
'routing_key': 'confirm.' + username,
'msg': {
'task': task,
'success': success
}
})
print("Start listening to queue: {}".format(task))
rc_rmq.start_consume({
'queue': task,
'routing_key': "git.*",
'cb': git_commit
})
rc_rmq.disconnect()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment