Files
distcloud/distributedcloud/dccommon/drivers/openstack/patching_v1.py
Tao Liu 7f3827f24d Keystone token and resource caching
Add the following misc. changes to dcorch and dcmanager components:
- Cache the master resource in dcorch audit
- Consolidate the openstack drivers to common module, combine the
  dcmanager and dcorch sysinv client. (Note: the sdk driver that
  used by nova, neutron and cinder will be cleaned as part of
  story 2006588).
- Update the common sdk driver:
  . in order to avoid creating new keystone client multiple times
  . to add a option for caching region clients, in addition to the
    keystone client
  . finally, to randomize the token early renewal duration
- Change subcloud audit manager, patch audit manager,
  and sw update manager to:
  utilize the sdk driver which caches the keystone client and token

Test cases:
1. Manage/unmanage subclouds
2. Platform resources sync and audit
3. Verify the keystone token is cached until the token is
   expired
4. Add/delete subclouds
5. Managed subcloud goes offline/online (power off/on)
6. Managed subcloud goes offline/online (delete/add a static route)
7. Apply a patch to all subclouds via patch Orchestration

Story: 2007267
Task: 38865

Change-Id: I75e0cf66a797a65faf75e7c64dafb07f54c2df06
Signed-off-by: Tao Liu <tao.liu@windriver.com>
2020-03-23 21:31:04 -04:00

197 lines
7.1 KiB
Python

# Copyright 2016 Ericsson AB
# 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.
#
# Copyright (c) 2017-2020 Wind River Systems, Inc.
#
# The right to copy, distribute, modify, or otherwise make use
# of this software may be licensed only pursuant to the terms
# of an applicable Wind River license agreement.
#
from oslo_log import log
import requests
from requests_toolbelt import MultipartEncoder
from dccommon.drivers import base
LOG = log.getLogger(__name__)
# Patch states
PATCH_STATE_AVAILABLE = 'Available'
PATCH_STATE_APPLIED = 'Applied'
PATCH_STATE_PARTIAL_APPLY = 'Partial-Apply'
PATCH_STATE_PARTIAL_REMOVE = 'Partial-Remove'
PATCH_STATE_COMMITTED = 'Committed'
PATCH_STATE_UNKNOWN = 'n/a'
class PatchingClient(base.DriverBase):
"""Patching V1 driver."""
def __init__(self, region, session):
# Get an endpoint and token.
self.endpoint = session.get_endpoint(service_type='patching',
region_name=region,
interface='internal')
self.token = session.get_token()
def query(self, state=None, release=None,):
"""Query patches"""
url = self.endpoint + '/v1/query'
if state is not None:
url += "?show=%s" % state.lower()
if release is not None:
url += "&release=%s" % release
headers = {"X-Auth-Token": self.token}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "query failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "query failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def query_hosts(self):
"""Query hosts"""
url = self.endpoint + '/v1/query_hosts'
headers = {"X-Auth-Token": self.token}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "query_hosts failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('data', [])
else:
message = "query_hosts failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def apply(self, patches):
"""Apply patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/apply/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "apply failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "apply failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def remove(self, patches):
"""Remove patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/remove/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "remove failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "remove failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def delete(self, patches):
"""Delete patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/delete/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "delete failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "delete failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def commit(self, patches):
"""Commit patches"""
patch_str = "/".join(patches)
url = self.endpoint + '/v1/commit/%s' % patch_str
headers = {"X-Auth-Token": self.token}
response = requests.post(url, headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "commit failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "commit failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)
def upload(self, files):
"""Upload patches"""
for file in sorted(list(set(files))):
enc = MultipartEncoder(fields={'file': (file,
open(file, 'rb'),
)})
url = self.endpoint + '/v1/upload'
headers = {"X-Auth-Token": self.token,
'Content-Type': enc.content_type}
response = requests.post(url,
data=enc,
headers=headers)
if response.status_code == 200:
data = response.json()
if 'error' in data and data["error"] != "":
message = "upload failed with error: %s" % data["error"]
LOG.error(message)
raise Exception(message)
else:
return data.get('pd', [])
else:
message = "upload failed with RC: %d" % response.status_code
LOG.error(message)
raise Exception(message)