示例#1
0
def delete(name):
    """
    Given a name, try to delete an organization.
    :param name: The organization to delete.
    :type name: str
    :returns: The status of the deletion.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'organizations/{0}'.format(name)
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.delete(url, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        if result.status_code == 403:
            result = {name: 'cannot be deleted'}
        elif result.status_code == 404:
            try:
                result = result.json()
            except:
                result = {name: 'not found'}
        else:
            result = result.json()
        LOGGER.info(error)
        return False, result
    return True, {}
示例#2
0
def find(limit, offset, organization):
    """
    List the builds.
    :param limit: The number of builds per request.
    :type limit: int
    :param offset: The offset to start to retrieve the builds from.
    :type offset: int
    :param organization: The build's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'builds/{}/'.format(organization)
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.get(url,
                          params={
                              'limit': limit,
                              'offset': offset
                          },
                          headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    return True, result.json()
示例#3
0
def find(offset, limit, organization):
    """
    List all the builds.
    :param limit: The number of builds per request.
    :type limit: int
    :param offset: The offset to start to retrieve the builds from.
    :type offset: int
    :param organization: The build's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to get all the builds
    status, infos = api.find(limit, offset, organization)
    if status:
        click.echo(
            '\n{0} : starting {1}, listing {2} builds on a total of {3} builds.\n'
            .format(organization, offset, min(limit, infos['count']),
                    infos['count']))
        click.echo('Project\tBranch\tUid\tStep\tFail\tCreation Date\n')
        for result in infos['results']:
            result = json.loads(result)
            click.echo('{0}\t{1}\t{2}\t{3}\t{4}\t{5}'.format(
                result['repo'], result['branch'], result['uid'],
                result['step'], result['fail'], result['date_created']))
            click.echo('')
        return True
    return display_errors(infos)
示例#4
0
def delete(name, organization):
    """
    Given a name, try to delete an app.
    :param name: The app to delete.
    :type name: str
    :param organization: The app's organization.
    :type organization: str
    :returns: The status of the deletion.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'projects/{0}/{1}/'.format(organization, name)
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.delete(url, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    #4. Cleanup the remote
    try:
        repo = git.Repo(os.getcwd())
    except git.exc.InvalidGitRepositoryError:
        pass
    else:
        url = '{0}.git'.format(name)
        if any(r for r in repo.remotes
               if r.name == 'baguette.io' and r.url.endswith(url)):
            repo.delete_remote('baguette.io')
    return True, {}
示例#5
0
def find(offset, limit, organization):
    """
    List all the deployments.
    :param limit: The number of deployments per request.
    :type limit: int
    :param offset: The offset to start to retrieve the deployments from.
    :type offset: int
    :param organization: The deployment's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to get all the deployments
    status, infos = api.find(limit, offset, organization)
    if status:
        click.echo(
            '\n{0} : starting {1}, listing {2} deployments on a total of {3} deployments.\n'
            .format(organization, offset, min(limit, infos['count']),
                    infos['count']))
        click.echo('Name\tUid\tStatus\tCreation Date\n')
        for result in infos['results']:
            result = json.loads(result)
            click.echo('{0}\t{1}\t{2}\t{3}'.format(result['name'],
                                                   result['uid'],
                                                   result['status'],
                                                   result['date_created']))
            click.echo('')
        return True
    return display_errors(infos)
示例#6
0
def create(name, organization):
    """
    Given a name, try to create an app.
    Idempotent.
    :param name: The app to create.
    :type name: str
    :param organization: The app's organization.
    :type organization: str
    :returns: The status of the creation.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'projects/{0}/'.format(organization)
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.post(url, json={'name': name}, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    return True, result.json()
示例#7
0
def create(name, organization):
    """
    Create an app of the current git repo.
    :param name: The app name. Optional.
    :type name: None, str
    :param organization: The namespace's organization.
    :type organization: str
    :returns: The status of the creation.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Check that we are currently in a git repo.
    try:
        git.Repo(os.getcwd())
    except git.exc.InvalidGitRepositoryError:
        click.echo('The current path is not the root of the repository.')
        return False
    #2. Check that the current git repo is not already in baguette.io
    if api.already_in_baguette():
        click.echo(
            'The current repository has already a baguette.io \'s remote.')
        return False
    #3. Generate the name if not set
    name = name or os.path.basename(os.getcwd())
    #4. Call the API to create the app
    created, infos = api.create(name, organization)
    if created:
        api.git_init(infos['uri'])
        click.echo("""{0}: {1} created.
baguette.io` remote added.
When pushing to this remote, your app will be automatically deployed.""".
                   format(organization, name))
        return True
    return display_errors(infos)
示例#8
0
def detail(uid, organization):
    """
    Stop a deployment.
    :param uid: The deployment's uid
    :type uid: str
    :param organization: The deployment's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to get the deployment detail
    status, infos = api.stop(uid, organization)
    if status:
        click.echo('{} is stopping'.format(uid))
        return True
    return display_errors(infos)
示例#9
0
def delete(name, organization):
    """
    Delete an app.
    :param name: The app name to delete.
    :type name: None, str
    :param organization: The namespace's organization.
    :type organization: str
    :returns: The status of the deletion.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to delete the key
    deleted, infos = api.delete(name, organization)
    if deleted:
        click.echo('{0} : app {1} deleted.'.format(organization, name))
        return True
    return display_errors(infos)
示例#10
0
def create(name, organization):
    """
    Create a namespace.
    Idempotent.
    :param name: The namespace name.
    :type name: str
    :param organization: The namespace's organization.
    :type organization: str
    :returns: The status of the creation.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to create the namespace
    created, infos = api.create(name, organization)
    if created:
        click.echo('{0} : namespace {1} created.'.format(organization, name))
        return True
    return display_errors(infos)
示例#11
0
def quotas():
    """
    List all the account quotas.
    :returns: The status of the request.
    :rtype: bool
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'quotas/'
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.get(url, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    return True, result.json()
示例#12
0
def create(name, key):
    """
    Given a name/public key, try to create a key.
    :param name: The key to create.
    :type name: str
    :returns: The status of the creation.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'keys/'
    url = baguette.settings.default['api'] + endpoint# pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.post(url, data={'name':name, 'public': key}, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    return True, result.json()
示例#13
0
def detail(uid, organization):
    """
    Detail a deployment.
    :param uid: The deployment's uid
    :type uid: str
    :param organization: The deployment's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: bool
    """
    user = utils.get('user')
    organization = organization or '{}-default'.format(user)
    #1. Call the API to get the deployment detail
    status, infos = api.detail(uid, organization)
    if status:
        click.echo('Project\tBranch\tStatus\tCreation Date\n')
        for result in infos['results']:
            result = json.loads(result)
            click.echo('{0}\t{1}\t{2}'.format(result['name'], result['status'],
                                              result['date_created']))
            click.echo('')
        return True
    return display_errors(infos)
示例#14
0
def detail(uid, organization):
    """
    Detail a build.
    :param build: The build's uid.
    :type build: str
    :param organization: The build's organization.
    :type organization: str
    :returns: The status of the request.
    :rtype: list (<bool>, <dict>)
    """
    #1. Check that we have a token.
    token = utils.get('token')
    #2. Variables for the request.
    endpoint = 'builds/{}/{}/'.format(organization, uid)
    url = baguette.settings.default['api'] + endpoint  # pylint:disable=no-member
    headers = {'Authorization': 'JWT {0}'.format(token)}
    #3. Query.
    result = requests.get(url, headers=headers)
    try:
        result.raise_for_status()
    except requests.exceptions.HTTPError as error:
        LOGGER.info(error)
        return False, result.json()
    return True, result.json()