From e80ea9ae109843b6576c6ed3e3b0a59440a92256 Mon Sep 17 00:00:00 2001 From: Sergi Castro Date: Fri, 20 Apr 2012 20:26:07 +0200 Subject: [PATCH] Added deploy configured kvm to mothership plugin --- config/mothership.conf | 10 +++- kahuna/plugins/mothership.py | 64 ++++++++++++++++++++-- kahuna/plugins/mothership/abiquo-aim.ini | 19 +++++++ kahuna/plugins/mothership/configure-kvm.sh | 6 ++ 4 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 kahuna/plugins/mothership/abiquo-aim.ini create mode 100644 kahuna/plugins/mothership/configure-kvm.sh diff --git a/config/mothership.conf b/config/mothership.conf index 6cd0aaa..5be4929 100644 --- a/config/mothership.conf +++ b/config/mothership.conf @@ -10,7 +10,15 @@ vdc = Ignasi ; The default virtual datacenter [deploy-chef] template = Chef Server 0\.10\.8 ; The name (regexp pattern) of the template template_user = abiquo ; The username to access the template -template_pass = abiquo ; The apssword to access the template +template_pass = abiquo ; The password to access the template requires_sudo = true ; If sudo is required to run commands cookbooks = apache2 chef-client openssh ; Cookbooks to install after deploying +# KVM deploy configuration +[deploy-kvm] +template = Abiquo 2\.0 Kvm ;The name (regexp pattern) of the template +template_user = root ;The username to access the template +template_pass = abiquo ;The password to access the template +requires_sudo = false ;If sudo is required to run commands +redis_host = 10.60.12.191 ;The redis host where kvm connects +redis_port = 6379 ;The redis port where kvm connects diff --git a/kahuna/plugins/mothership.py b/kahuna/plugins/mothership.py index f8fb5f5..ca8e403 100644 --- a/kahuna/plugins/mothership.py +++ b/kahuna/plugins/mothership.py @@ -1,6 +1,7 @@ #!/usr/bin/env jython import logging +from java.io import File from kahuna.config import ConfigLoader from com.google.common.collect import Iterables from org.jclouds import ContextBuilder @@ -8,6 +9,7 @@ from org.jclouds.abiquo import AbiquoContext from org.jclouds.compute import RunNodesException from org.jclouds.domain import LoginCredentials +from org.jclouds.io import Payloads from org.jclouds.logging.config import NullLoggingModule from org.jclouds.util import Strings2 from org.jclouds.ssh.jsch.config import JschSshClientModule @@ -27,6 +29,7 @@ def commands(self): """ Returns the provided commands, mapped to handler methods. """ commands = {} commands['deploy-chef'] = self.deploy_chef + commands['deploy-kvm'] = self.deploy_kvm return commands def deploy_chef(self, args): @@ -37,7 +40,7 @@ def deploy_chef(self, args): try: name = self.__config.get("deploy-chef", "template") log.info("Loading template: %s" % name) - options = self._template_options(compute) + options = self._template_options(compute, "deploy-chef") template = compute.templateBuilder() \ .imageNameMatches(name) \ .options(options.blockOnPort(4000, 60)) \ @@ -72,6 +75,45 @@ def deploy_chef(self, args): finally: context.close() + def deploy_kvm(self, args): + """ Deploys and configures a KVM Hypervisor """ + context = self._create_context() + compute = context.getComputeService() + + try: + name = self.__config.get("deploy-kvm", "template") + log.info("Loading template: %s" % name) + options = self._template_options(compute, "deploy-kvm") + template = compute.templateBuilder() \ + .imageNameMatches(name) \ + .options(options) \ + .build() + + log.info("Deploying node...") + node = Iterables.getOnlyElement( + compute.createNodesInGroup("kahuna-kvm", 1, template)) + log.info("KVM deployed at %s" + % Iterables.getOnlyElement(node.getPrivateAddresses())) + + ## TODO: read abiquo-aim.ini and replace redis host and port + self._upload_file_node(context, node, "/etc/", "abiquo-aim.ini") + f = open(self.__scriptdir + "/configure-kvm.sh", "r") + script = f.read() + f.close() + + log.info("Configuring kvm...") + compute.runScriptOnNode(node.getId(), script) + + log.info("KVM Hypervisor configured!") + + except RunNodesException, ex: + for error in ex.getExecutionErrors().values(): + print "Error %s" % error.getMessage() + for error in ex.getNodeErrors().values(): + print "Error %s" % error.getMessage() + finally: + context.close() + def _create_context(self): user = self.__config.get("mothership", "user") password = self.__config.get("mothership", "password") @@ -81,12 +123,12 @@ def _create_context(self): .modules([JschSshClientModule(), NullLoggingModule()]) \ .build(AbiquoContext) - def _template_options(self, compute): + def _template_options(self, compute, deploycommand): login = LoginCredentials.builder() \ - .authenticateSudo(self.__config.getboolean("deploy-chef", + .authenticateSudo(self.__config.getboolean(deploycommand, "requires_sudo")) \ - .user(self.__config.get("deploy-chef", "template_user")) \ - .password(self.__config.get("deploy-chef", "template_pass")) \ + .user(self.__config.get(deploycommand, "template_user")) \ + .password(self.__config.get(deploycommand, "template_pass")) \ .build() return compute.templateOptions() \ .overrideLoginCredentials(login) \ @@ -105,6 +147,18 @@ def _print_node_file(self, context, node, file): if ssh: ssh.disconnect() + def _upload_file_node(self, context, node, destination, filename): + ssh = context.getUtils().sshForNode().apply(node) + file = File(self.__scriptdir + "/" + filename) + try: + ssh.connect() + log.info("Uploading file %s..." % filename) + ssh.put(destination + "/" + filename, + Payloads.newFilePayload(file)) + finally: + if ssh: + ssh.disconnect() + def load(): """ Loads the current plugin. """ diff --git a/kahuna/plugins/mothership/abiquo-aim.ini b/kahuna/plugins/mothership/abiquo-aim.ini new file mode 100644 index 0000000..a133673 --- /dev/null +++ b/kahuna/plugins/mothership/abiquo-aim.ini @@ -0,0 +1,19 @@ + +# AIM configuration file + +[server] +port = 8889 + +[monitor] +uri = "qemu+unix:///system" +redisHost = 10.60.12.191 +redisPort = 6379 + +[rimp] +repository = /opt/vm_repository +datastore = /var/lib/virt + +[vlan] +ifconfigCmd = /sbin/ifconfig +vconfigCmd = /sbin/vconfig +brctlCmd = /usr/sbin/brctl diff --git a/kahuna/plugins/mothership/configure-kvm.sh b/kahuna/plugins/mothership/configure-kvm.sh new file mode 100644 index 0000000..333d951 --- /dev/null +++ b/kahuna/plugins/mothership/configure-kvm.sh @@ -0,0 +1,6 @@ +echo "mount nfs repository" +umount /opt/vm_repository +mount 10.60.1.104:/volume1/nfs-devel /opt/vm_repository + +echo "start abiquo-aim" +/etc/init.d/abiquo-aim restart