Example #1
0
def parse_args(args):
    project = CanariProject(args.out_path)

    if project.is_valid:
        args.out_path = project.common_dir
        args.out_file = project.entities_py
    else:
        args.out_path = project.root_dir
        args.out_file = os.path.join(args.out_path, 'entities.py')

    if os.path.exists(args.out_file) and not args.append and not \
            parse_bool('%r already exists. Are you sure you want to overwrite it?' % args.out_file, default=False):
        exit(-1)

    if not args.mtz_file:
        if not project.is_valid or not os.path.lexists(project.entities_mtz):
            print("Please specify a valid MTZ file.")
            exit(-1)
        args.mtz_file = project.entities_mtz

    if args.maltego_entities:
        args.namespace.extend(args.exclude_namespace)
        args.exclude_namespace = []

    args.project = project

    return args
Example #2
0
def parse_args(args):
    project = CanariProject(args.out_path)

    if project.is_valid:
        args.out_path = project.common_dir
        args.out_file = project.entities_py
    else:
        args.out_path = project.root_dir
        args.out_file = os.path.join(args.out_path, 'entities.py')

    if os.path.exists(args.out_file) and not args.append and not \
            parse_bool('%r already exists. Are you sure you want to overwrite it?' % args.out_file, default=False):
        exit(-1)

    if not args.mtz_file:
        if not project.is_valid or not os.path.lexists(project.entities_mtz):
            print("Please specify a valid MTZ file.", file=sys.stderr)
            exit(-1)
        args.mtz_file = project.entities_mtz

    if args.maltego_entities:
        args.namespace.extend(args.exclude_namespace)
        args.exclude_namespace = []

    args.project = project

    return args
Example #3
0
def dockerize_package(args):
    project = CanariProject()

    print('Dockerizing %s transform package...' % project.name)

    configurator = Configurator(
            'canari.resources.templates:dockerize_package',
            project.root_dir,
            {'non_interactive': True},
            variables={'project.name': project.name, 'canari.version': version}
    )

    print('Creating Dockerfile for %s...' % project.name)
    configurator.render()
    print('done!')

    if not find_executable('docker'):
        print """Could not find 'docker' in your system path. Please download and install Docker from http://docker.com
        and rerun this command again.
        """

        exit(-1)

    docker_hosts = [j for sublist in [('-H', i) for i in args.host] for j in sublist]
    container = '%s/%s:%s' % (project.name, project.name, args.os)

    if not args.host:
        if not find_executable('docker-machine'):
            print """Could not find 'docker-machine' in your system path. Please download and install Docker Machine from
            http://docker.com and rerun this command again or manually specify a Docker host using the '-H' parameter,
            instead.
            """
            exit(-1)

        print 'Attempting to discover available Docker machines.'
        machines = run_command(['docker-machine', 'ls', '-q'], stdout=subprocess.PIPE).communicate()[0].split('\n')
        machines.remove('')

        machine = question.parse_int('More than one Docker machine was detected. Which one would you like to use to'
                                     'build and run this container?', machines) if len(machines) != 1 else 0

        print 'Setting up environment for Docker machine %s' % machines[machine]

        # Inject docker environment variables
        env = run_command(['docker-machine', 'env', machines[machine]], stdout=subprocess.PIPE).communicate()[0]
        os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env))

    with PushDir(project.root_dir):
        p = run_command(['docker'] + docker_hosts + ['build', '-t', container, '-f', 'Dockerfile-%s' % args.os, '.'])
        p.communicate()
        if p.returncode:
            print 'An error occurred while building the Docker container.'
            exit(-1)

    if question.parse_bool('Would you like to run this container now?'):
        port = question.parse_int_range('Which port would you like Plume to listen on externally?', 0, 65535, 8080)
        print 'Plume will be listening on http://%s:%s' % (re.findall('://([^:]+)', os.environ['DOCKER_HOST'])[0], port)
        run_command(['docker'] + docker_hosts + ['run', '-it', '-p', '8080:%s' % port, container]).communicate()

    print 'done!'
Example #4
0
    def _write_config(src, dst, **kwargs):
        if os.path.lexists(dst) and not \
                parse_bool('%s already exists. Would you like to overwrite it?' % dst, default=False):
            return

        print('Writing %s to %s' % (src, dst))
        with file(dst, mode='wb') as w:
            if kwargs.pop('is_template', None):
                w.write(string.Template(file(src).read()).substitute(**kwargs))
            else:
                w.write(file(src).read())
Example #5
0
def install_wizard(opts):
    configurator = Configurator('canari.resources.templates:install_plume', '.',
                                {'non_interactive': False, 'remember_answers': False})
    configurator.ask_questions()

    if os.environ.get('VIRTUAL_ENV'):
        run_venv = parse_bool(
            "--> Canari has detected that you're running this install script from within a virtualenv.\n"
            "--> Would you like to run Plume from this virtualenv (%r) as well?" % os.environ['VIRTUAL_ENV'], True)
        configurator.variables['plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False

    configurator.render()
    finish(configurator)
Example #6
0
    def __init__(self, transform_classes, auto_sudo=False):
        locals_ = {}
        asked = False
        config = load_config()
        for transform_class in transform_classes:
            transform = transform_class()
            locals_['do' + transform.name.split('.')[-1]] = ShellCommand(transform, config)
            if not asked and transform.superuser and os.name == 'posix' and os.geteuid():
                if not auto_sudo and parse_bool("A transform requiring 'root' access was detected."
                                                " Would you like to run this shell as 'root'?", False):
                    sudo()
                asked = True

        InteractiveConsole.__init__(self, locals=locals_)
        MtgConsole.init_history(os.path.expanduser('~/.mtgsh_history'))
Example #7
0
def install_wizard(opts):
    configurator = Configurator('canari.resources.templates:install_plume',
                                '.', {
                                    'non_interactive': False,
                                    'remember_answers': False
                                })
    configurator.ask_questions()

    if os.environ.get('VIRTUAL_ENV'):
        run_venv = parse_bool(
            "--> Canari has detected that you're running this install script from within a virtualenv.\n"
            "--> Would you like to run Plume from this virtualenv (%r) as well?"
            % os.environ['VIRTUAL_ENV'], True)
        configurator.variables[
            'plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False

    configurator.render()
    finish(configurator)
Example #8
0
    def _write_config(src, dst, **kwargs):
        if os.path.lexists(dst) and not \
                parse_bool('%s already exists. Would you like to overwrite it?' % dst, default=False):
            return

        print ('Writing %s to %s' % (src, dst))
        with file(dst, mode='wb') as w:
            if kwargs.pop('is_template', None):
                w.write(
                        string.Template(
                                file(
                                        src
                                ).read()
                        ).substitute(**kwargs)
                )
            else:
                w.write(
                        file(
                                src
                        ).read()
                )
Example #9
0
    def configure(self,
                  install_prefix,
                  load=True,
                  remote=False,
                  defaults=False,
                  **kwargs):
        if load:
            dst = os.path.join(install_prefix, 'canari.conf')
            if os.path.lexists(dst) and not defaults and \
                    parse_bool('%s already exists. Would you like to overwrite it?' % dst, default=False):
                print 'Writing fresh copy of canari.conf to %r...' % dst
                variables = {
                    'canari.command':
                    ' '.join(sys.argv),
                    'profile.config':
                    self.config_file if self.name != 'canari' else '',
                    'profile.path':
                    '${PATH},/usr/local/bin,/opt/local/bin'
                    if os.name == 'posix' else ''
                }

                configurator = Configurator(
                    u'canari.resources.templates:create_profile',
                    install_prefix, {
                        'non_interactive': True,
                        'remember_answers': True
                    },
                    variables=variables)
                configurator.render()
                return

        if self._package_name != 'canari':
            if load:
                package_config = resource_filename(
                    self.get_resource_module('etc'), self.config_file)
                self._write_config(
                    package_config,
                    os.path.join(install_prefix, self.config_file))
            self._update_config(os.path.join(install_prefix, 'canari.conf'),
                                load, remote)
Example #10
0
    def configure(self, install_prefix, load=True, remote=False, defaults=False, **kwargs):
        if load:
            dst = os.path.join(install_prefix, 'canari.conf')
            if os.path.lexists(dst) and not defaults and \
                    parse_bool('%s already exists. Would you like to overwrite it?' % dst, default=False):
                print 'Writing fresh copy of canari.conf to %r...' % dst
                variables = {
                    'canari.command': ' '.join(sys.argv),
                    'profile.config': self.config_file if self.name != 'canari' else '',
                    'profile.path': '${PATH},/usr/local/bin,/opt/local/bin' if os.name == 'posix' else ''
                }

                configurator = Configurator('canari.resources.templates:create_profile',
                                            install_prefix,
                                            {'non_interactive': True, 'remember_answers': True},
                                            variables=variables)
                configurator.render()
                return

        if self._package_name != 'canari':
            if load:
                package_config = resource_filename(self.get_resource_module('etc'), self.config_file)
                self._write_config(package_config, os.path.join(install_prefix, self.config_file))
            self._update_config(os.path.join(install_prefix, 'canari.conf'), load, remote)
Example #11
0
def dockerize_package(args):
    project = CanariProject()

    print('Dockerizing %s transform package...' % project.name)

    configurator = Configurator('canari.resources.templates:dockerize_package',
                                project.root_dir, {'non_interactive': True},
                                variables={
                                    'project.name': project.name,
                                    'canari.version': version
                                })

    print('Creating Dockerfile for %s...' % project.name)
    configurator.render()
    print('done!')

    if not find_executable('docker'):
        print """Could not find 'docker' in your system path. Please download and install Docker from http://docker.com
        and rerun this command again.
        """
        exit(-1)

    if not args.host and os.path.exists('/var/run/docker.sock'):
        args.host = ['unix:///var/run/docker.sock']

    docker_hosts = [
        j for sublist in [('-H', i) for i in args.host] for j in sublist
    ]
    container = '%s/%s:%s' % (project.name, project.name, args.os)

    if not args.host:
        if not find_executable('docker-machine'):
            print """Could not find 'docker-machine' in your system path. Please download and install Docker Machine from
            http://docker.com and rerun this command again or manually specify a Docker host using the '-H' parameter,
            instead.
            """
            exit(-1)

        print 'Attempting to discover available Docker machines.'
        machines = run_command(
            ['docker-machine', 'ls', '-q'],
            stdout=subprocess.PIPE).communicate()[0].split('\n')
        machines.remove('')

        machine = question.parse_int(
            'More than one Docker machine was detected. Which one would you like to use to'
            'build and run this container?',
            machines) if len(machines) != 1 else 0

        print 'Setting up environment for Docker machine %s' % machines[machine]

        # Inject docker environment variables
        env = run_command(['docker-machine', 'env', machines[machine]],
                          stdout=subprocess.PIPE).communicate()[0]
        os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env))

    with PushDir(project.root_dir):
        p = run_command(
            ['docker'] + docker_hosts +
            ['build', '-t', container, '-f',
             'Dockerfile-%s' % args.os, '.'])
        p.communicate()
        if p.returncode:
            print 'An error occurred while building the Docker container.'
            exit(-1)

    if question.parse_bool('Would you like to run this container now?'):
        port = question.parse_int_range(
            'Which port would you like Plume to listen on externally?', 0,
            65535, 8080)
        print 'Plume will be listening on http://%s:%s' % (re.findall(
            '://([^:]+)', os.environ.get('DOCKER_HOST',
                                         'http://0.0.0.0'))[0], port)
        run_command(['docker'] + docker_hosts +
                    ['run', '-it', '-p',
                     '8080:%s' % port, container]).communicate()

    print 'done!'