Repository URL to install this package:
Version:
6.0.19 ▾
|
# Copyright 2015 TrilioData Inc.
# All Rights Reserved.
import os
import json
import errno
import socket
import fcntl
import struct
import subprocess
import errno
import re
from time import sleep
from multiprocessing import Process
from oslo_concurrency import processutils
import contextlib
import tempfile
import shutil
try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
from oslo_utils import strutils
try:
from oslo_log import log as logging
except ImportError:
from .nova.openstack.common import log as logging
import contego.conf
LOG = logging.getLogger(__name__)
CONF = contego.conf.CONF
URL_PREFIX = '//'
RWRWRW = 666
_SANITIZE_KEYS = ["data", "secret", "-password"]
def sanitize_message(message, secret="***"):
if not set(strutils._SANITIZE_KEYS).intersection(set(_SANITIZE_KEYS)) == set(_SANITIZE_KEYS):
strutils._SANITIZE_KEYS.extend(_SANITIZE_KEYS)
_FORMAT_PATTERNS_3 = [r'(%(key)s[0-9]*\s*[ ]\s*)[^\s^\'^\"]+']
for key in _SANITIZE_KEYS:
strutils._SANITIZE_PATTERNS_1[key] = []
strutils._SANITIZE_PATTERNS_2[key] = []
if hasattr(strutils, '_SANITIZE_PATTERNS_WILDCARD'):
strutils._SANITIZE_PATTERNS_WILDCARD[key] = []
for pattern in strutils._FORMAT_PATTERNS_WILDCARD:
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL | re.IGNORECASE)
strutils._SANITIZE_PATTERNS_WILDCARD[key].append(reg_ex)
for pattern in strutils._FORMAT_PATTERNS_2:
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL | re.IGNORECASE)
strutils._SANITIZE_PATTERNS_2[key].append(reg_ex)
for pattern in strutils._FORMAT_PATTERNS_1:
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL | re.IGNORECASE)
strutils._SANITIZE_PATTERNS_1[key].append(reg_ex)
for pattern in _FORMAT_PATTERNS_3:
reg_ex = re.compile(pattern % {'key': key}, re.DOTALL | re.IGNORECASE)
strutils._SANITIZE_PATTERNS_1[key].append(reg_ex)
if isinstance(message, list) or isinstance(message, tuple):
message = ' '.join(message)
if isinstance(message, str):
message = strutils.mask_password(message)
if isinstance(message, dict):
message = strutils.mask_dict_password(message)
return message
def run_process(method, progress_tracking_file_path, *args):
try:
process = Process(target=method, args=args)
process.start()
if process.is_alive():
sleep(10)
# update the timestamp so the workloadmgr knows contego
# is alive and kicking
os.utime(progress_tracking_file_path, None)
except Exception as ex:
raise ex
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(
fcntl.ioctl(
s.fileno(), 0x8915, struct.pack("256s", ifname[:15]) # SIOCGIFADDR
)[20:24]
)
def get_lan_ip(config_file):
def get_config_value(cfg, Section, Option, Key=None):
if cfg.has_option(Section, Option):
return cfg.get(Section, Option, Key)
else:
return None
cfg = ConfigParser.ConfigParser()
cfg.read(config_file)
ip = get_config_value(cfg, "DEFAULT", "my_ip")
if ip is not None:
return ip
return socket.gethostname()
def ensure_tree(path):
"""Create a directory (and any ancestor directories required)
:param path: Directory to create
"""
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST:
if not os.path.isdir(path):
raise
else:
raise
def touch_file(file_path):
""" Utility to simulate the filesystem "touch" command.
Args:
file_path (str): Path to file to be touched.
"""
# In order to support both local filesystem and Fuse mounts we
# need to open the file and call os.utime. Opening the file updates
# the time stamp for Fuse mounts, but os.utime() updates for local/nfs
# file systems.
with open(file_path, "a"):
os.utime(file_path, None)
def update_progress(progress_tracking_file_path, progress_msg):
LOG.info("update_progress %s: %s" % (progress_tracking_file_path, progress_msg))
try:
with open(progress_tracking_file_path, "a") as progress_tracking_file:
progress_tracking_file.write(progress_msg)
except Exception as ex:
sleep(10)
with open(progress_tracking_file_path, "a") as progress_tracking_file:
progress_tracking_file.write(progress_msg)
def get_progress(progress_tracking_file_path):
try:
with open(progress_tracking_file_path, "r") as progress_tracking_file:
return progress_tracking_file.readlines()
except Exception as ex:
sleep(10)
with open(progress_tracking_file_path, "r") as progress_tracking_file:
return progress_tracking_file.readlines()
def run_cmd(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False,
):
process = subprocess.Popen(
cmd, stdin=stdin, stdout=stdout, stderr=stderr, shell=False
)
return process
def poll_process(process, cmd, sleep_time=5, no_of_count=0, progress_file=None):
try:
count = 0
while process.poll() is None:
if no_of_count != 0 and count > no_of_count:
break
sleep(sleep_time)
count += 1
if progress_file is not None:
# update the timestamp so the workloadmgr knows contego
# is alive and kicking
touch_file(progress_file)
process.stdin.close()
_returncode = process.returncode
if _returncode:
LOG.error(("Result was %s" % _returncode))
LOG.exception(
"Execution error %(exit_code)d (%(stderr)s). "
"cmd %(cmd)s"
% {
"exit_code": _returncode,
"stderr": str(process.stderr.read(), "utf-8"),
"cmd": cmd,
}
)
return _returncode
except Exception as ex:
raise ex
def get_cmd_specs(cmd, **kwargs):
def _key_check(locals, keys):
for key in keys:
if locals[key] is None:
raise Exception(
"Required parameter: %s for %s connection can not be None"
% (key, locals["cmd"])
)
try:
authorized_key = kwargs.get("authorized_key", None)
user = kwargs.get("user", None)
host = kwargs.get("host", None)
if cmd == "ssh":
_key_check(locals(), ["authorized_key", "user", "host"])
cmdspec = [
"ssh",
"-o",
"StrictHostKeyChecking=no",
"-i",
authorized_key,
user + "@" + host,
]
elif cmd == "scp":
source_path = kwargs.get("source_path", None)
dest_path = kwargs.get("dest_path", None)
_key_check(
locals(),
["authorized_key", "user", "host", "source_path", "dest_path"],
)
cmdspec = [
"scp",
"-o",
"StrictHostKeyChecking=no",
"-i",
authorized_key,
"-r",
user + "@" + host + ":" + source_path + "/*",
dest_path,
]
return cmdspec
except Exception as ex:
raise ex
class get_dummy_object(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def strtime(at):
return at.strftime("%Y-%m-%dT%H:%M:%S.%f")
@contextlib.contextmanager
def tempdir(**kwargs):
argdict = kwargs.copy()
if 'dir' not in argdict:
argdict['dir'] = CONF.tempdir
tmpdir = tempfile.mkdtemp(**argdict)
try:
yield tmpdir
finally:
try:
shutil.rmtree(tmpdir)
except OSError as e:
LOG.error(_LE('Could not remove tmpdir: %s'), e)
def parse_encrypted_image_backing_file(backing_file):
"""
Replaces single quote with double quotes.
It Parses the Backing File and returns json.
"""
try:
json_key_position = backing_file.replace("\'", "\"").find(':') + 1
backing_file_context = backing_file[json_key_position:]
return json.loads(backing_file_context)['file']['filename']
except Exception as ex:
return backing_file
def change_device_permission(device, perm):
""" This utility method changes permission of device."""
try:
if device:
out, err = processutils.execute(
'sudo',
'nova-rootwrap',
CONF.rootwrap_config,
'chmod',
perm,
device)
if not err:
return True
except Exception as ex:
LOG.info('cannot change permission of {0} to {1}. {2}'.format(device, perm, ex))
return False