lib Package

bellatrix_util Module

Bellatrix utility functions

bellatrix.lib.bellatrix_util.getConfigFile(path, file_name, get_content, description=None)[source]
bellatrix.lib.bellatrix_util.getOutFile(original_file, suffix='_out', delete_file=True)[source]

cmds Module

Common utilities, meant to be used as ‘building blocks’. Every block has a different use, being possible in some cases to composite them.

Every command is a simple function that returns the list of commands to be executed into a list of strings. If any of the commands fails, the execution will be interrupted. It’s convenient that you add a small test at the very end of each set.

For example:

def install_pip():
return [
“touch new_file”, “cat new_file”, ]

It’s an example of a single-purposed operation that performs a test at the end. Should something happened to the operation, ‘cat new_file’ will fail. Additionally you will be able to verify the content of the file in the logging output.


Return the “sudo apt-get install” command

Parameters:package (string) – The new package to install

Executes apt-get update.

bellatrix.lib.cmds.chmod(mode, file_name, options='')[source]

Applies the chmod command.

  • mode (string) – New mode of the destination. Typical examples are: a+r to give read permissions to everyone, a+rx to give execution and read permissions to any user, etc.
  • file_name (string) – Destination of the new mode.
  • options (string) – Optional set of parameters for this command.
bellatrix.lib.cmds.copy(src, dest)[source]

Copy a file using -f so it doesn’t fail if the destination exists.

  • src (string) – Source file.
  • dest (string) – File destination.

Creates a new soft link.

  • Src – Source file or directory.
  • Dest – Name of the soft link.

Generate a new Python virtual environment using virtualenv

Parameters:env_name (string) – Name of the new virtual environment.
bellatrix.lib.cmds.create_django_project(project_name, dir_name='./')[source]

Creates a Django project.

  • project_name (string) – Name of the Django project.
  • dir_name (string) – Directory where the Django project needs to be executed. By default is the current directory.
bellatrix.lib.cmds.executeInVirtualEnv(env, cmd)[source]

Execute a command within a virtualenv environment

Parameters:env (str) – Name of the new virtual environment.

:type cmd : list :param cmd: List of commands to be executed.


Given a list of commands it will return a single one concatenated by ‘&&’ so they will be executed in sequence until any of them fails.

Parameters:cmds (list) – List of strings that contains commands.
Return type:string
Returns:A single string with the commands concatenated.
bellatrix.lib.cmds.installPackageInVirtualEnv(env, package, verify=True, verification_command=None, prefix='')[source]

Install a Python package into a virtualenv.

  • env (string) – Name of the virtual environment.
  • package (string) – Package to be installed.
  • verify (string) – True by default. If true, it will auto-generate a verification command to check the package has been installed correctly.
  • verification_command (string) – None by default. If None, the verification command will be ‘package –version’ Otherwise it will execute the one provided in this argument.
  • prefix (string) – Prefix of the pip command. It is ‘sudo’ by default.

Install Nginx in Ubuntu using the repo they provide as described in


Install pip using apt-get install.


Resizes the logical volume to new_size specified in the start command. This is necessary for some operating systems that do not automatically resize to this value.

Parameters:device (string) – logical volume to resize, e.g. /dev/xvda1 in RedHat family systems

Created a new directory. “-p” flag is used so the command generates the same result regardless whether the directory exists or not.

Parameters:directory (string) – directory to be created.
bellatrix.lib.cmds.pip_install(package, prefix='sudo', verify=True, verification_command=None)[source]

Install a Python package using pip.

  • package (string) – The package to install.
  • prefix (string) – prefix of the pip command. It is ‘sudo’ by default.
  • verify (bool) – True by default. If true, it will auto-generate a verification command to check the package has been installed correctly.
  • verification_command (string) – None by default. If None, the verification command will be ‘package –version’ Otherwise it will execute the one provided in this argument.
bellatrix.lib.cmds.scp(src, dest)[source]

Execute a list of commands using sudo.

Parameters:cmds (list) – List of commands.
bellatrix.lib.cmds.wget(url, dest=None)[source]

Downloads a web resource using wget.

  • url (string) – Url to download.
  • dest (string) – Optional. Destination in our machine of the downloaded resource.

ec2_lib Module

EC2 utilities wrapping boto methods

boto reference in:

class bellatrix.lib.ec2_lib.Ec2lib(key, sec)[source]
authorizeSecurityGroup(securityGroup, cidr_ip, from_port, security_group=None, to_port=None, ip_protocol='tcp')[source]
createImage(instance_id, name, description=None, no_reboot=False)[source]

Will create an AMI from the instance in the running or stopped state.

  • instance_id (string) – the ID of the instance to image.
  • name (string) – The name of the new image
  • description (string) – An optional human-readable string describing the contents and purpose of the AMI.
  • no_reboot (bool) – An optional flag indicating that the bundling process should not attempt to shutdown the instance before bundling. If this flag is True, the responsibility of maintaining file system integrity is left to the owner of the instance.
Return type:



The new image id

getDNSName(inst, TIME_OUT=300)[source]

get DNS name for an instance. This operation could take some time as the startup for new instances is not immediate

getEc2Instance(ami, key_name, security_group, instance_type, instance_initiated_shutdown_behavior='terminate', new_size=None)[source]

Given a instance string, returns an instance object


get all security groups from this account

percent_cb(complete, total)[source]

AMI’s and instances put into this list will be skipped in the shutdown process

setPermissionsToAmis(amis, account_permissions, retry=True)[source]

set account permissions to a set of ami’s

startInstance(ami, instance_type, key_name, security_groups, new_size=None)[source]
tagInstance(instance, key, value, time_out=None, step=None)[source]
uploadToBucket(bucket, file_to_upload, acl='public')[source]
uploadToS3(src, bucket, acl=None, key_prefix='', s3_conn=None, filter=None, pretend=None, starting_with=None)[source]

Copy the file specified by src (or contained in src if it is a directory) to the specified S3 bucket, pre-pending the optional key_prefix to the relative path of each file within src.

Function originally taken from here: @param bucket: The name of the bucket we are working with. It must already exist. @type bucket: string @param key_prefix: This will be prepended to every source path that we copy. Can be empty. Often is. @type key_prefix: string @param src: This is the source file or directory. @type src: string @param s3_conn: This is an active S3 connection. @type s3_conn: boto.s3.connection.S3Connection @param filter: Only files with extensions listed in this filter will be candidates for copying. You can have an empty string in this list to copy files with NO file extension. @type filter: list of strings @param acl: One of the supported ACL policies. @type acl: string @param pretend: If true, we just log what we would do, but we don’t do it. @type pretend: boolean @param starting_with: An option source file path. If specified, skips all the files preceeding it until this file is reached. @type starting_with: string @raise boto.exception.S3ResponseError: If you specify a non-existing bucket

waitForConnectionReady(inst, user, key, dns, TIME_OUT=300)[source]
waitUntilInstanceIsReady(inst, TIME_OUT=300)[source]

util Module

general utility functions

bellatrix.lib.util.getConfigFileContent(file_name, app_name)[source]

parse an xml file and return its dom

bellatrix.lib.util.getNodeValue(dom, nodename)[source]

get ‘myname’ from a xml structure like this one when you call this method with ‘name’: <?xml version=”1.0” encoding=”UTF-8” ?> <root> <name>myname</name> </root>


“Return list from file ignoring blanks and comments

bellatrix.lib.util.getValue(xml_file, value)[source]

return the value from a xml tag

this function will get ‘myname’ from a xml structure like this one when you call this method with ‘name’: <?xml version=”1.0” encoding=”UTF-8” ?> <root> <name>myname</name> </root>

bellatrix.lib.util.waitForSSHReady(user, key, dns, TIME_OUT=300)[source]
bellatrix.lib.util.writeFile(file_name, content)[source]

Table Of Contents

This Page