<commit_before>import json
import os
import re
import string
import subprocess as sp
import sys
import yaml

import click

from .. import utils


@click.group()
@click.pass_context
def cli(ctx):
    if ctx.invoked_subcommand not in ('init', 'ls'):
        try:
            with open("lxcwfile.yml", 'r') as stream:
                ctx.obj = yaml.load(stream)
                ctx.obj['vm']['box']['release'] = str(
                    ctx.obj['vm']['box']['release'])
                ctx.obj['vm']['hostnames'] = [ctx.obj['vm']['hostname']]
                if 'aliases' in ctx.obj['vm']:
                    ctx.obj['vm']['hostnames'] += ctx.obj['vm']['aliases']
                if 'provision' in ['vm']:
                    ctx.obj['vm']['provision']['ansible']['playbook'] = (
                        os.path.join(
                            os.getcwd(),
                            ctx.obj['vm']['provision']['ansible']['playbook']))
        except IOError as err:
            click.secho('No \'lxcwfile.yml\' present', fg='red')
            sys.exit(1)


@click.command()
@click.pass_context
def ssh(ctx):
    os.execvp('ssh',
              ['ssh', ctx.obj['vm']['hostname'], '-l', os.environ['USER']])


PLAYBOOK_UP_HOST = string.Template('''
---
- hosts: all
  become: yes
  tasks:
    - lineinfile: dest=/etc/default/lxc-net regexp=LXC_DHCP_CONFILE line=LXC_DHCP_CONFILE=/etc/dnsmasq.d/lxc
    - lineinfile: dest=/etc/dnsmasq.d/lxc line="dhcp-host=${hostname},${ip}"
    - lineinfile: dest=/etc/hosts line="${ip} ${hostnames}"
    - lineinfile: dest=/var/lib/lxc/${hostname}/rootfs/etc/sudoers line="${user} ALL=(ALL) NOPASSWD:ALL" state=present
''')


PLAYBOOK_UP_CONTAINER = '''
---
- hosts: all
  become: yes
  tasks:
  - apt: name={{ item }} state=latest
    with_items:
     - python
     - python-pip
     - python-dev
     - build-essential
    when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

  - yum: name={{ item }} state=latest
    with_items:
     - python
     - python-pip
    when: ansible_distribution == 'CentOS'
'''


@click.command()
@click.pass_context
def up(ctx):
    try:
        if not ctx.obj['nopasswd_sudoer']:
            # Add user to sudoers to allow run sudo commands without password
            utils.ansible(
                'localhost', 'lineinfile',
                'dest=/etc/sudoers line="{} ALL=(ALL) NOPASSWD:ALL"'
                ' state=present'.format(os.environ['USER']))
        output = sp.check_output(
            ['sudo', 'lxc-info', '--name', ctx.obj['vm']['hostname']])
    except sp.CalledProcessError:
        output = None
    finally:
        message = (
            'is not running' if utils.os_release() == '12.04'
            else "doesn't exist")
        if not output or message in output:
            cmd = ['sudo', 'lxc-create', '-t', ctx.obj['vm']['box']['distro'],
                   '--name', ctx.obj['vm']['hostname'], '--',
                   '--release', ctx.obj['vm']['box']['release']]
            if ctx.obj['vm']['box']['distro'] == 'ubuntu':
                cmd += ['--bindhome', os.environ['USER'], '--user',
                        os.environ['USER']]
            elif ctx.obj['vm']['box']['distro'] == 'centos':
                try:
                   sp.check_call(['dpkg', '-l', 'yum'])
                except sp.CalledProcessError:
                    click.secho(
                        'Please, to install CentOS containers'
                        ' "sudo apt-get install yum"', fg='blue')
                    sys.exit(1)
            else:
                click.secho('Ubuntu and CentOS containers supported', fg='red')
                sys.exit(1)
            sp.call(cmd)

            utils.ansible_playbook(
                'localhost', playbook_content=PLAYBOOK_UP_HOST.substitute(
                    hostname=ctx.obj['vm']['hostname'],
                    hostnames=' '.join(ctx.obj['vm']['hostnames']),
                    ip=utils.random_unused_ip(), user=os.environ['USER']))
            sp.call(['sudo', 'service', 'lxc-net', 'restart'])
            sp.call(
                ['sudo', 'lxc-start', '--name', ctx.obj['vm']['hostname']])

            utils.ansible_playbook(
                ctx.obj['vm']['hostname'],
                playbook_content=PLAYBOOK_UP_CONTAINER)
            if 'provision' in ctx.obj['vm']:
                utils.ansible_playbook(
                    ctx.obj['vm']['hostname'],
                    ctx.obj['vm']['provision']['ansible']['playbook'],
                    ctx.obj['vm']['provision']['ansible'].get('extra_vars'))
        else:
            sp.call(<commit_msg>Add user creation and minimal packages installation to centos container creation<commit_after>import crypt
import json
import os
import re
import string
import subprocess as sp
import sys
import yaml

import click

from .. import utils


@click.group()
@click.pass_context
def cli(ctx):
    if ctx.invoked_subcommand not in ('init', 'ls'):
        try:
            with open("lxcwfile.yml", 'r') as stream:
                ctx.obj = yaml.load(stream)
                ctx.obj['vm']['box']['release'] = str(
                    ctx.obj['vm']['box']['release'])
                ctx.obj['vm']['hostnames'] = [ctx.obj['vm']['hostname']]
                if 'aliases' in ctx.obj['vm']:
                    ctx.obj['vm']['hostnames'] += ctx.obj['vm']['aliases']
                if 'provision' in ['vm']:
                    ctx.obj['vm']['provision']['ansible']['playbook'] = (
                        os.path.join(
                            os.getcwd(),
                            ctx.obj['vm']['provision']['ansible']['playbook']))
        except IOError as err:
            click.secho('No \'lxcwfile.yml\' present', fg='red')
            sys.exit(1)


@click.command()
@click.pass_context
def ssh(ctx):
    os.execvp('ssh',
              ['ssh', ctx.obj['vm']['hostname'], '-l', os.environ['USER']])


PLAYBOOK_UP = string.Template('''
---
- hosts: all
  become: yes
  tasks:
    - lineinfile: dest=/etc/default/lxc-net regexp=LXC_DHCP_CONFILE line=LXC_DHCP_CONFILE=/etc/dnsmasq.d/lxc
    - lineinfile: dest=/etc/dnsmasq.d/lxc line="dhcp-host=${hostname},${ip}"
    - lineinfile: dest=/etc/hosts line="${ip} ${hostnames}"
    - lineinfile: dest=/var/lib/lxc/${hostname}/rootfs/etc/sudoers line="${user} ALL=(ALL) NOPASSWD:ALL" state=present
''')

@click.command()
@click.pass_context
def up(ctx):
    if not ctx.obj['nopasswd_sudoer']:
        # Add user to sudoers to allow run sudo commands without password
        utils.ansible(
            'localhost', 'lineinfile',
            'dest=/etc/sudoers line="{} ALL=(ALL) NOPASSWD:ALL"'
            ' state=present'.format(os.environ['USER']))

    try:
        output = sp.check_output(
            ['sudo', 'lxc-info', '--name', ctx.obj['vm']['hostname']])
    except sp.CalledProcessError:
        output = None
    finally:
        message = (
            'is not running' if utils.os_release() == '12.04'
            else "doesn't exist")
        if not output or message in output:
            cmd = ['sudo', 'lxc-create',
                   '--template', ctx.obj['vm']['box']['distro'],
                   '--name', ctx.obj['vm']['hostname'], '--',
                   '--release', ctx.obj['vm']['box']['release']]
            if ctx.obj['vm']['box']['distro'] == 'ubuntu':
                cmd += ['--bindhome', os.environ['USER'],
                        '--user', os.environ['USER'],
                        '--packages', ','.join([
                            'python', 'python-pip', 'python-dev',
                            'build-essential'])]
            elif ctx.obj['vm']['box']['distro'] == 'centos':
                if int(ctx.obj['vm']['box']['release']) < 6:
                    click.secho(
                        'Only CentOS >= 6 container supported', fg='red')
                    sys.exit(1)
                try:
                   sp.check_call(['dpkg', '-l', 'yum'])
                except sp.CalledProcessError:
                    click.secho(
                        'Please, to install CentOS containers'
                        ' "sudo apt-get install yum"', fg='blue')
                    sys.exit(1)
            else:
                click.secho(
                    'Only Ubuntu and CentOS containers supported', fg='red')
                sys.exit(1)
            sp.call(cmd)

            if ctx.obj['vm']['box']['distro'] == 'centos':
                sp.call(
                    ['sudo', 'chroot', '/var/lib/lxc/{}/rootfs'.format(
                        ctx.obj['vm']['hostname']),
                     'useradd', '--create-home', os.environ['USER'],
                     '-p', crypt.crypt('centos', 'aa')])
                sp.call(
                    ['sudo', 'chroot', '/var/lib/lxc/{}/rootfs'.format(
                        ctx.obj['vm']['hostname']),
                     'chown', os.environ['USER'],
                     '/home/{}'.format(os.environ['USER'])])
                sp.call(
                    ['sudo', 'chroot', '/var/lib/lxc/{}/rootfs'.format(
                        ctx.obj['vm']['hostname']),
                     'yum', 'install', '-y', 'sudo', 'epel-release'])
                sp.call(
                    ['sudo', 'chroot', '/var/lib/lxc/{}/rootfs'.format(
                        ctx.obj['vm']['hostname']),
                     'yum', 'install', '-y', 'python-pip'])

            utils.ansible_playbook(
                'localhost', playbook_content=PLAYBOOK_UP.substitute(
                    hostname=ctx.obj['vm']['hostname'],
                    hostnames=' '.join(ctx.obj['vm']['hostnames']),
                    ip=utils.random_unused_ip(), user=os.environ['USER']))
            sp.call(['sudo', 'service', 'lxc-net', 'restart'])
            sp.call(
                ['sudo', 'lxc-start', '--name', ctx.obj['vm']['hostname']])

            if 'provision' in ctx.obj['vm']:
                utils.ansible_playbook(
                    ctx.obj['vm']['hostname'],
                    ctx.obj['vm']['provision']['ansible']['playbook'],
                    ctx.obj['vm']['provision']['ansible'].get('extra_vars'))
        else:
            sp.call(
{'fraction_non_alphanumeric': 0.18072052616079318, 'fraction_numerical': 0.0016687935604201432, 'mean_word_length': 1.7632221318144834, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1744081', 'n_tokens_mistral': 3155, 'n_tokens_neox': 3000, 'n_words': 563}