Skip to content

Commit

Permalink
Storlet Docker deploy when a project is Crystal Enabled/Disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepSampe committed Oct 13, 2017
1 parent 9585a80 commit f5c092f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
REDIS_CON_POOL = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DATABASE)

# Storlet docker image
STORLET_DOCKER_IMAGE = '192.168.2.1:5001/ubuntu_16.04_jre8_storlets'
STORLET_DOCKER_IMAGE = 'ubuntu_16.04_jre8_storlets'

# Openstack Admin
MANAGEMENT_ACCOUNT = 'management'
Expand Down
1 change: 1 addition & 0 deletions api/controllers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def controller_detail(request, controller_id):

elif request.method == 'DELETE':
try:
# TODO: Check instances, return error message
controller = r.hgetall('controller:' + str(controller_id))
delete_file(controller['controller_name'], settings.CONTROLLERS_DIR)
r.delete("controller:" + str(controller_id))
Expand Down
61 changes: 60 additions & 1 deletion api/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from rest_framework.exceptions import ParseError
from rest_framework.parsers import JSONParser, MultiPartParser, FormParser
from rest_framework.views import APIView

from paramiko.ssh_exception import SSHException, AuthenticationException
from swiftclient import client as swift_client
import logging
import paramiko


from api.common import JSONResponse, get_redis_connection, \
Expand Down Expand Up @@ -112,14 +113,72 @@ def projects(request, project_id=None):

def create_docker_image(r, project_id):
nodes = r.keys('*_node:*')
already_created = list()
for node in nodes:
node_data = r.hgetall(node)
node_ip = node_data['ip']
if node_ip not in already_created:
if node_data['ssh_access']:
ssh_user = node_data['ssh_username']
ssh_password = node_data['ssh_password']
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
ssh_client.connect(node_ip, username=ssh_user, password=ssh_password)
except AuthenticationException:
r.hset(node, 'ssh_access', False)
ssh_client.close()
logger.error('An error occurred connecting to: '+node)
raise AuthenticationException('An error occurred connecting to: '+node)

try:
command = 'sudo docker tag '+settings.STORLET_DOCKER_IMAGE+' '+project_id[0:13]
ssh_client.exec_command(command)
ssh_client.close()
already_created.append(node_ip)
except SSHException:
ssh_client.close()
logger.error('An error occurred creating the Docker image in: '+node)
raise SSHException('An error occurred creating the Docker image in: '+node)
else:
logger.error('An error occurred connecting to: '+node)
raise AuthenticationException('An error occurred connecting to: '+node)


def delete_docker_image(r, project_id):
nodes = r.keys('*_node:*')
already_created = list()
for node in nodes:
node_data = r.hgetall(node)
node_ip = node_data['ip']
if node_ip not in already_created:
if node_data['ssh_access']:
ssh_user = node_data['ssh_username']
ssh_password = node_data['ssh_password']
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
ssh_client.connect(node_ip, username=ssh_user, password=ssh_password)
except AuthenticationException:
r.hset(node, 'ssh_access', False)
ssh_client.close()
logger.error('An error occurred connecting to: '+node)
raise AuthenticationException('An error occurred connecting to: '+node)

try:
command = 'sudo docker rmi -f '+project_id[0:13]
ssh_client.exec_command(command)
ssh_client.close()
already_created.append(node_ip)
except SSHException:
ssh_client.close()
logger.error('An error occurred creating the Docker image in: '+node)
raise SSHException('An error occurred creating the Docker image in: '+node)
else:
logger.error('An error occurred connecting to: '+node)
raise AuthenticationException('An error occurred connecting to: '+node)


#
Expand Down

0 comments on commit f5c092f

Please sign in to comment.