Esempio n. 1
0
def build_cmd(target, version, skip_tests):
    client = utils.get_docker_client()
    project_dir = utils.get_project_dir()
    config = load_shub_config()
    image = config.get_image(target)
    _create_setup_py_if_not_exists()
    image_name = utils.format_image_name(image, version)
    if not os.path.exists(os.path.join(project_dir, 'Dockerfile')):
        raise shub_exceptions.BadParameterException(
            "Dockerfile is not found, please use shub image 'init' command")
    is_built = False
    for data in client.build(path=project_dir, tag=image_name, decode=True):
        if 'stream' in data:
            utils.debug_log("{}".format(data['stream'][:-1]))
            is_built = re.search(r'Successfully built ([0-9a-f]+)',
                                 data['stream'])
        elif 'error' in data:
            click.echo("Error {}:\n{}".format(data['error'],
                                              data['errorDetail']))
    if not is_built:
        raise shub_exceptions.RemoteErrorException(
            "Build image operation failed")
    click.echo("The image {} build is completed.".format(image_name))
    # Test the image content after building it
    if not skip_tests:
        test_cmd(target, version)
Esempio n. 2
0
def _get_project_settings(project, endpoint, apikey):
    utils.debug_log('Getting settings for {} project:'.format(project))
    req = requests.get(urljoin(endpoint, '/api/settings/get.json'),
                       params={'project': project},
                       auth=(apikey, ''),
                       timeout=300,
                       allow_redirects=False)
    req.raise_for_status()
    utils.debug_log("Response: {}".format(req.json()))
    return {k: v for k, v in req.json().items() if k in SETTING_TYPES}
Esempio n. 3
0
def _check_image_exists(image_name, docker_client):
    """Check that the image exists on local machine."""
    # if there's no docker lib, the command will fail earlier
    # with an exception when getting a client in get_docker_client()
    from docker.errors import NotFound
    try:
        docker_client.inspect_image(image_name)
    except NotFound as exc:
        utils.debug_log("{}".format(exc))
        raise shub_exceptions.NotFoundException(
            "The image doesn't exist yet, please use build command at first.")
Esempio n. 4
0
def _get_project_settings(project, endpoint, apikey):
    utils.debug_log('Getting settings for {} project:'.format(project))
    req = requests.get(
        urljoin(endpoint, '/api/settings/get.json'),
        params={'project': project},
        auth=(apikey, ''),
        timeout=300,
        allow_redirects=False
    )
    req.raise_for_status()
    utils.debug_log("Response: {}".format(req.json()))
    return {k: v for k, v in req.json().items() if k in SETTING_TYPES}
Esempio n. 5
0
def _run_docker_command(client, image_name, command):
    """A helper to execute an arbitrary cmd with given docker image"""
    container = client.create_container(image=image_name, command=command)
    try:
        client.start(container)
        statuscode = client.wait(container=container['Id'])
        logs = client.logs(container=container['Id'], stdout=True,
                           stderr=True if statuscode else False,
                           stream=False, timestamps=False)
        utils.debug_log("{} results:\n{}".format(command, logs))
        return statuscode, logs
    finally:
        client.remove_container(container)
Esempio n. 6
0
def _check_image_size(image_name, docker_client):
    """Check that the image exists on local machine and validate its size."""
    # if there's no docker lib, the command will fail earlier
    # with an exception when getting a client in get_docker_client()
    from docker.errors import NotFound
    try:
        size = docker_client.inspect_image(image_name).get('Size')
        if size and isinstance(size, (int, long)) and size > IMAGE_SIZE_LIMIT:
            raise shub_exceptions.CustomImageTooLargeException(
                IMAGE_TOO_LARGE_WARNING)
    except NotFound as exc:
        utils.debug_log("{}".format(exc))
        raise shub_exceptions.NotFoundException(
            "The image doesn't exist yet, please use build command at first.")
Esempio n. 7
0
def _check_image_size(image_name, docker_client):
    """Check that the image exists on local machine and validate its size."""
    # if there's no docker lib, the command will fail earlier
    # with an exception when getting a client in get_docker_client()
    from docker.errors import NotFound
    try:
        size = docker_client.inspect_image(image_name).get('Size')
        if size and isinstance(size, (int, long)) and size > IMAGE_SIZE_LIMIT:
            raise shub_exceptions.CustomImageTooLargeException(
                IMAGE_TOO_LARGE_WARNING)
    except NotFound as exc:
        utils.debug_log("{}".format(exc))
        raise shub_exceptions.NotFoundException(
            "The image doesn't exist yet, please use build command at first.")
Esempio n. 8
0
def deploy_cmd(target, version, username, password, email, apikey, insecure,
               async):
    config = load_shub_config()
    target_conf = config.get_target_conf(target)
    endpoint, target_apikey = target_conf.endpoint, target_conf.apikey
    image = config.get_image(target)
    version = version or config.get_version()
    image_name = utils.format_image_name(image, version)
    username, password = utils.get_credentials(username=username,
                                               password=password,
                                               insecure=insecure,
                                               apikey=apikey,
                                               target_apikey=target_apikey)

    apikey = apikey or target_apikey
    params = _prepare_deploy_params(target_conf.project_id, version,
                                    image_name, endpoint, apikey, username,
                                    password, email)

    utils.debug_log('Deploy with params: {}'.format(params))
    req = requests.post(urljoin(endpoint, '/api/releases/deploy.json'),
                        data=params,
                        auth=(apikey, ''),
                        timeout=300,
                        allow_redirects=False)
    try:
        req.raise_for_status()
    except requests.exceptions.HTTPError:
        _handle_deploy_errors(req)

    click.echo("Deploy task results: {}".format(req))
    status_url = req.headers['location']

    status_id = utils.store_status_url(status_url,
                                       limit=STORE_N_LAST_STATUS_URLS)
    click.echo("You can check deploy results later with "
               "'shub image check --id {}'.".format(status_id))

    click.echo("Deploy results:")
    actual_state = _check_status_url(status_url)
    click.echo(" {}".format(actual_state))

    if not async:
        status = actual_state['status']
        while status in SYNC_DEPLOY_WAIT_STATUSES:
            time.sleep(SYNC_DEPLOY_REFRESH_TIMEOUT)
            actual_state = _check_status_url(status_url)
            if actual_state['status'] != status:
                click.echo(" {}".format(actual_state))
                status = actual_state['status']
Esempio n. 9
0
def _run_docker_command(client, image_name, command):
    """A helper to execute an arbitrary cmd with given docker image"""
    container = client.create_container(image=image_name, command=command)
    try:
        client.start(container)
        statuscode = client.wait(container=container['Id'])
        logs = client.logs(container=container['Id'],
                           stdout=True,
                           stderr=True if statuscode else False,
                           stream=False,
                           timestamps=False)
        utils.debug_log("{} results:\n{}".format(command, logs))
        return statuscode, logs
    finally:
        client.remove_container(container)
Esempio n. 10
0
def deploy_cmd(target, version, username, password, email,
               apikey, insecure, async_):
    config = load_shub_config()
    target_conf = config.get_target_conf(target)
    endpoint, target_apikey = target_conf.endpoint, target_conf.apikey
    image = config.get_image(target)
    version = version or config.get_version()
    image_name = utils.format_image_name(image, version)
    username, password = utils.get_credentials(
        username=username, password=password, insecure=insecure,
        apikey=apikey, target_apikey=target_apikey)

    apikey = apikey or target_apikey
    params = _prepare_deploy_params(
        target_conf.project_id, version, image_name, endpoint, apikey,
        username, password, email)

    click.echo("Deploying {}".format(image_name))
    utils.debug_log('Deploy parameters: {}'.format(params))
    req = requests.post(
        urljoin(endpoint, '/api/releases/deploy.json'),
        data=params,
        auth=(apikey, ''),
        timeout=300,
        allow_redirects=False
    )
    if req.status_code == 400:
        reason = req.json().get('non_field_errors')
        raise ShubException('\n'.join(reason) if reason else req.text)
    req.raise_for_status()
    status_url = req.headers['location']
    status_id = utils.store_status_url(
        status_url, limit=STORE_N_LAST_STATUS_URLS)
    click.echo(
        "You can check deploy results later with "
        "'shub image check --id {}'.".format(status_id))
    if async_:
        return
    if utils.is_verbose():
        deploy_progress_cls = _LoggedDeployProgress
    else:
        deploy_progress_cls = _DeployProgress
    events = _convert_status_requests_to_events(status_url)
    deploy_progress = deploy_progress_cls(events)
    deploy_progress.show()
Esempio n. 11
0
def deploy_cmd(target, version, username, password, email,
               apikey, insecure, async_):
    config = load_shub_config()
    target_conf = config.get_target_conf(target)
    endpoint, target_apikey = target_conf.endpoint, target_conf.apikey
    image = config.get_image(target)
    version = version or config.get_version()
    image_name = utils.format_image_name(image, version)
    username, password = utils.get_credentials(
        username=username, password=password, insecure=insecure,
        apikey=apikey, target_apikey=target_apikey)

    apikey = apikey or target_apikey
    params = _prepare_deploy_params(
        target_conf.project_id, version, image_name, endpoint, apikey,
        username, password, email)

    click.echo("Deploying {}".format(image_name))
    utils.debug_log('Deploy parameters: {}'.format(params))
    req = requests.post(
        urljoin(endpoint, '/api/releases/deploy.json'),
        data=params,
        auth=(apikey, ''),
        timeout=300,
        allow_redirects=False
    )
    if req.status_code == 400:
        reason = req.json().get('non_field_errors')
        raise ShubException('\n'.join(reason) if reason else req.text)
    req.raise_for_status()
    status_url = req.headers['location']
    status_id = utils.store_status_url(
        status_url, limit=STORE_N_LAST_STATUS_URLS)
    click.echo(
        "You can check deploy results later with "
        "'shub image check --id {}'.".format(status_id))
    if async_:
        return
    if utils.is_verbose():
        deploy_progress_cls = _LoggedDeployProgress
    else:
        deploy_progress_cls = _DeployProgress
    events = _convert_status_requests_to_events(status_url)
    deploy_progress = deploy_progress_cls(events)
    deploy_progress.show()
Esempio n. 12
0
 def handle_status_event(self, event):
     msg = "Logs:{} {}".format(event['status'], event.get('progress'))
     utils.debug_log(msg)
Esempio n. 13
0
File: build.py Progetto: rowhit/shub
 def handle_stream_event(self, event):
     utils.debug_log("{}".format(event['stream'][:-1]))
Esempio n. 14
0
 def handle_status_event(self, event):
     msg = "Logs:{} {}".format(event['status'], event.get('progress'))
     utils.debug_log(msg)
Esempio n. 15
0
 def handle_stream_event(self, event):
     utils.debug_log("{}".format(event['stream'].rstrip()))
Esempio n. 16
0
 def handle_stream_event(self, event):
     utils.debug_log("{}".format(event['stream'][:-1]))