def do(self): files = self.args['files'] or [] files_changed = [] for fd in files: dst = fd['dst'] if dst.startswith('~/'): dst = fd['dst'] = os.path.expanduser(dst) if 'linkto' in fd: link = fd['linkto'] if os.path.exists(dst): if os.path.islink(dst): if os.path.realpath(dst) != os.path.realpath(link): os.unlink(dst) else: os.remove(dst) if not os.path.exists(link): self.send_log( ('Invalid link destination for ' '{0[dst]} -> no such file {0[linkto]}').format(fd), level=logging.ERROR) if not os.path.islink(dst): os.symlink(link, dst) files_changed.append(dst) else: data = fd['data'] if dst.endswith(utils.ARCHIVE_EXTS): if not isinstance(data, bytes): data = data.encode('utf8') data = base64.b64decode(data) if os.path.exists(dst): if dst.endswith(utils.ARCHIVE_EXTS): with open(dst, 'rb') as fd_: if data != fd_.read(): files_changed.append(dst) else: with codecs.open(dst, 'rb', 'utf8') as fd_: if data != fd_.read(): files_changed.append(dst) else: files_changed.append(dst) if dst.endswith(utils.ARCHIVE_EXTS): with open(dst, 'wb') as fd_: fd_.write(data) else: with codecs.open(dst, 'wb', 'utf8') as fd_: fd_.write(data) mod = fd.get('mod') if mod is None and fd.get('executable'): st = os.stat(dst) mod = (st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH) if mod is not None: utils.chmod(dst, mod) return dict(rc=0, changed=files_changed)
def do(self): self.args['user'] = getpass.getuser() dst = os.path.expanduser('~/.my.cnf') old_data = '' if os.path.isfile(dst): with codecs.open(dst, 'r', 'utf8') as fd: old_data = fd.read() data = ('[client]\n' 'user={user}\n' 'password={password}\n').format(**self.args) changed = old_data != data if changed: with codecs.open(dst, 'w', 'utf8') as fd: fd.write(data) utils.chmod(dst, '600') self.args.pop('password') return dict(rc=0, changed=changed, dst=dst)
def do(self): dst = self.args['dst'] mod = self.args['mod'] recursive = self.args['recursive'] utils.chmod(dst, mod, recursive=recursive) return dict(rc=0, dst=dst, mod=mod)