Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. package org.sample;
  2.  
  3. import com.google.gson.JsonElement;
  4. import com.google.gson.JsonParser;
  5.  
  6. import java.io.FileNotFoundException;
  7. import java.io.FileReader;
  8. import java.io.Reader;
  9. import java.time.Instant;
  10. import java.time.format.DateTimeFormatter;
  11. import java.time.temporal.TemporalAccessor;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15. import java.util.concurrent.TimeUnit;
  16.  
  17. /**
  18. * This class finds CREATE_COMPLETE stacks that are more than five days old,
  19. * and output a aws cloudformation delete command to stdout.
  20. * This simply outputs the result, no actual stacks will be deleted.
  21. * <p>
  22. * First run this command:
  23. * export AWS_DEFAULT_REGION=us-east-1
  24. * aws cloudformation describe-stacks --query 'Stacks[?StackStatus==`CREATE_COMPLETE`]' --output json > /tmp/cfn-output-json
  25. * This shows all create_complete stacks in a given region
  26. * <p>
  27. * Then, run this class.
  28. * Finally, run the output in bash.
  29. * <p>
  30. * Requirements:
  31. * gson library should be in classpath
  32. */
  33. public class CloudformationStackDeleter {
  34.  
  35. private static final int OLDER_THAN_DAYS = 4;
  36. private static final String CFN_DESCRIBE_STACK_JSON_OUTPUT = "/tmp/cfn-output-json";
  37.  
  38. public static void main(String[] args) throws FileNotFoundException {
  39. Reader r = new FileReader(CFN_DESCRIBE_STACK_JSON_OUTPUT);
  40. JsonParser parser = new JsonParser();
  41. final JsonElement parsed = parser.parse(r);
  42. // final JsonElement parsed = parser.parse("{id: 123}");
  43.  
  44. List<String> stacksToDel = new ArrayList<>();
  45. for (JsonElement element : parsed.getAsJsonArray()) {
  46. final JsonElement creationTime = element.getAsJsonObject().get("CreationTime");
  47. String creationTimeAsString = creationTime.getAsString();
  48. DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_INSTANT;
  49. TemporalAccessor accessor = timeFormatter.parse(creationTimeAsString);
  50. final Instant ctI = Instant.from(accessor);
  51. Date date = Date.from(ctI);
  52. Date now = new Date();
  53.  
  54. final long diff = Math.abs(now.getTime() - date.getTime());
  55. final long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
  56. if (days > OLDER_THAN_DAYS) {
  57. final JsonElement stackName = element.getAsJsonObject().get("StackName");
  58. stacksToDel.add(stackName.getAsString());
  59. }
  60. }
  61.  
  62. System.out.println("Stacks to delete: " + stacksToDel.size());
  63. for (String name : stacksToDel) {
  64. System.out.println("aws cloudformation delete-stack --stack-name " + name);
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement