def GET(self, eid):
        help.check_access()
        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}

        # Decouples Servers.
        Services = db().executeLiteral(
            "SELECT ser.resource_name FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
            [eid])
        Servers = db().executeLiteral(
            "SELECT machine_address, m_port_num, display_name FROM servers WHERE eid = ?",
            [eid])

        if len(Services) > 0:
            for server in Servers:
                interface = Server(server[0], server[1])
                for service in Services:
                    resp = interface.send("DELETE", service[0], msgHeaders)
                    if resp == "FAILED":
                        help.append_user_alerts(
                            "warning", "Communication Failed",
                            "Could not connect with " + server[3] +
                            ", services may still be running on this machine.")

        db().execute("Environment_Forms", 7, [eid])

        # Deletes from Environments the env.
        db().execute("Environment_Forms", 6, [eid])

        # Decouples Services.
        db().executeLiteral("DELETE FROM service_deployment_lkp WHERE eid = ?",
                            [eid])

        # Decouples Paths.
        paths = db().executeLiteral(
            "SELECT p.pid, p.length, plkp.position FROM paths p, env_path_lkp plkp WHERE eid = ?",
            [eid])
        print(len(paths))
        for path in paths:
            print("GET to path change")
            # Shorten the length of the path
            db().executeLiteral("UPDATE paths SET length = ? WHERE pid = ?",
                                [int(path[1]) - 1, path[0]])
            affected = db().executeLiteral(
                "SELECT * FROM env_path_lkp WHERE pid = ? and position > ?",
                [path[0], path[2]])
            print(len(affected))
            for p in affected:

                db().executeLiteral(
                    "UPDATE env_path_lkp SET position = ? WHERE pid = ? and eid = ?",
                    [int(p[2]) - 1, p[0], p[1]])

        help.append_user_alerts(
            "info", "Successfully Deleted",
            "An environment has been successfully deleted")
        raise web.seeother("environments.html")
    def GET(self, sgid):
        help.check_access()

        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}

        # Collect the resouce of the service.
        resource = db().executeLiteral(
            "SELECT resource_name FROM services WHERE sgid = ?", [sgid])[0][0]
        # Collect a list of all servers the service is running on.
        servers = db().executeLiteral(
            "SELECT srv.machine_address, srv.m_port_num FROM services_versions sv, service_deployment_lkp sdl, servers srv WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = srv.eid GROUP BY srv.srv_id",
            [sgid])
        # Delete the resource on every server
        for eachServer in servers:
            Server(eachServer[0], eachServer[1]).send("DELETE", resource,
                                                      msgHeaders)

        # Delete local for service.
        clearDir(join("./services", str(sgid)))

        # Decouple Database
        db().executeLiteral("DELETE FROM services WHERE sgid = ?", [sgid])
        db().executeLiteral(
            "DELETE FROM service_deployment_lkp WHERE sid IN (SELECT sid FROM services_versions WHERE sgid = ?)",
            [sgid])
        db().executeLiteral("DELETE FROM services_versions WHERE sgid = ?",
                            [sgid])

        help.append_user_alerts(
            "info", "Successfully Deleted",
            "We have successfully deleted the service information.")
        raise web.seeother("manage_services.html")
예제 #3
0
    def GET(self, srv_id):

        # Tell server to delete its contents
        address, port = db().executeLiteral(
            "SELECT machine_address, m_port_num FROM servers WHERE srv_id = ?",
            [srv_id])[0]
        resources = [
            x[0] for x in db().executeLiteral(
                "SELECT ser.resource_name FROM services ser, services_versions sv, service_deployment_lkp sdl, servers srv WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = srv.eid AND srv.srv_id = ? GROUP BY ser.sgid",
                [srv_id])
        ]

        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}

        interface = Server(address, port)
        for res in resources:
            interface.send("DELETE", res, msgHeaders)

        # Delete Config
        try:
            remove(join("./configs", srv_id))
        except:
            web.debug("Tried to delete Analytics config but it was not found!")

        # Delete log files
        logLoc = join("./logs", srv_id)
        try:
            logs = listdir(logLoc)
            for log in logs:
                remove(join(logLoc, log))
            rmdir(logLoc)
        except:
            web.debug("Tried to delete log files at " + logLoc +
                      " but they didn't exist.")

        # Delete from database
        db().execute("Server_Forms", 4, [srv_id])

        help.append_user_alerts(
            'default', 'Successfully Deleted',
            "Some server information has been deleted? (get it! :P we don't even know what server it was!)"
        )
        raise web.seeother("/manage_servers.html")
예제 #4
0
	def GET(self, srv_id):
		# Message the server asking regarding its status
		help.check_access()
		usr = (web.ctx.session).username
		token = (web.ctx.session).access_token
		msgHeader = {"Username":usr, "Access-Token":token}

		serverInfo = db().executeLiteral( "SELECT machine_address, m_port_num FROM servers WHERE srv_id = ?" , [srv_id])[0]
		status = Server( serverInfo[0], serverInfo[1] ).send( "GET", "/STATUS/*", headers=msgHeader, timeout=5 )
		
		if status == "FAILED" or status[0] != "200":
			return '{"Management":"REJECTING", "Analytic":"REJECTING"}'
		else:
			return '{"Management":"ACCEPTING","Analytic":"' + status[1][-10:-1].upper() + '"}'
예제 #5
0
    def POST(self):
        help.check_access()

        # Collect and verity input information.
        input = web.input(eid="NULL", sid="NULL")
        if input.eid == "NULL" or input.sid == "NULL":
            raise web.badrequest()

        # Construct the message information plus target information.
        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}
        resource = "/ACTIVATE" + db().executeLiteral(
            "SELECT ser.resource_name FROM services ser, services_versions srv WHERE srv.sid = ? AND srv.sgid = ser.sgid",
            [input.sid])[0][0]
        Env_Ser = db().executeLiteral(
            "SELECT display_name, machine_address, m_port_num FROM servers WHERE eid = ?",
            input.eid)

        # Activate the service on all targets.
        count = len(Env_Ser)
        for server in Env_Ser:
            resp = Server(server[1], server[2]).send("GET", resource,
                                                     msgHeaders)
            print "RESPONSE FROM SERVER"
            print resp
            if resp == "FAILED" or resp[0] != "200":
                help.append_user_alerts(
                    "warning", "Whoops",
                    "Error on communicating with " + server[0])
                count -= 1

        if count == 0:
            #None of the services were correctly activated, do not change stored status.
            help.append_user_alerts(
                "danger", "Unable to activate",
                "The service has not been correctly activated on any server of the environment."
            )
        else:
            # Update the database of the change
            db().executeLiteral(
                "UPDATE service_deployment_lkp SET status = 'ACTIVE' WHERE eid = ? AND sid = ?",
                [input.eid, input.sid])
        raise web.seeother("/environment_details_" + input.eid)
예제 #6
0
    def POST(self, sid):
        help.check_access()

        # Extract information from the post request and validate.
        input = web.input(machine_name="NULL",
                          desc_text="NULL",
                          machine_address="NULL",
                          a_port_num="NULL",
                          m_port_num="NULL",
                          ssh_port_num="NULL",
                          eid="0",
                          ssh_username="******",
                          ssh_key_address="NULL")

        # The password on the page is editted to be incorrect.
        # Password to be changed by high level users in the future.
        ssh_password = db().executeLiteral(
            "SELECT ssh_password FROM servers WHERE srv_id = ?", [sid])[0][0]
        serverInfo = [
            int(input.eid), input.machine_name, input.desc_text,
            input.machine_address,
            int(input.a_port_num),
            int(input.m_port_num),
            int(input.ssh_port_num), input.ssh_username, ssh_password,
            input.ssh_key_address
        ]

        if any((x == "NULL")
               for x in serverInfo[0:6]) or (serverInfo[7] == "NULL"
                                             and serverInfo[8] == "NULL"):
            raise web.BadRequest()

        # Open a connection with the server and check to see if it exits.
        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}
        server = Server(input.machine_address, int(input.m_port_num))
        status = server.send("GET", "/STATUS/*", msgHeaders)
        if status == "FAILED" or status[0] != "200":
            help.append_user_alerts(
                'error', 'Connection Error',
                'Either the server is not currently accepting requests or the information input is invalid, please check both and try again.'
            )
            return help.renderPage(
                "Add Server", render.add_server(sid, env_data(), serverInfo))

        # Collect and replace the master Config of the Server.
        resp = server.retrieveMaster(msgHeaders, sid, input.ssh_username,
                                     ssh_password)
        if (resp == -1):
            help.append_user_alerts(
                'error', 'Invalid Authentication Information',
                'The ssh information entered was not valid, connection was rejected.'
            )
            return help.renderPage(
                "Add Server", render.add_server("New", env_data(), serverInfo))

        server.getLogs(sid, msgHeaders, input.ssh_username, ssh_password)

        # Update Server with Services if change of environment.
        OriginalInfo = db().executeLiteral(
            "SELECT * FROM servers WHERE srv_id = ?", [sid])[0]

        if (OriginalInfo[1] != int(input.eid)):
            currentServices = db().executeLiteral(
                "SELECT sv.sid, ser.resource_name FROM services ser, services_versions sv, service_deployment_lkp sdl, servers srv WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = srv.eid AND srv.srv_id = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
                [sid])
            newServices = db().executeLiteral(
                "SELECT sv.sid, ser.service_name, ser.resource_name, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
                [input.eid])

            cIDs = [x[0] for x in currentServices]
            nIDs = [x[0] for x in newServices]

            print(cIDs)
            print(nIDs)

            deleteing = [x for x in cIDs if x not in nIDs]
            creating = [x for x in nIDs if x not in cIDs]

            for service in currentServices:
                if service[0] in deleteing:
                    resp = server.send("DELETE", service[1], msgHeaders)
                    print(resp)

            for service in newServices:
                if service[0] in creating:
                    if service[8] != "NULL":
                        status = 302
                        HTML = service[8]
                    else:
                        status = 200
                        HTML = service[7]
                    data = {
                        "name": service[1],
                        "resource": service[2],
                        "status": "ACTIVE",
                        "version": service[3],
                        "mainScript": service[4],
                        "language": service[5],
                        "poolSize": service[6],
                        "HTTP_Status": status,
                        "HTML": HTML
                    }
                    directory = join("./services", str(service[0]),
                                     str(service[3]))
                    resp = server.createService(msgHeaders, input.ssh_username,
                                                ssh_password, directory, data)

        # Update info
        db().executeLiteral(
            "UPDATE servers SET eid = ?, display_name = ?, description = ?, machine_address = ?, a_port_num = ?, m_port_num = ?, ssh_port_num = ?, ssh_username = ?, ssh_password = ?, ssh_key_address = ? WHERE srv_id = ? ",
            serverInfo + [sid])

        help.append_user_alerts(
            'success', 'Successfully Edited',
            'You have changes information for "' + input.machine_name + '"')
        raise web.seeother("/manage_servers.html")
예제 #7
0
    def POST(self):
        help.check_access()

        # Extract the information from the post message and test to see if valid.
        input = web.input(machine_name="NULL",
                          desc_text="NULL",
                          machine_address="NULL",
                          a_port_num="NULL",
                          m_port_num="NULL",
                          ssh_port_num="NULL",
                          eid=0,
                          ssh_username="******",
                          ssh_password="******",
                          ssh_key_address="NULL")

        serverInfo = [
            input.eid, input.machine_name, input.desc_text,
            input.machine_address, input.a_port_num, input.m_port_num,
            input.ssh_port_num, input.ssh_username, input.ssh_password,
            input.ssh_key_address
        ]

        if ("NULL" in serverInfo[:6]) or (serverInfo[7] == "NULL"
                                          and serverInfo[8] == "NULL"):
            help.append_user_alerts(
                'error', 'Invalid Inputs',
                'Please ensure that all the inputs are correct, and try again.'
            )
            return help.renderPage(
                "Add Server", render.add_server("New", env_data(), serverInfo))

        # Open a connection with the server and check to see if it exits.
        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}
        server = Server(input.machine_address, int(input.m_port_num))
        status = server.send("GET", "/STATUS/*", msgHeaders)
        if status == "FAILED" or status[0] != "200":
            help.append_user_alerts(
                'error', 'Connection Error',
                'Either the server is not currently accepting requests or the information input is invalid, please check both and try again.'
            )
            return help.renderPage(
                "Add Server", render.add_server("New", env_data(), serverInfo))

        # Add Server into the database and collect the master Config of the Server.
        ID = db().executeID("Server_Forms", 2, serverInfo)
        resp = server.retrieveMaster(msgHeaders, ID, input.ssh_username,
                                     input.ssh_password)
        if (resp == -1):
            db().executeLiteral("DELETE FROM servers WHERE srv_id = ?", [ID])
            help.append_user_alerts(
                'error', 'Invalid Authentication Information',
                'The ssh information entered was not valid, connection was rejected.'
            )
            return help.renderPage(
                "Add Server", render.add_server("New", env_data(), serverInfo))

        server.getLogs(ID, msgHeaders, input.ssh_username, input.ssh_password)

        # Report success
        help.append_user_alerts(
            'success', 'Successfully Added', 'Your new server "' +
            input.machine_name + '" has been added into the system.')
        raise web.seeother("/manage_servers.html")
    def POST(self, eid):
        help.check_access()
        input = web.input(name="NULL", type="NULL", servers=[])

        inputServers = [int(x) for x in input.servers]

        if input.name == "NULL" or input.type == "NULL":
            help.append_user_alerts(
                "warning", "Invalid Entry",
                "The information you have input is invalid")
            return help.renderPage(
                "Add Environment",
                render.environment_form(eid, env_types(), free_servers(eid),
                                        selected_servers(eid),
                                        [input.name, input.desc, input.type]))

        # Ensure Paths are not affected.
        content = db().execute("Environment_Forms", 2, [eid])[0]
        if (content[2] != input.type):
            pathMembership = db().executeLiteral(
                "SELECT * FROM env_path_lkp WHERE eid = ?", [eid])
            if (len(pathMembership) != 0):
                help.append_user_alerts(
                    "warning", "Cannot Perform Action",
                    "The Environment is apart of a development path and therefore cannot have its type changed. Please decouple it before trying again."
                )
                return help.renderPage(
                    "Add Environment",
                    render.environment_form(
                        eid, env_types(), free_servers(eid),
                        selected_servers(eid),
                        [input.name, input.desc, input.type]))

        # Change servers if need be.

        services = db().executeLiteral(
            "SELECT sv.sid, ser.service_name, ser.resource_name, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
            [eid])
        current = [
            x[0] for x in db().executeLiteral(
                "SELECT srv_id FROM servers WHERE eid = ?", [eid])
        ]
        removed = [x for x in current if x not in inputServers]
        added = [x for x in inputServers if x not in current]

        print(removed, current, added)

        if len(removed) + len(added) > 0:
            usr = (web.ctx.session).username
            token = (web.ctx.session).access_token
            msgHeaders = {"Username": usr, "Access-Token": token}

        for srv_id in removed:
            info = db().executeLiteral(
                "SELECT machine_address, m_port_num, display_name FROM servers WHERE srv_id = ?",
                [srv_id])[0]
            for each in services:
                resp = Server(info[0], info[1]).send("DELETE", each[2],
                                                     msgHeaders)
                if (resp == -1):
                    help.append_user_alert(
                        "warning", "Connection Error", "Unable to access " +
                        info[2] + " to remove environment services")
            db().executeLiteral("UPDATE servers SET eid = 0 WHERE srv_id = ?",
                                [srv_id])

        for srv_id in added:

            info = db().executeLiteral(
                "SELECT machine_address, m_port_num, ssh_username, ssh_password, display_name FROM servers WHERE srv_id = ?",
                [srv_id])[0]

            print "GOT HERE"
            count = len(services)
            for service in services:
                print(service[7], service[8])
                if service[7] == "NULL":
                    status = 302
                    HTML = service[8]
                else:
                    status = 200
                    HTML = service[7]
                data = {
                    "name": service[1],
                    "resource": service[2],
                    "status": "ACTIVE",
                    "version": service[3],
                    "mainScript": service[4],
                    "language": service[5],
                    "poolSize": service[6],
                    "HTTP_Status": status,
                    "HTML": HTML
                }

                location = join("./services", str(service[0]), str(service[3]))

                resp = Server(info[0],
                              info[1]).createService(msgHeaders, info[2],
                                                     info[3], location, data)
                print(resp)
                if resp == -1:
                    help.append_user_alerts(
                        "warning", "Couldn't Connect!",
                        "Could not connect to " + info[4] + "!")
                    count -= 1
                    break
                elif resp[0] != "200":
                    help.append_user_alerts(
                        "warning", "Server Rejected Service", info[4] +
                        " rejected " + service[1] + " for reason: " + resp[1])
                    count -= 1

            if count > 0 or len(services) == 0:
                # Ensure we are not changing servers that are already on an environment
                db().executeLiteral(
                    "UPDATE servers SET eid = ? WHERE eid = 0 AND srv_id = ?",
                    [eid, srv_id])
            else:
                help.append_user_alerts(
                    "danger", "Server not added!",
                    "The server has rejected all the services and therefore is not apart of the environment."
                )

        # Change database row
        db().execute("Environment_Forms", 3,
                     [input.name, input.desc, input.type, eid])

        # return
        help.append_user_alerts(
            "info", "Successfully Updated",
            "The information has been updated in the database.")
        raise web.seeother("environment_details_" + eid)
    def GET(self, sid):
        help.check_access()

        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}

        # Collect the service resouce
        service = db().executeLiteral(
            "SELECT ser.sgid, ser.resource_name, sv.version FROM services ser, services_versions sv WHERE ser.sgid = sv.sgid AND sv.sid = ?",
            [sid])[0]
        # Collect Environment ids where the service may be running.
        environments = [
            x[0] for x in db().executeLiteral(
                "SELECT eid FROM service_deployment_lkp WHERE sid = ?", [sid])
        ]

        for env in environments:
            # Pull out the servers on the environment.
            servers = db().executeLiteral(
                "SELECT machine_address, m_port_num, ssh_username, ssh_password FROM servers WHERE eid = ?",
                [env])
            # Pull out the version running on the environment
            competition = db().executeLiteral(
                "SELECT ser.sgid, ser.service_name, ser.resource_name, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = ? AND ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? ORDER BY sv.version DESC",
                [service[0], env])

            if len(competition) == 1:
                # Service is only one running.

                for server in servers:
                    # Delete the resource on every server.
                    Server(server[0], server[1]).send("DELETE", service[1],
                                                      msgHeaders)

            else:
                # Extract the running service and the back up.
                top = competition[0]
                backup = competition[1]

                print(service[2], top[3])

                if service[2] == top[3]:
                    # Service is the running service

                    for server in servers:
                        interface = Server(server[0], server[1])
                        # Delete the resource on every server.
                        interface.send("DELETE", service[1], msgHeaders)

                        print(backup[7], backup[8])
                        # Push backup
                        if backup[7] == 'NULL' or (backup[8] != "NULL"
                                                   and backup[8] != ''):
                            status = 303
                            HTML = backup[8]
                        else:
                            status = 200
                            HTML = backup[7]
                        data = {
                            "name": backup[1],
                            "resource": backup[2],
                            "status": "ACTIVE",
                            "version": backup[3],
                            "mainScript": backup[4],
                            "language": backup[5],
                            "poolSize": backup[6],
                            "HTTP_Status": status,
                            "HTML": HTML
                        }

                        location = join("./services", str(backup[0]),
                                        str(backup[3]))
                        interface.createService(msgHeaders, server[2],
                                                server[3], location, data)

        # Delete Files
        clearDir(join("./services", str(service[0]), str(service[2])))

        # Decouple Database
        db().executeLiteral("DELETE FROM services_versions WHERE sid = ?",
                            [sid])
        db().executeLiteral("DELETE FROM service_deployment_lkp WHERE sid = ?",
                            [sid])

        raise web.seeother("/service_details_" + str(service[0]))
    def POST(self, sgid):
        help.check_access()
        redirect()

        # For adding a new version we only care about version information.
        input = web.input(version="NULL",
                          poolsize="NULL",
                          language="NULL",
                          main_script="NULL",
                          uploaded_files="NULL",
                          get_page_select="NULL",
                          redirect_URL="NULL")

        name, resource, eid = db().executeLiteral(
            "SELECT ser.service_name, ser.resource_name, epl.eid FROM services ser, paths p, env_path_lkp epl WHERE ser.sgid = ? AND ser.pid = p.pid AND p.pid = epl.pid ORDER BY epl.position ASC",
            [sgid])[0]
        topVersion = db().executeLiteral(
            "SELECT sv.version FROM services_versions sv WHERE sv.sgid = ? ORDER BY sv.version DESC",
            [sgid])

        if len(topVersion) == 0:
            versionNum = int(input.version)
        else:
            if int(input.version) <= int(topVersion[0][0]):
                versionNum = topVersion[0][0] + 1
            else:
                versionNum = int(input.version)

        version_info = [
            int(sgid), versionNum,
            int(input.poolsize),
            help.convert_lang(input.language), input.main_script,
            input.get_page_select, input.redirect_URL
        ]

        if "NULL" in version_info[:4]:
            help.append_user_alerts(
                "warning", "Invalid Inputs",
                "Some of the inputs are invalid, check them and try again.")
            paths = db().executeLiteral("SELECT pid, name FROM paths")
            return help.renderPage(
                "Add Version",
                render.service_version_form([0, sgid], [0] + version_info,
                                            paths, version))

        # Move files from temp to the newly created version space.
        moveFiles(sgid, versionNum, input.uploaded_files)
        location = join("./services", str(sgid), str(versionNum))

        # Delete previous version if any on starting server and push new version.
        servers = db().executeLiteral(
            "SELECT machine_address, m_port_num, ssh_username, ssh_password, display_name FROM servers WHERE eid = ?",
            [eid])
        versionCheck = db().executeLiteral(
            "SELECT count(*) FROM services_versions sv, service_deployment_lkp sdl WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = ?",
            [sgid, eid])[0][0]

        msgHeaders = {
            "Username": (web.ctx.session).username,
            "Access-Token": (web.ctx.session).access_token
        }

        count = len(servers)
        for server in servers:
            interface = Server(server[0], server[1])

            if versionCheck > 0:
                interface.send("DELETE", resource, msgHeaders)

            if input.redirect_URL != "NULL":
                status = 303
                html = input.redirect_URL
            else:
                status = 200
                html = input.get_page_select
            data = {
                "name": name,
                "resource": resource,
                "status": "ACTIVE",
                "version": versionNum,
                "mainScript": input.main_script,
                "language": help.convert_lang(input.language),
                "poolSize": input.poolsize,
                "HTTP_Status": status,
                "HTML": html
            }

            resp = interface.createService(msgHeaders, server[2], server[3],
                                           location, data)

            if resp == -1:
                help.append_user_alerts(
                    "warning", "Unable to make Connection!",
                    "A connection to the server " + server[4] +
                    " could not be made! Service not installed.")
                count -= 1
            elif resp[0] != "200":
                help.append_user_alerts(
                    "warning", "Server rejected new version", server[4] +
                    " has rejected the installation of the service. Reason being: "
                    + resp[1])
                count -= 1

        sid = db().executeID("Service_Forms", 1, version_info)

        if count > 0:
            db().executeLiteral(
                "INSERT INTO service_deployment_lkp VALUES ( ?,?,? )",
                [eid, sid, "ACTIVE"])
            return "service_details_" + str(sgid)
        else:
            help.append_user_alerts(
                "warning", "Service not deployed",
                "No server accepted the service, it is currently not deployed."
            )
            return "service_details_" + str(sgid)
    def POST(self):
        #Service add form submit
        help.check_access()
        redirect()

        input = web.input(service_name="NULL",
                          resource_name="NULL",
                          description="NULL",
                          poolsize="NULL",
                          language="NULL",
                          main_script="NULL",
                          get_page_select="NULL",
                          redirect_URL="NULL",
                          pid="NULL")

        # Define location of temp files uploaded.
        tempLocation = join("./temp/uploads/", (web.ctx.session).username)

        if not validateService(input.service_name, input.resource_name):
            # name or resource taken, delete files and move on.
            clearDir(tempLocation)
            raise web.seeother("add_service.html")

        # Create the service information and run it against the database
        service_info = [
            int(input.pid), input.service_name, input.resource_name,
            input.description
        ]
        sgid = db().executeID("Service_Forms", 0, service_info)

        # Create the service version information.
        if (input.get_page_select == "NULL" or input.get_page_select
                == "") and input.redirect_URL == "NULL":
            # The event when neither a dashboard or a redirect is given.
            sredirect = "www.google.com"
        else:
            sredirect = input.redirect_URL
        version_info = [
            sgid,
            si().getVersion(),
            int(input.poolsize),
            help.convert_lang(input.language), input.main_script,
            input.get_page_select, sredirect
        ]

        if ("NULL" in service_info or "NULL" in version_info[:5]):
            db().executeLiteral("DELETE FROM services WHERE sgid = ?", [sgid])
            clearDir(tempLocation)
            help.append_user_alerts(
                "warning", "Invalid information",
                "The information passes is invalid, please retry.")
            return web.seeother("add_service.html")

        # Move files to the correct location.
        moveFiles(sgid, si().getVersion(), input.uploaded_files)
        serviceLocation = join("./services", str(sgid), str(si().getVersion()))

        # Promote to the first environment.
        eid = db().executeLiteral(
            "SELECT eid FROM env_path_lkp WHERE pid = ? ORDER BY position ASC",
            [input.pid])[0][0]
        envServers = db().executeLiteral(
            "SELECT machine_address, m_port_num, ssh_username, ssh_password, display_name FROM servers WHERE eid = ?",
            [eid])

        # Construct service data.
        if input.redirect_URL != "NULL":
            status = 303
            html = sredirect
        else:
            status = 200
            html = input.get_page_select
        data = {
            "name": input.service_name,
            "resource": input.resource_name,
            "status": "ACTIVE",
            "version": help.strVersion(si().getVersion()),
            "mainScript": input.main_script,
            "language": help.convert_lang(input.language),
            "poolSize": input.poolsize,
            "HTTP_Status": status,
            "HTML": html
        }

        # Push to every server but check to ensure that the servers accept it.

        msgHeaders = {
            "Username": web.ctx.session.username,
            "Access-Token": web.ctx.session.access_token
        }

        count = len(envServers)

        print(count)
        for server in envServers:
            resp = Server(server[0],
                          server[1]).createService(msgHeaders, server[2],
                                                   server[3], serviceLocation,
                                                   data)

            # The Service was not deployed successfully.
            if resp == -1:
                help.append_user_alerts(
                    "warning", "Connection Error!",
                    "Could not form a connection with " + server[4] + ".")
                count -= 1
            elif resp[0] != "200":
                help.append_user_alerts(
                    "warning", "Server rejected!", "The Server " + server[4] +
                    " has reported that the service was not successfully installed! Reason being: "
                    + resp[1])
                count -= 1

        sid = db().executeID("Service_Forms", 1, version_info)
        if count > 0:
            # Add the service version into the database and append it to deployed.
            db().executeLiteral(
                "INSERT INTO service_deployment_lkp VALUES ( ?,?,? )",
                [eid, sid, "ACTIVE"])
        else:
            # No server accepted it so officially
            help.append_user_alerts(
                "info", "Created but not Deployed",
                "The service was created but was not successfully deployed to the first environment."
            )
            return "add_service.html"

        # Return a success
        help.append_user_alerts(
            'info', 'Operation Successful',
            'You have sucessfully created the new service: ' +
            input.service_name + '.')
        return "service_details_" + str(sgid)
    def POST(self, sgid):
        help.check_access()
        redirect()

        # Get current path information incase of path change.
        pid, name, resource = db().executeLiteral(
            "SELECT pid, service_name, resource_name FROM services WHERE sgid = ?",
            [sgid])[0]

        # Extract information and validate
        input = web.input(pid="NULL", service_name="NULL", description="NULL")
        service_info = [
            input.pid, input.service_name, resource, input.description,
            int(sgid)
        ]
        if "NULL" in service_info:
            help.append_user_alerts(
                "warning", "Invalid Input",
                "The information given is invalid, please try again.")
            paths = db().executeLiteral("SELECT pid, name FROM paths", [])
            return help.renderpage(
                "Edit Service",
                render.edit_service(sgid, [int(sgid)] + service_info, paths))

        if int(input.pid) != int(pid):

            # In prep for server communication
            usr = (web.ctx.session).username
            token = (web.ctx.session).access_token
            msgHeaders = {"Username": usr, "Access-Token": token}

            # Service has changed path and therefore needs to have the service versions changed.
            oldPathEnv = [
                int(x[0]) for x in db().executeLiteral(
                    "SELECT eid FROM env_path_lkp WHERE pid = ?", [pid])
            ]
            newPathEnv = [
                int(x[0]) for x in db().executeLiteral(
                    "SELECT eid FROM env_path_lkp WHERE pid = ?", [input.pid])
            ]
            Removed = [x for x in oldPathEnv if x not in newPathEnv]
            Persist = [x for x in newPathEnv if x in oldPathEnv]
            Added = [x for x in newPathEnv if x not in oldPathEnv]

            # Remove from the top of the path the environments that will not need to be added too.
            # Reverse it so that the top environment is at the front ( just cus )
            Cropped = newPathEnv[:newPathEnv.index(Persist[-1]) + 1][::-1]

            print(Removed, Persist, Added, Cropped)

            # Remove from old environments and decouple
            for eid in Removed:
                # Remove the service from all servers on the environment.
                envServers = db().executeLiteral(
                    "SELECT machine_address, m_port_num FROM servers WHERE eid = ?",
                    [eid])
                for server in envServers:
                    Server(server[0], server[1]).send("DELETE", resource,
                                                      msgHeaders)

                # Delete all services on that environment that are of this service.
                db().executeLiteral(
                    "DELETE FROM service_deployment_lkp WHERE eid = ? AND sid IN ( SELECT sid FROM services_versions WHERE sgid = ? )",
                    [eid, sgid])

            # Add Services to new servers if needed too, replace if needed too and so on.
            topVer = [0, 0, 0, 0, 0, 0, 0]
            for eid in Cropped:
                # Get Servers on environment.
                servers = db().executeLiteral(
                    "SELECT machine_address, m_port_num, ssh_username, ssh_password FROM servers WHERE eid = ?",
                    [eid])
                # Get the current version running on machine.
                current = db().executeLiteral(
                    "SELECT sv.sid, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services_versions sv, service_deployment_lkp sdl WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = ? ORDER BY sv.version DESC",
                    [sgid, eid])

                print(len(current))
                for i in current:
                    print i[1]

                if len(current) != 0:
                    if topVer[1] <= current[0][2]:
                        # Current version is better therefore leave it.
                        topVer = current[0]
                        continue
                    else:
                        # Version is worse, Delete Version.
                        for server in servers:
                            Server(server[0],
                                   server[1]).send("DELETE", resource,
                                                   msgHeaders)

                # No version on machine therefore push topVersion

                if topVer[6] != "NULL":
                    status = 303
                    html = topVer[6]
                else:
                    status = 200
                    html = topVer[5]
                data = {
                    "name": name,
                    "resource": resource,
                    "status": "ACTIVE",
                    "version": topVer[1],
                    "mainScript": topVer[2],
                    "language": topVer[3],
                    "poolSize": topVer[4],
                    "HTTP_Status": status,
                    "HTML": html
                }

                for server in servers:
                    service_location = join("./services", str(sgid),
                                            str(topVer[1]))
                    resp = Server(server[0], server[1]).createService(
                        msgHeaders, server[2], server[3], service_location,
                        data)
                    print(resp)

                # Add to Database new push
                db().executeLiteral(
                    "INSERT INTO service_deployment_lkp VALUES (?,?,?)",
                    [eid, topVer[0], "ACTIVE"])

        # Update the database
        db().executeLiteral(
            "UPDATE services SET pid = ?, service_name = ?, resource_name = ?, description = ? WHERE sgid = ?",
            service_info)

        # Inform the user and return
        help.append_user_alerts(
            'info', 'Operation Successful',
            'You have sucessfully edited the service: ' + input.service_name +
            '.')
        return web.seeother("manage_services.html")
예제 #13
0
    def POST(self, pid):
        help.check_access()

        input = web.input(dev='', test='', stage='', live='')

        # Define permissions for server interactions.
        usr = (web.ctx.session).username
        token = (web.ctx.session).access_token
        msgHeaders = {"Username": usr, "Access-Token": token}

        # Determine removed environments and newly added environments.
        originalEnvs = [
            x[0] for x in db().executeLiteral(
                "SELECT eid FROM env_path_lkp WHERE pid = ? ORDER BY position DESC",
                [pid])
        ][::-1]

        newEnvs = []
        sections = [input.dev, input.test, input.stage, input.live]
        for sec in sections:
            array = sec.split(',')
            for name in array:
                if (name == ''): break
                output = db().executeLiteral(
                    "SELECT eid FROM environments WHERE name = ?", [name])[0]
                if (len(output) != 0):
                    newEnvs.append(output[0])

        removed = [x for x in originalEnvs if x not in newEnvs]
        persist = [x for x in newEnvs if x in originalEnvs]
        added = [x for x in newEnvs if x not in originalEnvs]

        if len(added) != 0 and len(persist) > 0:
            # There are some added environments, and some evironments remain from the original.
            cropped = newEnvs[:newEnvs.index(persist[-1]) + 1][::-1]
        else:
            cropped = []

        # Delete resouce from all removed environments.
        for env in removed:
            # Get Every Server in the environment info.
            envServers = db().executeLiteral(
                "SELECT machine_address, m_port_num FROM servers WHERE eid = ?",
                [env])
            # Get Every Service on the path on that environment
            envServices = db().executeLiteral(
                "SELECT sv.sid, ser.resource_name FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? AND ser.pid = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
                [env, pid])

            for eachServer in envServers:
                server = Server(eachServer[0], eachServer[1])

                for eachService in envServices:
                    server.send("DELETE", eachService[1], msgHeaders)

                    # Decouple service and environment.
                    db().executeLiteral(
                        "DELETE FROM service_deployment_lkp WHERE sid = ? AND eid = ?",
                        [eachService[0], env])

        # Get the sgid for every service on the path and create holder.
        best = [[x[0], 0, 0, 0, 0, 0, 0, 0, 0, 0] for x in db().executeLiteral(
            "SELECT sgid FROM services WHERE pid = ?", [pid])]
        for env in cropped:
            # Get a list of all servers for this environment.
            envServers = db().executeLiteral(
                "SELECT machine_address, m_port_num, ssh_username, ssh_password FROM servers WHERE eid = ?",
                [env])

            # For each of the services do the good stuff.
            for i in range(len(best)):
                # Extract the service information.
                serviceG = best[i]
                # Get the best version of that service group that is running on that machine.
                running = db().executeLiteral(
                    "SELECT sv.sid, ser.service_name, ser.resource_name, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? AND ser.sgid = ? GROUP BY ser.sgid ORDER BY sv.version DESC",
                    [env, serviceG[0]])

                print(env, len(running))

                count = 1
                if len(running) == 0 or running[0][4] < serviceG[4]:
                    # Service needs to be pushed too.
                    if len(running) != 0:
                        # Service running was old.
                        for ser in envServers:
                            Server(ser[0],
                                   ser[1]).send("DELETE", serviceG[3],
                                                msgHeaders)

                    print("length ", len(serviceG))
                    print(serviceG)

                    if serviceG[1] == 0:
                        # Service not set yet
                        print("NOT SET NOT SET")
                        continue

                    # Construct Data for push
                    if serviceG[8] == "NULL":
                        status = 303
                        HTML = serviceG[9]
                    else:
                        status = 200
                        HTML = serviceG[8]
                    data = {
                        "name": serviceG[2],
                        "resource": serviceG[3],
                        "status": "ACTIVE",
                        "version": serviceG[4],
                        "mainScript": serviceG[5],
                        "language": serviceG[6],
                        "poolSize": serviceG[7],
                        "HTTP_Status": status,
                        "HTML": HTML
                    }

                    print(data)

                    location = join("./services", str(serviceG[0]),
                                    str(serviceG[4]))
                    count = len(envServers)
                    for ser in envServers:
                        resp = Server(ser[0], ser[1]).createService(
                            msgHeaders, ser[2], ser[3], location, data)

                        if resp == -1 or resp[0] != "200":
                            web.debug(resp)
                            count -= 1
                else:
                    # It was a better serivce, replace it.
                    best[i] = [best[i][0]] + list(running[0])
                    serviceG = best[i]

                # Pushed or not the services is now coupled with environment if linked correctly.
                if count > 0:
                    if env not in persist:
                        db().executeLiteral(
                            "INSERT INTO service_deployment_lkp VALUES ( ?,?,?) ",
                            [env, serviceG[1], "ACTIVE"])
                else:
                    help.append_user_alerts(
                        "warning", "Service not deployed",
                        serviceG[2] + " was not deployed onto an environment.")

        # Delete all path environments and correctly increment the new path.
        db().executeLiteral("DELETE FROM env_path_lkp WHERE pid = ?", [pid])
        pos = 1
        for env in newEnvs:
            db().executeLiteral("INSERT INTO env_path_lkp VALUES ( ?,?,? )",
                                [pid, env, pos])
            pos += 1
        db().executeLiteral("UPDATE paths SET length = ? WHERE pid = ?",
                            [len(newEnvs), pid])

        help.append_user_alerts('info', 'Updated sucessfully',
                                'The path information has been updated.')
        return 200
    def POST(self, sid):

        sgid, resource, version = db().executeLiteral(
            "SELECT ser.sgid, ser.resource_name, sv.version FROM services ser, services_versions sv WHERE ser.sgid = sv.sgid AND sv.sid = ?",
            [sid])[0]
        eid = db().executeLiteral(
            "SELECT sdl.eid FROM service_deployment_lkp sdl, env_path_lkp epl WHERE sdl.sid = ? AND sdl.eid = epl.eid ORDER BY epl.position DESC",
            [sid])
        if len(eid) != 0:
            # Service on environments
            eid = eid[0][0]
            versions = db().executeLiteral(
                "SELECT ser.sgid, ser.service_name, ser.resource_name, sv.version, sv.main_script, sv.language, sv.poolsize, sv.get_html_file, sv.get_redirect FROM services ser, services_versions sv, service_deployment_lkp sdl WHERE ser.sgid = ? AND ser.sgid = sv.sgid AND sv.sid = sdl.sid AND sdl.eid = ? ORDER BY sv.version DESC",
                [sgid, eid])
            runningVersion = versions[0][3]
            count = 1
            if version == runningVersion:
                msgHeaders = {
                    "Username": web.ctx.session.username,
                    "Access-Token": web.ctx.session.access_token
                }
                servers = db().executeLiteral(
                    "SELECT machine_address, m_port_num, ssh_username, ssh_password, display_name FROM servers WHERE eid = ?",
                    [eid])

                for server in servers:
                    Server(server[0], server[1]).send("DELETE", resource,
                                                      msgHeaders)

                if len(versions) > 1:
                    old = versions[1]

                    if old[7] == "NULL" or (old[8] != "NULL" and old[8] != ""):
                        status = 303
                        html = old[8]
                    else:
                        status = 200
                        html = old[7]
                    data = {
                        "name": old[1],
                        "resource": old[2],
                        "status": "ACTIVE",
                        "version": old[3],
                        "mainScript": old[4],
                        "language": old[5],
                        "poolSize": old[6],
                        "HTTP_Status": status,
                        "HTML": html
                    }

                    location = join("./services", str(sgid), str(old[3]))

                    count = len(servers)
                    for server in servers:
                        resp = Server(server[0], server[1]).createService(
                            msgHeaders, server[2], server[3], location, data)

                        if resp == -1:
                            help.append_user_alerts(
                                "warning", "Unable to make Connection!",
                                "A connection to the server " + server[4] +
                                " could not be made! Service not installed.")
                            count -= 1
                        elif resp[0] != "200":
                            help.append_user_alerts(
                                "warning", "Server rejected new version",
                                server[4] +
                                " has rejected the installation of the service. Reason being: "
                                + resp[1])
                            count -= 1

            db().executeLiteral(
                "DELETE FROM service_deployment_lkp WHERE sid = ? AND eid = ?",
                [sid, eid])

            if count > 0:
                help.append_user_alerts(
                    "info", "Successfully Demoted Service",
                    "The service was Successfully demoted from it's current position"
                )
                raise web.seeother("service_details_" + str(sgid))
            else:
                help.append_user_alerts(
                    "warning", "Demoted Service but...",
                    "The service was Successfully demoted but the server was unable to rollback to the previous version."
                )
                raise web.seeother("service_details_" + str(sgid))
        else:
            # Service not on environments
            clearDir(join("./services", str(sgid), str(version)))
            db().executeLiteral("DELETE FROM services_versions WHERE sid = ?",
                                [sid])
            help.append_user_alerts(
                "info", "Version Deleted Entirely",
                "The service has been demoted from existance.")
            raise web.seeother("service_details_" + str(sgid))
    def POST(self, sid):
        help.check_access()

        # Locate where the service is currently
        serviceInfo = db().executeLiteral(
            "SELECT ser.*, sv.* FROM services ser, services_versions sv WHERE ser.sgid = sv.sgid AND sv.sid = ?",
            [sid])[0]
        sgid, pid, name, resource, version = serviceInfo[0], serviceInfo[
            1], serviceInfo[2], serviceInfo[3], serviceInfo[7]
        position = db().executeLiteral(
            "SELECT epl.position FROM services_versions sv, service_deployment_lkp sdl, env_path_lkp epl WHERE sv.sid = ? AND sv.sid = sdl.sid AND sdl.eid = epl.eid GROUP BY sv.sid ORDER BY epl.position DESC",
            [sid])
        if len(position) == 0:
            position = 0
        else:
            position = position[0][0]

        # Locate the environment for the service to be deployed too.
        next = db().executeLiteral(
            "SELECT eid FROM env_path_lkp WHERE pid = ? AND position = ?",
            [pid, position + 1])
        if len(next) == 0:
            help.append_user_alerts(
                "warning", "Already at the top!",
                "The service version is already on the final environment of its path."
            )
            raise web.seeother("/service_details_" + str(sid))
        else:
            next = next[0][0]

        # Decide if the service should be put onto the environment.
        top = db().executeLiteral(
            "SELECT sv.version FROM services_versions sv, service_deployment_lkp sdl WHERE sv.sgid = ? AND sv.sid = sdl.sid AND sdl.eid = ? GROUP BY sv.sgid ORDER BY sv.version DESC",
            [sgid, next])
        if len(top) == 0:
            top = 0
        else:
            top = top[0][0]

        count = 1
        if not top > version:

            msgHeaders = {
                "Username": web.ctx.session.username,
                "Access-Token": web.ctx.session.access_token
            }

            envServers = db().executeLiteral(
                "SELECT machine_address, m_port_num, ssh_username, ssh_password, display_name FROM servers WHERE eid = ?",
                [next])

            if top != 0:
                for server in envServers:
                    Server(server[0], server[1]).send("DELETE", resource,
                                                      msgHeaders)

            if serviceInfo[12] != "NULL" or serviceInfo[12] != '':
                status = 303
                html = serviceInfo[12]
            else:
                status = 200
                html = serviceInfo[11]
            data = {
                "name": name,
                "resource": resource,
                "status": "ACTIVE",
                "version": version,
                "mainScript": serviceInfo[10],
                "language": serviceInfo[9],
                "poolSize": serviceInfo[8],
                "HTTP_Status": status,
                "HTML": html
            }

            location = join("./services", str(sgid), str(version))

            count = len(envServers)
            for server in envServers:
                resp = Server(server[0],
                              server[1]).createService(msgHeaders, server[2],
                                                       server[3], location,
                                                       data)

                if resp == -1:
                    help.append_user_alerts(
                        "warning", "No Connection",
                        "Could not form a connection with " + server[4] +
                        ", Service not deployed.")
                    count -= 1
                elif resp[0] != "200":
                    help.append_user_alerts(
                        "warning", "Server rejected service!",
                        "The server rejected the service with reason: " +
                        resp[1])
                    count -= 1

        if count > 0:
            db().executeLiteral(
                "INSERT INTO service_deployment_lkp VALUES ( ?,?,? )",
                [next, sid, "ACTIVE"])
            help.append_user_alerts(
                'info', 'Promoted ' + name + ' successfully',
                'The service has not been deployed to all servers of the target environment'
            )
        else:
            help.append_user_alerts(
                "warning", "Failed to deploy",
                "Through connection issues or rejections, the service has failed to be promoted."
            )

        raise web.seeother("/service_details_" + str(sgid))
예제 #16
0
import sys
import socket
from AnalyticsServerInterface import Server

from itertools import product
from string import ascii_lowercase
keywords = map(''.join, product(ascii_lowercase, repeat=3))

if len(sys.argv) != 6:
    print(
        "Insufficent test arguments, please provide: address, aPort, mPort, username, token"
    )
    exit()

Analytics = Server(sys.argv[1], int(sys.argv[2]))
Management = Server(sys.argv[1], int(sys.argv[3]))

test_passed = 0
test_failed = 0

############################
print("Test one: Valid http request")
permissions = {"Username": sys.argv[4], "Access-Token": sys.argv[5]}
resp = Management.send("OPTIONS", "/", permissions)
if resp == "FAILED" or resp[0] != "200":
    print("FAILED")
    test_failed += 1
else:
    print("PASSED")
    test_passed += 1