From f6e74b24509e35938fbbb0e7239eb8e4644afb31 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Thu, 29 Aug 2013 16:35:26 +0000 Subject: exts: Add openstack configure/write exts openstackssh.write: Write extension which deploy a raw image of baserock directly to an OpenStack machine using python-glanceclient. The raw image deployed has modified its bootloader to use virtio disks. vdaboot.configure: Configuration extension to change the mount point of "/" to use virtio disks (/dev/vda). --- openstack.write | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 openstack.write (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write new file mode 100755 index 00000000..8ee8767e --- /dev/null +++ b/openstack.write @@ -0,0 +1,140 @@ +#!/usr/bin/python +# Copyright (C) 2013 Codethink Limited +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + +'''A Morph deployment write extension for deploying to OpenStack.''' + + +import cliapp +import os +import tempfile +import urlparse + +import morphlib.writeexts + + +class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): + + '''Configure a raw disk image into an OpenStack host. + + The raw disk image is created during Morph's deployment and the + image is deployed in OpenStack using python-glanceclient. + + The location command line argument is the authentification url + of the OpenStack server using the following syntax: + + http://HOST:PORT/VERSION + + where + + * HOST is the host running OpenStack + * PORT is the port which is using OpenStack for authentifications. + * VERSION is the authentification version of OpenStack (Only v2.0 + supported) + + This extension needs in the environment the following variables: + + * OPENSTACK_USER is the username to use in the deployment. + * OPENSTACK_TENANT is the project name to use in the deployment. + * OPENSTACK_IMAGENAME is the name of the image to create. + * OPENSTACK_PASSWORD is the password of the user. + + + The extension will connect to OpenStack using python-glanceclient + to configure a raw image. + + ''' + + def process_args(self, args): + if len(args) != 2: + raise cliapp.AppException('Wrong number of command line args') + + temp_root, location = args + self.check_location(location) + + os_params = self.get_openstack_parameters() + + fd, raw_disk = tempfile.mkstemp() + os.close(fd) + self.create_local_system(temp_root, raw_disk) + self.status(msg='Temporary disk image has been created at %s' + % raw_disk) + + self.set_extlinux_root_to_virtio(raw_disk) + + self.configure_openstack_image(raw_disk, location, os_params) + + def set_extlinux_root_to_virtio(self, raw_disk): + '''Re-configures extlinux to use virtio disks''' + self.status(msg='Updating extlinux.conf') + mp = self.mount(raw_disk) + try: + path = os.path.join(mp, 'extlinux.conf') + + with open(path) as f: + extlinux_conf = f.read() + + extlinux_conf = extlinux_conf.replace('root=/dev/sda', + 'root=/dev/vda') + with open(path, "w") as f: + f.write(extlinux_conf) + + finally: + self.unmount(mp) + + def get_openstack_parameters(self): + '''Check the environment variables needed and returns all. + + The environment variables are described in the class documentation. + ''' + + keys = ('OPENSTACK_USER', 'OPENSTACK_TENANT', + 'OPENSTACK_IMAGENAME', 'OPENSTACK_PASSWORD') + for key in keys: + if key not in os.environ: + raise cliapp.AppException(key + ' was not given') + return (os.environ[key] for key in keys) + + def check_location(self, location): + x = urlparse.urlparse(location) + if x.scheme != 'http': + raise cliapp.AppException('URL schema must be http in %s' \ + % location) + if (x.path != '/v2.0' and x.path != '/v2.0/'): + raise cliapp.AppException('API version must be v2.0 in %s'\ + % location) + + def configure_openstack_image(self, raw_disk, auth_url, os_params): + '''Configure the image in OpenStack using glance-client''' + self.status(msg='Configuring OpenStack image...') + + username, tenant_name, image_name, password = os_params + cmdline = ['glance', + '--os-username', username, + '--os-tenant-name', tenant_name, + '--os-password', password, + '--os-auth-url', auth_url, + 'image-create', + '--name=%s' % image_name, + '--disk-format=raw', + '--container-format', 'bare', + '--file', raw_disk] + cliapp.runcmd(cmdline) + + self.status(msg='Image configured.') + +OpenStackWriteExtension().run() + -- cgit v1.2.1 From 7f566024ace800ca358e1bc73ce7e5c237ed6e21 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Fri, 5 Sep 2014 14:50:31 +0100 Subject: Check OpenStack credentials in openstack.check If the credentials are wrong, then morph will fail before attempting the OpenStack deployment. To achieve that openstack.check will attempt to run `glance image-list`. --- openstack.write | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index 8ee8767e..ac2e2c8a 100755 --- a/openstack.write +++ b/openstack.write @@ -96,27 +96,15 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): self.unmount(mp) def get_openstack_parameters(self): - '''Check the environment variables needed and returns all. + '''Get the environment variables needed. The environment variables are described in the class documentation. ''' keys = ('OPENSTACK_USER', 'OPENSTACK_TENANT', 'OPENSTACK_IMAGENAME', 'OPENSTACK_PASSWORD') - for key in keys: - if key not in os.environ: - raise cliapp.AppException(key + ' was not given') return (os.environ[key] for key in keys) - def check_location(self, location): - x = urlparse.urlparse(location) - if x.scheme != 'http': - raise cliapp.AppException('URL schema must be http in %s' \ - % location) - if (x.path != '/v2.0' and x.path != '/v2.0/'): - raise cliapp.AppException('API version must be v2.0 in %s'\ - % location) - def configure_openstack_image(self, raw_disk, auth_url, os_params): '''Configure the image in OpenStack using glance-client''' self.status(msg='Configuring OpenStack image...') -- cgit v1.2.1 From 03e794ca09e1583872fcc1c560a5ec7016983cb3 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Tue, 9 Sep 2014 17:49:22 +0100 Subject: Fix openstack write/check exts to pass the tests --- openstack.write | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index ac2e2c8a..dc18f9aa 100755 --- a/openstack.write +++ b/openstack.write @@ -1,5 +1,5 @@ #!/usr/bin/python -# Copyright (C) 2013 Codethink Limited +# Copyright (C) 2013 - 2014 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -- cgit v1.2.1 From abffc90e8a8150a1279f1d9c9722239e832e7172 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Tue, 9 Sep 2014 17:51:28 +0100 Subject: Fix up openstack.write extension The openstack.write extension was calling a nonexistent method 'check_location'. This metod was moved to openstack.check in the commit ba7d1d1ed3bad002ce36e5d4adf4e3794625091a. --- openstack.write | 1 - 1 file changed, 1 deletion(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index dc18f9aa..516fe367 100755 --- a/openstack.write +++ b/openstack.write @@ -63,7 +63,6 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): raise cliapp.AppException('Wrong number of command line args') temp_root, location = args - self.check_location(location) os_params = self.get_openstack_parameters() -- cgit v1.2.1 From beb86b3ad69f0712d432bdef1dad609171bfeb7a Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Mon, 1 Dec 2014 15:25:21 +0000 Subject: writeexts.py: convert 'mount' to context manager --- openstack.write | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index 516fe367..b1941d3c 100755 --- a/openstack.write +++ b/openstack.write @@ -79,8 +79,7 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): def set_extlinux_root_to_virtio(self, raw_disk): '''Re-configures extlinux to use virtio disks''' self.status(msg='Updating extlinux.conf') - mp = self.mount(raw_disk) - try: + with self.mount(raw_disk) as mp: path = os.path.join(mp, 'extlinux.conf') with open(path) as f: @@ -91,9 +90,6 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): with open(path, "w") as f: f.write(extlinux_conf) - finally: - self.unmount(mp) - def get_openstack_parameters(self): '''Get the environment variables needed. -- cgit v1.2.1 From bf19f21a4820f2cd0a08a8a5fb397932fc851cb6 Mon Sep 17 00:00:00 2001 From: Pete Fotheringham Date: Tue, 2 Dec 2014 13:58:13 +0000 Subject: OpenStack write extension documentation --- openstack.write | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index b1941d3c..faf47f54 100755 --- a/openstack.write +++ b/openstack.write @@ -28,40 +28,11 @@ import morphlib.writeexts class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): - '''Configure a raw disk image into an OpenStack host. - - The raw disk image is created during Morph's deployment and the - image is deployed in OpenStack using python-glanceclient. - - The location command line argument is the authentification url - of the OpenStack server using the following syntax: - - http://HOST:PORT/VERSION - - where - - * HOST is the host running OpenStack - * PORT is the port which is using OpenStack for authentifications. - * VERSION is the authentification version of OpenStack (Only v2.0 - supported) - - This extension needs in the environment the following variables: - - * OPENSTACK_USER is the username to use in the deployment. - * OPENSTACK_TENANT is the project name to use in the deployment. - * OPENSTACK_IMAGENAME is the name of the image to create. - * OPENSTACK_PASSWORD is the password of the user. - - - The extension will connect to OpenStack using python-glanceclient - to configure a raw image. - - ''' def process_args(self, args): if len(args) != 2: raise cliapp.AppException('Wrong number of command line args') - + temp_root, location = args os_params = self.get_openstack_parameters() @@ -69,7 +40,7 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): fd, raw_disk = tempfile.mkstemp() os.close(fd) self.create_local_system(temp_root, raw_disk) - self.status(msg='Temporary disk image has been created at %s' + self.status(msg='Temporary disk image has been created at %s' % raw_disk) self.set_extlinux_root_to_virtio(raw_disk) @@ -120,4 +91,3 @@ class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): self.status(msg='Image configured.') OpenStackWriteExtension().run() - -- cgit v1.2.1 From bfc74c3a2a1c5058e18653f4fb28e0390b66d520 Mon Sep 17 00:00:00 2001 From: Pete Fotheringham Date: Fri, 5 Dec 2014 15:34:07 +0000 Subject: Add a reference to write.help file --- openstack.write | 1 + 1 file changed, 1 insertion(+) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index faf47f54..d29d2661 100755 --- a/openstack.write +++ b/openstack.write @@ -28,6 +28,7 @@ import morphlib.writeexts class OpenStackWriteExtension(morphlib.writeexts.WriteExtension): + '''See openstack.write.help for documentation''' def process_args(self, args): if len(args) != 2: -- cgit v1.2.1 From ed741d8d090086e2380f7b9d68ddc3bd122acb9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jard=C3=B3n?= Date: Fri, 13 Mar 2015 18:18:55 +0000 Subject: Use the modern way of the GPL copyright header: URL instead real address Change-Id: I992dc0c1d40f563ade56a833162d409b02be90a0 --- openstack.write | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'openstack.write') diff --git a/openstack.write b/openstack.write index d29d2661..67e07c18 100755 --- a/openstack.write +++ b/openstack.write @@ -1,5 +1,5 @@ #!/usr/bin/python -# Copyright (C) 2013 - 2014 Codethink Limited +# Copyright (C) 2013-2015 Codethink Limited # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -11,8 +11,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# with this program. If not, see . '''A Morph deployment write extension for deploying to OpenStack.''' -- cgit v1.2.1