Example #1
0
def test_file_is_empty():
    from metrique.utils import file_is_empty, write_file, rand_chars
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars(prefix='empty_test_1'))
    f2 = os.path.join(cache_dir, rand_chars(prefix='not_empty_test_2'))

    write_file(f1, '')
    write_file(f2, 'not empty')

    assert file_is_empty(f1)
    assert exists(f1)
    assert file_is_empty(f1, remove=True)
    assert not exists(f1)

    assert not file_is_empty(f2)

    try:
        # not a valid path
        file_is_empty('DOES_NOT_EXIST')
    except RuntimeError:
        pass
    else:
        assert False

    try:
        # not a valid path
        file_is_empty(True)
    except RuntimeError:
        pass
    else:
        assert False

    remove_file(f2)
    assert not exists(f2)
Example #2
0
def test_file_is_empty():
    from metrique.utils import file_is_empty, write_file, rand_chars
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars(prefix='empty_test_1'))
    f2 = os.path.join(cache_dir, rand_chars(prefix='not_empty_test_2'))

    write_file(f1, '')
    write_file(f2, 'not empty')

    assert file_is_empty(f1)
    assert exists(f1)
    assert file_is_empty(f1, remove=True)
    assert not exists(f1)

    assert not file_is_empty(f2)

    try:
        # not a valid path
        file_is_empty('DOES_NOT_EXIST')
    except RuntimeError:
        pass
    else:
        assert False

    try:
        # not a valid path
        file_is_empty(True)
    except RuntimeError:
        pass
    else:
        assert False

    remove_file(f2)
    assert not exists(f2)
Example #3
0
def test_write_file():
    from metrique.utils import write_file, rand_chars, read_file
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars())
    write_file(f1, 'hello world')
    assert exists(f1)
    assert read_file(f1) == 'hello world'

    # can't overwrite files with default settings
    try:
        write_file(f1, 'hello world', force=False, exists_ext=None)
    except RuntimeError:
        pass
    else:
        assert False, "File overwritten without force=True"

    write_file(f1, 'hello metrique', force=True)
    assert exists(f1)
    assert read_file(f1) == 'hello metrique'

    write_file(f1, 'hello world', mode='a')
    assert exists(f1)
    assert read_file(f1) == 'hello metriquehello world'

    # done remove the file; write it again, second time will write '.new' file
    # .new file added to new file on write is exists_ext not null
    write_file(f1, 'hello world', force=False, exists_ext='new')
    assert read_file(f1)
    assert read_file('%s.new' % f1)

    remove_file(f1)
Example #4
0
def postgresql_firstboot(force=False):
    exists = os.path.exists(POSTGRESQL_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return
    utils.make_dirs(POSTGRESQL_PGDATA_PATH)

    cmd = 'pg_ctl -D %s -l %s init' % (POSTGRESQL_PGDATA_PATH,
                                       POSTGRESQL_LOGFILE)
    utils.sys_call(cmd)

    started = False
    try:
        started = postgresql_start()
        time.sleep(1)
        cmd = 'createdb -h 127.0.0.1'
        utils.sys_call(cmd)
        cmd = 'psql -h 127.0.0.1 -c "%s"'
        P = PASSWORD
        tz = "set timezone TO 'GMT';"
        encoding = "set client_encoding TO 'utf8';"
        admin_user = "******" % P
        admin_db = "CREATE DATABASE admin WITH OWNER admin;"
        test_user = "******" % P
        test_db = "CREATE DATABASE test WITH OWNER test;"
        [
            utils.sys_call(cmd % sql)
            for sql in (tz, encoding, admin_user, admin_db, test_user, test_db)
        ]
    finally:
        if started:
            postgresql_stop()

    utils.write_file(POSTGRESQL_FIRSTBOOT_PATH, '')
    return True
Example #5
0
def test_write_file():
    from metrique.utils import write_file, rand_chars, read_file
    from metrique.utils import remove_file

    f1 = os.path.join(cache_dir, rand_chars())
    write_file(f1, 'hello world')
    assert exists(f1)
    assert read_file(f1) == 'hello world'

    # can't overwrite files with default settings
    try:
        write_file(f1, 'hello world', force=False, exists_ext=None)
    except RuntimeError:
        pass
    else:
        assert False, "File overwritten without force=True"

    write_file(f1, 'hello metrique', force=True)
    assert exists(f1)
    assert read_file(f1) == 'hello metrique'

    write_file(f1, 'hello world', mode='a')
    assert exists(f1)
    assert read_file(f1) == 'hello metriquehello world'

    # done remove the file; write it again, second time will write '.new' file
    # .new file added to new file on write is exists_ext not null
    write_file(f1, 'hello world', force=False, exists_ext='new')
    assert read_file(f1)
    assert read_file('%s.new' % f1)

    remove_file(f1)
Example #6
0
def postgresql_firstboot(force=False):
    exists = os.path.exists(POSTGRESQL_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return
    utils.make_dirs(POSTGRESQL_PGDATA_PATH)

    cmd = 'pg_ctl -D %s -l %s init' % (POSTGRESQL_PGDATA_PATH,
                                       POSTGRESQL_LOGFILE)
    utils.sys_call(cmd)

    started = False
    try:
        started = postgresql_start()
        time.sleep(1)
        cmd = 'createdb -h 127.0.0.1'
        utils.sys_call(cmd)
        cmd = 'psql -h 127.0.0.1 -c "%s"'
        P = PASSWORD
        tz = "set timezone TO 'GMT';"
        encoding = "set client_encoding TO 'utf8';"
        admin_user = "******" % P
        admin_db = "CREATE DATABASE admin WITH OWNER admin;"
        test_user = "******" % P
        test_db = "CREATE DATABASE test WITH OWNER test;"
        [utils.sys_call(cmd % sql) for sql in (tz, encoding, admin_user,
                                               admin_db, test_user, test_db)]
    finally:
        if started:
            postgresql_stop()

    utils.write_file(POSTGRESQL_FIRSTBOOT_PATH, '')
    return True
Example #7
0
 def write_report(self, force=False):
     '''
     Writes the report to a file.
     '''
     path = self.title + '.html'
     value = self._template.format(
         title=self.title, body=self.body, sidebar=self.sidebar)
     write_file(path, value, force=force)
     plt.ion()
Example #8
0
 def write_report(self, force=False):
     '''
     Writes the report to a file.
     '''
     path = self.title + '.html'
     value = self._template.format(title=self.title,
                                   body=self.body,
                                   sidebar=self.sidebar)
     write_file(path, value, force=force)
     plt.ion()
Example #9
0
def pyclient_firstboot(force=False):
    exists = os.path.exists(METRIQUE_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_METRIQUE_JSON

    DEFAULT_METRIQUE_JSON = DEFAULT_METRIQUE_JSON % (
        LOCAL_IP, PASSWORD, LOCAL_IP, PASSWORD, CELERYD_BROKER_DB, PASSWORD,
        PASSWORD, LOCAL_IP)

    utils.write_file(METRIQUE_JSON, DEFAULT_METRIQUE_JSON)
    utils.write_file(METRIQUE_FIRSTBOOT_PATH, '')
Example #10
0
def nginx_firstboot(force=False):
    exists = os.path.exists(NGINX_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_NGINX_CONF
    DEFAULT_NGINX_CONF = DEFAULT_NGINX_CONF % (
        USER, NGINX_ERROR_LOG, NGINX_PIDFILE, TMP_DIR, TMP_DIR, CACHE_DIR,
        TMP_DIR, CACHE_DIR, TMP_DIR, CACHE_DIR, TMP_DIR, CACHE_DIR, LOCAL_IP,
        LOCAL_IP, LOCAL_IP, LOCAL_IP, NGINX_ERROR_LOG, NGINX_ACCESS_LOG,
        LOCAL_IP, SSL_CERT, SSL_KEY, STATIC_DIR)

    utils.write_file(NGINX_CONF, DEFAULT_NGINX_CONF)
    utils.write_file(NGINX_FIRSTBOOT_PATH, '')
Example #11
0
def pyclient_firstboot(force=False):
    exists = os.path.exists(METRIQUE_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_METRIQUE_JSON

    DEFAULT_METRIQUE_JSON = DEFAULT_METRIQUE_JSON % (
        LOCAL_IP, PASSWORD, LOCAL_IP, PASSWORD,
        CELERYD_BROKER_DB,
        PASSWORD, PASSWORD, LOCAL_IP)

    utils.write_file(METRIQUE_JSON, DEFAULT_METRIQUE_JSON)
    utils.write_file(METRIQUE_FIRSTBOOT_PATH, '')
Example #12
0
def nginx_firstboot(force=False):
    exists = os.path.exists(NGINX_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_NGINX_CONF
    DEFAULT_NGINX_CONF = DEFAULT_NGINX_CONF % (
        USER, NGINX_ERROR_LOG, NGINX_PIDFILE, TMP_DIR,
        TMP_DIR, CACHE_DIR, TMP_DIR, CACHE_DIR, TMP_DIR, CACHE_DIR,
        TMP_DIR, CACHE_DIR, LOCAL_IP, LOCAL_IP, LOCAL_IP, LOCAL_IP,
        NGINX_ERROR_LOG, NGINX_ACCESS_LOG, LOCAL_IP, SSL_CERT, SSL_KEY,
        STATIC_DIR)

    utils.write_file(NGINX_CONF, DEFAULT_NGINX_CONF)
    utils.write_file(NGINX_FIRSTBOOT_PATH, '')
Example #13
0
def supervisord_firstboot(force=False):
    exists = os.path.exists(SUPERVISORD_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_SUPERVISORD_CONF
    ENVIRONMENT = 'VIRTUAL_ENV="%s", METRIQUE_HOME="%s"' % (VIRTUAL_ENV,
                                                            HOME_DIR)
    RPC = ('supervisor.rpcinterface_factory = '
           'supervisor.rpcinterface:make_main_rpcinterface')
    DEFAULT_SUPERVISORD_CONF = DEFAULT_SUPERVISORD_CONF % (
        LOCAL_IP, PASSWORD, RPC, SUPERVISORD_LOGFILE, SUPERVISORD_PIDFILE,
        LOGS_DIR, USER, ENVIRONMENT, LOCAL_IP, PASSWORD,
        SUPERVISORD_HISTORYFILE)

    utils.write_file(SUPERVISORD_CONF, DEFAULT_SUPERVISORD_CONF)
    utils.write_file(SUPERVISORD_FIRSTBOOT_PATH, '')
Example #14
0
def supervisord_firstboot(force=False):
    exists = os.path.exists(SUPERVISORD_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    global DEFAULT_SUPERVISORD_CONF
    ENVIRONMENT = 'VIRTUAL_ENV="%s", METRIQUE_HOME="%s"' % (
        VIRTUAL_ENV, HOME_DIR)
    RPC = ('supervisor.rpcinterface_factory = '
           'supervisor.rpcinterface:make_main_rpcinterface')
    DEFAULT_SUPERVISORD_CONF = DEFAULT_SUPERVISORD_CONF % (
        LOCAL_IP, PASSWORD, RPC, SUPERVISORD_LOGFILE, SUPERVISORD_PIDFILE,
        LOGS_DIR, USER, ENVIRONMENT, LOCAL_IP, PASSWORD,
        SUPERVISORD_HISTORYFILE)

    utils.write_file(SUPERVISORD_CONF, DEFAULT_SUPERVISORD_CONF)
    utils.write_file(SUPERVISORD_FIRSTBOOT_PATH, '')
Example #15
0
def sys_firstboot(force=False):
    exists = os.path.exists(SYS_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    # create default dirs in advance
    [utils.make_dirs(p) for p in (PREFIX_DIR, PIP_CACHE_DIR, PIP_ACCEL_DIR,
                                  PIP_EGGS, TRASH_DIR, LOGS_DIR,
                                  ETC_DIR, BACKUP_DIR, TMP_DIR, CACHE_DIR,
                                  STATIC_DIR, PIDS_DIR)]

    # make sure the the default user python eggs dir is secure
    os.chmod(PIP_EGGS, 0700)

    # generate self-signed ssl certs
    try:
        ssl()
    except Exception as e:
        logger.warn('Failed to create ssl certs: %s' % e)

    utils.write_file(SYS_FIRSTBOOT_PATH, '')
Example #16
0
def sys_firstboot(force=False):
    exists = os.path.exists(SYS_FIRSTBOOT_PATH)
    if exists and not force:
        # skip if we have already run this before
        return

    # create default dirs in advance
    [
        utils.make_dirs(p)
        for p in (PREFIX_DIR, PIP_CACHE_DIR, PIP_ACCEL_DIR, PIP_EGGS,
                  TRASH_DIR, LOGS_DIR, ETC_DIR, BACKUP_DIR, TMP_DIR, CACHE_DIR,
                  STATIC_DIR, PIDS_DIR)
    ]

    # make sure the the default user python eggs dir is secure
    os.chmod(PIP_EGGS, 0700)

    # generate self-signed ssl certs
    try:
        ssl()
    except Exception as e:
        logger.warn('Failed to create ssl certs: %s' % e)

    utils.write_file(SYS_FIRSTBOOT_PATH, '')