Skip to content

Commit

Permalink
Working version: update_resources working, release_resources improved
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarmignani committed Jun 9, 2017
1 parent 4a7d109 commit 7901fcc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 38 deletions.
107 changes: 74 additions & 33 deletions eu/softfire/SecurityManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from eu.softfire.utils.utils import *
from eu.softfire.exceptions.exceptions import *
import yaml, os
import sqlite3, requests, tarfile
import sqlite3, requests, tarfile, shutil
from threading import Thread

logger = get_logger(config_path)
Expand All @@ -14,7 +14,7 @@
def add_rule_to_fw(fd, rule) :
fd.write("curl -X POST -H \"Content-Type: text/plain\" -d '%s' http://localhost:5000/ufw/rules\n" % rule)

'''

class UpdateStatusThread(Thread):
def __init__(self, manager):
Thread.__init__(self)
Expand All @@ -25,21 +25,21 @@ def run(self):
while not self.stopped:
time.sleep(int(self.manager.get_config_value('system', 'update-delay', '10')))
if not self.stopped:
try:
self.manager.send_update()
except Exception as e:
logger.error("got error while updating resources: %s " % e.args)
#try:
self.manager.send_update()
#except Exception as e:
# logger.error("got error while updating resources: %s " % e.args)

def stop(self):
self.stopped = True
'''
self.stopped = True


class SecurityManager(AbstractManager):

def __init__(self, config_path):
super(SecurityManager, self).__init__(config_path)
local_files_path = self.get_config_value("local-files", "path", "/etc/softfire/security-manager")
self.resources_db = '%s/security-manager.db' % local_files_path
self.local_files_path = self.get_config_value("local-files", "path", "/etc/softfire/security-manager")
self.resources_db = '%s/security-manager.db' % self.local_files_path


def refresh_resources(self, user_info):
Expand Down Expand Up @@ -132,10 +132,10 @@ def provide_resources(self, user_info, payload=None):
logger.info("Requested provide_resources by user %s" % user_info.name)

nsr_id = ""
log_dashboard_url = ""

local_files_path = self.get_config_value("local-files", "path", "/etc/softfire/security-manager")
random_id = random_string(6)
tmp_files_path = "%s/tmp/%s" % (local_files_path, random_id)
tmp_files_path = "%s/tmp/%s" % (self.local_files_path, random_id)
logger.debug("Store tmp files in folder %s" %tmp_files_path)
os.makedirs(tmp_files_path)

Expand Down Expand Up @@ -180,6 +180,7 @@ def provide_resources(self, user_info, payload=None):
'''Configure logging to send log messages to <collector_ip>'''
index = ""
collector_ip = ""
log_dashboard_url = ""

tar = tarfile.open(name=tar_filename, mode='w')

Expand Down Expand Up @@ -216,12 +217,12 @@ def provide_resources(self, user_info, payload=None):
#TODO Fix
#logger.error(e)

# TODO store reference between resource and user
# TODO store reference between resource and user. ADD status, api-ip, dashboard_url
conn = sqlite3.connect(self.resources_db)
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS resources (username, project_id, nsr_id, nsd_id, tmp_folder)''')
query = "INSERT INTO resources (username, project_id, nsr_id, nsd_id, tmp_folder) VALUES ('%s', '%s', '%s', '%s', '%s')" % \
(user_info.name, project_id, nsr_id, nsd_id, tmp_files_path)
cur.execute('''CREATE TABLE IF NOT EXISTS resources (username, project_id, nsr_id, nsd_id, random_id, log_dashboard_url)''')
query = "INSERT INTO resources (username, project_id, nsr_id, nsd_id, random_id, log_dashboard_url) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')" % \
(user_info.name, project_id, nsr_id, nsd_id, random_id, log_dashboard_url)
logger.debug("Executing %s" % query)

cur.execute(query)
Expand All @@ -233,33 +234,72 @@ def provide_resources(self, user_info, payload=None):
'''
return response

'''
def _update_status(self) -> dict:
logger.debug("Checking status update")
result = {}
conn = sqlite3.connect(self.resources_db)
conn.row_factory = sqlite3.Row
cur = conn.cursor()

query = "SELECT * FROM resources WHERE username = '%s'" % user_info.name
query = "SELECT * FROM resources"
res = cur.execute(query)
rows = res.fetchall()
for username, nsrs in get_nsrs_to_check().items():
logger.debug("Checking resources of user %s" % username)
if len(nsrs):
ob_client = OBClient(username)
for r in rows:
#TODO nsr_id e project_id could be empty with want_agent
nsr_id = r["nsr_id"]
project_id = r["project_id"]
username = r["username"]
#TODO FIX THESE
#download_link = r["download_link"]
#dashboard_url = r["dashboard_url"]
#api_url = r["api_url"]

if nsr_id == "" :
'''This resource does not correspond to a deployed NSR'''
logger.debug("Uninstantiated resource")
s = {"message" : "You have just downloaded the scripts to install the resource"}
#s["download_link"] = download_link

else :
'''Open Baton resource'''
logger.debug("Checking resource nsr_id: %s" % nsr_id)

try :
agent = ob_login(project_id)
nsr_agent = agent.get_ns_records_agent(project_id=project_id)
ob_resp = nsr_agent.find(nsr_id)
time.sleep(5)
ob_resp = json.loads(ob_resp)
logger.debug(ob_resp)
except Exception as e :
logger.error("Error contacting Open Baton to validate resource nsr_id: %s\n%s" % (nsr_id, e))

s = {}
s["status"] = ob_resp["status"]

print(s)
#if ACTIVE
if s["status"] == "ACTIVE" :
s["ip"] = ob_resp["vnfr"][0]["vdu"][0]["vnfc_instance"][0]["floatingIps"][0]["ip"]
s["api_url"] = "http://%s:5000" % s["ip"]
try :
api_resp = requests.get(s["api_url"])
logger.debug(api_resp)
except Exception:
s["status"] == "VM is running but API are unavailable"

'''
if dashboard_url != "" :
s["dashboard_url"] = dashboard_url
'''
if username not in result.keys():
result[username] = []
for nsr in nsrs:
nsr_new = ob_client.get_nsr(nsr.get('id'))
if isinstance(nsr_new, dict):
nsr_new = json.dumps(nsr_new)
status = json.loads(nsr_new).get('status')
result[username].append(nsr_new)
result[username].append(json.dumps(s))
return result

# result[username].append(json.dumps(nsr))

return result
'''
def release_resources(self, user_info, payload=None):
logger.info("Requested release_resources by user %s" % user_info.name)
logger.debug("Arrived release_resources\nPayload: %s" % payload)

conn = sqlite3.connect(self.resources_db)
Expand All @@ -271,12 +311,13 @@ def release_resources(self, user_info, payload=None):
rows = res.fetchall()
for r in rows:
delete_ns(nsr_id=r["nsr_id"], nsd_id=r["nsd_id"], project_id=r["project_id"])
shutil.rmtree("%s/tmp/%s" % (self.local_files_path, r["random_id"]))

query = "DELETE FROM resources WHERE username = '%s'" % user_info.name
################
cur.execute(query)
conn.commit()
conn.close()
logger.info("Requested release_resources by user %s" % user_info.name)
#TODO check on the properties defined in the payload

#TODO delete folders
return
10 changes: 5 additions & 5 deletions manager
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It will then work when installed in site-packages on a target system
where the runner script is in /usr/bin (or wherever)
"""
from sdk.softfire.main import start_manager
from eu.softfire.SecurityManager import SecurityManager #, UpdateStatusThread
from eu.softfire.SecurityManager import SecurityManager, UpdateStatusThread
#from eu.softfire.Api import Api

import asyncio
Expand All @@ -20,11 +20,11 @@ def start():
#print(os.environ["no_proxy"])
#sec_man = SecurityManager(get_logger("eu.softfire.security-manager"))
#executor = ProcessPoolExecutor(1)
'''
thread = UpdateStatusThread(nfv_manager)
sec_manager = SecurityManager(config_path)
thread = UpdateStatusThread(sec_manager)
thread.start()
'''
start_manager(SecurityManager(config_path))

start_manager(sec_manager)
#asyncio.ensure_future(loop.run_in_executor(executor, Api.start))


Expand Down

0 comments on commit 7901fcc

Please sign in to comment.