freezer/freezer/winutils.py
eldar nugaev 8f4b53da68 Command for changing logging level.
And pep8 fixing
(suddenly this pull request was rejected by jenkins and pep8.
so this commit also contains some small style changes for pep8.)

Currently the default value for logging is INFO and it is hardcoded.
Additional command will be helpful for debug/development process.

Implements blueprint: logging-level-cli

Change-Id: I1105e3da277e2a948f3ff50b04751169eddf26ed
2016-01-13 16:46:17 +00:00

105 lines
3.5 KiB
Python

# (c) Copyright 2014,2015 Hewlett-Packard Development Company, L.P.
#
# 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 ctypes
import json
import logging
import os
import sys
from freezer.utils import create_subprocess
def is_windows():
"""
:return: True if the running platform is windows
"""
return True if sys.platform == 'win32' else False
class DisableFileSystemRedirection:
"""
When a 32 bit program runs on a 64 bit operating system the paths
to C:/Windows/System32 automatically get redirected to the 32 bit
version (C:/Windows/SysWow64), if you really do need to access the
contents of System32, you need to disable the file system redirector first.
"""
def __init__(self):
if is_windows():
self._disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
self._revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
else:
raise Exception("Useless if not windows")
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
def use_shadow(to_backup, windows_volume):
""" add the shadow path to the backup directory """
return to_backup.replace(windows_volume, '{0}freezer_shadowcopy\\'
.format(windows_volume))
def stop_sql_server(sql_server_instance):
""" Stop a SQL Server instance to perform the backup of the db files """
logging.info('[*] Stopping SQL Server for backup')
with DisableFileSystemRedirection():
cmd = 'net stop "SQL Server ({0})"'\
.format(sql_server_instance)
(out, err) = create_subprocess(cmd)
if err != '':
raise Exception('[*] Error while stopping SQL Server,'
', error {0}'.format(err))
def start_sql_server(sql_server_instance):
""" Start the SQL Server instance after the backup is completed """
with DisableFileSystemRedirection():
cmd = 'net start "SQL Server ({0})"'.format(sql_server_instance)
(out, err) = create_subprocess(cmd)
if err != '':
raise Exception('[*] Error while starting SQL Server'
', error {0}'.format(err))
logging.info('[*] SQL Server back to normal')
def save_environment(home):
"""Read the environment from the terminal where the scheduler is
initialized and save the environment variables to be reused within the
windows service
"""
env_path = os.path.join(home, 'env.json')
with open(env_path, 'wb') as tmp:
json.dump(os.environ.copy(), tmp)
def set_environment(home):
"""Read the environment variables saved by the win_daemon and restore it
here for the windows service
"""
json_env = os.path.join(home, 'env.json')
with open(json_env, 'rb') as fp:
env = json.loads(fp.read())
for k, v in env.iteritems():
os.environ[str(k).strip()] = str(v).strip()