shipyard/shipyard_airflow/plugins/deckhand_base_operator.py
Anthony Lin 47cd7a25f4 Align Operators with UcpBaseOperator
All UCP Operators will inherit from the UcpBaseOperator [0]

This patch set will align the rest of the Operators, i.e. Armada,
Deckhand and Promenade Operators with the UcpBaseOperator

It also updates the name of the shipyard container to be
'shipyard-api' instead of 'shipyard'

[0] https://review.gerrithub.io/#/c/407736/

Change-Id: I516590c492e9bb5554161119dade278d74197374
2018-04-19 16:32:51 +00:00

140 lines
5.1 KiB
Python

# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# 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.
import configparser
import logging
from airflow.utils.decorators import apply_defaults
from airflow.plugins_manager import AirflowPlugin
from airflow.exceptions import AirflowException
from deckhand.client import client as deckhand_client
from service_endpoint import ucp_service_endpoint
from service_token import shipyard_service_token
from ucp_base_operator import UcpBaseOperator
LOG = logging.getLogger(__name__)
class DeckhandBaseOperator(UcpBaseOperator):
"""Deckhand Base Operator
All deckhand related workflow operators will use the deckhand
base operator as the parent and inherit attributes and methods
from this class
"""
@apply_defaults
def __init__(self,
committed_ver=None,
deckhandclient=None,
deckhand_client_read_timeout=None,
deckhand_svc_endpoint=None,
deckhand_svc_type='deckhand',
revision_id=None,
svc_session=None,
svc_token=None,
validation_read_timeout=None,
*args, **kwargs):
"""Initialization of DeckhandBaseOperator object.
:param committed_ver: Last committed version
:param deckhandclient: An instance of deckhand client
:param deckhand_client_read_timeout: Deckhand client connect timeout
:param deckhand_svc_endpoint: Deckhand Service Endpoint
:param deckhand_svc_type: Deckhand Service Type
:param revision_id: Target revision for workflow
:param svc_session: Keystone Session
:param svc_token: Keystone Token
:param validation_read_timeout: Deckhand validation timeout
"""
super(DeckhandBaseOperator,
self).__init__(
pod_selector_pattern=[{'pod_pattern': 'deckhand-api',
'container': 'deckhand-api'}],
*args, **kwargs)
self.committed_ver = committed_ver
self.deckhandclient = deckhandclient
self.deckhand_client_read_timeout = deckhand_client_read_timeout
self.deckhand_svc_endpoint = deckhand_svc_endpoint
self.deckhand_svc_type = deckhand_svc_type
self.revision_id = revision_id
self.svc_session = svc_session
self.svc_token = svc_token
self.validation_read_timeout = validation_read_timeout
@shipyard_service_token
def run_base(self, context):
# Read and parse shiyard.conf
config = configparser.ConfigParser()
config.read(self.shipyard_conf)
# Initialize variables
self.deckhand_client_read_timeout = int(config.get(
'requests_config', 'deckhand_client_read_timeout'))
self.validation_read_timeout = int(config.get(
'requests_config', 'validation_read_timeout'))
# Logs uuid of Shipyard action
LOG.info("Executing Shipyard Action %s",
self.action_info['id'])
# Retrieve Endpoint Information
self.deckhand_svc_endpoint = ucp_service_endpoint(
self, svc_type=self.deckhand_svc_type)
LOG.info("Deckhand endpoint is %s",
self.deckhand_svc_endpoint)
# Set up DeckHand Client
LOG.info("Setting up DeckHand Client...")
# NOTE: The communication between the Airflow workers
# and Deckhand happens via the 'internal' endpoint.
self.deckhandclient = deckhand_client.Client(
session=self.svc_session, endpoint_type='internal')
if not self.deckhandclient:
raise AirflowException('Failed to set up deckhand client!')
# Retrieve 'revision_id' from xcom for tasks other than
# 'deckhand_get_design_version'
#
# NOTE: In the case of 'deploy_site', the dag_id will
# be 'deploy_site.get_design_version' for the
# 'deckhand_get_design_version' task. We need to extract
# the xcom value from it in order to get the value of the
# last committed revision ID
if self.task_id != 'deckhand_get_design_version':
# Retrieve 'revision_id' from xcom
self.revision_id = self.xcom_puller.get_design_version()
if self.revision_id:
LOG.info("Revision ID is %d", self.revision_id)
else:
raise AirflowException('Failed to retrieve Revision ID!')
class DeckhandBaseOperatorPlugin(AirflowPlugin):
"""Creates DeckhandBaseOperator in Airflow."""
name = 'deckhand_base_operator_plugin'