update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b second try
[osm/SO.git] / rwcal / plugins / vala / rwcal_aws / rift / rwcal / aws / prepare_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
35 def prepare_vm_after_boot(drv,argument):
36 vm_inst = drv.get_instance(argument.server_id)
37 logger.info("Waiting for VM instance to get to running state")
38 vm_inst.wait_until_running()
39 logger.info("VM instance is now in running state")
40 if argument.vdu_name:
41 vm_inst.create_tags(Tags=[{'Key': 'Name','Value':argument.vdu_name}])
42 if argument.vdu_node_id is not None:
43 vm_inst.create_tags(Tags=[{'Key':'node_id','Value':argument.vdu_node_id}])
44
45 for index,port_id in enumerate(argument.vdu_port_list):
46 logger.info("Attaching network interface with id %s to VDU instance %s",port_id,vm_inst.id)
47 drv.attach_network_interface(NetworkInterfaceId = port_id,InstanceId = vm_inst.id,DeviceIndex=index+1)
48
49
50 def main():
51 """
52 Main routine
53 """
54 parser = argparse.ArgumentParser(description='Script to create AWS resources')
55 parser.add_argument('--aws_key',
56 action = "store",
57 dest = "aws_key",
58 type = str,
59 help='AWS Key')
60
61 parser.add_argument('--aws_secret',
62 action = "store",
63 dest = "aws_secret",
64 type = str,
65 help = "AWS Secret")
66
67 parser.add_argument('--aws_region',
68 action = "store",
69 dest = "aws_region",
70 type = str,
71 help = "AWS Region")
72
73 parser.add_argument('--server_id',
74 action = "store",
75 dest = "server_id",
76 type = str,
77 help = "Server ID on which boot operations needs to be performed")
78
79 parser.add_argument('--vdu_name',
80 action = "store",
81 dest = "vdu_name",
82 type = str,
83 help = "VDU name")
84
85 parser.add_argument('--vdu_node_id',
86 action = "store",
87 dest = "vdu_node_id",
88 help = "Node id for vdu")
89
90 parser.add_argument('--vdu_port_list',
91 action = "append",
92 dest = "vdu_port_list",
93 default = [],
94 help = "Port id list for vdu")
95
96 argument = parser.parse_args()
97
98 if not argument.aws_key:
99 logger.error("ERROR: AWS key is not configured")
100 sys.exit(1)
101 else:
102 logger.debug("Using AWS key: %s" %(argument.aws_key))
103
104 if not argument.aws_secret:
105 logger.error("ERROR: AWS Secret is not configured")
106 sys.exit(1)
107 else:
108 logger.debug("Using AWS Secret: %s" %(argument.aws_secret))
109
110 if not argument.aws_region:
111 logger.error("ERROR: AWS Region is not configured")
112 sys.exit(1)
113 else:
114 logger.debug("Using AWS Region: %s" %(argument.aws_region))
115
116 if not argument.server_id:
117 logger.error("ERROR: Server ID is not configured")
118 sys.exit(1)
119 else:
120 logger.debug("Using Server ID : %s" %(argument.server_id))
121
122 try:
123 pid = os.fork()
124 if pid > 0:
125 # exit for parent
126 sys.exit(0)
127 except OSError as e:
128 logger.error("fork failed: %d (%s)\n" % (e.errno, e.strerror))
129 sys.exit(2)
130
131 drv = aws_drv.AWSDriver(key = argument.aws_key,
132 secret = argument.aws_secret,
133 region = argument.aws_region)
134 prepare_vm_after_boot(drv, argument)
135 sys.exit(0)
136
137 if __name__ == "__main__":
138 main()
139
140