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 run_sensor(opts): put_sensor() sensor_output_file = start_sensor(opts, run) sensor_result = {} try: yield sensor_result sensor_result.update(parse_sensor_file(get_rf(sensor_output_file))) finally: stop_sensor(run, sensor_output_file)
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)