From 8f509fea52826d18df504f18ad0e702f54320f48 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 6 May 2014 16:49:19 +0100 Subject: [PATCH] mclu initial commit. --- .gitignore | 19 ++++ COPYING | 339 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Makefile.am | 55 +++++++++ README | 144 +++++++++++++++++++++++ config.py.in | 30 +++++ configure.ac | 54 +++++++++ lib.py | 108 ++++++++++++++++++ m4/.exists | 0 mclu.conf | 66 +++++++++++ mclu.in | 29 +++++ mclu.py | 124 ++++++++++++++++++++ mclu.spec.in | 54 +++++++++ mclu_build.py | 184 ++++++++++++++++++++++++++++++ mclu_console.py | 61 ++++++++++ mclu_info.py | 33 ++++++ mclu_list.py | 61 ++++++++++ mclu_migrate.py | 76 +++++++++++++ mclu_shutdown.py | 66 +++++++++++ mclu_start.py | 65 +++++++++++ mclu_status.py | 45 ++++++++ mclu_stop.py | 54 +++++++++ mclu_viewer.py | 62 ++++++++++ mclu_wake.py | 62 ++++++++++ node.py | 100 ++++++++++++++++ run.in | 22 ++++ xmls/.exists | 0 26 files changed, 1913 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 Makefile.am create mode 100644 README create mode 100644 config.py.in create mode 100644 configure.ac create mode 100644 lib.py create mode 100644 m4/.exists create mode 100644 mclu.conf create mode 100644 mclu.in create mode 100755 mclu.py create mode 100644 mclu.spec.in create mode 100644 mclu_build.py create mode 100644 mclu_console.py create mode 100644 mclu_info.py create mode 100644 mclu_list.py create mode 100644 mclu_migrate.py create mode 100644 mclu_shutdown.py create mode 100644 mclu_start.py create mode 100644 mclu_status.py create mode 100644 mclu_stop.py create mode 100644 mclu_viewer.py create mode 100644 mclu_wake.py create mode 100644 node.py create mode 100644 run.in create mode 100644 xmls/.exists diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b87c9e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +*~ +*.pyc + +Makefile +Makefile.in + +/aclocal.m4 +/autom4te.cache +/configure +/config.log +/config.py +/config.status +/local* +/install-sh +/mclu +/mclu.spec +/missing +/run +/xmls/*.xml diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/COPYING @@ -0,0 +1,339 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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; either version 2 of the License, or + (at your option) any later version. + + 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. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af13282 --- /dev/null +++ b/Makefile.am @@ -0,0 +1,55 @@ +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +ACLOCAL_AMFLAGS = -I m4 + +# This rule just ensures that the wrapper binary and Python files get +# installed when we do 'make install'. + +bin_SCRIPTS = mclu + +pkgdata_SCRIPTS = \ + config.py \ + lib.py \ + mclu.py \ + mclu_build.py \ + mclu_console.py \ + mclu_info.py \ + mclu_list.py \ + mclu_migrate.py \ + mclu_shutdown.py \ + mclu_start.py \ + mclu_status.py \ + mclu_stop.py \ + mclu_viewer.py \ + mclu_wake.py \ + node.py + +# Configuration file. + +confdir = $(sysconfdir)/$(PACKAGE_NAME) +conf_DATA = mclu.conf + +install-data-hook: + mkdir -p $(DESTDIR)/$(sysconfdir)/$(PACKAGE_NAME)/xmls + +CLEANFILES = \ + *~ \ + *.pyc \ + config.py \ + mclu \ + mclu.spec diff --git a/README b/README new file mode 100644 index 0000000..65aa631 --- /dev/null +++ b/README @@ -0,0 +1,144 @@ +mclu (mini cluster) is a virtualization cluster manager. + +The key features: + + - Entirely command line driven. + + - No required dependencies except libvirt and ssh. + + - Only designed for tiny clusters (up to around 10 hosts). + + - Nothing to install on the nodes except libvirtd and sshd. + + - Single, simple configuration file. + +Example commands +---------------------------------------------------------------------- + + mclu status Display status of the cluster + mclu list List all virtual machines on the cluster + mclu wake ham0 Wake up node 'ham0' in the cluster + mclu shutdown ham0 Shut down node 'ham0' in the cluster + mclu start ham0:vm Start vm on node 'ham0' + mclu stop ham0:* Stop all VMs on node 'ham0' + mclu migrate *:* ham2: Live migrate all VMs to 'ham2' + mclu build ham3:vm fedora-20 Build and run a new Fedora 20 VM on node 'ham3' + mclu console ham3:fedora-20 Show me the serial console of a VM + mclu viewer ham3:fedora-20 Show me the graphical console of a VM + mclu info Print general configuration information + mclu --help Print help on all commands + +Configuration notes +---------------------------------------------------------------------- + +mclu is based around the idea that you have a small collection of +fairly similar "nodes". Ideally they would be identical nodes, if you +want live migration to work seamlessly, but they don't have to be. + +Here is a picture of the cluster that I run mclu on: +http://rwmj.wordpress.com/2014/04/28/caseless-virtualization-cluster-part-5/#content + +The nodes can be up or down. mclu deals transparently with nodes +being switched off. If you configure wake-on-LAN (usually a BIOS +setting) then mclu will be able to wake up nodes. + +There is also one "control" node, which could be your laptop or could +be one of the cluster nodes. This is where you run the 'mclu' +command, and also where the single configuration file is located +(mclu.conf, usually located in /etc/mclu). The guest libvirt XML +files are also stored on the control node (usually /etc/mclu/xmls). + +Each node must be accessible from the control node over ssh. Each +node must be running the libvirt daemon (libvirtd). + +mclu uses a mix of ssh commands and remote libvirt to manage the +nodes. You should configure ssh so it can access the nodes without +needing passwords (eg. using ssh-agent). If you use the default +libvirt URI (see config file) then you also need to set up +passwordless root ssh access to the nodes; there are other ways to +configure this, eg. opening the libvirtd port on each node, but they +are probably not as secure. + +Each node, including the control node, must have access to shared +storage where the guest disk images are stored. The easiest way to do +this is to export /var/lib/libvirt/images from one machine and +NFS-mount it on all the nodes (and also to have a nice fast network). +Cluster filesystems are another possibility. mclu does NOT support +non-shared storage nor storage migration. + +Guests run on a single node at a time. You can list/start/stop/ +migrate them using mclu. The requirement for a guest to be running on +a single node may be enforced if you run libvirt sanlock or virtlockd. +This requires further configuration, see: +http://libvirt.org/locking.html +https://rwmj.wordpress.com/2014/05/08/setting-up-virtlockd-on-nfs/#content + +If sanlock/virtlockd is not running then mclu will try its best not to +have the guest running in two places at once (if it happens, this will +cause permanent disk corruption in the guest). + +For guest live migration to work transparently, you will probably want +to configure libvirt bridged networking and open firewall ports +49152-49215 on every node. + +Bridged networking means that each guest appears as a local machine on +your network, and if it migrates then network connections will not be +interrupted. See: +http://wiki.libvirt.org/page/Networking#Bridged_networking_.28aka_.22shared_physical_device.22.29 + +The firewall ports have to be opened because libvirt cannot (yet?) do +fully managed migration over two SSH connections (even though the +documentation says it can). Hopefully they will fix this soon. + +Dependencies +---------------------------------------------------------------------- + +To get a full list of the required and optional dependencies, look at: + + - configure.ac + - mclu.spec.in + +Building it +---------------------------------------------------------------------- + +If building straight from git, then do: + + autoreconf -i + +To build: + + ./configure --prefix /usr --sysconfdir /etc + make + +To run without installing: + + - Edit the configuration file (mclu.conf). + + - Run commands such as: + + ./run status + ./run list + +To install: + + - sudo make install + + - Edit the configuration file (/etc/mclu/mclu.conf). + + - Run commands such as: + + mclu status + mclu list + +Developer information +---------------------------------------------------------------------- + +The license is GPLv2+. + +The git repo is: + + http://git.annexia.org/?p=mclu.git;a=summary + +There is no mclu mailing list. Send patches to the virt-tools mailing list: + + http://www.redhat.com/mailman/listinfo/virt-tools-list diff --git a/config.py.in b/config.py.in new file mode 100644 index 0000000..d340d32 --- /dev/null +++ b/config.py.in @@ -0,0 +1,30 @@ +# mclu (mini cluster) +# @configure_input@ +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +PACKAGE_NAME="@PACKAGE_NAME@" +PACKAGE_VERSION="@PACKAGE_VERSION@" + +prefix="@prefix@" +sysconfdir="@sysconfdir@" +# Avoid stupid crap with datarootdir: +pkgdatadir="@prefix@/share/@PACKAGE_NAME@" + +SSH="@SSH@" +WOL="@WOL@" +VIRT_BUILDER="@VIRT_BUILDER@" +VIRT_VIEWER="@VIRT_VIEWER@" diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..3ba8958 --- /dev/null +++ b/configure.ac @@ -0,0 +1,54 @@ +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +AC_INIT([mclu],1.0) +AM_INIT_AUTOMAKE([foreign]) +AC_CONFIG_MACRO_DIR([m4]) + +dnl Python 2.x (required). +AC_PATH_PROG([PYTHON],[python]) +if test "x$PYTHON" = "xno"; then + AC_MSG_ERROR([Python 2.x is required]) +fi + +AC_MSG_CHECKING([Python version]) +PYTHON_VERSION_MAJOR=`$PYTHON -c "import sys; print (sys.version_info@<:@0@:>@)"` +PYTHON_VERSION_MINOR=`$PYTHON -c "import sys; print (sys.version_info@<:@1@:>@)"` +AC_MSG_RESULT([$PYTHON_VERSION_MAJOR.$PYTHON_VERSION_MINOR]) + +if test $PYTHON_VERSION_MAJOR -ne 2; then + AC_MSG_ERROR([Python version 2 is required, found $PYTHON_VERSION_MAJOR]) +fi + +dnl SSH client (required). +AC_PATH_PROG([SSH],[ssh],[no]) +if test "x$SSH" = "xno"; then + AC_MSG_ERROR([SSH client is required]) +fi + +dnl Wake-on-LAN client (optional). +AC_PATH_PROG([WOL],[wol],[no]) + +dnl virt-builder (optional). +AC_PATH_PROG([VIRT_BUILDER],[virt-builder],[no]) + +dnl virt-viewer (optional). +AC_PATH_PROG([VIRT_VIEWER],[virt-viewer],[no]) + +AC_CONFIG_FILES([run], [chmod +x,-w run]) +AC_CONFIG_FILES([Makefile config.py mclu mclu.spec]) +AC_OUTPUT diff --git a/lib.py b/lib.py new file mode 100644 index 0000000..a4981da --- /dev/null +++ b/lib.py @@ -0,0 +1,108 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import sys +import os +import re +import libvirt + +# Helper function to ping all the nodes. +def ping_nodes (nodes): + for node in nodes.values(): + node.ping () + +# Helper function to get node objects from a list of node names. +def get_nodes_by_name (all_nodes, names, all): + if not all: + return map (lambda name : get_node_by_name (all_nodes, name), names) + else: + return all_nodes.values() + +def get_node_by_name (nodes, name): + if name in nodes: + return nodes[name] + else: + sys.exit ("error: node does not exist: %s" % name) + +# Get separate list of running and inactive guests. +def get_all_guests (c, nodes): + running = {} + inactive = {} + + # Find running guests. + for node in nodes: + # Get the flat list of guests from this node: + doms = node.guests () + # Store which node the guest is running on, and turn list into a map. + for dom in doms: + name = dom.name() + if name in running: + sys.exit ("error: virtual machine %s is running on two nodes!" % name) + running[name] = { 'dom' : dom, 'node' : node } + + # Find inactive guests (XML configuration files). + for name in get_guest_configs (c, nodes): + if name not in running: + inactive[name] = name + + return running, inactive + +# Get the names of guests from the XML configuration files in xmls_dir. +def get_guest_configs (c, nodes): + names = [] + for filename in sorted (os.listdir (c['xmls_dir'])): + m = re.search (r'^(.*)\.xml$', filename) + if m: + names.append (m.group (1)) + return names + +# Convert virDomainState to string. +# Copied from virt-manager. +def pretty_run_status (status): + if status == libvirt.VIR_DOMAIN_RUNNING: + return "running" + elif status == libvirt.VIR_DOMAIN_PAUSED: + return "paused" + elif status == libvirt.VIR_DOMAIN_SHUTDOWN: + return "shutting down" + elif status == libvirt.VIR_DOMAIN_SHUTOFF: + return "shutoff" + elif status == libvirt.VIR_DOMAIN_CRASHED: + return "crashed" + elif status == libvirt.VIR_DOMAIN_PMSUSPENDED: + return "suspended" + +# Start a guest running on a given node. The node must not be +# running anywhere already. +def start_guest (c, node, guest_name): + fp = open ("%s/%s.xml" % (c['xmls_dir'], guest_name), "r") + xml = fp.read () + fp.close () + + conn = node.get_connection() + conn.createXML (xml) + +def pick_any_node_which_is_up (nodes): + node = None + for n in nodes.values(): + if n.ping (): + node = n + break + if not node: + sys.exit ("error: no nodes are up, use mclu wake [node|--all]") + return node diff --git a/m4/.exists b/m4/.exists new file mode 100644 index 0000000..e69de29 diff --git a/mclu.conf b/mclu.conf new file mode 100644 index 0000000..8ed1c39 --- /dev/null +++ b/mclu.conf @@ -0,0 +1,66 @@ +# mclu (mini cluster) configuration file. +# +# This file is parsed by Python's ConfigParser. +# +# This is the real configuration file I use to control my +# virtualization cluster. For a picture of it see: +# http://rwmj.wordpress.com/2014/04/28/caseless-virtualization-cluster-part-5/#content + +# Some defaults are provided: +# %(home)s expands to $HOME +# %(config_dir)s expands to the directory containing mclu.conf +# Note that in the Python '%(...)s' syntax, 's' means 'string'. It is +# not part of the expanded output. + +# The global section has general configuration. +[global] + +# The location of guest disk images. This location MUST be shared +# between all nodes including the machine running mclu (eg. using NFS +# or some sort of clustered storage). +images_dir = /var/lib/libvirt/images + +# The location of libvirt XML configuration files for guests. +# +# Note: This does NOT need to be shared or even visible on the cluster +# nodes. But it must be available on the machine running 'mclu'. +xmls_dir = %(config_dir)s/xmls/ + +# The nodes section lists all nodes. The keys don't need to be +# sequential, but must start with 'node'. The values are the short +# names of the nodes. If a node goes out of service permanently, you +# can just comment it out here. +[nodes] +node0 = ham0 +node1 = ham1 +node2 = ham2 +node3 = ham3 + +# You need one section per node listed in [nodes]. +# Possible fields are: +# host +# Hostname or IP address of the node, if omitted it uses the +# node name as the hostname +# mac +# MAC (ethernet) address (only used for wake-on-LAN) +# uri +# Libvirt URI used to access the remote libvirt daemon running +# on the node. The default is: qemu+ssh://root@%(host)s/system +# Note that you must allow passwordless root ssh access (eg. +# using ssh-agent). +[ham0] +host = ham0.home.annexia.org +mac = 74:d4:35:55:85:3f +#uri = qemu+ssh://root@%(host)s/system +[ham1] +host = ham1.home.annexia.org +mac = 74:d4:35:51:ab:86 +#uri = qemu+ssh://root@%(host)s/system +[ham2] +host = ham2.home.annexia.org +mac = 74:d4:35:55:82:96 +#uri = qemu+ssh://root@%(host)s/system +[ham3] +host = ham3.home.annexia.org +mac = 74:d4:35:55:84:b4 +#uri = qemu+ssh://root@%(host)s/system diff --git a/mclu.in b/mclu.in new file mode 100644 index 0000000..10376db --- /dev/null +++ b/mclu.in @@ -0,0 +1,29 @@ +#!/bin/sh - +# mclu (mini cluster) +# @configure_input@ +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +pkgdatadir="@prefix@/share/@PACKAGE_NAME@" + +if [ ! -x "$pkgdatadir/mclu.py" ]; then + echo "mclu: wrapper script cannot find installed mclu.py" + echo "Did you forget to run 'make install'?" + echo "OR if you want to run without installing, use:" + echo " ./run [args...]" + exit 1 +fi +"$pkgdatadir/mclu.py" "$@" diff --git a/mclu.py b/mclu.py new file mode 100755 index 0000000..0d1c72d --- /dev/null +++ b/mclu.py @@ -0,0 +1,124 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import os +import re +import argparse +import ConfigParser + +import config +from node import Node + +parser = argparse.ArgumentParser ( + prog='mclu', + description='Mini virtualization cluster management tool', +) +parser.add_argument ( + '-f', + type=file, + help='specify location of the configuration file', + metavar='MCLU.CONF', +) + +# Add subcommands. +subparsers = parser.add_subparsers () +import mclu_build +mclu_build.cmdline (subparsers) +import mclu_console +mclu_console.cmdline (subparsers) +import mclu_info +mclu_info.cmdline (subparsers) +import mclu_list +mclu_list.cmdline (subparsers) +import mclu_migrate +mclu_migrate.cmdline (subparsers) +import mclu_shutdown +mclu_shutdown.cmdline (subparsers) +import mclu_start +mclu_start.cmdline (subparsers) +import mclu_status +mclu_status.cmdline (subparsers) +import mclu_stop +mclu_stop.cmdline (subparsers) +import mclu_viewer +mclu_viewer.cmdline (subparsers) +import mclu_wake +mclu_wake.cmdline (subparsers) + +args = parser.parse_args() + +# Default location of the config file if not defined on the command line: +if not args.f: + config_dir = config.sysconfdir + "/" + config.PACKAGE_NAME + args.f = open (config_dir + "/" + "mclu.conf") +else: + # config_dir is the directory containing mclu.conf + config_dir = os.path.dirname (args.f.name) + +# Configuration file default settings. You cannot set defaults per +# section, so we have to rely on setting names not overlapping. +conf_defaults = { + "home" : os.getenv ("HOME"), + "host" : "SET.THIS.IN.MCLU.CONF", + "config_dir" : config_dir, + "uri" : "qemu+ssh://root@%(host)s/system", +} + +# Read the configuration file. +conf = ConfigParser.SafeConfigParser (conf_defaults) +conf.readfp (args.f) + +# Global configuration. +images_dir = conf.get ("global", "images_dir") +if not os.path.isdir (images_dir): + sys.exit ("configuration error: [globals] 'images_dir' (%s) directory does not exist" % images_dir) +xmls_dir = conf.get ("global", "xmls_dir") +if not os.path.isdir (xmls_dir): + sys.exit ("configuration error: [globals] 'xmls_dir' (%s) directory does not exist", xmls_dir) + +# Get the list of node names. +node_names = conf.items ("nodes") +node_names = filter (lambda (x, _) : re.search (r'^node', x), node_names) +node_names = [ value for _, value in node_names ] +if not node_names: + sys.exit ("configuration error: [nodes] section in configuration file is empty") + +# Get information about each node. +nodes = {} +for node_name in node_names: + host = conf.get (node_name, "host") + if not host: + host = node + mac = conf.get (node_name, "mac") + uri = conf.get (node_name, "uri") + node = Node (node_name, host, mac, uri) + nodes[node_name] = node + +# A config dict with less-used configuration settings. +c = { + "config_file" : args.f.name, + "config_dir" : config_dir, + "images_dir" : images_dir, + "node_names" : node_names, + "xmls_dir" : xmls_dir, + + "conf" : conf, +} + +# Run the subcommand. +args.run (c, args, nodes) diff --git a/mclu.spec.in b/mclu.spec.in new file mode 100644 index 0000000..6d99f13 --- /dev/null +++ b/mclu.spec.in @@ -0,0 +1,54 @@ +Name: @PACKAGE_NAME@ +Version: @PACKAGE_VERSION@ +Release: 1%{?dist} +Summary: Mini cluster, a virtualization cluster manager + +License: GPLv2+ +#URL: http://people.redhat.com/~rjones/ +#Source0: http://people.redhat.com/~rjones/ + +BuildRequires: python-devel +BuildRequires: libvirt-python +BuildRequires: /usr/bin/ssh + +Requires: libvirt-python +Requires: /usr/bin/ssh + +# These are optional: comment them out to get a less functional mclu. +BuildRequires: /usr/bin/wol +BuildRequires: /usr/bin/virt-builder +BuildRequires: /usr/bin/virt-viewer +Requires: /usr/bin/wol +Requires: /usr/bin/virt-builder +Requires: /usr/bin/virt-viewer + + +%description +mclu (mini cluster) is a virtualization cluster manager. + + +%prep +%setup -q + + +%build +%configure +make %{?_smp_mflags} + + +%install +make DESTDIR=$RPM_BUILD_ROOT install + + +%files +%doc COPYING README +%dir %{_sysconfdir}/mclu/ +%dir %{_sysconfdir}/mclu/xmls/ +%config(noreplace) %{_sysconfdir}/mclu/mclu.conf +%{_bindir}/mclu +%{_pkgdatadir}/ + + +%changelog +* Thu May 8 2014 Richard W.M. Jones - @PACKAGE_VERSION@-1 +- Initial release. diff --git a/mclu_build.py b/mclu_build.py new file mode 100644 index 0000000..ce077a6 --- /dev/null +++ b/mclu_build.py @@ -0,0 +1,184 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import os +import re +import subprocess +import sys +import libvirt + +import config +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'build', + help='build and run a new virtual machine', + ) + p.add_argument ( + '--memory', default=1024, type=int, + help='RAM to give to guest (default unit is megabytes)' + ) + p.add_argument ( + '--vcpus', default=4, type=int, + help='virtual CPUs to give to guest' + ) + p.add_argument ( + '--virtio', default=True, + help='use virtio disks and network' + ) + p.add_argument ( + '--size', required=True, + help='size of disk' + ) + p.add_argument ( + 'name', + help='name of the new VM (or use "vm:name")' + ) + p.add_argument ( + 'os_version', + help='OS & version of guest (see virt-builder docs)' + ) + p.add_argument ( + 'vbargs', nargs='*', + help='virt-builder arguments (after "--")' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + # Did the user request a particular node? If not, we'll run it + # on any node which is up. + m = re.match (r'^(.*):(.*)$', args.name) + if m: + node_name = m.group (1) + vm_name = m.group (2) + if node_name in nodes: + node = nodes[node_name] + if not node.ping (): + sys.exit ("error: requested node (%s) is not up, use mclu wake %s" % + (node_name, node_name)) + else: + sys.exit ("error: requested node (%s) does not exist" % node_name) + else: + node = lib.pick_any_node_which_is_up (nodes) + vm_name = args.name + + # Get all the guests, so we can tell if the name is a duplicate. + running, inactive = lib.get_all_guests (c, nodes.values ()) + + if vm_name in running or vm_name in inactive: + sys.exit ("error: node name (%s) already exists" % vm_name) + + output = '%s/%s.img' % (c['images_dir'], vm_name) + + # Call out to virt-builder to build the disk image. + vbargs = [ + config.VIRT_BUILDER, + args.os_version, + '--output', output, + '--format', 'qcow2', + ] + if args.size: + vbargs.extend (['--size', args.size]) + if args.vbargs: + vbargs.extend (args.vbargs) + subprocess.check_call (vbargs) + + # XXX Unfortunately this is necessary so qemu can access the disk. + os.chmod (output, 0666) + + # Generate the XML. Would be nice to use virt-install here, but + # it doesn't work: RHBZ#1095789 + network_model = "virtio" + if not args.virtio: + network_model = "e1000" + + # XXX Quoting, and we should use a real XML generator. + xml = """ + + %s + %d + %d + %d + + hvm + + + + + + + + + + + + + + destroy + restart + restart + + + + + + + + + + + + + +""" % (vm_name, args.memory, args.memory, args.vcpus, network_model) + + # virtio-scsi or IDE disk: + if args.virtio: + xml += """ + + + + + + +""" % output + else: + xml += """ + + + + + +""" % output + + xml += """ + + +""" + + # Write the XML to the xmls_dir. + fp = open ("%s/%s.xml" % (c['xmls_dir'], vm_name), "w") + fp.write (xml) + fp.close () + + # Start the guest. + lib.start_guest (c, node, vm_name) diff --git a/mclu_console.py b/mclu_console.py new file mode 100644 index 0000000..9da4f4b --- /dev/null +++ b/mclu_console.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import re +import sys +import subprocess + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'console', + help='connect to console of named VM', + ) + p.add_argument ( + 'vm', + help='vm (or node:vm) to connect to' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + running, _ = lib.get_all_guests (c, nodes.values ()) + + m = re.match (r'^(.*):(.*)$', args.vm) + node_name = None + if m: + # We don't actually care about the node, but we check it + # is the expected one below. + node_name = m.group (1) + vm_name = m.group (2) + else: + vm_name = args.vm + + if vm_name not in running: + sys.exit ("error: vm %s not found or not running" % vm_name) + + dom = running[vm_name]['dom'] + node = running[vm_name]['node'] + + if node_name and node.name != node_name: + sys.exit ("error: vm %s is not running on node %s, did you mean %s:%s ?" % + (vm_name, node_name, node.name, vm_name)) + + # Run the virsh console command. + subprocess.call (["virsh", "-c", node.uri, "console", vm_name]) diff --git a/mclu_info.py b/mclu_info.py new file mode 100644 index 0000000..b800e5b --- /dev/null +++ b/mclu_info.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'info', + help='print general configuration information', + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + print "config_dir=%s" % c['config_dir'] + print "config_file=%s" % c['config_file'] + print "images_dir=%s" % c['images_dir'] + print "node_names=%s" % c['node_names'] + print "xmls_dir=%s" % c['xmls_dir'] diff --git a/mclu_list.py b/mclu_list.py new file mode 100644 index 0000000..d3bbe7f --- /dev/null +++ b/mclu_list.py @@ -0,0 +1,61 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import libvirt + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'list', + help='list virtual machines', + ) + p.add_argument ( + '--running', action='store_const', const=True, + help='list only running VMs' + ) + p.add_argument ( + '--inactive', action='store_const', const=True, + help='list only inactive VMs' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + show_running = True + show_inactive = True + if not args.running or not args.inactive: + if args.running: + show_running = True + show_inactive = False + if args.inactive: + show_running = False + show_inactive = True + + running, inactive = lib.get_all_guests (c, nodes.values ()) + + if show_running: + for guest in running.values(): + node_name = guest['node'].name + dom_name = guest['dom'].name() + dom_state = lib.pretty_run_status (guest['dom'].state()[0]) + print "%s:%s\t%s" % (node_name, dom_name, dom_state) + + if show_inactive: + for name in inactive.values(): + print "%s\tinactive" % name diff --git a/mclu_migrate.py b/mclu_migrate.py new file mode 100644 index 0000000..33e2ae6 --- /dev/null +++ b/mclu_migrate.py @@ -0,0 +1,76 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import fnmatch +import re +import sys +import libvirt + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'migrate', + help='live migrate virtual machine(s)', + ) + p.add_argument ( + 'wildcards', nargs='+', + help='virtual machine(s) to be migrated' + ) + p.add_argument ( + 'dest', + help='destination node' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + running, _ = lib.get_all_guests (c, nodes.values ()) + + # Identify the VMs to be migrated. + migrate_vms = [] + for vm in running.values(): + node = vm['node'] + dom = vm['dom'] + # Form the name of this VM (eg. "ham0:vm") so we can match it + # against the wildcards (eg. "ham0:*") + name = node.name + ":" + dom.name() + for wc in args.wildcards: + if fnmatch.fnmatch (name, wc) or fnmatch.fnmatch (dom.name(), wc): + migrate_vms.append (vm) + + if not migrate_vms: + sys.exit ("error: no VMs are going to be migrated") + + # Get destination node. It can be written either 'dest' or 'dest:' + m = re.match (r'(.*):$', args.dest) + if m: + args.dest = m.group (1) + + if args.dest not in nodes: + sys.exit ("error: destination node (%s) does not exist" % args.dest) + dest = nodes[args.dest] + + dconn = libvirt.open (dest.uri) + if dconn == None: + sys.exit ("error: could not open a libvirt connection to %s (URI: %s)" % + (dest.host, dest.uri)) + + for vm in migrate_vms: + dom = vm['dom'] + dom.migrate (dconn, libvirt.VIR_MIGRATE_LIVE) diff --git a/mclu_shutdown.py b/mclu_shutdown.py new file mode 100644 index 0000000..2a791dd --- /dev/null +++ b/mclu_shutdown.py @@ -0,0 +1,66 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import sys +import os +import subprocess + +import argparse +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'shutdown', + help='power off a node (or nodes)' + ) + p.add_argument ( + '--all', action='store_const', const=True, + help='power off all nodes' + ) + p.add_argument ( + 'nodes', nargs='*', + help='node name to be powered off' + ) + p.set_defaults (run=run) + +def run (c, args, all_nodes): + nodes = lib.get_nodes_by_name (all_nodes, args.nodes, args.all) + + # Check the nodes have no guests. + for node in nodes: + guests = node.guests () + if guests != []: + names = map (lambda x : x.name(), guests) + sys.exit ("error: node %s has running guests %s, migrate them off or stop them first" % (node.name, names)) + + # Power them off. + up = 0 + for node in nodes: + if node.ping (force=True): + node.shutdown () + up += 1 + + pings = 60 + while pings > 0 and up > 0: + if not node.up: + if not node.ping (force=True): + up -= 1 + pings -= 1 + + if pings == 0: + sys.exit ('warning: some nodes did not power off') diff --git a/mclu_start.py b/mclu_start.py new file mode 100644 index 0000000..1187942 --- /dev/null +++ b/mclu_start.py @@ -0,0 +1,65 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import fnmatch +import re +import sys +import libvirt + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'start', + help='start virtual machine(s)', + ) + p.add_argument ( + 'vms', nargs='+', + help='virtual machine(s) to be started' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + _, inactive = lib.get_all_guests (c, nodes.values ()) + + # User supplied a list of node:VMs. + for a in args.vms: + m = re.match (r'^(.*):(.*)$', a) + if m: + node_name = m.group (1) + wc = m.group (2) + if node_name not in nodes: + sys.exit ("error: node %s does not exist" % node_name) + node = nodes[node_name] + else: + wc = a + node = lib.pick_any_node_which_is_up (nodes) + started = [] + for vm_name in inactive: + if fnmatch.fnmatch (vm_name, wc): + lib.start_guest (c, node, vm_name) + started.append (vm_name) + + if not started: + sys.exit ("error: no VMs matched pattern %s" % a) + + # Make sure we don't start the same VMs again the next + # time around the loop: + for vm_name in started: + del inactive[vm_name] diff --git a/mclu_status.py b/mclu_status.py new file mode 100644 index 0000000..3d8fa74 --- /dev/null +++ b/mclu_status.py @@ -0,0 +1,45 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'status', + help='display cluster status' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + for node_name in sorted (nodes.keys ()): + node = nodes[node_name] + print "%s (%s)" % (node_name, node.host), + if node.ping(): + print "\tup", + if node.ssh_ping(): + print "\tssh: OK", + if node.libvirt_ping(): + print "\tlibvirt: OK" + else: + print "libvirt: dead" + else: + print "ssh: dead" + else: + print "down" diff --git a/mclu_stop.py b/mclu_stop.py new file mode 100644 index 0000000..bd7f116 --- /dev/null +++ b/mclu_stop.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import fnmatch +import libvirt + +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'stop', + help='stop virtual machine(s)', + ) + p.add_argument ( + '--force', action='store_const', const=True, + help='power off the virtual machine(s) forcibly' + ) + p.add_argument ( + 'wildcards', nargs='+', + help='virtual machine(s) to be stopped' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + running, _ = lib.get_all_guests (c, nodes.values ()) + + for vm in running.values(): + node = vm['node'] + dom = vm['dom'] + # Form the name of this VM (eg. "ham0:vm") so we can match it + # against the wildcards (eg. "ham0:*") + name = node.name + ":" + dom.name() + for wc in args.wildcards: + if fnmatch.fnmatch (name, wc) or fnmatch.fnmatch (dom.name(), wc): + if args.force: + dom.destroy() + else: + dom.shutdown() diff --git a/mclu_viewer.py b/mclu_viewer.py new file mode 100644 index 0000000..66cb5e6 --- /dev/null +++ b/mclu_viewer.py @@ -0,0 +1,62 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import argparse +import re +import sys +import subprocess + +import config +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'viewer', + help='connect to graphical console of named VM', + ) + p.add_argument ( + 'vm', + help='vm (or node:vm) to connect to' + ) + p.set_defaults (run=run) + +def run (c, args, nodes): + running, _ = lib.get_all_guests (c, nodes.values ()) + + m = re.match (r'^(.*):(.*)$', args.vm) + node_name = None + if m: + # We don't actually care about the node, but we check it + # is the expected one below. + node_name = m.group (1) + vm_name = m.group (2) + else: + vm_name = args.vm + + if vm_name not in running: + sys.exit ("error: vm %s not found or not running" % vm_name) + + dom = running[vm_name]['dom'] + node = running[vm_name]['node'] + + if node_name and node.name != node_name: + sys.exit ("error: vm %s is not running on node %s, did you mean %s:%s ?" % + (vm_name, node_name, node.name, vm_name)) + + # Run the virsh console command. + subprocess.call ([config.VIRT_VIEWER, "-c", node.uri, vm_name]) diff --git a/mclu_wake.py b/mclu_wake.py new file mode 100644 index 0000000..2cee72b --- /dev/null +++ b/mclu_wake.py @@ -0,0 +1,62 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import sys +import os +import subprocess + +import argparse +import lib + +def cmdline (subparsers): + p = subparsers.add_parser ( + 'wake', + help='wake up a node (or nodes)' + ) + p.add_argument ( + '--all', action='store_const', const=True, + help='wake up all nodes' + ) + p.add_argument ( + 'nodes', nargs='*', + help='node name to be woken up' + ) + p.set_defaults (run=run) + +def run (c, args, all_nodes): + nodes = lib.get_nodes_by_name (all_nodes, args.nodes, args.all) + + # Wake them up. + up = 0 + for node in nodes: + if node.ping (force=True): + up += 1 + else: + node.wake() + + # Wait for them to come up. + pings = 30 + while pings > 0 and up < len (nodes): + for node in nodes: + if not node.up: + if node.ping (force=True): + up += 1 + pings -= 1 + + if pings == 0: + sys.exit ('warning: some nodes did not wake up') diff --git a/node.py b/node.py new file mode 100644 index 0000000..345c4c2 --- /dev/null +++ b/node.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +import os +import subprocess +import libvirt + +import config + +class Node: + def __init__ (self, name, host, mac, uri): + self.name = name + self.host = host + self.mac = mac + self.uri = uri + + def ping (self, force = False): + """Test if node is up.""" + if not force: + if hasattr (self, 'up'): + return self.up + + devnull = open (os.devnull, "w") + r = subprocess.call (["ping", "-c", "1", self.host], + stdout = devnull, stderr = devnull) + self.up = r == 0 + return self.up + + def ssh_ping (self): + """Test if we can open an SSH connection""" + if self.ping (): + devnull = open (os.devnull, "w") + r = subprocess.call ([config.SSH, "-l", "root", self.host, ":"], + stdout = devnull, stderr = devnull) + return r == 0 + else: + return False + + def libvirt_ping (self): + """Test if we can open a libvirt connection""" + if self.ping(): + conn = libvirt.openReadOnly (self.uri) + return conn != None + else: + return False + + def wake (self): + if not self.ping (): + devnull = open (os.devnull, "w") + subprocess.check_call ([config.WOL, self.mac], + stdout = devnull, stderr = devnull) + + def shutdown (self): + if self.ping (): + devnull = open (os.devnull, "w") + r = subprocess.call ([config.SSH, "-l", "root", self.host, + "/sbin/poweroff"], + stdout = devnull, stderr = devnull) + # returns a non-zero exit status, for unknown reasons + + def guests (self): + """Return the list of VMs running on this node""" + guests = [] + if self.ping (): + conn = self.get_connection () + for id in conn.listDomainsID(): + try: + dom = conn.lookupByID (id) + except: + continue + guests.append (dom) + return guests + + def get_connection (self): + """Return cached libvirt connection, fail if not possible.""" + if not self.ping (): + sys.exit ("error: node %s is not awake" % self.name) + if hasattr (self, 'conn'): + return self.conn + conn = libvirt.open (self.uri) + if conn == None: + sys.exit ("error: could not open a libvirt connection to %s (URI: %s)" % + (self.host, self.uri)) + self.conn = conn + return self.conn diff --git a/run.in b/run.in new file mode 100644 index 0000000..7954b50 --- /dev/null +++ b/run.in @@ -0,0 +1,22 @@ +#!/bin/bash - +# mclu (mini cluster) +# Copyright (C) 2014 Red Hat Inc. +# +# 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; either version 2 of the License, or +# (at your option) any later version. +# +# 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. + +b=@abs_top_builddir@ + +echo $b/mclu.py -f $b/mclu.conf "$@" +exec $b/mclu.py -f $b/mclu.conf "$@" diff --git a/xmls/.exists b/xmls/.exists new file mode 100644 index 0000000..e69de29 -- 1.8.3.1