Commit 1a9519c7 authored by Tulio Ruiz's avatar Tulio Ruiz
Browse files

[FIX] Login via api so the push can work

[IMP] Push coverage

[FIX] Incluse push coverage command

[FIX] Append the project name in case the branch is already in use

[FIX] Use the slug because is url friendly

[FIX] Use subdir instead of subdomain so we don't have to type credentials everytime we check the coverage

[IMP] Use https
parent 531ce2b5
Pipeline #15237 failed with stage
in 22 seconds
......@@ -3,7 +3,7 @@
import click
import logging
from deployv_addon_gitlab_tools.commands import (
check_keys, test_images, upload_image, deployv_tests
check_keys, test_images, upload_image, deployv_tests, push_coverage
)
__version__ = "0.2.12"
......@@ -30,6 +30,7 @@ for command in [
test_images.test_images,
upload_image.upload_image,
deployv_tests.deployv_tests,
push_coverage.push_coverage,
]:
gitlab_tools.add_command(command)
......
......@@ -67,6 +67,14 @@ def check_docker():
return distutils.spawn.find_executable("docker")
def is_docker_login():
""" Check if we have all we need to docker login (via cli or api), that is:
- We have docker binary available
- We have the env vars properly set (DOCKER_PASSWORD and DOCKER_USER)"""
return check_docker() and environ.get('DOCKER_PASSWORD', False) and environ.get('DOCKER_USER', False)
def docker_login():
""" Execute docker login from the actual console because whe done via the api they won't persist """
cmd = 'bash -c "echo ${DOCKER_PASSWORD} | docker login --username ${DOCKER_USER} --password-stdin"'
......@@ -79,8 +87,7 @@ def check_credentials(private_deploy_key):
add_private_key(private_deploy_key, ssh_folder)
scan_keys(ssh_folder)
if check_docker() and \
environ.get('DOCKER_PASSWORD', False) and environ.get('DOCKER_USER', False):
if is_docker_login():
docker_login()
......
# coding: utf-8
import click
import logging
from os import environ, path, walk, rename
import paramiko
from sys import exit
_logger = logging.getLogger('deployv.' + __name__)
def check_vars():
return environ.get('COVERAGE_PASS', False)
def get_values():
res = {
'username': 'coverage',
'password': environ.get('COVERAGE_PASS'),
'commit_ref': environ.get('CI_COMMIT_REF_NAME'),
'commit_slug': environ.get('CI_COMMIT_REF_SLUG'),
'project_name': environ.get('CI_PROJECT_NAME')
}
return res
def get_sftp_client(username, password):
_logger.info('Connecting to the remote host')
transport = paramiko.Transport(('coverage.vauxoo.com', 8451))
transport.connect(username=username,
password=password)
return paramiko.SFTPClient.from_transport(transport)
def push_files():
values = get_values()
local_path = path.join('.', '{}-{}'.format(
values.get('commit_slug'),
values.get('project_name')
))
remote_path = '.'
_logger.info('Pushing files from: %s', local_path)
sftp = get_sftp_client(values.get('username'), values.get('password'))
rename(values.get('commit_ref'), local_path)
for root, dirs, files in walk(local_path):
_logger.debug('Root: %s .. Dirs: %s .. Files: %s', root, dirs, files)
remote = path.join(remote_path, root)
_logger.debug('Creating remote folder: %s', remote)
try:
sftp.mkdir(remote)
except Exception as error:
_logger.warning(str(error))
pass
for f in files:
_logger.debug('Uploading file: %s', f)
sftp.put(path.join(root, f), path.join(remote_path, root, f))
_logger.info('Pushed files')
def show_url():
values = get_values()
print("Coverage report can be found in:")
print("https://coverage.vauxoo.com/{}-{}".format(values.get('commit_slug'), values.get('project_name')))
def push_coverage_result():
if not check_vars():
_logger.error('Variable COVERAGE_PASS is not defined, required to push the files')
exit(1)
push_files()
show_url()
exit(0)
@click.command()
def push_coverage():
push_coverage_result()
# coding: utf-8
from deployv_addon_gitlab_tools.common import check_env_vars
from deployv_addon_gitlab_tools.commands.check_keys import check_credentials
from deployv_addon_gitlab_tools.commands.check_keys import check_credentials, is_docker_login
from deployv.helpers import utils
from docker import errors, APIClient as Client
from os import environ, unsetenv
......@@ -259,6 +259,8 @@ def is_running():
def push_image(image_name, image_tag):
logger.info('Pushing image %s to %s:%s', image_name, environ['_IMAGE_REPO'], image_tag)
_cli.tag(image_name, environ['_IMAGE_REPO'], tag=image_tag)
if is_docker_login():
_cli.login(environ['DOCKER_USER'], environ['DOCKER_PASSWORD'], registry='quay.io')
for attempt in range(4):
try:
for result in _cli.push(environ['_IMAGE_REPO'], tag=image_tag, stream=True):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment