Ejemplo n.º 1
0
def _get_git_root():
    """
    Retrieve the git directory, or prompt to create one if not found
    """
    git_root = None

    try:
        git_root = str(git('rev-parse', '--show-toplevel')).strip()
    except ErrorReturnCode as e:
        if e.exit_code != 128:
            fatal(e.message, e.exit_code)

    if not git_root:
        warning('You must be in a git repository directory to '
                'initialize a new project.')

        if not click.confirm('Do you want to create a new git '
                             'repository here?', default=True):
            fatal('Please run %s' % style('git init', bold=True))

        try:
            vgit('init')
            git_root = os.getcwd()
        except ErrorReturnCode as e:
            fatal('An error occurred when trying to initialize a '
                  'new repo.', e.exit_code)

    if git_root == aeriscloud_path:
        fatal('You cannot init AerisCloud from the AerisCloud directory!')

    return git_root
Ejemplo n.º 2
0
def update_inventory(name, dest_path):
    if not os.path.exists(os.path.join(dest_path, '.git')):
        warning("The %s inventory is not a git repository and has not "
                "been updated." % name)
        return

    click.echo('We will update the %s inventory' % name)
    os.chdir(dest_path)
    try:
        vgit('pull')
    except ErrorReturnCode:
        fatal("Unable to update the inventory %s." % name)
    success("The %s inventory has been updated." % name)
Ejemplo n.º 3
0
def _ask_general_info(git_root):
    _title('General')

    # Take the basename of the git root as default name for the application
    default_name = os.path.basename(git_root) \
        .lower()

    app_name = None
    app_id = None

    click.secho('\nSelect your %s (only lowercase letters, numbers and '
                'dash are accepted).' % style('application name', bold=True))
    while True:
        app_name = get_input('[default: %s] > ' % default_name)
        if not app_name:
            app_name = default_name
        if re.match("^[a-z0-9-]+$", app_name):
            break
        else:
            click.echo('Only lowercase letters and numbers are accepted.')

    organization_list = AerisCompletableList(get_organization_list())

    click.secho('\nWhich organization contains the playbook you want to '
                'use to provision the boxes of this project?')
    organization = AerisPrompt('> ', completer=organization_list).get_input()

    if not organization:
        organization = config.get('config', 'default_organization',
                                  default=None)

    click.echo('\nProvide your %s.\n'
               'If you don\'t have one, ask your system operators!' %
               style('application ID', bold=True))
    while True:
        app_id = get_input('> ')
        if len(app_id) > 0:
            try:
                app_id = int(app_id)
                if app_id > 0:
                    break
                warning('Please enter a valid ID.')
            except ValueError:
                warning('Please enter a valid ID.')

    return app_name, organization, app_id
Ejemplo n.º 4
0
def cli(up, make_cmd, box_name, project_name):
    """
    Allows you to fetch a project from GitHub
    """
    _check_github_config()

    if not project_name:
        project = from_cwd()
        if not project:
            fatal("""You are not in a project directory.
You need to specify what project you want to fetch.""")
        project_name = project.name()
    else:
        project = get(project_name)

    if not project:
        _clone_project(project_name)
    else:
        _update_project(project)

    # reload project if created
    project = get(project_name)

    # move the user in the project directory
    move_shell_to(project.folder())

    if not project.initialized():
        warning('warning: this is not an aeriscloud project, aborting...')
        sys.exit(1)

    # make_cmd implies up
    if make_cmd:
        up = True

    box = project.box(box_name)
    if up:
        info('AerisCloud will now start your box and provision it, '
             'this op might take a while.')
        start_box(box)

        if make_cmd:
            print(make_cmd)
            box.ssh_shell('make %s' % make_cmd)
Ejemplo n.º 5
0
def install(name, path):
    """
    Install inventories.
    """
    if not name:
        update()
        return

    if not name.isalnum():
        fatal("Your inventory name should only contains alphanumeric "
              "characters.")

    dest_path = os.path.join(inventory_path, name)
    if os.path.exists(dest_path):
        if not os.path.exists(os.path.join(dest_path, '.git')):
            warning("The %s inventory is not a git repository and has not "
                    "been updated." % name)
            return

        click.echo('We will update the %s inventory' % name)
        os.chdir(dest_path)
        try:
            vgit('pull')
        except ErrorReturnCode:
            fatal("Unable to update the inventory %s." % name)
        success("The %s inventory has been updated." % name)
        return

    if not path:
        fatal("You must specify a path to a local directory or an URL to a "
              "git repository to install a new inventory.")

    if os.path.exists(path) and os.path.isdir(path):
        os.symlink(path, dest_path)
    else:
        click.echo('We will clone %s in %s\n' % (path, dest_path))
        try:
            vgit('clone', path, dest_path)
        except ErrorReturnCode:
            fatal("Unable to install the inventory %s." % name)
    success("The %s inventory has been installed." % name)
Ejemplo n.º 6
0
def cli(up, make, box_name, folder):
    """
    Initialize a new AerisCloud project
    """
    if not folder:
        folderpath = os.getcwd()
    else:
        folderpath = os.path.join(os.curdir, folder)
        relpath = os.path.relpath(folderpath, projects_path())
        if relpath[:2] == '..':
            warning("""You are trying to create a new project in %s
which is outside of your projects' directory (%s).""" %
                    (os.path.abspath(folderpath), projects_path()))
            folder_in_projectpath = os.path.join(projects_path(), folder)
            if click.confirm("Do you want to create it in %s instead?" %
                             folder_in_projectpath, default=True):
                folderpath = folder_in_projectpath

    if not os.path.exists(folderpath):
        os.makedirs(folderpath)

    os.chdir(folderpath)

    git_root = _get_git_root()
    project = Project(git_root)

    _ask_project_details(git_root, project)

    click.echo('\nWriting .aeriscloud.yml ... ', nl=False)
    project.save()
    click.echo('done')

    move_shell_to(project.folder())

    if up or make:
        # Retrieve the proper box
        box = project.box(box_name)

        _up(box, make)
Ejemplo n.º 7
0
def init(name, repository):
    """
    Initialize a new organization.

    The following usages are supported:

        cloud organization init <name> <git repository url>

    \b
It will create a new AerisCloud organization and set the origin remote to
the specified url.

    \b
If the GitHub integration is enabled, you can also use the following
commands:

        cloud organization init <github organization name>

    \b
It will create a new AerisCloud organization and set the origin remote to
[email protected]/<organization>/aeriscloud-organization.git

        cloud organization init <github organization name>/<project name>

    \b
It will create a new AerisCloud organization and set the origin remote to
[email protected]/<organization>/<project>-aeriscloud-organization.git

        cloud organization init <org name>/<customer>/<project name>

    \b
It will create a new AerisCloud organization and set the origin remote to
[email protected]/<organization>/<customer>-<project>-aeriscloud-organization.git
"""
    dirname = '-'.join(name.split('/'))

    dest_path = get_env_path(dirname)
    if os.path.exists(dest_path):
        fatal("The organization %s already exists." % dirname)

    # If remote is not specified
    if not repository:
        # If GH integration is enabled
        if has_github_integration():
            gh = Github()

            if '/' in name:
                split = name.split('/')
                name = split[0]
                repo_name = '-'.join(split[1:]) + "-aeriscloud-organization"
            else:
                repo_name = "aeriscloud-organization"

            # If member of the organization
            orgs = [org for org in gh.get_organizations()
                    if org.login.lower() == name.lower()]
            if orgs:
                # If repo exists
                repos = [repo.name for repo in orgs[0].iter_repos()
                         if repo.name.lower() == repo_name.lower()]
                if not repos:
                    # Give instructions to create the repo
                    info("""The repository {repo} has not been found in the {org} organization.
You can create a new repo at the following address: https://github.com/new."""
                         .format(repo=repo_name, org=name))
                    if not click.confirm("Do you want to continue?",
                                         default=False):
                        info("Aborted. Nothing has been done.")
                        return
            # If not member of the organization
            else:
                warning("We were not able to verify the existence of the "
                        "repository as you don't belong to the {org} "
                        "organization.".format(org=name))
                if not click.confirm("Do you want to continue?",
                                     default=False):
                    info("Aborted. Nothing has been done.")
                    return

            repository = "[email protected]:{org}/{repo}.git".format(
                org=name,
                repo=repo_name
            )
        else:
            fatal("You need to specify a repository URL or enable the GitHub "
                  "integration in AerisCloud.")

    archive_url = "https://github.com/AerisCloud/sample-organization/" \
                  "archive/master.tar.gz"

    os.makedirs(dest_path)
    os.chdir(dest_path)
    curl(archive_url,
         o='%s/master.tar.gz' % dest_path,
         silent=True,
         location=True)

    tar = tarfile.open("%s/master.tar.gz" % dest_path, 'r:gz')
    members = [m for m in tar.getmembers() if '/' in m.name]
    for m in members:
        m.name = m.name[m.name.find('/') + 1:]
    tar.extractall(path=dest_path, members=members)
    tar.close()

    os.unlink("%s/master.tar.gz" % dest_path)

    vgit("init")

    move_shell_to(dest_path)
    info("You have been moved to the organization folder.")
    success("The %s organization has been created." % dirname)

    run_galaxy_install(dirname)

    vgit("remote", "add", "origin", repository)

    info("""You can now manage your organization like a standard git repository.
Edit your files, do some commits and push them!""")