Coverage for src/tagmania/stop_cluster.py: 92%

12 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-08-02 01:51 +0000

1"""AWS Cluster Stop CLI. 

2 

3This module provides a command-line interface for stopping all EC2 instances 

4in a cluster identified by their "Cluster" tag. It's a simple utility that 

5stops all running instances in the specified cluster. 

6 

7The tool relies on EC2 instances having "Cluster" tags to identify which 

8instances belong to the cluster that should be stopped. 

9 

10Features: 

11 - Stop all instances in a cluster simultaneously 

12 - Support for AWS profile selection 

13 - Simple command-line interface 

14 - Confirmation message on completion 

15 

16Usage: 

17 The module is typically invoked via the cluster-stop CLI command: 

18 

19 ```bash 

20 # Stop a cluster using default AWS profile 

21 cluster-stop production-web 

22 

23 # Stop a cluster using specific AWS profile 

24 cluster-stop --profile myprofile production-web 

25 ``` 

26 

27Note: 

28 This operation stops all instances in the cluster regardless of their 

29 current state. Stopped instances are not affected. 

30 

31Warning: 

32 Stopping instances will interrupt any running processes and may result 

33 in data loss if applications don't handle graceful shutdown properly. 

34""" 

35 

36import argparse 

37 

38from tagmania.iac_tools.clusterset import ClusterSet 

39 

40 

41def main(): 

42 """Main entry point for the cluster stop CLI. 

43 

44 Parses command line arguments to identify the cluster name and optional 

45 AWS profile, then stops all EC2 instances in the specified cluster. 

46 

47 Args: 

48 Command line arguments are parsed internally: 

49 - cluster: Name of the cluster to stop (required) 

50 - --profile: AWS profile to use (optional) 

51 

52 Raises: 

53 SystemExit: On invalid command line arguments. 

54 AWSError: On AWS API failures during instance stop operations. 

55 """ 

56 parser = argparse.ArgumentParser( 

57 description="AWS cluster stop tool.", 

58 epilog='This tool relies on the "Cluster" and "Owner" tags on instances and ' 

59 "volumes. IAC automation puts this in place." 

60 "Stops the cluster CLUSTER.", 

61 ) 

62 parser.add_argument( 

63 "cluster", 

64 help="the name CLUSTER of the cluster in question. This can be found by looking at any node " 

65 'in the AWS console and looking for the "Cluster" tag.', 

66 ) 

67 

68 parser.add_argument("--profile", "-p", help="the AWS profile to use", default=None) 

69 args = parser.parse_args() 

70 

71 cluster = ClusterSet(args.cluster, profile=args.profile) 

72 cluster.stop_instances() 

73 print(f"Cluster {cluster.cluster_names} stopped successfully.") 

74 

75 

76if __name__ == "__main__": 

77 main()