Пример #1
0
 def test_command(self):
     """Test command invocation.
     """
     subproc.call(['ls', '/tmp'])
     subprocess.call.assert_called_with(['/bin/ls', '-al', '/tmp'],
                                        env=mock.ANY,
                                        close_fds=True)
Пример #2
0
def start_service(app_root, service, once=True):
    """Starts a service in the app_root/services/service directory."""
    if once:
        opt = '-o'
    else:
        opt = '-u'
    subproc.call(['s6_svc', opt, os.path.join(app_root, service)])
Пример #3
0
def delete_chain(table, chain):
    """Delete a chain in the given table.

    :param ``str`` table:
        Name of the table where the chain resides.
    :param ``str`` chain:
        Name of the chain to delete
    """
    subproc.call(['iptables', '-t', table, '-X', chain])
Пример #4
0
def flush_chain(table, chain):
    """Flush a chain in the given table.

    :param ``str`` table:
        Name of the table where the chain resides.
    :param ``str`` chain:
        Name of the chain to create
    """
    subproc.call(['iptables', '-t', table, '-vF', chain])
Пример #5
0
def create_chain(table, chain):
    """Creates new chain in the given table.

    :param ``str`` table:
        Name of the table where the chain resides.
    :param ``str`` chain:
        Name of the chain to create
    """
    subproc.call(['iptables', '-t', table, '-N', chain])
Пример #6
0
def kill_service(app_root, service, signal='TERM'):
    """Send the service the specified signal."""
    signal_opts = dict([('STOP', '-p'), ('CONT', '-c'), ('HUP', '-h'),
                        ('ALRM', '-a'), ('INT', '-i'), ('TERM', '-t'),
                        ('KILL', '-k')])

    if signal not in signal_opts:
        utils.fatal('Unsupported signal: %s', signal)
    opt = signal_opts[signal]
    subproc.call(['s6_svc', opt, os.path.join(app_root, service)])
Пример #7
0
    def monitor(manifest, container_dir, appevents_dir):
        """Monitor container services."""
        app = yaml.load(manifest.read())
        with lc.LogContext(_LOGGER, app['name'], lc.ContainerAdapter) as log:
            svc_presence = presence.ServicePresence(
                app,
                container_dir,
                appevents_dir,
            )

            sys_dir = os.path.join(container_dir, 'sys')
            svc_sup_dir = os.path.join(sys_dir, 'start_container')

            failed_svc = None
            killed = False

            # Check that start_container was not terminated. This fixed race
            # condition if the presence exits and while restarted,
            # start_container is terminated.
            svc_sup_ran_once = os.path.exists(
                os.path.join(svc_sup_dir, 'self.pid'))
            log.info('services supervisor ran once: %s', svc_sup_ran_once)
            svc_sup_down = presence.is_down(svc_sup_dir)
            log.info('services supervisor down: %s', svc_sup_down)
            if svc_sup_down and svc_sup_ran_once:
                log.info('services supervisor was terminated, exiting.')
            else:
                svc_presence.ensure_supervisors_running()

                # Try to start the service, taking into account number of
                # restarts.
                # If the number of restarts is more than specified, delete app
                # from the model, which will trigger container shutdown.
                #
                # In case of container shutdown (application evicted from the
                # server), exit_app will not be called.
                while True:
                    success, failed_svc = svc_presence.start_all()
                    if not success:
                        break

                    svc_presence.wait_for_exit(svc_sup_dir)
                    if presence.is_down(svc_sup_dir):
                        log.info('Container services supervisor is down.')
                        failed_svc = None
                        killed = True
                        break

            svc_presence.exit_app(failed_svc, killed=killed)

            log.info('Shutting down sys supervisor.')
            subproc.call(['s6-svscanctl', '-pi', sys_dir])
Пример #8
0
 def _refresh_supervisor(self, instance_names=()):
     """Notify the supervisor of new instances to run."""
     subproc.check_call(['s6_svscanctl', '-an', self.tm_env.running_dir])
     for instance_name in instance_names:
         with lc.LogContext(_LOGGER, instance_name):
             _LOGGER.info('Starting')
             instance_run_link = os.path.join(self.tm_env.running_dir,
                                              instance_name)
             # Wait for the supervisor to pick up the new instance.
             for _ in range(10):
                 res = subproc.call([
                     's6_svok',
                     instance_run_link,
                 ])
                 if res == 0:
                     break
                 else:
                     _LOGGER.warning('Supervisor has not picked it up yet')
                     time.sleep(0.5)
             # Bring the instance up.
             subproc.check_call([
                 's6_svc',
                 '-uO',
                 instance_run_link,
             ])
Пример #9
0
def _renew_tickets(tkt_spool_dir):
    """Try and renew all tickets that match a pattern."""
    for tkt in glob.glob(os.path.join(tkt_spool_dir, '*')):
        if tkt.startswith('.'):
            continue

        _LOGGER.info('Renew ticket: %s', tkt)
        try:
            subproc.check_call(
                ['kinit', '-R'],
                environ={'KRB5CCNAME': 'FILE:' + tkt},
            )
            _LOGGER.info('Tickets renewed successfully.')
        except subproc.CalledProcessError as err:
            _LOGGER.info('Tickets not renewable, kinit rc: %s', err.returncode)
        except OSError as os_err:
            _LOGGER.warning('Error renewing tickets: %s', os_err)

        subproc.call(['klist', '-e', '-5', tkt])
Пример #10
0
    def wait_for_exit(self, container_svc_dir):
        """Waits for service to be down, reports status to zk."""
        watched_dirs = [
            os.path.join(self.services_dir, svc) for svc in self.services
        ]
        if container_svc_dir:
            watched_dirs.append(container_svc_dir)

        _LOGGER.info('waiting for service exit: %r', watched_dirs)

        # Wait for one of the services to come down.
        # TODO: need to investigate why s6-svwait returns 111 rather
        #                than 0.
        subproc.call(['s6_svwait', '-o', '-D'] + watched_dirs)

        # Wait for the supervisor to report finished status.
        time.sleep(1)

        for service in self.services:
            # If service is running, update_exit_status is noop.
            self.update_exit_status(service)
Пример #11
0
def archive_filesystem(block_dev, rootdir, archive, files):
    """Archive the filesystem of an application

    :param block_dev:
        Block device from where to archive the new filesystem
    :type block_dev:
        ``str``
    :param rootdir:
        Path to a directory where to mount the root device
    :type rootdir:
        ``str``
    :param archive:
        Path to the archive file to create
    :type archive:
        ``str``
    :param files:
        List of files and folders to include in the archive
        relative to rootdir. This is input list of files to
        tar.
    :type list:
        ``str``
    """
    if not files:
        _LOGGER.info('Nothing to archive.')
        return True

    if not os.path.exists(rootdir):
        _LOGGER.error('Root device directory does not exist: %s', rootdir)
        return False

    archive_cmd = '{tm_root}/sbin/archive_container.sh'.format(
        tm_root=utils.rootdir())

    arguments = [
        'unshare',
        '--mount',
        archive_cmd,
        block_dev,
        # rootdir
        os.path.realpath(rootdir),
        # archive
        os.path.realpath(archive)
    ]

    # Make sure files are relative path.
    safe_files = [filename.lstrip('/') for filename in files]
    arguments.extend(safe_files)

    result = subproc.call(arguments)
    return result == 0
Пример #12
0
def benchmark(directory,
              volume=BENCHMARK_VOLUME,
              rw_type=BENCHMARK_RW_TYPE,
              job_number=BENCHMARK_JOB_NUMBER,
              thread_number=BENCHMARK_THREAD_NUMBER,
              block_size=BENCHMARK_IOPS_BLOCK_SIZE,
              max_seconds=BENCHMARK_MAX_SECONDS):
    """Use fio to do benchmark.
    """
    result = {}
    config_file = os.path.join(directory, _BENCHMARK_CONFIG_FILE)
    result_file = os.path.join(directory, _BENCHMARK_RESULT_FILE)

    # prepare fio config
    config = configparser.SafeConfigParser()
    global_section = 'global'
    config.add_section(global_section)
    config.set(global_section, 'group_reporting', '1')
    config.set(global_section, 'unlink', '1')
    config.set(global_section, 'time_based', '1')
    config.set(global_section, 'direct', '1')
    config.set(global_section, 'size', volume)
    config.set(global_section, 'rw', rw_type)
    config.set(global_section, 'numjobs', job_number)
    config.set(global_section, 'iodepth', thread_number)
    config.set(global_section, 'bs', block_size)
    config.set(global_section, 'runtime', max_seconds)
    drive_section = 'drive'
    config.add_section(drive_section)
    config.set(drive_section, 'directory', directory)
    fs.write_safe(config_file, lambda f: config.write(EqualSpaceRemover(f)))

    # start fio
    ret = subproc.call([
        'fio', config_file, '--norandommap', '--minimal', '--output',
        result_file
    ])

    # parse fio terse result
    # http://fio.readthedocs.io/en/latest/fio_doc.html#terse-output
    if ret == 0:
        with io.open(result_file) as fp:
            metric_list = fp.read().split(';')
            result[Metrics.READ_BPS.value] = int(float(metric_list[6]) * 1024)
            result[Metrics.READ_IOPS.value] = int(metric_list[7])
            result[Metrics.WRITE_BPS.value] = int(
                float(metric_list[47]) * 1024)
            result[Metrics.WRITE_IOPS.value] = int(metric_list[48])

    return result
Пример #13
0
def blk_fs_test(block_dev):
    """Test the existence of a filesystem on a given block device.

    We essentially try to read the superblock and assume no filesystem if we
    fail.

    :param block_dev:
        Block device where to create the new filesystem.
    :type block_dev:
        ``str``
    :returns ``bool``:
        True if the block device contains a filesystem.
    """
    res = subproc.call(['tune2fs', '-l', block_dev])
    return bool(res == 0)
Пример #14
0
def _reforward_ticket(ticket_file, tkt_final_dir, endpoint):
    """Forward ticket to self, potentially correcting ticket enc type."""
    _LOGGER.info('Reforwarding to: %s', endpoint)
    _LOGGER.info('Before loopback forward: %s', ticket_file)
    subproc.call(['klist', '-e', '-5', ticket_file])

    host, port = endpoint.split(':')
    subproc.call(['tkt_send_v2', '-h{}'.format(host), '-p{}'.format(port)],
                 environ={'KRB5CCNAME': 'FILE:' + ticket_file})

    final_tkt_path = os.path.join(tkt_final_dir, os.path.basename(ticket_file))
    _LOGGER.info('After loopback forward: %s', final_tkt_path)
    subproc.call(['klist', '-e', '-5', final_tkt_path])
Пример #15
0
def stop_service(app_root, service):
    """Stops the service and do not restart it."""
    subproc.call(['s6_svc', '-d', os.path.join(app_root, service)])
Пример #16
0
def exec_root_supervisor(directory):
    """Execs svscan in the directory."""
    if os.name == 'nt':
        subproc.call(['s6_svscan', directory])
    else:
        subproc.exec_pid1(['s6_svscan', directory])