Ejemplo n.º 1
0
def init_package(opts):
    """
    initialise a repo with a basic cirrus setup
    """
    if opts.bootstrap:
        with working_dir(opts.repo):
            bootstrap_repo(opts)

    setup_branches(opts)
    # write files
    files = create_files(opts)
    with working_dir(opts.repo):
        commit_and_tag(opts, *files)

    if opts.register_with_pypi:
        # run setup.py sdist and then
        # call register_package with dist file
        package = setup_sdist(opts)
        LOGGER.info("Registering package {} with pypi {}".format(
            package, opts.register_with_pypi))
        register_package(package, opts.register_with_pypi)

    msg = ("\nA basic cirrus.conf file has been added to your package\n"
           "please review it and add any additional fields and commit it\n"
           "The files have been added to the {} branch").format(opts.develop)
    LOGGER.info(msg)
Ejemplo n.º 2
0
    def test_init_container(self):
        """test init_container call"""

        opts = mock.Mock()
        opts.repo = self.dir
        opts.template_dir = "container-template"
        opts.image_dir = "container-image"
        opts.container = "steve/awesome-image:latest"
        opts.entrypoint = "/bin/bash"
        opts.docker_registry = None
        opts.virtualenv = "/pyenvy/venvs/data_py2"
        with working_dir(self.dir):
            init_container(opts)

        templates = os.path.join(self.dir, 'container-template')
        expected_files = [
            '.dockerstache',
            'context.json',
            'Dockerfile.mustache',
            'post_script.sh',
            'pre_script.sh',
            'local_pip_install.sh.mustache',
            'pypi_pip_install.sh.mustache'
        ]
        self.failUnless(os.path.exists(templates))
        found = os.listdir(templates)
        for exp in expected_files:
            self.failUnless(exp in found)
        dockerfile = os.path.join(templates, 'Dockerfile.mustache')
        with open(dockerfile, 'r') as handle:
            content = handle.read()

        self.failUnless("FROM steve/awesome-image:latest" in content)
        self.failUnless("MAINTAINER [email protected]" in content)
        self.failUnless("ENTRYPOINT [\"/bin/bash\"]" in content)

        with open(self.cirrus_conf, 'r') as handle:
            conf = handle.read()
        self.failUnless('[docker]' in conf)
        self.failUnless('dockerstache_template = container-template' in conf)
        self.failUnless('dockerstache_context = container-template/context.json' in conf)
        self.failUnless('directory = container-image' in conf)

        local_install = os.path.join(
            templates,
            'local_pip_install.sh.mustache'
        )
        with open(local_install, 'r') as handle:
            local = handle.read()
            self.failUnless('. /pyenvy/venvs/data_py2/bin/activate' in local)
            self.failUnless('pip install PIP_OPTS' in local)
        pypi_install = os.path.join(
            templates,
            'pypi_pip_install.sh.mustache'
        )
        with open(pypi_install, 'r') as handle:
            pypi =  handle.read()
            self.failUnless('pip install PIP_OPTS' in pypi)
            self.failUnless('. /pyenvy/venvs/data_py2/bin/activate' in pypi)
Ejemplo n.º 3
0
def init_package(opts):
    """
    initialise a repo with a basic cirrus setup
    """
    if opts.bootstrap:
        with working_dir(opts.repo):
            bootstrap_repo(opts)

    setup_branches(opts)
    # write files
    files = create_files(opts)
    with working_dir(opts.repo):
        commit_and_tag(opts, *files)

    msg = ("\nA basic cirrus.conf file has been added to your package\n"
           "please review it and add any additional fields and commit it\n"
           "The files have been added to the {} branch").format(opts.develop)
    LOGGER.info(msg)
Ejemplo n.º 4
0
def setup_branches(opts):
    """
    set up git branches, starting from master
    """
    do_push = not opts.no_remote
    LOGGER.info("setting up branches master={} develop={}".format(
        opts.master, opts.develop))
    with working_dir(opts.repo):
        initializer = RepoInitializer(opts.repo)
        initializer.init_branch(opts.master, opts.origin, remote=do_push)
        initializer.init_branch(opts.develop, opts.origin, remote=do_push)
        branch(opts.repo, opts.develop, opts.master)
    LOGGER.info("Working on {}".format(get_active_branch(opts.repo)))
Ejemplo n.º 5
0
def setup_branches(opts):
    """
    set up git branches, starting from master
    """
    do_push = not opts.no_remote
    LOGGER.info("setting up branches master={} develop={}".format(
        opts.master, opts.develop))
    with working_dir(opts.repo):
        branch(opts.repo, opts.master, opts.master)
        branch(opts.repo, opts.develop, opts.master)
        if do_push:
            LOGGER.info("Pushing {}...".format(opts.develop))
            push(opts.repo)

    LOGGER.info("Working on {}".format(get_active_branch(opts.repo)))
Ejemplo n.º 6
0
def init_container(opts):
    """
    Initialise a basic container-template setup
    for this package
    """
    cirrus_conf = os.path.join(opts.repo, 'cirrus.conf')
    if not os.path.exists(cirrus_conf):
        msg = "No cirrus.conf found, need to init repo first?"
        LOGGER.error(msg)
        sys.exit(1)

    config = load_configuration(opts.repo)
    print "cirrus_conf=", cirrus_conf
    template_dir = os.path.join(opts.repo, opts.template_dir)
    if not os.path.exists(template_dir):
        LOGGER.info("Creating Template in {}".format(template_dir))
        os.makedirs(template_dir)

    docker_file = os.path.join(template_dir, 'Dockerfile.mustache')
    dotfile = os.path.join(template_dir, '.dockerstache')
    pre_script = os.path.join(template_dir, 'pre_script.sh')
    post_script = os.path.join(template_dir, 'post_script.sh')
    context = os.path.join(template_dir, 'context.json')
    local_install = os.path.join(template_dir, 'local_pip_install.sh.mustache')
    pypi_install = os.path.join(template_dir, 'pypi_pip_install.sh.mustache')
    opts.context_file = os.path.join(opts.template_dir, 'context.json')

    # make sure repo is clean
    if has_unstaged_changes(opts.repo):
        msg = ("Error: Unstaged changes are present on the branch "
               "Please commit them or clean up before proceeding")
        LOGGER.error(msg)
        raise RuntimeError(msg)

    main_branch = config.gitflow_branch_name()
    LOGGER.info("checking out latest {} branch...".format(main_branch))
    checkout_and_pull(opts.repo, main_branch, not opts.no_remote)

    venv_option = ""
    if opts.virtualenv:
        venv_option = ". {}/bin/activate".format(opts.virtualenv)

    with working_dir(template_dir):
        write_basic_dockerfile(opts, config, docker_file)
        write_json_file(dotfile, {
            "post_script": "post_script.sh",
            "pre_script": "pre_script.sh"
        })
        write_json_file(context, {})
        write_script(pre_script, DOCKER_PRE_SCRIPT)
        write_script(local_install,
                     LOCAL_INSTALL_SCRIPT,
                     virtualenv=venv_option)
        write_script(
            pypi_install,
            PYPI_INSTALL_SCRIPT,
            virtualenv=venv_option,
            pip_options=config.pip_options() if config.pip_options() else "")
        if opts.local_install:
            write_script(post_script,
                         DOCKER_POST_SCRIPT,
                         copy_dist=LOCAL_INSTALL_COMMAND.format(
                             package=config.package_name()))
        else:
            write_script(post_script, DOCKER_POST_SCRIPT, copy_dist="")
        edit_cirrus_conf(opts, config)

        modified = [
            cirrus_conf, docker_file, dotfile, pre_script, post_script,
            local_install, pypi_install, context
        ]
        LOGGER.info("commiting changes...")
        commit_files_optional_push(opts.repo,
                                   "git cirrus package container-init",
                                   not opts.no_remote, *modified)
Ejemplo n.º 7
0
def init_container(opts):
    """
    Initialise a basic container-template setup
    for this package
    """
    cirrus_conf = os.path.join(opts.repo, 'cirrus.conf')
    if not os.path.exists(cirrus_conf):
        msg = "No cirrus.conf found, need to init repo first?"
        LOGGER.error(msg)
        sys.exit(1)

    config = load_configuration(opts.repo)
    template_dir = os.path.join(opts.repo, opts.template_dir)
    if not os.path.exists(template_dir):
        LOGGER.info("Creating Template in {}".format(template_dir))
        os.makedirs(template_dir)

    docker_file = os.path.join(template_dir, 'Dockerfile.mustache')
    dotfile = os.path.join(template_dir, '.dockerstache')
    pre_script = os.path.join(template_dir, 'pre_script.sh')
    post_script = os.path.join(template_dir, 'post_script.sh')
    context = os.path.join(template_dir, 'context.json')
    installer_script = os.path.join(template_dir, 'install_script.sh.mustache')
    opts.context_file = os.path.join(opts.template_dir, 'context.json')

    # make sure repo is clean
    if has_unstaged_changes(opts.repo):
        msg = ("Error: Unstaged changes are present on the branch "
               "Please commit them or clean up before proceeding")
        LOGGER.error(msg)
        raise RuntimeError(msg)

    main_branch = config.gitflow_branch_name()
    LOGGER.info("checking out latest {} branch...".format(main_branch))
    checkout_and_pull(opts.repo, main_branch, not opts.no_remote)

    venv_option = ""
    if opts.virtualenv:
        venv_option = ". {}/bin/activate".format(opts.virtualenv)

    with working_dir(template_dir):
        write_basic_dockerfile(opts, config, docker_file)
        write_json_file(
            dotfile, {
                "post_script": "post_script.sh",
                "pre_script": "pre_script.sh",
                "inclusive": True,
                "excludes":
                ["post_script.sh", "post_script.sh", ".dockerstache"]
            })
        write_json_file(context, {})

        # render templates for container scripts
        template_context = {
            'open_brace': '{{',
            'close_brace': '}}',
            'package': config.package_name(),
            'virtualenv': venv_option,
            'pip_options': config.pip_options() if config.pip_options() else ""
        }

        install_template = find_template('install_script.sh.mustache')
        pre_template = find_template('pre_script.sh.mustache')
        post_template = find_template('post_script.sh.mustache')

        renderer = pystache.Renderer()
        install_result = renderer.render_path(install_template,
                                              template_context)

        with open(installer_script, 'w') as handle:
            handle.write(install_result)

        post_result = renderer.render_path(post_template, template_context)
        with open(post_script, 'w') as handle:
            handle.write(post_result)

        pre_result = renderer.render_path(pre_template, template_context)
        with open(pre_script, 'w') as handle:
            handle.write(pre_result)

        make_executable(pre_script, opts.repo)
        make_executable(post_script, opts.repo)
        make_executable(installer_script, opts.repo)

        edit_cirrus_conf(opts, config)

        modified = [
            cirrus_conf, docker_file, dotfile, pre_script, post_script,
            installer_script, context
        ]
        LOGGER.info("commiting changes...")
        commit_files_optional_push(opts.repo,
                                   "git cirrus package container-init",
                                   not opts.no_remote, *modified)
Ejemplo n.º 8
0
def init_container(opts):
    """
    Initialise a basic container-template setup
    for this package
    """
    cirrus_conf = os.path.join(opts.repo, 'cirrus.conf')
    if not os.path.exists(cirrus_conf):
        msg = "No cirrus.conf found, need to init repo first?"
        LOGGER.error(msg)
        sys.exit(1)

    config = load_configuration(opts.repo)
    template_dir = os.path.join(opts.repo, opts.template_dir)
    if not os.path.exists(template_dir):
        LOGGER.info("Creating Template in {}".format(template_dir))
        os.makedirs(template_dir)

    docker_file = os.path.join(template_dir, 'Dockerfile.mustache')
    dotfile = os.path.join(template_dir, '.dockerstache')
    pre_script = os.path.join(template_dir, 'pre_script.sh')
    post_script = os.path.join(template_dir, 'post_script.sh')
    context = os.path.join(template_dir, 'context.json')
    installer_script = os.path.join(template_dir, 'install_script.sh.mustache')
    opts.context_file = os.path.join(opts.template_dir, 'context.json')

     # make sure repo is clean
    if has_unstaged_changes(opts.repo):
        msg = (
            "Error: Unstaged changes are present on the branch "
            "Please commit them or clean up before proceeding"
        )
        LOGGER.error(msg)
        raise RuntimeError(msg)

    main_branch = config.gitflow_branch_name()
    LOGGER.info("checking out latest {} branch...".format(main_branch))
    checkout_and_pull(opts.repo,  main_branch, not opts.no_remote)

    venv_option = ""
    if opts.virtualenv:
        venv_option = ". {}/bin/activate".format(opts.virtualenv)

    with working_dir(template_dir):
        write_basic_dockerfile(opts, config, docker_file)
        write_json_file(dotfile, {
            "post_script": "post_script.sh",
            "pre_script": "pre_script.sh",
            "inclusive": True,
            "excludes": ["post_script.sh", "post_script.sh", ".dockerstache"]
        })
        write_json_file(context, {})

        # render templates for container scripts
        template_context = {
            'open_brace': '{{',
            'close_brace': '}}',
            'package': config.package_name(),
            'virtualenv': venv_option,
            'pip_options': config.pip_options() if config.pip_options() else ""
        }

        install_template = find_template('install_script.sh.mustache')
        pre_template = find_template('pre_script.sh.mustache')
        post_template = find_template('post_script.sh.mustache')

        renderer = pystache.Renderer()
        install_result = renderer.render_path(install_template, template_context)

        with open(installer_script, 'w') as handle:
            handle.write(install_result)

        post_result = renderer.render_path(post_template, template_context)
        with open(post_script, 'w') as handle:
            handle.write(post_result)

        pre_result = renderer.render_path(pre_template, template_context)
        with open(pre_script, 'w') as handle:
            handle.write(pre_result)

        make_executable(pre_script, opts.repo)
        make_executable(post_script, opts.repo)
        make_executable(installer_script, opts.repo)

        edit_cirrus_conf(opts, config)

        modified = [
            cirrus_conf,
            docker_file,
            dotfile,
            pre_script,
            post_script,
            installer_script,
            context
        ]
        LOGGER.info("commiting changes...")
        commit_files_optional_push(
            opts.repo,
            "git cirrus package container-init",
            not opts.no_remote,
            *modified
        )