-
Notifications
You must be signed in to change notification settings - Fork 1
/
nova-install
executable file
·160 lines (137 loc) · 8.09 KB
/
nova-install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
# coding=utf-8
# Copyright 2013 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import sys
import signal
import argparse
from novaimagebuilder.Singleton import Singleton
from novaimagebuilder.OSInfo import OSInfo
from novaimagebuilder.Builder import Builder
class Arguments(Singleton):
def _singleton_init(self, *args, **kwargs):
super(Arguments, self)._singleton_init()
self.log = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
self.argparser = self._argparser_setup()
self.args = self.argparser.parse_args()
def _argparser_setup(self):
app_name = sys.argv[0].rpartition('/')[2]
description_text = """Creates a new VM image in Nova using an OS's native installation tools."""
argparser = argparse.ArgumentParser(description=description_text, prog=app_name)
argparser.add_argument('--os', help='The shortid of an OS. Required for both installation types.')
argparser.add_argument('--os_list', action='store_true', default=False,
help='Show the OS list available for image building.')
install_location_group = argparser.add_mutually_exclusive_group()
install_location_group.add_argument('--install_iso', help='Location of the installation media ISO.')
install_location_group.add_argument('--install_tree', help='Location of an installation file tree.')
argparser.add_argument('--install_script', type=argparse.FileType(),
help='Custom install script file to use instead of generating one.')
argparser.add_argument('--admin_pw', help='The password to set for the admin user in the image.')
argparser.add_argument('--license_key', help='License/product key to use if needed.')
argparser.add_argument('--arch', default='x86_64',
help='The architecture the image is built for. (default: %(default)s)')
argparser.add_argument('--disk_size', type=int, default=10,
help='Size of the image root disk in gigabytes. (default: %(default)s)')
argparser.add_argument('--instance_flavor', default='2',
help='The type of instance to use for building the image. (default: %(default)s)')
argparser.add_argument('--name', help='A name to assign to the built image.', default='new-image')
argparser.add_argument('--image_storage', choices=('glance', 'cinder', 'both'), default='glance',
help='Where to store the final image: glance, cinder, both (default: %(default)s)')
argparser.add_argument('--debug', action='store_true', default=False,
help='Print debugging output to the logfile. (default: %(default)s)')
argparser.add_argument('--direct_boot', action='store_true', default=False,
help='Provide kernel command line at launch of instance. Instead of building a syslinux image. (default: %(default)s)')
argparser.add_argument('--public', action='store_true', default=False,
help='Make image publically available in Glance. (default: %(default)s)')
argparser.add_argument('--inactivity_timeout', default='180',
help='Amount of seconds to wait for disk and network activity before timing out. (default: %(default)s)')
argparser.add_argument('--request_floating_ip', action='store_true', default=False,
help='Assign floating ip to the install instance. Some cloud providers don not allow access to outside world without a floating IP. (default: %(default)s)')
return argparser
class Application(Singleton):
def _singleton_init(self, *args, **kwargs):
super(Application, self)._singleton_init()
self.arguments = Arguments().args
self.log = self._logger(debug=self.arguments.debug)
if not self.log:
print 'No logger!!! stopping...'
sys.exit(1)
signal.signal(signal.SIGTERM, self.signal_handler)
self.osinfo = OSInfo()
self.builder = None
def _logger(self, debug=False):
if debug:
level = logging.DEBUG
else:
level = logging.WARNING
logging.basicConfig(level=level, format='%(asctime)s %(levelname)s %(name)s thread(%(threadName)s) Message: %(message)s')
logger = logging.getLogger('%s.%s' % (__name__, self.__class__.__name__))
#filehandler = logging.FileHandler('/var/log/%s' % sys.argv[0].rpartition('/')[2])
#formatter = logging.Formatter('%(asctime)s %(levelname)s %(name)s thread(%(threadName)s) Message: %(message)s')
#filehandler.setFormatter(formatter)
#logger.addHandler(filehandler)
return logger
def signal_handler(self, signum, stack):
if signum == signal.SIGTERM:
logging.warning('caught signal SIGTERM, stopping...')
if self.builder:
self.builder.abort()
sys.exit(0)
def main(self):
if self.arguments.os:
if self.arguments.install_iso:
location = self.arguments.install_iso
install_type = 'iso'
elif self.arguments.install_tree:
location = self.arguments.install_tree
install_type = 'tree'
else:
# if iso or tree is missing, print a message and exit non-zero
print('One of --install_iso or --install_tree must be given.')
return 1
install_config = {'admin_password': self.arguments.admin_pw,
'license_key': self.arguments.license_key,
'arch': self.arguments.arch,
'disk_size': self.arguments.disk_size,
'flavor': self.arguments.instance_flavor,
'storage': self.arguments.image_storage,
'name': self.arguments.name,
'direct_boot': self.arguments.direct_boot,
'public': self.arguments.public,
'timeout': int(self.arguments.inactivity_timeout),
'floating_ip': self.arguments.request_floating_ip}
self.builder = Builder(self.arguments.os,
install_location=location,
install_type=install_type,
install_script=self.arguments.install_script,
install_config=install_config)
# TODO: create a better way to run this.
# The inactivity timeout is 180 seconds
self.builder.run()
if not self.builder.wait_for_completion(install_config['timeout']):
sys.exit(1)
elif self.arguments.os_list:
# possible distro values from libosinfo (for reference):
# 'osx', 'openbsd', 'centos', 'win', 'mandrake', 'sled', 'sles', 'netbsd', 'winnt', 'fedora', 'solaris',
# 'rhel', 'opensuse', 'rhl', 'mes', 'ubuntu', 'debian', 'netware', 'msdos', 'gnome', 'opensolaris',
# 'freebsd', 'mandriva'
os_dict = self.osinfo.os_ids(distros={'fedora': 17, 'rhel': 5, 'ubuntu': 12, 'win': 6})
if len(os_dict) > 0:
for os in sorted(os_dict.keys()):
print '%s - %s' % (os, os_dict[os])
else:
Arguments().argparser.parse_args(['--help'])
if __name__ == '__main__':
sys.exit(Application().main())