shipyard/shipyard_airflow/plugins/openstack_operators.py
Anthony Lin b66d39e468 Update OpenStack Operator
The existing OpenStack operator will only work for
Python 2.7 Airflow Containers and will fail to function
properly for Airflow Containers running on Python 3.x

This update will fix the problems with the environment
variables.

The current assumption is that the shipyard.conf will
contain the required information for the OpenStack
environment and it will be consumed by Airflow Dag to
execute the OpenStack commands.  This will likely change
in future when DeckHand is ready for integration as the
required information will be retrieved from DeckHand instead

The xcom portion has been removed as it does not really
serve any purpose at the moment.  It might get added back
in future depending on the needs.

Change-Id: I0c5fb56462fdbc43a20897373118f510cbb2e01c
2017-08-18 04:09:39 +00:00

85 lines
2.8 KiB
Python

# -*- 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.
import logging
import subprocess
import os
import configparser
from airflow.exceptions import AirflowException
from airflow.models import BaseOperator
from airflow.plugins_manager import AirflowPlugin
from airflow.utils.decorators import apply_defaults
class OpenStackOperator(BaseOperator):
"""
Performs OpenStack CLI calls
:shipyard_conf: Location of shipyard.conf
:openstack_command: The OpenStack command to be executed
"""
@apply_defaults
def __init__(self,
shipyard_conf,
openstack_command=None,
xcom_push=False,
*args, **kwargs):
super(OpenStackOperator, self).__init__(*args, **kwargs)
self.shipyard_conf = shipyard_conf
self.openstack_command = openstack_command
self.xcom_push_flag = xcom_push
def execute(self, context):
logging.info("Running OpenStack Command: %s", self.openstack_command)
# Read and parse shiyard.conf
config = configparser.ConfigParser()
config.read(self.shipyard_conf)
# Construct Envrionment variables
for attr in ('OS_AUTH_URL', 'OS_PROJECT_ID', 'OS_PROJECT_NAME',
'OS_USER_DOMAIN_NAME', 'OS_USERNAME', 'OS_PASSWORD',
'OS_REGION_NAME', 'OS_IDENTITY_API_VERSION'):
os.environ[attr] = config.get('keystone', attr)
# Execute the OpenStack CLI Command
openstack_cli = subprocess.Popen(
self.openstack_command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
# Logs Output
logging.info("Output:")
line = ''
for line in iter(openstack_cli.stdout.readline, b''):
line = line.strip()
logging.info(line)
# Wait for child process to terminate
# Set and return returncode attribute.
openstack_cli.wait()
logging.info("Command exited with "
"return code {0}".format(openstack_cli.returncode))
# Raise Execptions if OpenStack Command Fails
if openstack_cli.returncode:
raise AirflowException("OpenStack Command Failed")
class OpenStackCliPlugin(AirflowPlugin):
name = "openstack_cli_plugin"
operators = [OpenStackOperator]