1 package org.kuali.common.devops.archive.sas.test; 2 3 import static com.google.common.collect.Iterables.filter; 4 import static com.google.common.collect.Ordering.natural; 5 import static com.google.common.collect.Sets.newHashSet; 6 import static java.lang.Integer.parseInt; 7 import static java.lang.String.format; 8 import static java.nio.file.Files.createSymbolicLink; 9 import static java.nio.file.Files.delete; 10 import static java.nio.file.Files.isSymbolicLink; 11 import static org.apache.commons.io.FileUtils.readFileToString; 12 import static org.apache.commons.lang.StringUtils.substringBetween; 13 import static org.apache.commons.lang3.StringUtils.removeStart; 14 import static org.kuali.common.core.io.Files.listParents; 15 import static org.kuali.common.core.io.Paths.fromFile; 16 import static org.kuali.common.util.encrypt.Encryption.getDefaultEncryptor; 17 import static org.kuali.common.util.log.Loggers.newLogger; 18 19 import java.io.File; 20 import java.util.List; 21 import java.util.Set; 22 23 import org.junit.Test; 24 import org.kuali.common.aws.model.ImmutableAWSCredentials; 25 import org.kuali.common.aws.s3.DefaultS3Service; 26 import org.kuali.common.aws.s3.S3Service; 27 import org.kuali.common.aws.s3.model.ObjectSummary; 28 import org.kuali.common.devops.archive.sas.HostnameToKey; 29 import org.kuali.common.util.encrypt.Encryptor; 30 import org.slf4j.Logger; 31 32 import com.amazonaws.auth.AWSCredentials; 33 import com.google.common.base.Function; 34 import com.google.common.base.Predicate; 35 36 public class RestoreFilesTest { 37 38 private static final Logger logger = newLogger(); 39 40 @Test 41 public void test() { 42 try { 43 String hostname = "testci.kuali.org"; 44 String bucket = "archive.kuali.org"; 45 String job = "/home/tomcat7/.jenkins/jobs"; 46 String prefix = HostnameToKey.INSTANCE.apply(hostname) + job; 47 info("bucket ---> %s", bucket); 48 info("hostname -> %s", hostname); 49 info("prefix ---> %s", prefix); 50 S3Service s3 = DefaultS3Service.build(getFoundation()); 51 List<ObjectSummary> archives = s3.getCompleteList(bucket, prefix); 52 List<ObjectSummary> sorted = natural().onResultOf(GetKey.INSTANCE).immutableSortedCopy(filter(archives, HasSize.INSTANCE)); 53 info("files ----> %s", sorted.size()); 54 int count = 0; 55 for (ObjectSummary archive : sorted) { 56 count++; 57 File file = getFile(hostname, archive.getKey()); 58 Set<File> set = newHashSet(); 59 set.addAll(listParents(file)); 60 List<File> parents = natural().immutableSortedCopy(set); 61 for (File parent : parents) { 62 boolean symbolicLink = isSymbolicLink(fromFile(parent)); 63 if (symbolicLink) { 64 info("delete symbolic link -> %s", parent); 65 delete(fromFile(parent)); 66 } 67 } 68 info("create file ----------> %s %s of %s", count, sorted.size(), file); 69 s3.copyObjectToFile(bucket, archive.getKey(), file); 70 if (file.getName().equals("build.xml")) { 71 String contents = readFileToString(file); 72 int buildNumber = parseInt(substringBetween(contents, "<number>", "</number>")); 73 File parent = file.getParentFile(); 74 File link = new File(parent.getParent(), buildNumber + ""); 75 File target = parent; 76 if (!link.exists()) { 77 createSymbolicLink(fromFile(link), fromFile(target).getFileName()); 78 } 79 } 80 } 81 } catch (Throwable e) { 82 e.printStackTrace(); 83 } 84 } 85 86 private enum HasSize implements Predicate<ObjectSummary> { 87 INSTANCE; 88 89 @Override 90 public boolean apply(ObjectSummary summary) { 91 return summary.getSize() > 0; 92 } 93 } 94 95 private enum GetKey implements Function<ObjectSummary, String> { 96 INSTANCE; 97 98 @Override 99 public String apply(ObjectSummary summary) { 100 return summary.getKey(); 101 } 102 } 103 104 private File getFile(String hostname, String key) { 105 String prefix = HostnameToKey.INSTANCE.apply(hostname); 106 String path = removeStart(key, prefix); 107 return new File(path); 108 } 109 110 private static AWSCredentials getFoundation() { 111 Encryptor enc = getDefaultEncryptor(); 112 String accessKey = enc.decrypt("U2FsdGVkX19A2e6dN/ipVfb/9n0DROCPIrLK6H8PvvPmt0h6cBqccGaJW0NSoX3S"); 113 String secretKey = enc.decrypt("U2FsdGVkX19Y9SZ5GAU82/X5Z0xZdeQf7DFuVDW07R9lfyHK4VaOj5R7pviRBKmIyn7jrVT2lv8Edeu7098k1A=="); 114 return new ImmutableAWSCredentials(accessKey, secretKey); 115 } 116 117 protected void info(String msg, Object... args) { 118 logger.info((args == null || args.length == 0) ? msg : format(msg, args)); 119 } 120 }