4.5. Running scripts directly

In many occasions it is handy for administrators to run scripts directly. So, instead of having to create a custom operation script, then a custom operation, then granting permissions, refreshing the browser and running, there is a menu called Run script, which presents a text box where the script may be typed in or pasted, which can be executed directly. Of course, only the basic bindings are available.

The result of the script can be either a string, which is then displayed as plain text, or an object / map compatible with org.cyclos.model.system.scripts.ScriptResult. So, for example, to return an HTML text with a title, the script can return [title:"The result title", richText:"<b>Formatted</b> text"]. To show a notification, the the script can return [notification:"Notification text"]. The same prefixes available on notifications for custom operations are available on notifications: [INFO], [WARN] and [ERROR].

4.5.1. Examples

4.5.1.1. Remove all users, transactions and related data

Here is an example of a script to remove all regular users (not administrators) and related data, as well as all system to system transactions. This script must be executed in a network. Be advised that there will be no confirmation, and all users and all related data will be removed.

The script works by first recreating all database constraints with the option ON DELETE CASCADE. Then, all users are removed, which will cascade the removal to accounts, transfers, advertisements, records, messages, notifications, references and so on. For this reason all tables are locked, and the script will likely fail if there is any activity in the system, such as active users or background tasks.

Be careful when running in systems where specific users are used in the configuration, such as fees that are paid by a specific user, or payment types which are restricted to specific users. In such configurations, all such related data will be removed as well. Also, note that it may take a while to run, so, please, wait before the script completes.

AGAIN: be very careful when using this script! Only run it on test instances and always have a database backup before running it.

import org.cyclos.db.DeleteNetworkData
import org.cyclos.impl.utils.cache.CacheType
import org.cyclos.model.ValidationException

if (sessionData.network == null) {
    throw new ValidationException("This script can only be executed in a network")
}

def deleteNetworkData = beanHandler.autowire(DeleteNetworkData)
def users = deleteNetworkData.deleteUsersAndBanking(sessionData.network)
CacheType.all().each { cacheHandler.scheduleClear(it) }
searchHandler.reindex()
return "Removed ${users} users"