Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
rabbitmq_agents
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
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
Bo-Chun Chen
rabbitmq_agents
Commits
c606ea5f
Commit
c606ea5f
authored
5 years ago
by
Cloud User
Browse files
Options
Downloads
Patches
Plain Diff
Adding dev versions of other agents for testing
parent
da7661b7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
flask_producer.py
+60
-0
60 additions, 0 deletions
flask_producer.py
ohpc_account_create.py
+62
-0
62 additions, 0 deletions
ohpc_account_create.py
ood_account_create.py
+64
-0
64 additions, 0 deletions
ood_account_create.py
with
186 additions
and
0 deletions
flask_producer.py
0 → 100644
+
60
−
0
View file @
c606ea5f
import
pika
import
sys
import
socket
import
json
import
rabbit_config
as
rcfg
if
len
(
sys
.
argv
)
<
3
:
sys
.
stderr
.
write
(
"
Usage: {} TAG USERNAME
"
.
format
(
sys
.
argv
[
0
]))
exit
(
1
)
node
=
sys
.
argv
[
1
]
user_name
=
sys
.
argv
[
2
]
message
=
{
"
username
"
:
user_name
,
"
fullname
"
:
"
Full Name
"
,
"
reason
"
:
"
Reason1, Reason2.
"
}
hostname
=
socket
.
gethostname
().
split
(
"
.
"
,
1
)[
0
]
connect_host
=
rcfg
.
Server
if
hostname
!=
rcfg
.
Server
else
"
localhost
"
# Set up credentials to connect to RabbitMQ server
credentials
=
pika
.
PlainCredentials
(
rcfg
.
User
,
rcfg
.
Password
)
parameters
=
pika
.
ConnectionParameters
(
connect_host
,
rcfg
.
Port
,
rcfg
.
VHost
,
credentials
)
# Establish connection to RabbitMQ server
connection
=
pika
.
BlockingConnection
(
parameters
)
channel
=
connection
.
channel
()
channel
.
exchange_declare
(
exchange
=
rcfg
.
Exchange
,
exchange_type
=
'
direct
'
)
channel
.
basic_publish
(
exchange
=
rcfg
.
Exchange
,
routing_key
=
node
,
body
=
json
.
dumps
(
message
))
print
(
"
[x] Sent {}: {}
"
.
format
(
node
,
json
.
dumps
(
message
)))
# creates a named queue
result
=
channel
.
queue_declare
(
queue
=
user_name
,
exclusive
=
False
)
# bind the queue with exchange
channel
.
queue_bind
(
exchange
=
rcfg
.
Exchange
,
queue
=
user_name
,
routing_key
=
user_name
)
def
work
(
ch
,
method
,
properties
,
body
):
msg
=
json
.
loads
(
body
)
print
(
"
Received message from {}:
\n\t
{}
"
.
format
(
method
.
routing_key
,
msg
))
#queue_unbind(queue, exchange=None, routing_key=None, arguments=None, callback=None)
channel
.
queue_delete
(
method
.
routing_key
)
# ingest messages, and assume delivered via auto_ack
channel
.
basic_consume
(
queue
=
sys
.
argv
[
2
],
on_message_callback
=
work
,
auto_ack
=
True
)
print
(
"
Subscribing to queue: {}
"
.
format
(
sys
.
argv
[
2
]))
# initiate message ingestion
try
:
channel
.
start_consuming
()
except
KeyboardInterrupt
:
print
(
"
Disconnecting from broker.
"
)
channel
.
stop_consuming
()
connection
.
close
()
This diff is collapsed.
Click to expand it.
ohpc_account_create.py
0 → 100644
+
62
−
0
View file @
c606ea5f
#!/usr/bin/env python
import
pika
# python client
import
sys
import
rabbit_config
as
rcfg
import
socket
import
subprocess
import
time
import
json
from
pwd
import
getpwnam
hostname
=
socket
.
gethostname
().
split
(
"
.
"
,
1
)[
0
]
connect_host
=
rcfg
.
Server
if
hostname
!=
rcfg
.
Server
else
"
localhost
"
queue_name
=
"
ohpc_account_create
"
duration
=
2
# Set up credentials to connect to RabbitMQ server
credentials
=
pika
.
PlainCredentials
(
rcfg
.
User
,
rcfg
.
Password
)
parameters
=
pika
.
ConnectionParameters
(
connect_host
,
rcfg
.
Port
,
rcfg
.
VHost
,
credentials
)
# Establish connection to RabbitMQ server
connection
=
pika
.
BlockingConnection
(
parameters
)
channel
=
connection
.
channel
()
print
"
connection established. Listening for messages:
"
# create exchange to pass messages
channel
.
exchange_declare
(
exchange
=
rcfg
.
Exchange
,
exchange_type
=
'
direct
'
)
# creates a random name for the newly generated queue
result
=
channel
.
queue_declare
(
queue
=
queue_name
,
exclusive
=
False
)
channel
.
queue_bind
(
exchange
=
rcfg
.
Exchange
,
queue
=
queue_name
,
routing_key
=
queue_name
)
def
slurm_account_create
(
ch
,
method
,
properties
,
body
):
msg
=
json
.
loads
(
body
)
print
(
"
Message received {}
"
.
format
(
msg
))
username
=
msg
[
'
username
'
]
try
:
subprocess
.
call
([
"
sudo
"
,
"
useradd
"
,
"
-m
"
,
username
])
except
:
print
(
"
Failed to create user
"
)
channel
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
msg
[
'
uid
'
]
=
getpwnam
(
username
).
pw_uid
msg
[
'
gid
'
]
=
getpwnam
(
username
).
pw_gid
channel
.
basic_publish
(
exchange
=
rcfg
.
Exchange
,
routing_key
=
'
ood_account_create
'
,
body
=
json
.
dumps
(
msg
))
# ingest messages
channel
.
basic_consume
(
queue
=
queue_name
,
on_message_callback
=
slurm_account_create
)
# initiate message ingestion
try
:
channel
.
start_consuming
()
except
KeyboardInterrupt
:
print
(
"
Disconnecting from broker.
"
)
channel
.
stop_consuming
()
connection
.
close
()
This diff is collapsed.
Click to expand it.
ood_account_create.py
0 → 100644
+
64
−
0
View file @
c606ea5f
#!/usr/bin/env python
import
pika
# python client
import
sys
import
rabbit_config
as
rcfg
import
socket
import
subprocess
import
time
import
json
hostname
=
socket
.
gethostname
().
split
(
"
.
"
,
1
)[
0
]
connect_host
=
rcfg
.
Server
if
hostname
!=
rcfg
.
Server
else
"
localhost
"
queue_name
=
"
ood_account_create
"
duration
=
2
# Set up credentials to connect to RabbitMQ server
credentials
=
pika
.
PlainCredentials
(
rcfg
.
User
,
rcfg
.
Password
)
parameters
=
pika
.
ConnectionParameters
(
connect_host
,
rcfg
.
Port
,
rcfg
.
VHost
,
credentials
)
# Establish connection to RabbitMQ server
connection
=
pika
.
BlockingConnection
(
parameters
)
channel
=
connection
.
channel
()
print
"
connection established. Listening for messages:
"
# create exchange to pass messages
channel
.
exchange_declare
(
exchange
=
rcfg
.
Exchange
,
exchange_type
=
'
direct
'
)
# creates a random name for the newly generated queue
result
=
channel
.
queue_declare
(
queue
=
queue_name
,
exclusive
=
False
)
channel
.
queue_bind
(
exchange
=
rcfg
.
Exchange
,
queue
=
queue_name
,
routing_key
=
queue_name
)
def
slurm_account_create
(
ch
,
method
,
properties
,
body
):
msg
=
json
.
loads
(
body
)
print
(
"
Message received {}
"
.
format
(
msg
))
username
=
msg
[
'
username
'
]
user_uid
=
msg
[
'
uid
'
]
user_gid
=
msg
[
'
gid
'
]
try
:
subprocess
.
call
([
"
sudo
"
,
"
groupadd
"
,
"
-r
"
,
"
-g
"
,
user_gid
,
username
])
time
.
sleep
(
10
)
subprocess
.
call
([
"
sudo
"
,
"
useradd
"
,
"
-u
"
,
user_uid
,
"
-g
"
,
user_gid
,
"
-m
"
,
username
])
except
:
print
(
"
Failed to create user
"
)
channel
.
basic_ack
(
delivery_tag
=
method
.
delivery_tag
)
channel
.
basic_publish
(
exchange
=
rcfg
.
Exchange
,
routing_key
=
'
slurm_add_account
'
,
body
=
json
.
dumps
(
msg
))
# ingest messages
channel
.
basic_consume
(
queue
=
queue_name
,
on_message_callback
=
slurm_account_create
)
# initiate message ingestion
try
:
channel
.
start_consuming
()
except
KeyboardInterrupt
:
print
(
"
Disconnecting from broker.
"
)
channel
.
stop_consuming
()
connection
.
close
()
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