def test_replace(): with warnings.catch_warnings(): fname = os.tmpnam() t1 = 'some data' t2 = 'other text' header = '-' * 50 footer = '=' * 50 mkfl = lambda x : header + '\n' + x + '\n' + footer core.put_rf(fname, mkfl(t1)) try: ok(core.replace_in_file(fname, t2, t1)) == False ok(core.replace_in_file(fname, t1, t2)) == True ok(core.get_rf(fname)) == mkfl(t2) sudo('chown root.root ' + fname) sudo('chmod o-w ' + fname) ok(lambda : core.replace_in_file(fname, t2, t1)).raises(SystemExit) ok(core.replace_in_file(fname, t1, t2, use_sudo=True)) == False ok(core.replace_in_file(fname, t2, t1, use_sudo=True)) == True ok(core.get_rf(fname)) == mkfl(t1) finally: sudo('rm ' + fname)
def test_get_put(): with warnings.catch_warnings(): fname = os.tmpnam() t1 = 'some data' t2 = 'other text' core.put_rf(fname, t1) try: ok(core.get_rf(fname)) == t1 sudo('chown root.root ' + fname) sudo('chmod o-w ' + fname) ok(lambda : core.put_rf(fname, t2)).raises(SystemExit) core.put_rf(fname, t2, use_sudo=True) ok(core.get_rf(fname)) == t2 with core.remote_fl(fname, use_sudo=True) as fc: ok(fc.getvalue()) == t2 fc.setvalue(t1) ok(core.get_rf(fname)) == t1 finally: sudo('rm ' + fname)
def psql(cmd, user='******'): fname = get_tfile() put_rf(fname, cmd) try: sudo("""su - {0} -c "psql -f {1}" """.format(user, fname)) finally: run('rm ' + fname)
def set_static_ip(ip_and_sz, gw=None): network = ipaddr.IPNetwork(ip_and_sz) ip = ip_and_sz.split('/',1)[0] if gw is None: gw = str(network.network + 1) nnew = static_ip.format(ip=ip, mask=str(network.netmask), gw=gw, bcast=str(network.broadcast)) put_rf('/etc/network/interfaces', nnew, use_sudo=True)
def add_supervisor_prog(name, **params): scfg = get_rf(supervisord_config) prog_line = '[program:{0}]'.format(name) if prog_line not in scfg: lines = [prog_line] lines.extend("{0}={1}".format(*itm) for itm in params.items()) scfg = scfg.rstrip() + "\n" + "\n".join(lines) + "\n" put_rf(supervisord_config, scfg, use_sudo=True)
def install_oracle_java(ver='1.6.0_27'): if ver != '1.6.0_27': raise RuntimeError("Only '1.6.0_27' version of java support by now") JAVAC = check_cmd('javac') JAVA_RT = check_cmd('java') if JAVAC and JAVA_RT: return JAVAH = '/usr/local/java' JDK_DIR = os.path.join(JAVAH, 'jdk1.6.0_27') JRE_DIR = os.path.join(JAVAH, 'jre1.6.0_27') JDK_URL = 'http://download.oracle.com/otn-pub/java/jdk/6u27-b07/jdk-6u27-linux-x64.bin' JRE_URL = 'http://download.oracle.com/otn-pub/java/jdk/6u27-b07/jre-6u27-linux-x64.bin' if not exists(JAVAH): sudo('mkdir ' + JAVAH) with cd(JAVAH): expect_cmd = 'spawn sh {0};' + \ ' expect "Press Enter to continue.....\\n"; send "\\n"' if not exists(os.path.join(JDK_DIR,'bin/javac')): if exists(JDK_DIR): sudo('rm -rf {0}/*'.format(JDK_DIR)) wget(JDK_URL, use_sudo=True) sudo("expect -c '{0}'".format( expect_cmd.format('jdk-6u27-linux-x64.bin'))) sudo('rm ' + 'jdk-6u27-linux-x64.bin') if not exists(os.path.join(JRE_DIR,'bin/java')): if exists(JRE_DIR): sudo('rm -rf {0}/*'.format(JRE_DIR)) wget(JRE_URL, use_sudo=True) sudo("expect -c '{0}'".format( expect_cmd.format('jre-6u27-linux-x64.bin'))) sudo('rm ' + 'jre-6u27-linux-x64.bin') ln1 = 'export JAVA_HOME=' + JRE_DIR ln2 = 'export PATH="$PATH:{0}/bin:{1}/bin"'.format(JRE_DIR, JDK_DIR) profile = get_rf('/etc/profile') put_rf('/etc/profile', "\n".join((profile, ln1, ln2, '')), use_sudo=True)
def add_to_hosts(host, ip): cont = get_rf('/etc/hosts') for i in cont.split('\n'): i = i.strip() if i.startswith('#') or i == '' or ' ' not in i: continue fip, fhost = i.split(' ', 1) if fhost.strip() == host and fip.strip() == ip: return put_rf('/etc/hosts', cont + '\n{} {}'.format(ip,host))
def set_host_name(name): fc = get_rf('/etc/hostname') if name != fc.strip(): put_rf('/etc/hostname', name, use_sudo=True)
def install_nova(ip, net, net_prefix, lvm_dev, ext_network, proj_name, nova_adm='ubuntu', dbpasswd='nova', compute_backend='lxc'): install('bridge-utils,postgresql,python-psycopg2') replace_in_file("/etc/postgresql/9.1/main/postgresql.conf", "#listen_addresses\s+=\s+'localhost'(.*)", r"listen_addresses = '*'\1", use_sudo=True) pg_hba = '/etc/postgresql/9.1/main/pg_hba.conf' add = "host all all 0.0.0.0/0 md5" sudo("chmod a+rw " + pg_hba) try: append(pg_hba, add) finally: sudo("chmod 640 " + pg_hba) sudo("/etc/init.d/postgresql restart") install('glance') cmd = "CREATE user glancedbadmin;\n" + \ "ALTER user glancedbadmin with password '{0}';\n" + \ 'CREATE DATABASE glance;\n' + \ 'GRANT ALL PRIVILEGES ON database glance TO glancedbadmin;\n' psql(cmd.format(dbpasswd)) replace_in_file("/etc/glance/glance-registry.conf", 'sql_connection = .*?$', 'sql_connection = postgresql://glancedbadmin:{0}@{1}/glance'\ .format(dbpasswd,ip), use_sudo=True) sudo('restart glance-registry') install('rabbitmq-server,nova-common,nova-doc,python-nova,nova-api,' + \ 'nova-network,nova-volume,nova-objectstore,nova-scheduler,' + \ 'nova-compute,euca2ools,unzip,nova-compute-' + compute_backend) cmd = 'CREATE user novadbadmin;\n' + \ "ALTER user novadbadmin with password '{0}';\n" + \ 'CREATE DATABASE nova;\n' + \ 'GRANT ALL PRIVILEGES ON database nova TO novadbadmin;\n' psql(cmd.format(dbpasswd)) cfg = nova_config.format(ip, net, '8', dbpasswd, net_prefix) put_rf('/etc/nova/nova.conf', cfg , use_sudo=True) use_lvm = False if use_lvm: install('iscsitarget,iscsitarget-dkms') sudo("sed -i 's/false/true/g' /etc/default/iscsitarget") sudo("service iscsitarget restart") sudo("pvcreate " + lvm_dev) sudo("vgcreate nova-volumes " + lvm_dev) sudo("chown -R root:nova /etc/nova") sudo("chmod 644 /etc/nova/nova.conf") all_nova_services = ('libvirt-bin,nova-network,nova-compute,nova-api,' + \ 'nova-objectstore,nova-scheduler,' + \ 'glance-api,glance-registry,nova-volume').split(',') for service in all_nova_services: upstart_restart(service) sudo('nova-manage db sync') sudo('nova-manage network create private {0} 1 255'.format(net)) sudo('nova-manage floating create --ip_range=' + ext_network) sudo('nova-manage user admin novaadmin') sudo('nova-manage project create {0} novaadmin'.format(proj_name)) for service in all_nova_services: upstart_restart(service) sudo('mkdir -p /home/{0}/creds'.format(nova_adm)) sudo('nova-manage project zipfile proj novaadmin ' + \ '/home/{0}/creds/novacreds.zip'.format(nova_adm)) with cd('/home/{0}/creds'.format(nova_adm)): sudo('unzip novacreds.zip') sudo('chown {0}:{0} /home/{0}/creds -R'.format(nova_adm)) res = sudo('nova-manage user exports novaadmin') EC2_ACCESS_KEY = None EC2_SECRET_KEY = None for string in str(res).split('\n'): string = string.strip() if string.startswith('export EC2_ACCESS_KEY='): EC2_ACCESS_KEY = string[len('export EC2_ACCESS_KEY='):] elif string.startswith('export EC2_SECRET_KEY='): EC2_SECRET_KEY = string[len('export EC2_SECRET_KEY='):] replace_in_file('/home/{0}/creds/novarc'.format(nova_adm), 'export EC2_ACCESS_KEY="novaadmin:proj"'.format(proj_name), 'export EC2_ACCESS_KEY="{0}:{1}"'.format(EC2_ACCESS_KEY, proj_name)) replace_in_file('/home/{0}/creds/novarc'.format(nova_adm), 'export NOVA_PROJECT_ID="proj"', 'export NOVA_PROJECT_ID="{0}"'.format(proj_name)) return