def print_env(full=False): if full: for key in env.keys(): print yellow(key), green(getattr(env, key)) else: for key in "projectname package_version build_dir dist_dir version_file".split(): print yellow(key), green(getattr(env, key))
def postgresql(): """PostgreSQL 8.4 + PostGIS 1.5""" with settings(show("user"), hide("warnings", "running", "stdout", "stderr")): project() if "pgpass" not in env.keys(): prompt("Passe PostgreSQL :", default=pgpass, key="pgpass") print(yellow("Configuration PostgreSQL+PostGIS...")) pretty_apt( [ "libpq-dev", "binutils", "gdal-bin", "libproj-dev", "postgresql-8.4-postgis", "postgresql-server-dev-8.4", "python-psycopg2", ] ) fabtools.deb.upgrade() # création d'un utilisateur postgresql avec le meme nom d'utilisateur if not fabtools.postgres.user_exists(env.user): fabtools.postgres.create_user(env.user, env.pgpass) sudo('''psql -c "ALTER ROLE %(user)s CREATEDB;"''' % env, user="******") sudo('''psql -c "ALTER USER %(user)s with SUPERUSER;"''' % env, user="******") print(green('Création d’un superuser "%(user)s" PostgreSQL.' % env)) if not exists(".pgpass"): run('echo "*:*:*:%(user)s:%(pgpass)s" >> .pgpass' % env) sudo("chmod 0600 .pgpass") print(green("Création du fichier .pgpass.")) postgis_template() postgresql_net_access() icanhaz.postgres.server() # start server
def get_sriov_details(compute_host_string): sriov_string = "" if 'sriov' not in env.keys(): return sriov_string if compute_host_string not in env.sriov: return sriov_string intf_list = env.sriov[compute_host_string] for intf in intf_list: if 'interface' in intf: if not intf.get('VF'): continue if not intf.get('physnets'): continue if not len(intf['physnets']): continue if sriov_string: sriov_string += "," sriov_string += intf['interface'] + ":" + str(intf['VF']) + ":" for phynet in intf['physnets']: sriov_string += phynet if intf['physnets'][-1] != phynet: sriov_string += "%" return sriov_string
def write_hosts(): """ Set a /etc/hosts file based on the hosts provided with the -H parameter """ if 'systems' not in env.keys(): env.systems = {} for host in env.all_hosts: ip = str(socket.gethostbyname(host)) if ip in env.systems.keys(): env.systems[ip].append(host) else: env.systems[ip] = [host] if not contains("/etc/hosts", "## Added from fabric configurator"): ip = str(socket.gethostbyname(env.host)) hostnames = ' '.join(env.systems[ip]) run('mv /etc/hosts /etc/hosts.old') append("/etc/hosts", "## Added from fabric configurator") append('/etc/hosts', "127.0.0.1 %s" % hostnames) #append('/etc/hosts', "::1 %s" % hostnames) append('/etc/hosts', "127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4") append('/etc/hosts', "::1 localhost localhost.localdomain localhost6 localhost6.localdomain6") for system in env.systems.items(): if not env.host in system[1]: append('/etc/hosts', "%s %s" % (system[0], ' '.join(system[1]))) print(green("/etc/hosts of the system %s configured" % env.host)) else: print(yellow("The system %s had the /etc/hosts already configured" % env.host))
def get_os_version(self): ''' Figure out the os type on each node in the cluster ''' if 'os_type' in env.keys(): self.os_type = env.os_type return self.os_type else: env.os_type = {} for host_ip in self.host_ips: username = self.host_data[host_ip]['username'] password = self.host_data[host_ip]['password'] with settings( host_string='%s@%s' % (username, host_ip), password=password, warn_only=True, abort_on_prompts=False): output = run('uname -a') if 'el6' in output: env.os_type[host_ip] = 'centos_el6' if 'fc17' in output: env.os_type[host_ip] = 'fc17' if 'xen' in output: env.os_type[host_ip] = 'xenserver' if 'Ubuntu' in output: env.os_type[host_ip] = 'ubuntu' if 'el7' in output: env.os_type[host_ip] = 'redhat' self.os_type = env.os_type return self.os_type
def postgresql(): '''PostgreSQL 8.4 + PostGIS 1.5''' with settings(show('user'), hide('warnings', 'running', 'stdout', 'stderr')): project() if 'pgpass' not in env.keys(): prompt('Passe PostgreSQL :', default=pgpass, key='pgpass') print(yellow('Configuration PostgreSQL+PostGIS...')) pretty_apt([ 'libpq-dev', 'binutils', 'gdal-bin', 'libproj-dev', 'postgresql-8.4-postgis', 'postgresql-server-dev-8.4', 'python-psycopg2' ]) fabtools.deb.upgrade() # création d'un utilisateur postgresql avec le meme nom d'utilisateur if not fabtools.postgres.user_exists(env.user): fabtools.postgres.create_user(env.user, env.pgpass) sudo('''psql -c "ALTER ROLE %(user)s CREATEDB;"''' % env, user='******') sudo('''psql -c "ALTER USER %(user)s with SUPERUSER;"''' % env, user='******') print(green('Création d’un superuser "%(user)s" PostgreSQL.' % env)) if not exists('.pgpass'): run('echo "*:*:*:%(user)s:%(pgpass)s" >> .pgpass' % env) sudo('chmod 0600 .pgpass') print(green('Création du fichier .pgpass.')) postgis_template() postgresql_net_access() icanhaz.postgres.server() #start server
def decorated(*args, **kwargs): env.setdefault('use_sudo', True) env.setdefault('python_bin', 'python') env.setdefault('remote_owner', 'www-data') env.setdefault('remote_group', 'www-data') env.setdefault('pip_install_command', 'pip install -r requirements.txt') env.setdefault('domain_path', "%(base_dir)s/%(app_name)s" % {'base_dir': env.base_dir, 'app_name': env.app_name}) env.setdefault('current_path', "%(domain_path)s/current" % {'domain_path': env.domain_path}) env.setdefault('releases_path', "%(domain_path)s/releases" % {'domain_path': env.domain_path}) env.setdefault('shared_path', "%(domain_path)s/shared" % {'domain_path': env.domain_path}) if 'releases' not in env.keys(): if dir_exists(env.releases_path): env.releases = sorted(run('ls -x %(releases_path)s' % {'releases_path': env.releases_path}).split()) if len(env.releases) >= 1: env.current_revision = env.releases[-1] env.current_release = "%(releases_path)s/%(current_revision)s" % \ {'releases_path': env.releases_path, 'current_revision': env.current_revision} if len(env.releases) > 1: env.previous_revision = env.releases[-2] env.previous_release = "%(releases_path)s/%(previous_revision)s" % \ {'releases_path': env.releases_path, 'previous_revision': env.previous_revision} else: env.releases = [] return func(*args, **kwargs)
def install_celery(): if all([e in env.keys() for e in ('celery_version',)]): # Configure the celery file in /etc/init.d initd_path = join(sep, 'etc', 'init.d') with fabric.api.cd(initd_path): fabric.api.sudo( 'wget -c https://raw.githubusercontent.com/celery/celery/%s/extra/generic-init.d/celeryd -O %s'\ % (env.celery_version, celery_service_name()) ) fabric.api.sudo('chmod 755 %s' % celery_service_name()) # Configure the celery file in /etc/default celeryd_path = join(sep, 'etc', 'default') celeryd_filepath = join(celeryd_path, celery_service_name()) with fabric.api.cd(celeryd_path): fabtools.files.upload_template( 'celeryd.tpl', celeryd_filepath, context=env, template_dir=join(dirname(__file__), 'templates'), use_jinja=True, use_sudo=True, user='******', chown=True, mode='644') else: fabric.api.abort('Please provide parameters for Celery installation !')
def postgresql_setup(): '''PostgreSQL 9.1 + PostGIS 1.5''' with settings(show('user'), hide('warnings', 'running', 'stdout', 'stderr')): set_project() if 'pgpass' not in env.keys(): prompt('Passe PostgreSQL :', default=pgpass, key='pgpass') print(yellow('Configuration PostgreSQL+PostGIS...')) pretty_apt(['postgresql', 'binutils', 'gdal-bin', 'libproj-dev', 'postgresql-9.1-postgis', 'postgresql-server-dev-9.1', 'python-psycopg2', 'libgeoip1']) # print(yellow('Upgrading all packages...')) # fabtools.deb.upgrade() # création d'un utilisateur postgresql avec le meme nom d'utilisateur if not fabtools.postgres.user_exists(env.user): fabtools.postgres.create_user(env.user, env.pgpass) sudo('''psql -c "ALTER ROLE %(user)s CREATEDB;"''' % env, user='******') sudo('''psql -c "ALTER USER %(user)s with SUPERUSER;"''' % env, user='******') print(green('Création d’un superuser "%(user)s" PostgreSQL.' % env)) if not exists('.pgpass'): run('echo "*:*:*:%(user)s:%(pgpass)s" >> .pgpass' % env) sudo('chmod 0600 .pgpass') print(green('Création du fichier .pgpass.')) run('curl https://docs.djangoproject.com/en/dev/_downloads/create_template_postgis-debian.sh -o postgis.sh') run('chmod +x postgis.sh') run('./postgis.sh') #postgresql_net_access() icanhaz.postgres.server() # start server
def get_unique_random_name(*args, **kwargs): if 'unique_random_name' not in env.keys(): env['unique_random_name'] = list() while True: name = get_random_name(*args, **kwargs) if name not in env.unique_random_name: env.unique_random_name.append(name) return name
def dump(): from otto.cm.ubuntu import Precise box = Precise() print('initial_setup: ' + green(box.initial_setup)) (pre, pkgs, post) = box.wsgi_server print('wsgi_server: ' + green(' '.join(pkgs))) for key in sorted(env.keys()): print "%s = %s" % (key, env[key])
def copy_fabfile_to_agent(): src = 'tcutils/fabfile.py' dst = '~/fabfile.py' if 'fab_copied_to_hosts' not in env.keys(): env.fab_copied_to_hosts = list() if not env.host_string in env.fab_copied_to_hosts: if not exists(dst): put(src, dst) env.fab_copied_to_hosts.append(env.host_string)
def get_sriov_enabled(): if 'sriov' not in env.keys(): return False for compute in env.roledefs['compute']: if compute in env.sriov: return True return False
def decorated(*args, **kwargs): env.setdefault('use_sudo', True) env.setdefault('python_bin', 'python') env.setdefault('remote_owner', 'www-data') env.setdefault('remote_group', 'www-data') env.setdefault('pip_install_command', 'pip install -r requirements.txt') env.setdefault( 'domain_path', "%(base_dir)s/%(app_name)s" % { 'base_dir': env.base_dir, 'app_name': env.app_name }) env.setdefault( 'repo_path', "%(domain_path)s/repo" % {'domain_path': env.domain_path}) env.setdefault( 'current_path', "%(domain_path)s/current" % {'domain_path': env.domain_path}) env.setdefault( 'releases_path', "%(domain_path)s/releases" % {'domain_path': env.domain_path}) env.setdefault( 'shared_path', "%(domain_path)s/shared" % {'domain_path': env.domain_path}) env.setdefault( 'revisions_log_path', "%(domain_path)s/revisions.log" % {'domain_path': env.domain_path }) # TODO(Guodong Ding) complete this in 'after_deploy()' env.setdefault( 'current_time', time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) if 'releases' not in env.keys(): if dir_exists(env.releases_path): env.releases = sorted( run('ls -x %(releases_path)s' % { 'releases_path': env.releases_path }).split()) if len(env.releases) >= 1: env.current_revision = env.releases[-1] env.current_release = "%(releases_path)s/%(current_revision)s" % \ {'releases_path': env.releases_path, 'current_revision': env.current_revision} if len(env.releases) > 1: env.previous_revision = env.releases[-2] env.previous_release = "%(releases_path)s/%(previous_revision)s" % \ {'releases_path': env.releases_path, 'previous_revision': env.previous_revision} else: env.releases = [] return func(*args, **kwargs)
def show(keyword=''): """ Displays a list of all environment key/value pairs for the current role. """ keyword = keyword.strip().lower() max_len = max(len(k) for k in env.iterkeys()) keyword_found = False for k in sorted(env.keys()): if keyword and keyword not in k.lower(): continue keyword_found = True #print '%s: %s' % (k, env[k]) print('%s: ' % (k.ljust(max_len),)) pprint(env[k], indent=4) if keyword: if not keyword_found: print('Keyword "%s" not found.' % keyword)
def tissu_print(): """ Print FABRIC env dict and TISSU settings public properties """ if is_tissu_loaded(): puts(magenta("Fabric env :")) keys = env.keys() keys.sort() for k in keys: v = env[k] puts("%s : %s" % (yellow(k), v)) puts("\n") puts(magenta("Tissu settings :")) from tissu.conf import settings props = (name for name in dir(settings) if not name.startswith('_')) for k in props: puts("%s : %s" % (yellow(k), getattr(settings, k))) else: abort(red("No environnement loaded, please run fab e:{envnanme}"))
def install_postgres_server(user=None,dbname=None,password=None): """ Install postgres server & add user for postgres if no parameters are provided using (if exists) :: default_db_user default_db_name default_db_password """ if not (user and dbname and password): if all([e in env.keys() for e in ('default_db_user', 'default_db_name', 'default_db_password')]): user = env.default_db_user dbname = env.default_db_name password = env.default_db_password else: fabric.api.abort('Please provide user,dbname,password parameters for postgres.') fabric.api.execute(pydiploy.require.databases.postgres.install_postgres_server) fabric.api.execute(pydiploy.require.databases.postgres.add_postgres_user,user,password=password) fabric.api.execute(pydiploy.require.databases.postgres.add_postgres_database,dbname,owner=user,locale=env.locale)
def decorated(*args, **kwargs): env.setdefault('use_sudo', True) env.setdefault('python_bin', 'python') env.setdefault('remote_owner', 'www-data') env.setdefault('remote_group', 'www-data') env.setdefault('pip_install_command', 'pip install -r requirements.txt') env.setdefault('domain_path', "%(base_dir)s/%(app_name)s" % {'base_dir': env.base_dir, 'app_name': env.app_name}) env.setdefault('repo_path', "%(domain_path)s/repo" % {'domain_path': env.domain_path}) env.setdefault('current_path', "%(domain_path)s/current" % {'domain_path': env.domain_path}) env.setdefault('releases_path', "%(domain_path)s/releases" % {'domain_path': env.domain_path}) env.setdefault('shared_path', "%(domain_path)s/shared" % {'domain_path': env.domain_path}) env.setdefault('revisions_log_path', "%(domain_path)s/revisions.log" % {'domain_path': env.domain_path}) # TODO(Guodong Ding) complete this in 'after_deploy()' env.setdefault('current_time', time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) if 'releases' not in env.keys(): if dir_exists(env.releases_path): env.releases = sorted(run('ls -x %(releases_path)s' % {'releases_path': env.releases_path}).split()) if len(env.releases) >= 1: env.current_revision = env.releases[-1] env.current_release = "%(releases_path)s/%(current_revision)s" % \ {'releases_path': env.releases_path, 'current_revision': env.current_revision} if len(env.releases) > 1: env.previous_revision = env.releases[-2] env.previous_release = "%(releases_path)s/%(previous_revision)s" % \ {'releases_path': env.releases_path, 'previous_revision': env.previous_revision} else: env.releases = [] return func(*args, **kwargs)
def install_sap_client(): """ installs sap's specif client for Python saprfc for example. needed env vars in fabfile: * env.sap_download_url : eg 'http://libshost/lib/oracle_repo/'' * env.sap_packages : name(s) of zip file(s) for oracle's packages to deploy """ sap_lib_path = join(sep, 'usr', 'sap') if all([e in env.keys() for e in ('sap_download_url', 'sap_packages')]): # system libs and goodies installation fabtools.require.deb.packages(['libstdc++5']) fabtools.require.files.directory(path=sap_lib_path, use_sudo=True, mode='755') # get oracle's zip file(s) and unzip with fabric.api.cd(sap_lib_path): for package in env.sap_packages: fabric.api.sudo('wget -c %s%s' % (env.sap_download_url, package)) fabric.api.sudo('tar xvf %s' % package) fabric.api.sudo('chmod -R 755 rfcsdk') fabric.api.sudo('rm %s' % package) with fabric.api.cd(join(sep, 'lib')): if not fabtools.files.is_link('librfccm.so', use_sudo=True): fabric.api.sudo( 'ln -s %s .' % join(sap_lib_path, 'rfcsdk', 'lib', 'librfccm.so')) else: fabric.api.abort('Please provide parameters for sap installation !')
def install_sap_client(): """ installs sap's specif client for Python saprfc for example. needed env vars in fabfile: * env.sap_download_url : eg 'http://libshost/lib/oracle_repo/'' * env.sap_packages : name(s) of zip file(s) for oracle's packages to deploy """ sap_lib_path = join(sep, 'usr', 'sap') if all([e in env.keys() for e in ('sap_download_url', 'sap_packages')]): # system libs and goodies installation fabtools.require.deb.packages(['libstdc++5']) fabtools.require.files.directory(path=sap_lib_path, use_sudo=True, mode='755') # get oracle's zip file(s) and unzip with fabric.api.cd(sap_lib_path): for package in env.sap_packages: fabric.api.sudo('wget -c %s%s' % (env.sap_download_url, package)) fabric.api.sudo('tar xvf %s' % package) fabric.api.sudo('chmod -R 755 rfcsdk') fabric.api.sudo('rm %s' % package) with fabric.api.cd(join(sep, 'lib')): if not fabtools.files.is_link('librfccm.so', use_sudo=True): fabric.api.sudo('ln -s %s .' % join(sap_lib_path, 'rfcsdk', 'lib', 'librfccm.so')) else: fabric.api.abort('Please provide parameters for sap installation !')
def install_oracle_client(): """ installs oracle's specif client for Python oracle_cx for example. needed env vars in fabfile: * env.oracle_client_version eg : '11.2.0.2.0' * env.oracle_download_url : eg 'http://libshost/lib/oracle_repo/'' * env.oracle_remote_dir : name of oracle installation directore eg : 'oracle_client' * env.oracle_packages : name(s) of zip file(s) for oracle's packages to deploy """ if all([e in env.keys() for e in ('oracle_client_version', 'oracle_download_url', 'oracle_remote_dir', 'oracle_packages')]): # system libs and goodies installation fabtools.require.deb.packages(['libaio-dev', 'unzip']) fabtools.require.files.directory( path=os.path.join(env.remote_home, env.oracle_remote_dir), use_sudo=True, owner=env.remote_owner, group=env.remote_group, mode='750') # get oracle's zip file(s) and unzip with fabric.api.cd(env.remote_home): for package in env.oracle_packages: fabric.api.sudo('wget -c %s%s' % (env.oracle_download_url, package)) fabric.api.sudo('unzip %s -d %s' % (package, env.oracle_remote_dir)) fabric.api.sudo( 'rm %s' % os.path.join(env.remote_home, package)) oracle_dir = 'instantclient_%s' % '_'.join( env.oracle_client_version.split('.')[:2]) oracle_root_path = os.path.join(env.oracle_remote_dir, oracle_dir) oracle_full_path = os.path.join(env.remote_home, oracle_root_path) with fabric.api.cd(oracle_root_path): if not fabtools.files.is_link('libclntsh.so', use_sudo=True): fabric.api.sudo('ln -s libclntsh.so.* libclntsh.so') # library configuration oracle_conf = Template( "# ORACLE CLIENT CONFIGURATION" "\nexport ORACLE_HOME=$oracle_dir" "\nexport LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$ORACLE_HOME" ) fabric.api.sudo('pwd') fabric.api.sudo('echo \'%s\' >> .bashrc' % oracle_conf.substitute(oracle_dir=oracle_full_path)) fabric.api.sudo('source .bashrc') fabric.api.sudo( 'echo %s > /etc/ld.so.conf.d/oracle.conf' % oracle_full_path) fabric.api.sudo('ldconfig') else: fabric.api.abort('Please provide parameters for oracle installation !')
def install_oracle_client(): """ installs oracle's specif client for Python oracle_cx for example. needed env vars in fabfile: * env.oracle_client_version eg : '11.2.0.2.0' * env.oracle_download_url : eg 'http://libshost/lib/oracle_repo/'' * env.oracle_remote_dir : name of oracle installation directore eg : 'oracle_client' * env.oracle_packages : name(s) of zip file(s) for oracle's packages to deploy """ if all([ e in env.keys() for e in ('oracle_client_version', 'oracle_download_url', 'oracle_remote_dir', 'oracle_packages') ]): # system libs and goodies installation fabtools.require.deb.packages(['libaio-dev', 'unzip']) fabtools.require.files.directory(path=os.path.join( env.remote_home, env.oracle_remote_dir), use_sudo=True, owner=env.remote_owner, group=env.remote_group, mode='750') # get oracle's zip file(s) and unzip with fabric.api.cd(env.remote_home): for package in env.oracle_packages: fabric.api.sudo('wget -c %s%s' % (env.oracle_download_url, package)) fabric.api.sudo('unzip %s -d %s' % (package, env.oracle_remote_dir)) fabric.api.sudo('rm %s' % os.path.join(env.remote_home, package)) oracle_dir = 'instantclient_%s' % '_'.join( env.oracle_client_version.split('.')[:2]) oracle_root_path = os.path.join(env.oracle_remote_dir, oracle_dir) oracle_full_path = os.path.join(env.remote_home, oracle_root_path) with fabric.api.cd(oracle_root_path): if not fabtools.files.is_link('libclntsh.so', use_sudo=True): fabric.api.sudo('ln -s libclntsh.so.* libclntsh.so') # library configuration oracle_conf = Template( "# ORACLE CLIENT CONFIGURATION" "\nexport ORACLE_HOME=$oracle_dir" "\nexport LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$$ORACLE_HOME") fabric.api.sudo('pwd') fabric.api.sudo( 'echo \'%s\' >> .bashrc' % oracle_conf.substitute(oracle_dir=oracle_full_path)) fabric.api.sudo('source .bashrc') fabric.api.sudo('echo %s > /etc/ld.so.conf.d/oracle.conf' % oracle_full_path) fabric.api.sudo('ldconfig') else: fabric.api.abort('Please provide parameters for oracle installation !')
def project(): if 'projet' not in env.keys(): prompt('Nom du projet django:', default=projet, key='projet')
from fabric.api import env, put, puts, run, settings from fabric.decorators import runs_once from boto.ec2 import connect_to_region import json import re import time import calendar import os aws_region = None if "default_region" in env.keys(): aws_region = env.default_region @runs_once def region(arg_region): """ Set the region global. For example: region:ap-southeast-1 """ global aws_region aws_region = arg_region @runs_once def query(value=None, tag="Name"): """ query:tag=cluster_name,value=some_application """ conn = connect_to_region(aws_region) query_filters = {"tag:%s" % tag: value} env.hosts = []
def domain(): if 'domain' not in env.keys(): prompt('Nom de domaine:', default=domain, key='domain')
def set_project(): if 'projet' not in env.keys(): if 'alias' in env.keys() and not 'projet' in env.keys(): prompt('Nom du projet :', default=env['alias'], key='projet') else: prompt('Nom du projet :', default=projet, key='projet')
def set_domain(): if 'domain' not in env.keys(): prompt('DNS:', default=domain, key='domain')
def is_lbaas_enabled(): if 'enable_lbaas' not in env.keys(): return False else: return env.enable_lbaas
# objective: set and use environment variables to connect to a host from fabric.api import env from fabric.api import run print('env is a {env_class!s} class of object.'.format( env_class=env.__class__)) print('env contains the following methods:\n{env_methods!s}.'.format( env_methods=dir(env))) print('env contains the following keys:\n{env_keys!s}.'.format( env_keys=env.keys())) env.gateway = 'mygateway.mydomain.com' env.hosts = ['myhost.mydomain.com'] env.key_filename = '/path/to/.ssh/private_key' env.user = '******' # env.use_ssh_config is a good alternative # example command: fab get_hostname def get_hostname(): # run command hostname run on remote host print 'Calling run command: hostname' run('hostname')
# objective: set and use environment variables to connect to a host from fabric.api import env from fabric.api import run print ('env is a {env_class!s} class of object.'.format (env_class=env.__class__)) print ('env contains the following methods:\n{env_methods!s}.'.format (env_methods=dir(env))) print ('env contains the following keys:\n{env_keys!s}.'.format (env_keys=env.keys())) env.gateway = 'mygateway.mydomain.com' env.hosts = ['myhost.mydomain.com'] env.key_filename = '/path/to/.ssh/private_key' env.user = '******' # env.use_ssh_config is a good alternative # example command: fab get_hostname def get_hostname(): # run command hostname run on remote host print 'Calling run command: hostname' run('hostname')
gen_md5sums() create_iso(name) if __name__ == 'chicken': from fabric.api import env if not env['hosts']: env['hosts'] = ['localhost'] elif len(env['hosts']) > 1: print ('error: this script works only with 1 host at the same time') sys.exit() defaults = { 'storage_dir': '/var/lib/libvirt/images', 'git_dir': os.path.expanduser('~/git'), 'git_repo': 'ecg-puppet-staging', 'git_target': '/tmp/chicken/acmefactory', 'home_dir': os.path.expanduser('~'), # Home directory 'iso': 'debian-6.0.1a-amd64-netinst.iso', 'iso_url': 'http://cdimage.debian.org/debian-cd/6.0.1a/amd64/iso-cd', 'repository': '192.168.122.1', 'work_dir': '/tmp/chicken', # What to use as a working directory 'work_dir_local': '/tmp/chicken/local', # What to use as a working directory 'work_dir_remote': '/tmp/chicken/remote' # What to use as a working directory } for key in defaults.keys(): if not key in env.keys(): env[key] = defaults[key]
def configure_test_env(contrail_fab_path='/opt/contrail/utils', test_dir='/contrail-test'): """ Configure test environment by creating sanity_params.ini and sanity_testbed.json files """ sys.path.insert(0, contrail_fab_path) from fabfile.testbeds import testbed from fabfile.utils.host import get_openstack_internal_vip,\ get_control_host_string, get_authserver_ip, get_admin_tenant_name, \ get_authserver_port, get_env_passwords, get_authserver_credentials, \ get_vcenter_ip, get_vcenter_port, get_vcenter_username, \ get_vcenter_password, get_vcenter_datacenter, get_vcenter_compute from fabfile.utils.multitenancy import get_mt_enable from fabfile.utils.interface import get_data_ip cfgm_host = env.roledefs['cfgm'][0] with settings(warn_only=True): with lcd(contrail_fab_path): if local('git branch').succeeded: fab_revision = local('git log --format="%H" -n 1', capture=True) else: with settings(host_string=cfgm_host): fab_revision = run('cat /opt/contrail/contrail_packages/VERSION') with lcd(test_dir): if local('git branch').succeeded: revision = local('git log --format="%H" -n 1', capture=True) else: with settings(host_string=cfgm_host): revision = run('cat /opt/contrail/contrail_packages/VERSION') sanity_testbed_dict = { 'hosts': [], 'vgw': [], 'esxi_vms':[], 'hosts_ipmi': [], 'tor':[], } sample_ini_file = test_dir + '/' + 'sanity_params.ini.sample' with open(sample_ini_file, 'r') as fd_sample_ini: contents_sample_ini = fd_sample_ini.read() sanity_ini_templ = string.Template(contents_sample_ini) with settings(host_string = env.roledefs['openstack'][0]): openstack_host_name = run("hostname") with settings(host_string = env.roledefs['cfgm'][0]): cfgm_host_name = run("hostname") control_host_names = [] for control_host in env.roledefs['control']: with settings(host_string = control_host): host_name = run("hostname") control_host_names.append(host_name) cassandra_host_names = [] if 'database' in env.roledefs.keys(): for cassandra_host in env.roledefs['database']: with settings(host_string = cassandra_host): host_name = run("hostname") cassandra_host_names.append(host_name) internal_vip = get_openstack_internal_vip() for host_string in env.roledefs['all']: if host_string in env.roledefs.get('test',[]): continue host_ip = host_string.split('@')[1] with settings(host_string = host_string): host_name = run("hostname") host_dict = {} host_dict['ip'] = host_ip host_dict['data-ip']= get_data_ip(host_string)[0] if host_dict['data-ip'] == host_string.split('@')[1]: host_dict['data-ip'] = get_data_ip(host_string)[0] host_dict['control-ip']= get_control_host_string(host_string).split('@')[1] host_dict['name'] = host_name host_dict['username'] = host_string.split('@')[0] host_dict['password'] =get_env_passwords(host_string) host_dict['roles'] = [] if not internal_vip: if host_string in env.roledefs['openstack']: role_dict = {'type': 'openstack', 'params': {'cfgm': cfgm_host_name}} host_dict['roles'].append(role_dict) if host_string in env.roledefs['cfgm']: role_dict = {'type': 'cfgm', 'params': {'collector': host_name, 'cassandra': ' '.join(cassandra_host_names)}} if internal_vip: role_dict['openstack'] = 'contrail-vip' else: role_dict['openstack'] = openstack_host_name host_dict['roles'].append(role_dict) if host_string in env.roledefs['control']: role_dict = {'type': 'bgp', 'params': {'collector': cfgm_host_name, 'cfgm': cfgm_host_name}} host_dict['roles'].append(role_dict) if 'database' in env.roledefs.keys() and host_string in env.roledefs['database']: role_dict = { 'type': 'database', 'params': {'cassandra': ' '.join(cassandra_host_names)} } host_dict['roles'].append(role_dict) if host_string in env.roledefs['compute']: role_dict = {'type': 'compute', 'params': {'collector': cfgm_host_name, 'cfgm': cfgm_host_name}} role_dict['params']['bgp'] = [] if len(env.roledefs['control']) == 1: role_dict['params']['bgp'] = control_host_names else: for control_node in control_host_names: role_dict['params']['bgp'].append(control_node) # role_dict['params']['bgp'].extend(control_host_names[randrange(len(env.roledefs['control']))]) host_dict['roles'].append(role_dict) if 'collector' in env.roledefs.keys() and host_string in env.roledefs['collector']: role_dict = { 'type': 'collector', 'params': {'cassandra': ' '.join(cassandra_host_names)} } host_dict['roles'].append(role_dict) if 'webui' in env.roledefs.keys() and host_string in env.roledefs['webui']: role_dict = { 'type': 'webui', 'params': {'cfgm': cfgm_host_name} } host_dict['roles'].append(role_dict) sanity_testbed_dict['hosts'].append(host_dict) if env.has_key('vgw'): sanity_testbed_dict['vgw'].append(env.vgw) # Read ToR config sanity_tor_dict = {} if env.has_key('tor_agent'): sanity_testbed_dict['tor_agent'] = env.tor_agent # Read any tor-host config if env.has_key('tor_hosts'): sanity_testbed_dict['tor_hosts'] = env.tor_hosts # Read any MX config (as physical_router ) if env.has_key('physical_routers'): sanity_testbed_dict['physical_routers'] = env.physical_routers esxi_hosts = getattr(testbed, 'esxi_hosts', None) if esxi_hosts: for esxi in esxi_hosts: host_dict = {} host_dict['ip'] = esxi_hosts[esxi]['ip'] host_dict['data-ip'] = host_dict['ip'] host_dict['control-ip'] = host_dict['ip'] host_dict['name'] = esxi host_dict['username'] = esxi_hosts[esxi]['username'] host_dict['password'] = esxi_hosts[esxi]['password'] host_dict['contrail_vm'] = esxi_hosts[esxi]['contrail_vm']['host'] host_dict['roles'] = [] sanity_testbed_dict['hosts'].append(host_dict) sanity_testbed_dict['esxi_vms'].append(host_dict) # Adding vip VIP dict for HA test setup with settings(host_string = env.roledefs['openstack'][0]): if internal_vip: host_dict = {} host_dict['data-ip']= get_authserver_ip() host_dict['control-ip']= get_authserver_ip() host_dict['ip']= get_authserver_ip() host_dict['name'] = 'contrail-vip' with settings(host_string = env.roledefs['cfgm'][0]): host_dict['username'] = host_string.split('@')[0] host_dict['password'] = get_env_passwords(host_string) host_dict['roles'] = [] role_dict = {'type': 'openstack', 'params': {'cfgm': cfgm_host_name}} host_dict['roles'].append(role_dict) sanity_testbed_dict['hosts'].append(host_dict) # get host ipmi list if env.has_key('hosts_ipmi'): sanity_testbed_dict['hosts_ipmi'].append(env.hosts_ipmi) # generate json file and copy to cfgm sanity_testbed_json = json.dumps(sanity_testbed_dict) stop_on_fail = env.get('stop_on_fail', False) mail_to = env.get('mail_to', '') log_scenario = env.get('log_scenario', 'Sanity') stack_user, stack_password = get_authserver_credentials() stack_tenant = get_admin_tenant_name() # Few hardcoded variables for sanity environment # can be removed once we move to python3 and configparser stack_domain = 'default-domain' webserver_host = '10.204.216.50' webserver_user = '******' webserver_password = '******' webserver_log_path = '/home/bhushana/Documents/technical/logs/' webserver_report_path = '/home/bhushana/Documents/technical/sanity' webroot = 'Docs/logs' mail_server = '10.204.216.49' mail_port = '25' fip_pool_name = 'floating-ip-pool' public_virtual_network='public' public_tenant_name='admin' fixture_cleanup = 'yes' generate_html_report = 'True' key = 'key1' mailSender = '*****@*****.**' use_devicemanager_for_md5 = getattr(testbed, 'use_devicemanager_for_md5', False) orch = getattr(env, 'orchestrator', 'openstack') router_asn = getattr(testbed, 'router_asn', '') public_vn_rtgt = getattr(testbed, 'public_vn_rtgt', '') public_vn_subnet = getattr(testbed, 'public_vn_subnet', '') ext_routers = getattr(testbed, 'ext_routers', '') router_info = str(ext_routers) test_verify_on_setup = getattr(env, 'test_verify_on_setup', True) webui = getattr(testbed, 'webui', False) horizon = getattr(testbed, 'horizon', False) ui_config = getattr(testbed, 'ui_config', False) ui_browser = getattr(testbed, 'ui_browser', False) if 'mail_server' in env.keys(): mail_server = env.mail_server mail_port = env.mail_port vcenter_dc = '' if orch == 'vcenter': public_tenant_name='vCenter' if env.has_key('vcenter'): if env.vcenter: vcenter_dc = env.vcenter['datacenter'] sanity_params = sanity_ini_templ.safe_substitute( {'__testbed_json_file__' : 'sanity_testbed.json', '__nova_keypair_name__' : key, '__orch__' : orch, '__stack_user__' : stack_user, '__stack_password__' : stack_password, '__auth_ip__' : get_authserver_ip(), '__auth_port__' : get_authserver_port(), '__stack_tenant__' : stack_tenant, '__stack_domain__' : stack_domain, '__multi_tenancy__' : get_mt_enable(), '__address_family__' : get_address_family(), '__log_scenario__' : log_scenario, '__generate_html_report__': generate_html_report, '__fixture_cleanup__' : fixture_cleanup, '__webserver__' : webserver_host, '__webserver_user__' : webserver_user, '__webserver_password__' : webserver_password, '__webserver_log_dir__' : webserver_log_path, '__webserver_report_dir__': webserver_report_path, '__webroot__' : webroot, '__mail_server__' : mail_server, '__mail_port__' : mail_port, '__sender_mail_id__' : mailSender, '__receiver_mail_id__' : mail_to, '__http_proxy__' : env.get('http_proxy', ''), '__ui_browser__' : ui_browser, '__ui_config__' : ui_config, '__horizon__' : horizon, '__webui__' : webui, '__devstack__' : False, '__public_vn_rtgt__' : public_vn_rtgt, '__router_asn__' : router_asn, '__router_name_ip_tuples__': router_info, '__public_vn_name__' : fip_pool_name, '__public_virtual_network__':public_virtual_network, '__public_tenant_name__' :public_tenant_name, '__public_vn_subnet__' : public_vn_subnet, '__test_revision__' : revision, '__fab_revision__' : fab_revision, '__test_verify_on_setup__': test_verify_on_setup, '__stop_on_fail__' : stop_on_fail, '__ha_setup__' : getattr(testbed, 'ha_setup', ''), '__ipmi_username__' : getattr(testbed, 'ipmi_username', ''), '__ipmi_password__' : getattr(testbed, 'ipmi_password', ''), '__vcenter_dc__' : vcenter_dc, '__vcenter_server__' : get_vcenter_ip(), '__vcenter_port__' : get_vcenter_port(), '__vcenter_username__' : get_vcenter_username(), '__vcenter_password__' : get_vcenter_password(), '__vcenter_datacenter__' : get_vcenter_datacenter(), '__vcenter_compute__' : get_vcenter_compute(), '__use_devicemanager_for_md5__' : use_devicemanager_for_md5, }) ini_file = test_dir + '/' + 'sanity_params.ini' testbed_json_file = test_dir + '/' + 'sanity_testbed.json' with open(ini_file, 'w') as ini: ini.write(sanity_params) with open(testbed_json_file,'w') as tb: tb.write(sanity_testbed_json)
def domain(): if "domain" not in env.keys(): prompt("Nom de domaine:", default=domain, key="domain")
def env_info(): print('Enabled extenders: {}'.format(','.join(env.ENABLED_EXTENDERS))) print() print('Env settings:\n * {}'.format('\n * '.join( '{}={}'.format(k, repr(env[k])) for k in sorted(env.keys()) if k )))
def test_get(): keys = env.keys() print keys print 'passwords' in keys print 'hosts' in keys
def project(): if "projet" not in env.keys(): prompt("Nom du projet django:", default=projet, key="projet")