diff --git a/ohpc_account_create.py b/ohpc_account_create.py new file mode 100644 index 0000000000000000000000000000000000000000..9c3f6a642a507859ac0aab2917a865c9ccb15afe --- /dev/null +++ b/ohpc_account_create.py @@ -0,0 +1,63 @@ +#!/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]) + print("User {} has been added to {}".format(username, hostname)) + 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() diff --git a/ood_account_create.py b/ood_account_create.py new file mode 100644 index 0000000000000000000000000000000000000000..fcdac83e5b1caded8965884f8f41b552ba6a61c1 --- /dev/null +++ b/ood_account_create.py @@ -0,0 +1,64 @@ +#!/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 = str(msg['uid']) + user_gid = str(msg['gid']) + try: + subprocess.call(["sudo", "groupadd", "-r", "-g", user_gid, username]) + subprocess.call(["sudo", "useradd", "-u", user_uid, "-g", user_gid, "-m", username]) + print("User {} has been added to {}".format(username, hostname)) + 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() diff --git a/rabbit_config.py b/rabbit_config.py new file mode 100644 index 0000000000000000000000000000000000000000..dab1785849563492f2016c799cb7f905613624f2 --- /dev/null +++ b/rabbit_config.py @@ -0,0 +1,6 @@ +Exchange = 'RegUsr' +User = 'reggie' +Password = 'reg19890' +VHost = '/' +Server = 'ood' +Port = 5672 diff --git a/rabbit_config.py.example b/rabbit_config.py.example new file mode 100644 index 0000000000000000000000000000000000000000..27c3dd9a3dcf7440766df3c2e20bc67eac289b28 --- /dev/null +++ b/rabbit_config.py.example @@ -0,0 +1,6 @@ +Exchange = 'RegUsr' +User = 'reggie' +Password = 'CHANGE_IT_TO_YOUR_OWN_PASSWORD' +VHost = '/' +Server = 'ood' +Port = 5672 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..721d7c72cc757638f1d85e56d160b2bf10078315 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,33 @@ +argh==0.26.2 +Click==7.0 +dominate==2.3.5 +eventlet==0.17.4 +Flask==1.0.2 +Flask-Bootstrap==3.3.7.1 +Flask-Login==0.4.1 +Flask-SocketIO==4.2.1 +Flask-Testing==0.7.1 +Flask-WTF==0.14.2 +greenlet==0.4.15 +gunicorn==18.0 +itsdangerous==1.1.0 +Jinja2==2.10.1 +Mako==1.0.7 +MarkupSafe==1.1.1 +pathtools==0.1.2 +pbr==5.1.3 +pika==1.1.0 +python-dateutil==1.5 +python-editor==1.0.4 +python-engineio==3.10.0 +python-socketio==4.3.1 +pytz==2013.7 +PyYAML==5.1.2 +six==1.12.0 +stevedore==1.30.1 +virtualenv==16.4.3 +virtualenv-clone==0.5.1 +virtualenvwrapper==4.8.4 +visitor==0.1.3 +Werkzeug==0.14.1 +WTForms==2.2.1 diff --git a/slurm_agent.py b/slurm_agent.py new file mode 100755 index 0000000000000000000000000000000000000000..5ce3727c0cde324020a56ab5e4a929305dd88ef4 --- /dev/null +++ b/slurm_agent.py @@ -0,0 +1,65 @@ +#!/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 = "slurm_add_account" +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", "sacctmgr", "add", "account", username, "-i", "Descripition: Add user"]) + subprocess.call(["sudo", "sacctmgr", "add", "user", username, "account="+username, "-i"]) + print("SLURM account for user {} has been added".format(username)) + except: + print("Failed to create user") + + channel.basic_ack(delivery_tag=method.delivery_tag) + + channel.basic_publish(exchange=rcfg.Exchange, routing_key=username, 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() + + +