update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwcal / plugins / vala / rwcal_aws / rift / rwcal / aws / delete_vm.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 import rift.rwcal.aws as aws_drv
20 import logging
21 import argparse
22 import rwlogger
23 import sys, os, time
24
25 logging.basicConfig(level=logging.DEBUG)
26
27 logger = logging.getLogger()
28 rwlog_handler = rwlogger.RwLogger(category="rw-cal-log",
29 subcategory="aws",)
30 logger.addHandler(rwlog_handler)
31 #logger.setLevel(logging.DEBUG)
32
33
34 def cleanup_vm(drv,argument):
35 vm_inst = drv.get_instance(argument.server_id)
36 logger.info("Waiting for VM instance to get to terminating state")
37 vm_inst.wait_until_terminated()
38 logger.info("VM inst is now in terminating state")
39
40 for port_id in argument.vdu_port_list:
41 logger.info("Deleting network interface with id %s",port_id)
42 port = drv.get_network_interface(port_id)
43 if port:
44 if port.association_attribute and 'AssociationId' in port.association_attribute:
45 drv.disassociate_public_ip_from_network_interface(NetworkInterfaceId=port.id)
46 drv.delete_network_interface(port.id)
47 else:
48 logger.error("Newtork interface with id %s not found when deleting interface",port_id)
49
50
51 def main():
52 """
53 Main routine
54 """
55 parser = argparse.ArgumentParser(description='Script to create AWS resources')
56 parser.add_argument('--aws_key',
57 action = "store",
58 dest = "aws_key",
59 type = str,
60 help='AWS Key')
61
62 parser.add_argument('--aws_secret',
63 action = "store",
64 dest = "aws_secret",
65 type = str,
66 help = "AWS Secret")
67
68 parser.add_argument('--aws_region',
69 action = "store",
70 dest = "aws_region",
71 type = str,
72 help = "AWS Region")
73
74 parser.add_argument('--server_id',
75 action = "store",
76 dest = "server_id",
77 type = str,
78 help = "Server ID on which delete operations needs to be performed")
79
80 parser.add_argument('--vdu_port_list',
81 action = "append",
82 dest = "vdu_port_list",
83 default = [],
84 help = "Port id list for vdu")
85
86 argument = parser.parse_args()
87
88 if not argument.aws_key:
89 logger.error("ERROR: AWS key is not configured")
90 sys.exit(1)
91 else:
92 logger.debug("Using AWS key: %s" %(argument.aws_key))
93
94 if not argument.aws_secret:
95 logger.error("ERROR: AWS Secret is not configured")
96 sys.exit(1)
97 else:
98 logger.debug("Using AWS Secret: %s" %(argument.aws_secret))
99
100 if not argument.aws_region:
101 logger.error("ERROR: AWS Region is not configured")
102 sys.exit(1)
103 else:
104 logger.debug("Using AWS Region: %s" %(argument.aws_region))
105
106 if not argument.server_id:
107 logger.error("ERROR: Server ID is not configured")
108 sys.exit(1)
109 else:
110 logger.debug("Using Server ID : %s" %(argument.server_id))
111
112 try:
113 pid = os.fork()
114 if pid > 0:
115 # exit for parent
116 sys.exit(0)
117 except OSError as e:
118 logger.error("fork failed: %d (%s)\n" % (e.errno, e.strerror))
119 sys.exit(2)
120
121 drv = aws_drv.AWSDriver(key = argument.aws_key,
122 secret = argument.aws_secret,
123 region = argument.aws_region)
124 cleanup_vm(drv, argument)
125 sys.exit(0)
126
127 if __name__ == "__main__":
128 main()
129
130