def UpdateHuntOutputPluginState(self, hunt_id, state_index, update_fn, cursor=None): """Updates hunt output plugin state for a given output plugin.""" hunt_id_int = db_utils.HuntIDToInt(hunt_id) query = "SELECT hunt_id FROM hunts WHERE hunt_id = %s" rows_returned = cursor.execute(query, [hunt_id_int]) if rows_returned == 0: raise db.UnknownHuntError(hunt_id) columns = ", ".join(_HUNT_OUTPUT_PLUGINS_STATES_COLUMNS) query = ("SELECT {columns} FROM hunt_output_plugins_states " "WHERE hunt_id = %s AND plugin_id = %s".format( columns=columns)) rows_returned = cursor.execute(query, [hunt_id_int, state_index]) if rows_returned == 0: raise db.UnknownHuntOutputPluginStateError(hunt_id, state_index) state = self._HuntOutputPluginStateFromRow(cursor.fetchone()) modified_plugin_state = update_fn(state.plugin_state) query = ("UPDATE hunt_output_plugins_states " "SET plugin_state = %s " "WHERE hunt_id = %s AND plugin_id = %s") args = [ modified_plugin_state.SerializeToString(), hunt_id_int, state_index ] cursor.execute(query, args) return state
def WriteHuntOutputPluginsStates(self, hunt_id, states, cursor=None): """Writes hunt output plugin states for a given hunt.""" columns = ", ".join(_HUNT_OUTPUT_PLUGINS_STATES_COLUMNS) placeholders = mysql_utils.Placeholders( 2 + len(_HUNT_OUTPUT_PLUGINS_STATES_COLUMNS)) hunt_id_int = db_utils.HuntIDToInt(hunt_id) for index, state in enumerate(states): query = ("INSERT INTO hunt_output_plugins_states " "(hunt_id, plugin_id, {columns}) " "VALUES {placeholders}".format(columns=columns, placeholders=placeholders)) args = [hunt_id_int, index, state.plugin_descriptor.plugin_name] if state.plugin_descriptor.plugin_args is None: args.append(None) else: args.append( state.plugin_descriptor.plugin_args.SerializeToString()) args.append(state.plugin_state.SerializeToString()) try: cursor.execute(query, args) except MySQLdb.IntegrityError as e: raise db.UnknownHuntError(hunt_id=hunt_id, cause=e)
def UpdateHuntObject(self, hunt_id, duration=None, client_rate=None, client_limit=None, hunt_state=None, hunt_state_comment=None, start_time=None, num_clients_at_start_time=None, cursor=None): """Updates the hunt object by applying the update function.""" vals = [] args = {} if duration is not None: vals.append("duration_micros = %(duration_micros)s") args["duration_micros"] = duration.microseconds if client_rate is not None: vals.append("client_rate = %(client_rate)s") args["client_rate"] = client_rate if client_limit is not None: vals.append("client_limit = %(client_limit)s") args["client_limit"] = client_limit if hunt_state is not None: vals.append("hunt_state = %(hunt_state)s") args["hunt_state"] = int(hunt_state) if hunt_state_comment is not None: vals.append("hunt_state_comment = %(hunt_state_comment)s") args["hunt_state_comment"] = hunt_state_comment if start_time is not None: vals.append(""" init_start_time = IFNULL(init_start_time, FROM_UNIXTIME(%(start_time)s)) """) vals.append(""" last_start_time = FROM_UNIXTIME(%(start_time)s) """) args["start_time"] = mysql_utils.RDFDatetimeToTimestamp(start_time) if num_clients_at_start_time is not None: vals.append( "num_clients_at_start_time = %(num_clients_at_start_time)s") args["num_clients_at_start_time"] = num_clients_at_start_time vals.append("last_update_timestamp = NOW(6)") query = """ UPDATE hunts SET {updates} WHERE hunt_id = %(hunt_id)s """.format(updates=", ".join(vals)) args["hunt_id"] = db_utils.HuntIDToInt(hunt_id) rows_modified = cursor.execute(query, args) if rows_modified == 0: raise db.UnknownHuntError(hunt_id)
def ReadHuntOutputPluginsStates(self, hunt_id, cursor=None): """Reads all hunt output plugins states of a given hunt.""" columns = ", ".join(_HUNT_OUTPUT_PLUGINS_STATES_COLUMNS) query = ("SELECT {columns} FROM hunt_output_plugins_states " "WHERE hunt_id = %s".format(columns=columns)) rows_returned = cursor.execute(query, [db_utils.HuntIDToInt(hunt_id)]) if rows_returned > 0: states = [] for row in cursor.fetchall(): states.append(self._HuntOutputPluginStateFromRow(row)) return states query = "SELECT hunt_id FROM hunts WHERE hunt_id = %s" rows_returned = cursor.execute(query, [db_utils.HuntIDToInt(hunt_id)]) if rows_returned == 0: raise db.UnknownHuntError(hunt_id) return []
def ReadHuntObject(self, hunt_id, cursor=None): """Reads a hunt object from the database.""" query = ("SELECT {columns} " "FROM hunts WHERE hunt_id = %s".format( columns=_HUNT_COLUMNS_SELECT)) nr_results = cursor.execute(query, [db_utils.HuntIDToInt(hunt_id)]) if nr_results == 0: raise db.UnknownHuntError(hunt_id) return self._HuntObjectFromRow(cursor.fetchone())
def DeleteHuntObject(self, hunt_id, cursor=None): """Deletes a given hunt object.""" query = "DELETE FROM hunts WHERE hunt_id = %s" hunt_id_int = db_utils.HuntIDToInt(hunt_id) rows_deleted = cursor.execute(query, [hunt_id_int]) if rows_deleted == 0: raise db.UnknownHuntError(hunt_id) query = "DELETE FROM hunt_output_plugins_states WHERE hunt_id = %s" cursor.execute(query, [hunt_id_int])