Repository URL to install this package:
Version:
3.4.36 ▾
|
# Copyright 2015 TrilioData Inc.
# All Rights Reserved.
import os
import errno
import socket
import fcntl
import struct
import ConfigParser
import subprocess
import errno
from time import sleep
from multiprocessing import Process
try:
from oslo_log import log as logging
except ImportError:
from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__)
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, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[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):
with open(progress_tracking_file_path, 'a') as progress_tracking_file:
progress_tracking_file.write(progress_msg)
def get_progress(progress_tracking_file_path):
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=shell)
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': unicode(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