Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rabbitmq_agents
Manage
Activity
Members
Labels
Plan
Issues
15
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
51504d48
Commit
51504d48
authored
1 year ago
by
Bo-Chun Chen
Browse files
Options
Downloads
Patches
Plain Diff
Send message to group member agent
parent
cc8f9504
Loading
Loading
1 merge request
!146
Feat group manager
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
group_manager.py
+82
-0
82 additions, 0 deletions
group_manager.py
with
82 additions
and
0 deletions
group_manager.py
+
82
−
0
View file @
51504d48
#!/usr/bin/env python3
import
argparse
import
grp
import
json
import
os
import
pika
import
pwd
import
rabbit_config
as
rcfg
import
sys
import
uuid
from
rc_rmq
import
RCRMQ
from
rc_util
import
timeout
# Instantiate rabbitmq object
rc_rmq
=
RCRMQ
({
"
exchange
"
:
rcfg
.
Exchange
,
"
exchange_type
"
:
"
topic
"
})
def
user_exists
(
username
):
...
...
@@ -22,8 +32,72 @@ def group_exists(groupname):
return
True
@timeout
(
rcfg
.
Function_timeout
)
def
manage_group
(
op
,
usernames
,
groupname
,
debug
=
False
):
callback_queue
=
rc_rmq
.
bind_queue
(
exclusive
=
True
)
rpc_queue
=
f
"
group_member.
{
op
}
"
corr_id
=
str
(
uuid
.
uuid4
())
status
=
dict
.
fromkeys
(
usernames
,
False
)
response
=
0
def
handler
(
ch
,
method
,
properties
,
body
):
if
debug
:
print
(
"
Message received:
"
)
print
(
body
)
nonlocal
corr_id
nonlocal
status
nonlocal
response
msg
=
json
.
loads
(
body
)
username
=
msg
[
"
username
"
]
if
properties
.
correlation_id
==
corr_id
:
status
[
username
]
=
msg
[
"
success
"
]
response
+=
1
if
not
msg
[
"
success
"
]:
print
(
"
Something
'
s wrong, please try again.
"
)
if
len
(
status
)
==
response
:
rc_rmq
.
stop_consume
()
rc_rmq
.
disconnect
()
if
debug
:
print
(
f
"
Adding user(s)
{
'
,
'
.
join
(
usernames
)
}
to group
{
groupname
}
"
)
print
(
f
"
Callback queue:
{
callback_queue
}
, correlation_id:
{
corr_id
}
"
)
for
user
in
usernames
:
rc_rmq
.
publish_msg
(
{
"
routing_key
"
:
rpc_queue
,
"
props
"
:
pika
.
BasicProperties
(
correlation_id
=
corr_id
,
reply_to
=
callback_queue
),
"
msg
"
:
{
"
groups
"
:
{
f
"
{
op
}
"
:
[
f
"
{
groupname
}
"
]},
"
username
"
:
user
,
},
}
)
rc_rmq
.
start_consume
(
{
"
queue
"
:
callback_queue
,
"
exclusive
"
:
True
,
"
bind
"
:
False
,
"
cb
"
:
handler
,
}
)
if
__name__
==
"
__main__
"
:
parser
=
argparse
.
ArgumentParser
(
description
=
"
Group management script
"
)
parser
.
add_argument
(
"
--debug
"
,
action
=
"
store_true
"
,
help
=
"
Debug mode
"
)
parser
.
add_argument
(
"
-d
"
,
"
--delete
"
,
action
=
"
store_true
"
,
help
=
"
Delete the user(s) from the group
"
,
)
parser
.
add_argument
(
"
-g
"
,
"
--group
"
,
required
=
True
,
help
=
"
The Group to add the user(s)
"
)
...
...
@@ -57,3 +131,11 @@ if __name__ == "__main__":
print
(
"
A user and/or group does not exist.
"
,
file
=
sys
.
stderr
)
print
(
"
Abort.
"
,
file
=
sys
.
stderr
)
exit
(
1
)
elif
exist_users
:
op
=
"
remove
"
if
args
.
delete
else
"
add
"
manage_group
(
op
,
exist_users
,
args
.
group
,
args
.
debug
)
else
:
print
(
"
No user to change
"
,
file
=
sys
.
stderr
)
print
(
"
Abort.
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
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