Source code for provy.more.debian.database.mongodb
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Roles in this namespace are meant to provide `MongoDB <http://www.mongodb.org/>`_ database management utilities for Debian distributions.
'''
from cStringIO import StringIO
from configobj import ConfigObj
from provy.core import Role
from provy.more.debian.package.aptitude import AptitudeRole
[docs]class MongoDBRole(Role):
'''
This role provides `MongoDB <http://www.mongodb.org/>`_ database management utilities for Debian distributions.
Example:
::
from provy.core import Role
from provy.more.debian import MongoDBRole
class MySampleRole(Role):
def provision(self):
with self.using(MongoDBRole) as role:
role.restart()
'''
[docs] def provision(self):
'''
Installs `MongoDB <http://www.mongodb.org/>`_ and its dependencies.
This method should be called upon if overriden in base classes, or MongoDB won't work properly in the remote server.
Example:
::
from provy.core import Role
from provy.more.debian import MongoDBRole
class MySampleRole(Role):
def provision(self):
self.provision_role(MongoDBRole) # no need to call this if using with block.
'''
distro_info = self.get_distro_info()
self.log('Installing MongoDB via packages')
if distro_info.distributor_id == 'Ubuntu':
self.provision_to_ubuntu()
else:
self.provision_to_debian()
self.log('MongoDB installed')
[docs] def provision_to_debian(self):
'''
Installs MongoDB and its dependencies via Debian-specific repository.
It's not recommended that you use this method directly; Instead, provision this role directly and it will find out the best way to provision.
Example:
::
from provy.core import Role
from provy.more.debian import MongoDBRole
class MySampleRole(Role):
def provision(self):
with self.using(MongoDBRole) as mongo:
mongo.provision_to_debian()
'''
initialization_type = 'debian-sysvinit'
self.__provision_with_init_type(initialization_type)
[docs] def provision_to_ubuntu(self):
'''
Installs MongoDB and its dependencies via Ubuntu-specific repository.
It's not recommended that you use this method directly; Instead, provision this role directly and it will find out the best way to provision.
Example:
::
from provy.core import Role
from provy.more.debian import MongoDBRole
class MySampleRole(Role):
def provision(self):
with self.using(MongoDBRole) as mongo:
mongo.provision_to_ubuntu()
'''
initialization_type = 'ubuntu-upstart'
self.__provision_with_init_type(initialization_type)
def __provision_with_init_type(self, initialization_type):
with self.using(AptitudeRole) as aptitude:
aptitude.ensure_gpg_key('http://docs.mongodb.org/10gen-gpg-key.asc')
aptitude.ensure_aptitude_source('deb http://downloads-distro.mongodb.org/repo/%s dist 10gen' % initialization_type)
aptitude.force_update()
aptitude.ensure_package_installed('mongodb-10gen')
[docs] def restart(self):
'''
Restarts the MongoDB database.
Example:
::
from provy.core import Role
from provy.more.debian import MongoDBRole
class MySampleRole(Role):
def provision(self):
with self.using(MongoDBRole) as mongo:
mongo.restart()
'''
self.execute('service mongodb restart', sudo=True)
def __tmp_file_with_config(self, config):
output_buffer = StringIO()
config.write(output_buffer)
tmp_file = self.write_to_temp_file(output_buffer.getvalue())
return tmp_file
def __config_from_remote(self, mongodb_config_path):
config_content = self.read_remote_file(mongodb_config_path, sudo=True)
config_buffer = StringIO(config_content)
config = ConfigObj(infile=config_buffer)
return config
def __set_config_items(self, configuration, config):
for key, value in configuration.items():
if isinstance(value, bool):
value = str(value).lower()
config[key] = value