Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rabbitmq_agents
Manage
Activity
Members
Labels
Plan
Issues
14
Issue boards
Milestones
Wiki
Code
Merge requests
8
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
rc
rabbitmq_agents
Commits
b2df7ebd
Commit
b2df7ebd
authored
5 years ago
by
Bo-Chun Chen
Browse files
Options
Downloads
Patches
Plain Diff
Add logging module
replace print statement with logging
parent
38b86f2e
No related branches found
No related tags found
6 merge requests
!147
Merge previous default branch feat-cod-rmq into main
,
!85
kill nginx process running under user from login node
,
!51
Fix acct create wait
,
!39
WIP:Feat cod rmq
,
!38
WIP: Feat cod rmq
,
!21
Feat git commit agent
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
git_commit.py
+49
-7
49 additions, 7 deletions
git_commit.py
with
49 additions
and
7 deletions
git_commit.py
+
49
−
7
View file @
b2df7ebd
...
...
@@ -4,6 +4,7 @@ import sh
import
sys
import
json
import
argparse
import
logging
from
rc_rmq
import
RCRMQ
task
=
'
git_commit
'
...
...
@@ -18,15 +19,32 @@ cheaha_ldapsearch_ldif = repo_location + '/users/cheaha-openldap-ldapsearch.ldif
slapcat_bin
=
'
/cm/local/apps/openldap/sbin/slapcat
'
slapd_conf
=
'
/cm/local/apps/openldap/etc/slapd.conf
'
# Default logger level
logger_lvl
=
logging
.
WARNING
# Parse arguments
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'
-v
'
,
'
--verbose
'
,
action
=
'
store_true
'
,
help
=
'
verbose output
'
)
parser
.
add_argument
(
'
-n
'
,
'
--dry-run
'
,
action
=
'
store_true
'
,
help
=
'
enable dry run mode
'
)
args
=
parser
.
parse_args
()
git
=
sh
.
git
.
bake
(
'
-C
'
,
repo_location
)
slapcat
=
sh
.
Command
(
slapcat_bin
)
ldapsearch
=
sh
.
Command
(
'
ldapsearch
'
)
if
args
.
verbose
:
logger_lvl
=
logging
.
DEBUG
if
not
args
.
dry_run
:
git
=
sh
.
git
.
bake
(
'
-C
'
,
repo_location
)
slapcat
=
sh
.
Command
(
slapcat_bin
)
ldapsearch
=
sh
.
Command
(
'
ldapsearch
'
)
else
:
logger_lvl
=
logging
.
INFO
git
=
sh
.
echo
.
bake
(
'
git
'
,
'
-C
'
,
repo_location
)
slapcat
=
sh
.
echo
.
bake
(
slapcat_bin
)
ldapsearch
=
sh
.
echo
.
bake
(
'
ldapsearch
'
)
# Logger
logging
.
basicConfig
(
format
=
'
%(asctime)s [%(module)s] - %(message)s
'
,
level
=
logger_lvl
)
logger
=
logging
.
getLogger
(
__name__
)
def
git_commit
(
ch
,
method
,
properties
,
body
):
msg
=
json
.
loads
(
body
)
...
...
@@ -35,32 +53,53 @@ def git_commit(ch, method, properties, body):
success
=
False
branch_name
=
'
issue-
'
+
ticketnum
logger
.
info
(
"
Received: %s
"
,
msg
)
logger
.
debug
(
"
ticketnum: %s
"
,
ticketnum
)
logger
.
debug
(
"
branch_name: %s
"
,
branch_name
)
try
:
logger
.
debug
(
'
git checkout master
'
)
git
.
checkout
(
'
master
'
)
logger
.
debug
(
'
git pull
'
)
git
.
pull
()
logger
.
debug
(
'
git checkout -b %s
'
,
branch_name
)
git
.
checkout
(
'
-b
'
,
branch_name
)
logger
.
debug
(
"
open(%s,
'
w
'
), open(%s,
'
w
'
)
"
,
cheaha_ldif
,
cheaha_ldapsearch_ldif
)
with
open
(
cheaha_ldif
,
'
w
'
)
as
ldif_f
,
\
open
(
cheaha_ldapsearch_ldif
,
'
w
'
)
as
ldapsearch_ldif_f
:
logger
.
debug
(
"
%s -f %s -b
'
dc=cm,dc=cluster
'
> %s
"
,
slapcat_bin
,
slapcat_config
,
cheaha_ldif
)
slapcat
(
'
-f
'
,
slapd_conf
,
'
-b
'
,
"'
dc=cm,dc=cluster
'"
,
_out
=
ldif_f
)
logger
.
debug
(
"
ldapsearch -LLL -x -h ldapserver -b
'
dc=cm,dc=cluster
'
> %s
"
,
ldapsearch_ldif
)
ldapsearch
(
'
-LLL
'
,
'
-x
'
,
'
-h
'
,
'
ldapserver
'
,
'
-b
'
,
"'
dc=cm,dc=cluster
'"
,
_out
=
ldapsearch_ldif_f
)
logger
.
info
(
'
ldif files generated.
'
)
logger
.
debug
(
'
git diff
'
)
git
.
diff
()
logger
.
debug
(
'
git add %s
'
,
cheaha_ldif
)
git
.
add
(
cheaha_ldif
)
logger
.
debug
(
'
git add %s
'
,
cheaha_ldapsearch_ldif
)
git
.
add
(
cheaha_ldapsearch_ldif
)
logger
.
debug
(
"
git commit -m
'
Added new cheaha user: %s
'"
,
branch_name
)
git
.
commit
(
m
=
"
Added new cheaha user:
"
+
username
)
logger
.
debug
(
'
git checkout master
'
)
git
.
checkout
(
'
master
'
)
logger
.
debug
(
'
git merge %s --no-ff --no-edit
'
,
branch_name
)
git
.
merge
(
branch_name
,
'
--no-ff
'
,
'
--no-edit
'
)
logger
.
debug
(
'
git push origin master
'
)
git
.
push
(
'
origin
'
,
'
master
'
)
# merge with gitlab api
logger
.
info
(
'
updated ldif files and committed to git repo
'
)
success
=
True
except
:
e
=
sys
.
exc_info
()[
0
]
print
(
"
[{}]: Error: {}
"
.
format
(
task
,
e
))
except
Exception
as
exception
:
logger
.
error
(
''
,
exc_info
=
True
)
# Send confirm message
logger
.
debug
(
'
rc_rmq.publish_msge()
'
)
rc_rmq
.
publish_msg
({
'
routing_key
'
:
'
confirm.
'
+
username
,
'
msg
'
:
{
...
...
@@ -68,16 +107,19 @@ def git_commit(ch, method, properties, body):
'
success
'
:
success
}
})
logger
.
info
(
'
confirmation sent
'
)
# Acknowledge message
logger
.
debug
(
'
ch.basic_ack()
'
)
ch
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
print
(
"
Start listening to queue:
{}
"
.
format
(
task
)
)
logger
.
info
(
"
Start listening to queue:
%s
"
,
task
)
rc_rmq
.
start_consume
({
'
queue
'
:
task
,
'
routing_key
'
:
"
git.*
"
,
'
cb
'
:
git_commit
})
logger
.
info
(
"
Disconnected
"
)
rc_rmq
.
disconnect
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment