forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-71] Add support for private Docker images
Pulling images from private Docker registries requires authentication, so additional parameters are added in order to perform the login step. (cherry picked from commit f101ff0) Signed-off-by: Bolke de Bruin <[email protected]>
- Loading branch information
1 parent
d2f9d18
commit d4406c0
Showing
8 changed files
with
389 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from docker import Client | ||
from docker.errors import APIError | ||
|
||
from airflow.exceptions import AirflowException | ||
from airflow.hooks.base_hook import BaseHook | ||
from airflow.utils.log.logging_mixin import LoggingMixin | ||
|
||
class DockerHook(BaseHook, LoggingMixin): | ||
""" | ||
Interact with a private Docker registry. | ||
:param docker_conn_id: ID of the Airflow connection where | ||
credentials and extra configuration are stored | ||
:type docker_conn_id: str | ||
""" | ||
def __init__(self, | ||
docker_conn_id='docker_default', | ||
base_url=None, | ||
version=None, | ||
tls=None | ||
): | ||
if not base_url: | ||
raise AirflowException('No Docker base URL provided') | ||
if not version: | ||
raise AirflowException('No Docker API version provided') | ||
|
||
conn = self.get_connection(docker_conn_id) | ||
if not conn.host: | ||
raise AirflowException('No Docker registry URL provided') | ||
if not conn.login: | ||
raise AirflowException('No username provided') | ||
extra_options = conn.extra_dejson | ||
|
||
self.__base_url = base_url | ||
self.__version = version | ||
self.__tls = tls | ||
self.__registry = conn.host | ||
self.__username = conn.login | ||
self.__password = conn.password | ||
self.__email = extra_options.get('email') | ||
self.__reauth = False if extra_options.get('reauth') == 'no' else True | ||
|
||
def get_conn(self): | ||
client = Client( | ||
base_url=self.__base_url, | ||
version=self.__version, | ||
tls=self.__tls | ||
) | ||
self.__login(client) | ||
return client | ||
|
||
def __login(self, client): | ||
self.log.debug('Logging into Docker registry') | ||
try: | ||
client.login( | ||
username=self.__username, | ||
password=self.__password, | ||
registry=self.__registry, | ||
email=self.__email, | ||
reauth=self.__reauth | ||
) | ||
self.log.debug('Login successful') | ||
except APIError as docker_error: | ||
self.log.error('Docker registry login failed: %s', str(docker_error)) | ||
raise AirflowException('Docker registry login failed: %s', str(docker_error)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.