charm-keystone/scripts/fernet_rotate_and_sync.py
Alex Kavanagh 59561fdda0 Convert the charm to Python 3 only
Major changes:

 * decoupling the hooks/manager.py file from the charm.  It is now a
   script that is called from hooks/keystone_utils.py as it has to use
   the same Python version/libraries as the installed keystone payload
   software.  keystone_utils.py and manager.py communicate via a Unix
   Domain Socket using json, encoded to base64.
 * As Python3 requires absolute imports, the charmhelpers symlink has
   been removed from hooks, and the hooks and charmhelpers symlinks have
   been removed from the actions directory.  Instead, the path is
   adjusted so that the modules can be found.

Change-Id: I18996e15d2d08b1dacf0533132eae880cbb9aa32
2018-09-21 09:09:47 +00:00

52 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2018 Canonical Ltd.
#
# 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 __future__ import print_function
import os
import sys
import time
dir_path = os.path.dirname(os.path.realpath(__file__))
hooks_path = os.path.abspath(os.path.join(dir_path, "..", "hooks"))
root_path = os.path.abspath(os.path.join(dir_path, ".."))
for p in [hooks_path, root_path]:
if p not in sys.path:
sys.path.append(p)
# now we can import charm related items
import charmhelpers.core.hookenv
import keystone_utils
def cli_log(msg, level=charmhelpers.core.hookenv.INFO):
"""Helper function to write log message to stdout/stderr for CLI usage."""
if level == charmhelpers.core.hookenv.DEBUG:
return charmhelpers.core.hookenv.log(msg, level=level)
elif level in [charmhelpers.core.hookenv.ERROR,
charmhelpers.core.hookenv.WARNING]:
output = sys.stderr
else:
output = sys.stdout
print('{}: {}'.format(time.ctime(), msg), file=output)
# the rotate_and_sync_keys() function checks for leadership AND whether to
# rotate the keys or not.
if __name__ == "__main__":
keystone_utils.fernet_keys_rotate_and_sync(log_func=cli_log)