Version Differences for Miscellaneous Scripts

(Initial Alphabetization)
(More alphabetization)
Line 260:
  }</pre>    }</pre> 
       
    + = Annihilate a Source Config on a Project =  
    + * This script needs to be ran as an evaluate script step. It will search for the first project that matches the name string and obliterate the source config on it. Please keep a backup of the database/project handy in case this is used accidentally/maliciously.  
    + ==== AHPSCRIPTS-130 ====  
    + <pre>import com.urbancode.anthill3.domain.project.Project;  
    + import com.urbancode.anthill3.domain.project.ProjectFactory;  
    + import com.urbancode.anthill3.domain.workflow.Workflow;  
    + import com.urbancode.anthill3.domain.source.SourceConfig;  
    + import com.urbancode.anthill3.domain.profile.BuildProfile;  
    + import com.urbancode.anthill3.domain.profile.BuildProfileFactory;  
    + import com.urbancode.anthill3.domain.project.*;  
       
    + Project project = ProjectFactory.getInstance().restoreForName("PUT THE NAME OF YOUR PROJECT HERE");  
       
    + for (Workflow workflow : project.getOriginatingWorkflowArray()) {  
    + BuildProfile profile = workflow.getBuildProfile();  
    + SourceConfig sourceConfig = profile.getSourceConfig();  
    + sourceConfig.delete();  
    + profile.setSourceConfig(null);  
    + }</pre>  
       
    + = Build Step Via Script and Run It =  
    + * This was created for a customer who wanted to change some inputs to our CvsGetChangelog Step, but could be used for other steps as well.  
       
    + ==== AHPSCRIPTS-98 ====  
    + <pre>import com.urbancode.anthill3.domain.source.cvs.CvsGetChangelogStepConfig;  
    + import com.urbancode.anthill3.domain.buildlife.*;  
    + import com.urbancode.anthill3.services.jobs.*;  
    + import org.apache.log4j.Logger;  
    + Logger log = Logger.getLogger("script");  
       
    + CvsGetChangelogStepConfig stepConfig = new CvsGetChangelogStepConfig();  
    + var status = StatusLookup.getStatusByName("passed-tests");  
       
    + BuildLife buildLife = BuildLifeLookup.mostRecentByStatus(status);  
    + if (buildLife == null) {  
    + var array = BuildLifeFactory.getInstance().restoreAllBefore(BuildLifeLookup.getCurrent());  
    + for (int i = array.length - 1; i >= 0; i--) {  
    + var curBuildLife = array[i];  
    + if (curBuildLife.hasStatus(ProjectLookup.getCurrent().getStatusGroup().getSuccessStatus())) {  
    + buildLife = curBuildLife;  
    + break;  
    + }  
    + }  
    + }  
       
    + stepConfig.setStartDate(buildLife.getActualWorkspaceDate());  
    + var step = stepConfig.buildStep();  
    + var stepExecutor = StepExecutor.getCurrent();  
    + step.setAgent(JobTraceLookup.getCurrent().getAgent());  
       
    + try {  
    + stepExecutor.execute(step, "Generated Changelog Step");  
    + }  
    + catch (Throwable e) {  
    + log.error("Exception:", e);  
    + }</pre>  
       
    +  
    + = Change Owner of Project or Other Resource =  
    + This script must be run as the admin user. Just change the '''projectName''' and '''newOwnerName'''.  
       
    + ==== AHPSCRIPTS-35 ====  
    + <pre>import com.urbancode.anthill3.main.client.AnthillClient;  
    + import com.urbancode.anthill3.persistence.UnitOfWork;  
    + import com.urbancode.anthill3.domain.project.*;  
    + import com.urbancode.anthill3.domain.security.*;  
       
       
    + String projectName = "JUnit";  
    + String newOwnerName = "user";  
       
       
    + String serverHost = "localhost";  
    + int serverPort = 4567;  
    + String userName = "admin";  
    + String password = "admin";  
       
    + //serverHost = bsh.args[0];  
    + //serverPort = Integer.parseInt(bsh.args[1]);  
    + //userName = bsh.args[2];  
    + //password = bsh.args[3];  
       
    + // obtain connection to the Anthill server  
    + AnthillClient anthill = AnthillClient.connect(serverHost, serverPort,  
    + userName, password);  
       
    + // create a Unit of Work  
    + UnitOfWork uow = anthill.createUnitOfWork();  
       
       
    + // Project  
    + Project project = ProjectFactory.getInstance().restoreForName(projectName);  
    + if (project == null) {  
    + throw new Exception("Project not found. Please check the project name and if this user has permissions to it.");  
    + }  
       
    + User user = UserFactory.getInstance().restoreForName(newOwnerName);  
    + if (user == null) {  
    + throw new Exception("New Owner User not found. Please check the user name and if this user has permissions to it.");  
    + }  
       
    + Role ownerRole = RoleFactory.getInstance().restoreUserRole(user);  
    + if (ownerRole == null) {  
    + throw new Exception("New Owner User Role not found. This could be due to data inconsistency, every user should have a unique role.");  
    + }  
       
    + Resource resource = ResourceFactory.getInstance().restoreForPersistent(project);  
    + if (resource == null) {  
    + throw new Exception("Project Resource not found. Please contact support.");  
    + }  
       
    + Permission[] permissions = PermissionFactory.getInstance().restoreAllForResource(resource);  
    + for (int p=0; p<permissions.length; p++) {  
    + System.out.println("Found " + projectName + " / " + permissions[p].getRole().getName() + " / " + permissions[p].getAction());  
    + if (permissions[p].getRole().isUserRole() && permissions[p].isDeleteable()) {  
    + System.out.println(" -- Removing this permission.");  
    + permissions[p].delete();  
    + }  
    + }  
    + uow.commit();  
       
    + System.out.println("====================");  
    + Permission read = new Permission(resource, "read", ownerRole);  
    + System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / read");  
    + read.store();  
    + Permission write = new Permission(resource, "write", ownerRole);  
    + System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / write");  
    + write.store();  
    + Permission security = new Permission(resource, "security", ownerRole);  
    + System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / security");  
    + security.store();  
       
    + // commit to database  
    + uow.commitAndClose();  
       
    + System.out.println("Changed owner of project " + project.getName() + " [" + project.getId() + "] to " + user.getName());</pre>  
       
    + = Clean up Massive Job Log Directories =  
    + *A script to find and remove unnecessary (empty) directories in the job log (or other) directory in the anthill server.  
    + ==== AHPSCRIPTS-116 ====  
    + <pre>/**  
    + * It is advisable that this script only be run by Anthill administrators.  
    + * This script will only remove empty directories left by our cleanup policy.  
    + * Typically directories are not removed because they are likely to be reused  
    + * soon. For some clients, the sheer number of jobs requires these empty  
    + * directories to be managed, despite not being part of the more typical  
    + * cleanup policy.  
    + *  
    + * This script does not remove any files! Only directories.  
    + *  
    + * To prevent conflicts, it is advisable to run this script during a time that  
    + * uses very little activity.  
    + *  
    + * This script uses the AH3ServerJobLogDir property. Ensure this is set when  
    + * running the script.  
    + *  
    + * If the AH3ServerJobLogDir property does not conform to the expected value,  
    + * it will not proceed. This is to prevent the accidental deletion of logs or  
    + * directories.  
    + *  
    + * During testing, the performance of the script is about 2000 directory reads  
    + * per second, and about 1000 directory deletes per second. Milage may vary.  
    + *  
    + * With very little effort, this script could be modified to handle this  
    + * procedure on any accessible directory. Note that the safeguards in place  
    + * should also be updated accordingly.  
    + */  
    + import com.urbancode.devilfish.services.var.VarService;  
    + import com.urbancode.anthill3.main.DefaultConfiguration;  
       
    + /**  
    + * function to delete the given directory if and only if  
    + * it consists of empty directories or is empty itself.  
    + */  
    + int deleteEmptyDirs(File dir) {  
    + int deleteCount = 0;  
    + // we only do this to directories  
    + if (dir.isDirectory()) {  
    + // verify we can list the files here  
    + try {  
    + // first, recurse for all directories in this directory  
    + for (File subdir : dir.listFiles()) {  
    + if(subdir.isDirectory()) {  
    + deleteCount += deleteEmptyDirs(subdir);  
    + }  
    + }  
       
    + // only delete if we managed to eliminate the subdirectories  
    + if (dir.listFiles().length == 0 && dir.delete()) {  
    + deleteCount++;  
    + }  
    + }  
    + catch (Exception e) {  
    + System.out.println("Permission access error in " + dir.toString() + ".");  
    + }  
    + }  
    + return deleteCount;  
    + }  
       
    + /**  
    + * Begin actual script.  
    + */  
    + VarService vs = VarService.getInstance();  
    + String logDirStr = vs.getVarValue(DefaultConfiguration.LOGS_DIR) + File.separator + "job";  
    + File logDir = new File(logDirStr);  
    + if(logDir == null) {  
    + throw new IllegalStateException("Log parent directory not found: " + logDir);  
    + }  
    + if(!logDir.isDirectory()) {  
    + throw new IllegalStateException("Log parent directory strangely not a directory: " + logDir);  
    + }  
       
    + // init metrics  
    + int totalDeletes = 0;  
    + long start = System.currentTimeMillis();  
       
    + // perform the delete  
    + for(File dir : logDir.listFiles()) {  
    + totalDeletes += deleteEmptyDirs(dir);  
    + }  
    + // calc metrics  
    + long end = System.currentTimeMillis();  
    + long secs = (end-start) / 1000;  
    + double deletionRate = ((double)totalDeletes) / ((double)secs);  
    + System.out.println("Deleted " + totalDeletes + " empty directories in " + secs + " seconds (" + deletionRate + " deletes per second).");</pre>  
    +  
  = Template for adding users to realms =    = Template for adding users to realms = 
       
Line 660:
       
       
- = Change Owner of Project or Other Resource =      
- This script must be run as the admin user. Just change the '''projectName''' and '''newOwnerName'''.      
       
- ==== AHPSCRIPTS-35 ====      
- <pre>import com.urbancode.anthill3.main.client.AnthillClient;      
- import com.urbancode.anthill3.persistence.UnitOfWork;      
- import com.urbancode.anthill3.domain.project.*;      
- import com.urbancode.anthill3.domain.security.*;      
       
       
- String projectName = "JUnit";      
- String newOwnerName = "user";      
       
       
- String serverHost = "localhost";      
- int serverPort = 4567;      
- String userName = "admin";      
- String password = "admin";      
       
- //serverHost = bsh.args[0];      
- //serverPort = Integer.parseInt(bsh.args[1]);      
- //userName = bsh.args[2];      
- //password = bsh.args[3];      
       
- // obtain connection to the Anthill server      
- AnthillClient anthill = AnthillClient.connect(serverHost, serverPort,      
- userName, password);      
       
- // create a Unit of Work      
- UnitOfWork uow = anthill.createUnitOfWork();      
       
       
- // Project      
- Project project = ProjectFactory.getInstance().restoreForName(projectName);      
- if (project == null) {      
- throw new Exception("Project not found. Please check the project name and if this user has permissions to it.");      
- }      
       
- User user = UserFactory.getInstance().restoreForName(newOwnerName);      
- if (user == null) {      
- throw new Exception("New Owner User not found. Please check the user name and if this user has permissions to it.");      
- }      
       
- Role ownerRole = RoleFactory.getInstance().restoreUserRole(user);      
- if (ownerRole == null) {      
- throw new Exception("New Owner User Role not found. This could be due to data inconsistency, every user should have a unique role.");      
- }      
       
- Resource resource = ResourceFactory.getInstance().restoreForPersistent(project);      
- if (resource == null) {      
- throw new Exception("Project Resource not found. Please contact support.");      
- }      
       
- Permission[] permissions = PermissionFactory.getInstance().restoreAllForResource(resource);      
- for (int p=0; p<permissions.length; p++) {      
- System.out.println("Found " + projectName + " / " + permissions[p].getRole().getName() + " / " + permissions[p].getAction());      
- if (permissions[p].getRole().isUserRole() && permissions[p].isDeleteable()) {      
- System.out.println(" -- Removing this permission.");      
- permissions[p].delete();      
- }      
- }      
- uow.commit();      
       
- System.out.println("====================");      
- Permission read = new Permission(resource, "read", ownerRole);      
- System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / read");      
- read.store();      
- Permission write = new Permission(resource, "write", ownerRole);      
- System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / write");      
- write.store();      
- Permission security = new Permission(resource, "security", ownerRole);      
- System.out.println("Creating " + projectName + " / " + ownerRole.getName() + " / security");      
- security.store();      
       
- // commit to database      
- uow.commitAndClose();      
       
- System.out.println("Changed owner of project " + project.getName() + " [" + project.getId() + "] to " + user.getName());</pre>      
  = Set Property Based on Results of RESOLVE DEPENDENCY ARTIFACTS Step =    = Set Property Based on Results of RESOLVE DEPENDENCY ARTIFACTS Step = 
  Use this script if you need a property to define whether the resolve step was successful or had conflicts.    Use this script if you need a property to define whether the resolve step was successful or had conflicts. 
Line 1629:
  }</pre>    }</pre> 
       
- = Build Step Via Script and Run It =      
- * This was created for a customer who wanted to change some inputs to our CvsGetChangelog Step, but could be used for other steps as well.      
       
- ==== AHPSCRIPTS-98 ====      
- <pre>import com.urbancode.anthill3.domain.source.cvs.CvsGetChangelogStepConfig;      
- import com.urbancode.anthill3.domain.buildlife.*;      
- import com.urbancode.anthill3.services.jobs.*;      
- import org.apache.log4j.Logger;      
- Logger log = Logger.getLogger("script");      
       
- CvsGetChangelogStepConfig stepConfig = new CvsGetChangelogStepConfig();      
- var status = StatusLookup.getStatusByName("passed-tests");      
       
- BuildLife buildLife = BuildLifeLookup.mostRecentByStatus(status);      
- if (buildLife == null) {      
- var array = BuildLifeFactory.getInstance().restoreAllBefore(BuildLifeLookup.getCurrent());      
- for (int i = array.length - 1; i >= 0; i--) {      
- var curBuildLife = array[i];      
- if (curBuildLife.hasStatus(ProjectLookup.getCurrent().getStatusGroup().getSuccessStatus())) {      
- buildLife = curBuildLife;      
- break;      
- }      
- }      
- }      
       
- stepConfig.setStartDate(buildLife.getActualWorkspaceDate());      
- var step = stepConfig.buildStep();      
- var stepExecutor = StepExecutor.getCurrent();      
- step.setAgent(JobTraceLookup.getCurrent().getAgent());      
       
- try {      
- stepExecutor.execute(step, "Generated Changelog Step");      
- }      
- catch (Throwable e) {      
- log.error("Exception:", e);      
- }</pre>      
       
  = Overwrite Dependency Pushed Properties =    = Overwrite Dependency Pushed Properties = 
       
Line 2378:
       
       
- = Clean up Massive Job Log Directories =      
- *A script to find and remove unnecessary (empty) directories in the job log (or other) directory in the anthill server.      
- ==== AHPSCRIPTS-116 ====      
- <pre>/**      
- * It is advisable that this script only be run by Anthill administrators.      
- * This script will only remove empty directories left by our cleanup policy.      
- * Typically directories are not removed because they are likely to be reused      
- * soon. For some clients, the sheer number of jobs requires these empty      
- * directories to be managed, despite not being part of the more typical      
- * cleanup policy.      
- *      
- * This script does not remove any files! Only directories.      
- *      
- * To prevent conflicts, it is advisable to run this script during a time that      
- * uses very little activity.      
- *      
- * This script uses the AH3ServerJobLogDir property. Ensure this is set when      
- * running the script.      
- *      
- * If the AH3ServerJobLogDir property does not conform to the expected value,      
- * it will not proceed. This is to prevent the accidental deletion of logs or      
- * directories.      
- *      
- * During testing, the performance of the script is about 2000 directory reads      
- * per second, and about 1000 directory deletes per second. Milage may vary.      
- *      
- * With very little effort, this script could be modified to handle this      
- * procedure on any accessible directory. Note that the safeguards in place      
- * should also be updated accordingly.      
- */      
- import com.urbancode.devilfish.services.var.VarService;      
- import com.urbancode.anthill3.main.DefaultConfiguration;      
       
- /**      
- * function to delete the given directory if and only if      
- * it consists of empty directories or is empty itself.      
- */      
- int deleteEmptyDirs(File dir) {      
- int deleteCount = 0;      
- // we only do this to directories      
- if (dir.isDirectory()) {      
- // verify we can list the files here      
- try {      
- // first, recurse for all directories in this directory      
- for (File subdir : dir.listFiles()) {      
- if(subdir.isDirectory()) {      
- deleteCount += deleteEmptyDirs(subdir);      
- }      
- }      
       
- // only delete if we managed to eliminate the subdirectories      
- if (dir.listFiles().length == 0 && dir.delete()) {      
- deleteCount++;      
- }      
- }      
- catch (Exception e) {      
- System.out.println("Permission access error in " + dir.toString() + ".");      
- }      
- }      
- return deleteCount;      
- }      
       
- /**      
- * Begin actual script.      
- */      
- VarService vs = VarService.getInstance();      
- String logDirStr = vs.getVarValue(DefaultConfiguration.LOGS_DIR) + File.separator + "job";      
- File logDir = new File(logDirStr);      
- if(logDir == null) {      
- throw new IllegalStateException("Log parent directory not found: " + logDir);      
- }      
- if(!logDir.isDirectory()) {      
- throw new IllegalStateException("Log parent directory strangely not a directory: " + logDir);      
- }      
       
- // init metrics      
- int totalDeletes = 0;      
- long start = System.currentTimeMillis();      
       
- // perform the delete      
- for(File dir : logDir.listFiles()) {      
- totalDeletes += deleteEmptyDirs(dir);      
- }      
- // calc metrics      
- long end = System.currentTimeMillis();      
- long secs = (end-start) / 1000;      
- double deletionRate = ((double)totalDeletes) / ((double)secs);      
- System.out.println("Deleted " + totalDeletes + " empty directories in " + secs + " seconds (" + deletionRate + " deletes per second).");</pre>      
       
  = Need Report of Agents Not In Environments =    = Need Report of Agents Not In Environments = 
  ==== AHPSCRIPTS-117 ====    ==== AHPSCRIPTS-117 ==== 
Line 2724:
       
  buildAndWait("project", "workflow");</pre>    buildAndWait("project", "workflow");</pre> 
       
- = Annihilate a Source Config on a Project =      
- * This script needs to be ran as an evaluate script step. It will search for the first project that matches the name string and obliterate the source config on it. Please keep a backup of the database/project handy in case this is used accidentally/maliciously.      
- ==== AHPSCRIPTS-130 ====      
- <pre>import com.urbancode.anthill3.domain.project.Project;      
- import com.urbancode.anthill3.domain.project.ProjectFactory;      
- import com.urbancode.anthill3.domain.workflow.Workflow;      
- import com.urbancode.anthill3.domain.source.SourceConfig;      
- import com.urbancode.anthill3.domain.profile.BuildProfile;      
- import com.urbancode.anthill3.domain.profile.BuildProfileFactory;      
- import com.urbancode.anthill3.domain.project.*;      
       
- Project project = ProjectFactory.getInstance().restoreForName("PUT THE NAME OF YOUR PROJECT HERE");      
       
- for (Workflow workflow : project.getOriginatingWorkflowArray()) {      
- BuildProfile profile = workflow.getBuildProfile();      
- SourceConfig sourceConfig = profile.getSourceConfig();      
- sourceConfig.delete();      
- profile.setSourceConfig(null);      
- }</pre>