Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rudimentary windows support for bcfg2 #391

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b5ed496
Add basic windows support for bcfg2
chschenk Jul 12, 2017
354df63
Fix PEP-8 Style issues
chschenk Jul 18, 2017
cb7c965
Return extra services in WinService
chschenk Jul 18, 2017
63a45c2
Add import for binary Files
chschenk Jul 18, 2017
7c7990c
Implement a locking mechanism which works on Windows and Linux
chschenk Jul 21, 2017
6c5832d
Readd locked function for bcfg2-server
chschenk Jul 21, 2017
0dc7c3f
Fix Style Issues
chschenk Jul 21, 2017
19f05d5
More Style Fixes
chschenk Jul 21, 2017
1a28369
Fix style issues
chschenk Jul 21, 2017
4c907fe
Fix Error with lambda functions
chschenk Jul 24, 2017
54c0c1e
Use compat to import modules which doesnt exists on windows
chschenk Jul 24, 2017
ef4aea5
Fix more Style Issues
chschenk Jul 24, 2017
a19f5c3
Fix Style Issues
chschenk Jul 24, 2017
ad69ab5
More Style Fixes
chschenk Jul 24, 2017
cdd5a7d
Fix more Style Issues
chschenk Jul 24, 2017
4059e22
Better error handling in Flock class
chschenk Jul 24, 2017
190d785
Fix style and error handling issues
chschenk Jul 24, 2017
2bd891f
In Actions Shell should be False by default
chschenk Aug 8, 2017
c7e9e5a
Add WinFS support for nonexistent path types
chschenk Apr 23, 2018
c40da03
Fixing pylint errors
chschenk Jun 8, 2018
03ad988
Fixing more pylint errors
chschenk Jun 13, 2018
82dd867
Don't use "except as" because it only works in python 2.6 and newer
chschenk Aug 1, 2018
17d8888
Don't use "except as" because it only works in python 2.6 and newer
chschenk Aug 1, 2018
80efd67
Added the "real" logger to locking mechanism
chschenk Sep 20, 2018
febfafd
Added missing import for python3 compatibility
chschenk Sep 20, 2018
460979c
Fixing exception Handling for Python 2.4
chschenk Sep 25, 2018
73adee8
Fixing exception Handling for Python 2.4
chschenk Sep 25, 2018
ebc546d
Disable pylint W0622 in WinFS.py like in POSIX/File.py
chschenk Sep 25, 2018
8815e8f
Added missing space to comment
chschenk Sep 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/lib/Bcfg2/Client/Tools/WinAction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""WinAction driver"""
import subprocess

import Bcfg2.Client.Tools
from Bcfg2.Utils import safe_input


class WinAction(Bcfg2.Client.Tools.Tool):
"""Implement Actions"""
name = 'WinAction'
__handles__ = [('Action', None)]
__req__ = {'Action': ['name', 'timing', 'when', 'command', 'status']}

def RunAction(self, entry):
"""This method handles command execution and status return."""
shell = False
shell_string = ''
if entry.get('shell', 'false') == 'true':
shell = True
shell_string = '(in shell) '

if not Bcfg2.Options.setup.dry_run:
if Bcfg2.Options.setup.interactive:
prompt = ('Run Action %s%s, %s: (y/N): ' %
(shell_string, entry.get('name'),
entry.get('command')))
ans = safe_input(prompt)
if ans not in ['y', 'Y']:
return False
if Bcfg2.Options.setup.service_mode == 'build':
if entry.get('build', 'true') == 'false':
self.logger.debug("Action: Deferring execution of %s due "
"to build mode" % entry.get('command'))
return False
self.logger.debug("Running Action %s %s" %
(shell_string, entry.get('name')))
rv = self.cmd.run(entry.get('command'), shell=shell,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=False)
self.logger.debug("Action: %s got return code %s" %
(entry.get('command'), rv.retval))
entry.set('rc', str(rv.retval))
return entry.get('status', 'check') == 'ignore' or rv.success
else:
self.logger.debug("In dryrun mode: not running action: %s" %
(entry.get('name')))
return False

def VerifyAction(self, dummy, _):
"""Actions always verify true."""
return True

def InstallAction(self, entry):
"""Run actions as pre-checks for bundle installation."""
if entry.get('timing') != 'post':
return self.RunAction(entry)
return True

def BundleUpdated(self, bundle):
"""Run postinstalls when bundles have been updated."""
states = dict()
for action in bundle.findall("Action"):
if action.get('timing') in ['post', 'both']:
if not self._install_allowed(action):
continue
states[action] = self.RunAction(action)
return states

def BundleNotUpdated(self, bundle):
"""Run Actions when bundles have not been updated."""
states = dict()
for action in bundle.findall("Action"):
if (action.get('timing') in ['post', 'both'] and
action.get('when') != 'modified'):
if not self._install_allowed(action):
continue
states[action] = self.RunAction(action)
return states
229 changes: 229 additions & 0 deletions src/lib/Bcfg2/Client/Tools/WinFS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
"""All Windows Type client support for Bcfg2."""
import sys
import os
import stat
import shutil
import tempfile
import Bcfg2.Options
import Bcfg2.Client.Tools
from Bcfg2.Compat import unicode, b64decode # pylint: disable=W0622


class WinFS(Bcfg2.Client.Tools.Tool):
"""Windows File support code."""
name = 'WinFS'
__handles__ = [('Path', 'file'), ('Path', 'nonexistent')]

def __init__(self, config):
Bcfg2.Client.Tools.Tool.__init__(self, config)
self.__req__ = dict(Path=dict())
self.__req__['Path']['file'] = ['name', 'mode', 'owner', 'group']
self.__req__['Path']['nonexistent'] = ['name', 'recursive']

def _getFilePath(self, entry):
"""Evaluates the enviroment Variables and returns the file path"""
file_path = os.path.expandvars(os.path.normpath(entry.get('name')[1:]))
if not file_path[1] == ':':
self.logger.info(
"Skipping \"%s\" because it doesnt look like a "
"Windows Path" %
file_path)
return False
return file_path

def VerifyPath(self, entry, _):
"""Path always verify true."""
file_path = self._getFilePath(entry)
if not file_path:
return False

if entry.get('type') == 'nonexistent':
if os.path.exists(file_path):
self.logger.debug("WinFS: %s exists but should not" %
file_path)
return False
return True

ondisk = self._exists(file_path)
tempdata = self._get_data(entry)[0]
if isinstance(tempdata, str) and str != unicode:
tempdatasize = len(tempdata)
else:
tempdatasize = len(tempdata.encode(Bcfg2.Options.setup.encoding))

different = False
content = None
if not ondisk:
# first, see if the target file exists at all; if not,
# they're clearly different
different = True
content = ""
elif tempdatasize != ondisk[stat.ST_SIZE]:
# next, see if the size of the target file is different
# from the size of the desired content
different = True
else:
# finally, read in the target file and compare them
# directly. comparison could be done with a checksum,
# which might be faster for big binary files, but slower
# for everything else
try:
content = open(file_path).read()
except UnicodeDecodeError:
content = open(file_path,
encoding=Bcfg2.Options.setup.encoding).read()
except IOError:
self.logger.error("Windows: Failed to read %s: %s" %
(file_path, sys.exc_info()[1]))
return False
different = str(content) != str(tempdata)
return not different

def InstallPath(self, entry):
"""Install device entries."""
file_path = self._getFilePath(entry)
ename = entry.get('name')
print("Handeling %s" % file_path)
if not file_path:
return False
if entry.get('type') == "nonexistent":
recursive = entry.get('recursive', '').lower() == 'true'
if recursive:
# ensure that configuration spec is consistent first
for struct in self.config.getchildren():
for el in struct.getchildren():
if (el.tag == 'Path' and
el.get('type') != 'nonexistent' and
el.get('name').startswith(ename)):
self.logger.error('POSIX: Not removing %s. One '
'or more files in this '
'directory are specified '
'in your configuration.' %
file_path)
return False
try:
self._remove(file_path, recursive=recursive)
rv = True
except OSError:
err = sys.exc_info()[1]
self.logger.error('WinFS: Failed to renive %s: %s' %
(file_path, err))
rv = False
return rv

self.logger.debug("Installing: " + file_path)
if not os.path.exists(os.path.dirname(file_path)):
if not self._makedirs(path=file_path):
return False
newfile = self._write_tmpfile(entry, file_path)
if not newfile:
return False
rv = True
if not self._rename_tmpfile(newfile, file_path):
rv = False

return rv

def _makedirs(self, path):
""" os.makedirs helpfully creates all parent directories for us."""
rv = True
try:
os.makedirs(os.path.dirname(path))
except OSError:
err = sys.exc_info()[1]
self.logger.error('Windows: Failed to create directory %s: %s' %
(path, err))
rv = False
return rv

def _write_tmpfile(self, entry, file_path):
""" Write the file data to a temp file """
filedata = self._get_data(entry)[0]
# get a temp file to write to that is in the same directory as
# the existing file in order to preserve any permissions
# protections on that directory, and also to avoid issues with
# /tmp set nosetuid while creating files that are supposed to
# be setuid
try:
(newfd, newfile) = \
tempfile.mkstemp(prefix=os.path.basename(file_path),
dir=os.path.dirname(file_path))
except OSError:
err = sys.exc_info()[1]
self.logger.error(
"Windows: Failed to create "
"temp file in %s: %s" % (file_path, err))
return False
try:
if isinstance(filedata, str) and str != unicode:
os.fdopen(newfd, 'w').write(filedata)
else:
os.fdopen(newfd, 'wb').write(
filedata.encode(Bcfg2.Options.setup.encoding))
except (OSError, IOError):
err = sys.exc_info()[1]
self.logger.error(
"Windows: Failed to open temp file %s for writing "
"%s: %s" %
(newfile, file_path, err))
return False
return newfile

def _get_data(self, entry):
""" Get a tuple of (<file data>, <is binary>) for the given entry """
is_binary = entry.get('encoding', 'ascii') == 'base64'
if entry.get('empty', 'false') == 'true' or not entry.text:
tempdata = ''
elif is_binary:
tempdata = b64decode(entry.text)
else:
tempdata = entry.text
if isinstance(tempdata, unicode) and unicode != str:
try:
tempdata = tempdata.encode(Bcfg2.Options.setup.encoding)
except UnicodeEncodeError:
err = sys.exc_info()[1]
self.logger.error("Windows: Error encoding file %s: %s" %
(entry.get('name'), err))
return (tempdata, is_binary)

def _rename_tmpfile(self, newfile, file_path):
""" Rename the given file to the appropriate filename for entry """
try:
if os.path.isfile(file_path):
os.unlink(file_path)
os.rename(newfile, file_path)
return True
except OSError:
err = sys.exc_info()[1]
self.logger.error(
"Windows: Failed to rename temp file %s to %s: %s"
% (newfile, file_path, err))
try:
os.unlink(newfile)
except OSError:
err = sys.exc_info()[1]
self.logger.error(
"Windows: Could not remove temp file %s: %s" %
(newfile, err))
return False

def _exists(self, file_path):
""" check for existing paths and optionally remove them. if
the path exists, return the lstat of it """
try:
return os.lstat(file_path)
except OSError:
return None

def _remove(self, file_path, recursive=True):
""" Remove a Path entry, whatever that takes """
if os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
if recursive:
shutil.rmtree(file_path)
else:
os.rmdir(file_path)
else:
os.unlink(file_path)
Loading