Skip to content

Commit

Permalink
Added deploy configured kvm to mothership plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergi Castro committed Apr 20, 2012
1 parent e7bf3b5 commit e80ea9a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 6 deletions.
10 changes: 9 additions & 1 deletion config/mothership.conf
Expand Up @@ -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
64 changes: 59 additions & 5 deletions kahuna/plugins/mothership.py
@@ -1,13 +1,15 @@
#!/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
from org.jclouds.abiquo import AbiquoApiMetadata
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
Expand All @@ -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):
Expand All @@ -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)) \
Expand Down Expand Up @@ -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")
Expand All @@ -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) \
Expand All @@ -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. """
Expand Down
19 changes: 19 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions 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

0 comments on commit e80ea9a

Please sign in to comment.