mirror of https://github.com/ansible/ansible.git
Add shell_plugins to abstract shell-specific functions out of runner, add winrm connection plugin, add initial Windows modules.
parent
627ff30a6f
commit
5dcaa30476
@ -0,0 +1,254 @@
|
|||||||
|
# (c) 2014, Chris Church <chris@ninemoreminutes.com>
|
||||||
|
#
|
||||||
|
# This file is (not yet) part of Ansible.
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import base64
|
||||||
|
import hashlib
|
||||||
|
import imp
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import shlex
|
||||||
|
import traceback
|
||||||
|
import urlparse
|
||||||
|
from ansible import errors
|
||||||
|
from ansible import utils
|
||||||
|
from ansible.callbacks import vvv, vvvv
|
||||||
|
|
||||||
|
try:
|
||||||
|
from winrm import Response
|
||||||
|
from winrm.exceptions import WinRMTransportError
|
||||||
|
from winrm.protocol import Protocol
|
||||||
|
except ImportError:
|
||||||
|
raise errors.AnsibleError("winrm is not installed")
|
||||||
|
|
||||||
|
# When running with unmodified Ansible (1.6.x), load local hacks.
|
||||||
|
try:
|
||||||
|
_winrm_hacks = imp.load_source('_winrm_hacks', os.path.join(os.path.dirname(__file__), '_winrm_hacks.py'))
|
||||||
|
except (ImportError, IOError):
|
||||||
|
_winrm_hacks = None
|
||||||
|
|
||||||
|
_winrm_cache = {
|
||||||
|
# 'user:pwhash@host:port': <protocol instance>
|
||||||
|
}
|
||||||
|
|
||||||
|
class Connection(object):
|
||||||
|
'''WinRM connections over HTTP/HTTPS.'''
|
||||||
|
|
||||||
|
def __init__(self, runner, host, port, user, password, *args, **kwargs):
|
||||||
|
self.runner = runner
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
self.user = user
|
||||||
|
self.password = password
|
||||||
|
self.has_pipelining = False
|
||||||
|
self.default_shell = 'powershell'
|
||||||
|
self.default_suffixes = ['.ps1', '']
|
||||||
|
self.protocol = None
|
||||||
|
self.shell_id = None
|
||||||
|
self.delegate = None
|
||||||
|
if _winrm_hacks:
|
||||||
|
_winrm_hacks.patch_module_finder(self)
|
||||||
|
|
||||||
|
def _winrm_connect(self):
|
||||||
|
'''
|
||||||
|
Establish a WinRM connection over HTTP/HTTPS.
|
||||||
|
'''
|
||||||
|
if _winrm_hacks:
|
||||||
|
port = _winrm_hacks.get_port(self)
|
||||||
|
else:
|
||||||
|
port = self.port or 5986
|
||||||
|
vvv("ESTABLISH WINRM CONNECTION FOR USER: %s on PORT %s TO %s" % \
|
||||||
|
(self.user, port, self.host), host=self.host)
|
||||||
|
netloc = '%s:%d' % (self.host, port)
|
||||||
|
cache_key = '%s:%s@%s:%d' % (self.user, hashlib.md5(self.password).hexdigest(), self.host, port)
|
||||||
|
if cache_key in _winrm_cache:
|
||||||
|
vvvv('WINRM REUSE EXISTING CONNECTION: %s' % cache_key, host=self.host)
|
||||||
|
return _winrm_cache[cache_key]
|
||||||
|
transport_schemes = [('plaintext', 'https'), ('plaintext', 'http')] # FIXME: ssl/kerberos
|
||||||
|
if port == 5985:
|
||||||
|
transport_schemes = reversed(transport_schemes)
|
||||||
|
exc = None
|
||||||
|
for transport, scheme in transport_schemes:
|
||||||
|
endpoint = urlparse.urlunsplit((scheme, netloc, '/wsman', '', ''))
|
||||||
|
vvvv('WINRM CONNECT: transport=%s endpoint=%s' % (transport, endpoint),
|
||||||
|
host=self.host)
|
||||||
|
protocol = Protocol(endpoint, transport=transport,
|
||||||
|
username=self.user, password=self.password)
|
||||||
|
try:
|
||||||
|
protocol.send_message('')
|
||||||
|
_winrm_cache[cache_key] = protocol
|
||||||
|
return protocol
|
||||||
|
except WinRMTransportError, exc:
|
||||||
|
err_msg = str(exc.args[0])
|
||||||
|
if re.search(r'Operation\s+?timed\s+?out', err_msg, re.I):
|
||||||
|
raise
|
||||||
|
m = re.search(r'Code\s+?(\d{3})', err_msg)
|
||||||
|
if m:
|
||||||
|
code = int(m.groups()[0])
|
||||||
|
if code == 411:
|
||||||
|
_winrm_cache[cache_key] = protocol
|
||||||
|
return protocol
|
||||||
|
vvvv('WINRM CONNECTION ERROR: %s' % err_msg, host=self.host)
|
||||||
|
continue
|
||||||
|
# FIXME: Cache connection!!!
|
||||||
|
if exc:
|
||||||
|
raise exc
|
||||||
|
|
||||||
|
def _winrm_escape(self, value, include_vars=False):
|
||||||
|
'''
|
||||||
|
Return value escaped for use in PowerShell command.
|
||||||
|
'''
|
||||||
|
# http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
|
||||||
|
# http://stackoverflow.com/questions/764360/a-list-of-string-replacements-in-python
|
||||||
|
subs = [('\n', '`n'), ('\r', '`r'), ('\t', '`t'), ('\a', '`a'),
|
||||||
|
('\b', '`b'), ('\f', '`f'), ('\v', '`v'), ('"', '`"'),
|
||||||
|
('\'', '`\''), ('`', '``'), ('\x00', '`0')]
|
||||||
|
if include_vars:
|
||||||
|
subs.append(('$', '`$'))
|
||||||
|
pattern = '|'.join('(%s)' % re.escape(p) for p, s in subs)
|
||||||
|
substs = [s for p, s in subs]
|
||||||
|
replace = lambda m: substs[m.lastindex - 1]
|
||||||
|
return re.sub(pattern, replace, value)
|
||||||
|
|
||||||
|
def _winrm_get_script_cmd(self, script):
|
||||||
|
'''
|
||||||
|
Convert a PowerShell script to a single base64-encoded command.
|
||||||
|
'''
|
||||||
|
vvvv('WINRM SCRIPT: %s' % script, host=self.host)
|
||||||
|
encoded_script = base64.b64encode(script.encode('utf-16-le'))
|
||||||
|
return ['PowerShell', '-NoProfile', '-NonInteractive',
|
||||||
|
'-EncodedCommand', encoded_script]
|
||||||
|
|
||||||
|
def _winrm_exec(self, command, args):
|
||||||
|
vvvv("WINRM EXEC %r %r" % (command, args), host=self.host)
|
||||||
|
if not self.protocol:
|
||||||
|
self.protocol = self._winrm_connect()
|
||||||
|
if not self.shell_id:
|
||||||
|
self.shell_id = self.protocol.open_shell()
|
||||||
|
command_id = None
|
||||||
|
try:
|
||||||
|
command_id = self.protocol.run_command(self.shell_id, command, args)
|
||||||
|
response = Response(self.protocol.get_command_output(self.shell_id, command_id))
|
||||||
|
vvvv('WINRM RESULT %r' % response, host=self.host)
|
||||||
|
vvvv('WINRM STDERR %s' % response.std_err, host=self.host)
|
||||||
|
return response
|
||||||
|
finally:
|
||||||
|
if command_id:
|
||||||
|
self.protocol.cleanup_command(self.shell_id, command_id)
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
if not _winrm_hacks:
|
||||||
|
if not self.protocol:
|
||||||
|
self.protocol = self._winrm_connect()
|
||||||
|
# When using hacks, connect lazily on first command, to allow for
|
||||||
|
# runner to set self.delegate, needed if actual host vs. host name are
|
||||||
|
# different.
|
||||||
|
return self
|
||||||
|
|
||||||
|
def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', in_data=None, su=None, su_user=None):
|
||||||
|
cmd = cmd.encode('utf-8')
|
||||||
|
vvv("EXEC %s" % cmd, host=self.host)
|
||||||
|
cmd_parts = shlex.split(cmd, posix=False)
|
||||||
|
vvvv("WINRM PARTS %r" % cmd_parts, host=self.host)
|
||||||
|
# For script/raw support.
|
||||||
|
if len(cmd_parts) == 1 and cmd_parts[0].lower().endswith('.ps1'):
|
||||||
|
cmd_parts = ['PowerShell', '-ExecutionPolicy', 'Unrestricted', '-File', cmd_parts[0]]
|
||||||
|
if _winrm_hacks:
|
||||||
|
cmd_parts = _winrm_hacks.filter_cmd_parts(self, cmd_parts)
|
||||||
|
if not cmd_parts:
|
||||||
|
vvv('WINRM NOOP')
|
||||||
|
return (0, '', '', '')
|
||||||
|
try:
|
||||||
|
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||||
|
except Exception, e:
|
||||||
|
traceback.print_exc()
|
||||||
|
raise errors.AnsibleError("failed to exec cmd %s" % cmd)
|
||||||
|
return (result.status_code, '', result.std_out.encode('utf-8'), result.std_err.encode('utf-8'))
|
||||||
|
|
||||||
|
def put_file(self, in_path, out_path):
|
||||||
|
if _winrm_hacks:
|
||||||
|
out_path = _winrm_hacks.fix_slashes(out_path)
|
||||||
|
vvv("PUT %s TO %s" % (in_path, out_path), host=self.host)
|
||||||
|
if not os.path.exists(in_path):
|
||||||
|
raise errors.AnsibleFileNotFound("file or module does not exist: %s" % in_path)
|
||||||
|
buffer_size = 1024 # FIXME: Find max size or optimize.
|
||||||
|
with open(in_path) as in_file:
|
||||||
|
in_size = os.path.getsize(in_path)
|
||||||
|
for offset in xrange(0, in_size, buffer_size):
|
||||||
|
try:
|
||||||
|
out_data = in_file.read(buffer_size)
|
||||||
|
if offset == 0:
|
||||||
|
if out_data.lower().startswith('#!powershell') and not out_path.lower().endswith('.ps1'):
|
||||||
|
out_path = out_path + '.ps1'
|
||||||
|
b64_data = base64.b64encode(out_data)
|
||||||
|
script = '''
|
||||||
|
$bufferSize = %d;
|
||||||
|
$stream = [System.IO.File]::OpenWrite("%s");
|
||||||
|
$stream.Seek(%d, [System.IO.SeekOrigin]::Begin) | Out-Null;
|
||||||
|
$data = "%s";
|
||||||
|
$buffer = [System.Convert]::FromBase64String($data);
|
||||||
|
$stream.Write($buffer, 0, $buffer.length) | Out-Null;
|
||||||
|
$stream.SetLength(%d) | Out-Null;
|
||||||
|
$stream.Close() | Out-Null;
|
||||||
|
''' % (buffer_size, self._winrm_escape(out_path), offset, b64_data, in_size)
|
||||||
|
cmd_parts = self._winrm_get_script_cmd(script)
|
||||||
|
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||||
|
if result.status_code != 0:
|
||||||
|
raise RuntimeError(result.std_err.encode('utf-8'))
|
||||||
|
script = u''
|
||||||
|
except Exception: # IOError?
|
||||||
|
traceback.print_exc()
|
||||||
|
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
||||||
|
|
||||||
|
def fetch_file(self, in_path, out_path):
|
||||||
|
if _winrm_hacks:
|
||||||
|
in_path = _winrm_hacks.fix_slashes(in_path)
|
||||||
|
out_path = out_path.replace('\\', '/')
|
||||||
|
vvv("FETCH %s TO %s" % (in_path, out_path), host=self.host)
|
||||||
|
buffer_size = 2**20 # 1MB chunks
|
||||||
|
if not os.path.exists(os.path.dirname(out_path)):
|
||||||
|
os.makedirs(os.path.dirname(out_path))
|
||||||
|
with open(out_path, 'wb') as out_file:
|
||||||
|
offset = 0
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
script = '''
|
||||||
|
$bufferSize = %d;
|
||||||
|
$stream = [System.IO.File]::OpenRead("%s");
|
||||||
|
$stream.Seek(%d, [System.IO.SeekOrigin]::Begin) | Out-Null;
|
||||||
|
$buffer = New-Object Byte[] $bufferSize;
|
||||||
|
$bytesRead = $stream.Read($buffer, 0, $bufferSize);
|
||||||
|
$bytes = $buffer[0..($bytesRead-1)];
|
||||||
|
[System.Convert]::ToBase64String($bytes);
|
||||||
|
$stream.Close() | Out-Null;
|
||||||
|
''' % (buffer_size, self._winrm_escape(in_path), offset)
|
||||||
|
cmd_parts = self._winrm_get_script_cmd(script)
|
||||||
|
result = self._winrm_exec(cmd_parts[0], cmd_parts[1:])
|
||||||
|
data = base64.b64decode(result.std_out.strip())
|
||||||
|
out_file.write(data)
|
||||||
|
if len(data) < buffer_size:
|
||||||
|
break
|
||||||
|
offset += len(data)
|
||||||
|
except Exception: # IOError?
|
||||||
|
traceback.print_exc()
|
||||||
|
raise errors.AnsibleError("failed to transfer file to %s" % out_path)
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.protocol and self.shell_id:
|
||||||
|
self.protocol.close_shell(self.shell_id)
|
||||||
|
self.shell_id = None
|
@ -0,0 +1,6 @@
|
|||||||
|
from ansible.runner.shell_plugins.sh import ShellModule as ShModule
|
||||||
|
|
||||||
|
class ShellModule(ShModule):
|
||||||
|
|
||||||
|
def env_prefix(self, **kwargs):
|
||||||
|
return 'env %s' % super(ShellModule, self).env_prefix(**kwargs)
|
@ -0,0 +1,6 @@
|
|||||||
|
from ansible.runner.shell_plugins.sh import ShellModule as ShModule
|
||||||
|
|
||||||
|
class ShellModule(ShModule):
|
||||||
|
|
||||||
|
def env_prefix(self, **kwargs):
|
||||||
|
return 'env %s' % super(ShellModule, self).env_prefix(**kwargs)
|
@ -0,0 +1,72 @@
|
|||||||
|
import base64
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import random
|
||||||
|
import shlex
|
||||||
|
import time
|
||||||
|
|
||||||
|
class ShellModule(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _escape(self, value, include_vars=False):
|
||||||
|
'''
|
||||||
|
Return value escaped for use in PowerShell command.
|
||||||
|
'''
|
||||||
|
# http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
|
||||||
|
# http://stackoverflow.com/questions/764360/a-list-of-string-replacements-in-python
|
||||||
|
subs = [('\n', '`n'), ('\r', '`r'), ('\t', '`t'), ('\a', '`a'),
|
||||||
|
('\b', '`b'), ('\f', '`f'), ('\v', '`v'), ('"', '`"'),
|
||||||
|
('\'', '`\''), ('`', '``'), ('\x00', '`0')]
|
||||||
|
if include_vars:
|
||||||
|
subs.append(('$', '`$'))
|
||||||
|
pattern = '|'.join('(%s)' % re.escape(p) for p, s in subs)
|
||||||
|
substs = [s for p, s in subs]
|
||||||
|
replace = lambda m: substs[m.lastindex - 1]
|
||||||
|
return re.sub(pattern, replace, value)
|
||||||
|
|
||||||
|
def _get_script_cmd(self, script):
|
||||||
|
'''
|
||||||
|
Convert a PowerShell script to a single base64-encoded command.
|
||||||
|
'''
|
||||||
|
encoded_script = base64.b64encode(script.encode('utf-16-le'))
|
||||||
|
return ' '.join(['PowerShell', '-NoProfile', '-NonInteractive',
|
||||||
|
'-EncodedCommand', encoded_script])
|
||||||
|
|
||||||
|
def env_prefix(self, **kwargs):
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def join_path(self, *args):
|
||||||
|
return os.path.join(*args).replace('/', '\\')
|
||||||
|
|
||||||
|
def chmod(self, mode, path):
|
||||||
|
return ''
|
||||||
|
|
||||||
|
def remove(self, path, recurse=False):
|
||||||
|
path = self._escape(path)
|
||||||
|
if recurse:
|
||||||
|
return self._get_script_cmd('''Remove-Item "%s" -Force -Recurse;''' % path)
|
||||||
|
else:
|
||||||
|
return self._get_script_cmd('''Remove-Item "%s" -Force;''' % path)
|
||||||
|
|
||||||
|
def mkdtemp(self, basefile=None, system=False, mode=None):
|
||||||
|
if not basefile:
|
||||||
|
basefile = 'ansible-tmp-%s-%s' % (time.time(), random.randint(0, 2**48))
|
||||||
|
basefile = self._escape(basefile)
|
||||||
|
# FIXME: Support system temp path!
|
||||||
|
return self._get_script_cmd('''(New-Item -Type Directory -Path $env:temp -Name "%s").FullName;''' % basefile)
|
||||||
|
|
||||||
|
def md5(self, path):
|
||||||
|
path = self._escape(path)
|
||||||
|
return self._get_script_cmd('''(Get-FileHash -Path "%s" -Algorithm MD5).Hash.ToLower();''' % path)
|
||||||
|
|
||||||
|
def build_module_command(self, env_string, shebang, cmd, rm_tmp=None):
|
||||||
|
cmd_parts = shlex.split(cmd, posix=False)
|
||||||
|
if not cmd_parts[0].lower().endswith('.ps1'):
|
||||||
|
cmd_parts[0] = '%s.ps1' % cmd_parts[0]
|
||||||
|
cmd_parts = ['PowerShell', '-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Unrestricted', '-File'] + ['"%s"' % x for x in cmd_parts]
|
||||||
|
script = ' '.join(cmd_parts)
|
||||||
|
if rm_tmp:
|
||||||
|
script = '%s; Remove-Item "%s" -Force -Recurse;' % (script, self._escape(rm_tmp))
|
||||||
|
return self._get_script_cmd(script)
|
@ -0,0 +1,71 @@
|
|||||||
|
|
||||||
|
import os
|
||||||
|
import pipes
|
||||||
|
import ansible.constants as C
|
||||||
|
|
||||||
|
class ShellModule(object):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def env_prefix(self, **kwargs):
|
||||||
|
'''Build command prefix with environment variables.'''
|
||||||
|
env = dict(
|
||||||
|
LANG = C.DEFAULT_MODULE_LANG,
|
||||||
|
LC_CTYPE = C.DEFAULT_MODULE_LANG,
|
||||||
|
)
|
||||||
|
env.update(kwargs)
|
||||||
|
return ' '.join(['%s=%s' % (k, pipes.quote(unicode(v))) for k,v in env.items()])
|
||||||
|
|
||||||
|
def join_path(self, *args):
|
||||||
|
return os.path.join(*args)
|
||||||
|
|
||||||
|
def chmod(self, mode, path):
|
||||||
|
path = pipes.quote(path)
|
||||||
|
return 'chmod %s %s' % (mode, path)
|
||||||
|
|
||||||
|
def remove(self, path, recurse=False):
|
||||||
|
path = pipes.quote(path)
|
||||||
|
if recurse:
|
||||||
|
return "rm -rf %s >/dev/null 2>&1" % path
|
||||||
|
else:
|
||||||
|
return "rm -f %s >/dev/null 2>&1" % path
|
||||||
|
|
||||||
|
def mkdtemp(self, basefile=None, system=False, mode=None):
|
||||||
|
if not basefile:
|
||||||
|
basefile = 'ansible-tmp-%s-%s' % (time.time(), random.randint(0, 2**48))
|
||||||
|
basetmp = self.join_path(C.DEFAULT_REMOTE_TMP, basefile)
|
||||||
|
if system and basetmp.startswith('$HOME'):
|
||||||
|
basetmp = self.join_path('/tmp', basefile)
|
||||||
|
cmd = 'mkdir -p %s' % basetmp
|
||||||
|
if mode:
|
||||||
|
cmd += ' && chmod %s %s' % (mode, basetmp)
|
||||||
|
cmd += ' && echo %s' % basetmp
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
def md5(self, path):
|
||||||
|
path = pipes.quote(path)
|
||||||
|
# The following test needs to be SH-compliant. BASH-isms will
|
||||||
|
# not work if /bin/sh points to a non-BASH shell.
|
||||||
|
test = "rc=0; [ -r \"%s\" ] || rc=2; [ -f \"%s\" ] || rc=1; [ -d \"%s\" ] && echo 3 && exit 0" % ((path,) * 3)
|
||||||
|
md5s = [
|
||||||
|
"(/usr/bin/md5sum %s 2>/dev/null)" % path, # Linux
|
||||||
|
"(/sbin/md5sum -q %s 2>/dev/null)" % path, # ?
|
||||||
|
"(/usr/bin/digest -a md5 %s 2>/dev/null)" % path, # Solaris 10+
|
||||||
|
"(/sbin/md5 -q %s 2>/dev/null)" % path, # Freebsd
|
||||||
|
"(/usr/bin/md5 -n %s 2>/dev/null)" % path, # Netbsd
|
||||||
|
"(/bin/md5 -q %s 2>/dev/null)" % path, # Openbsd
|
||||||
|
"(/usr/bin/csum -h MD5 %s 2>/dev/null)" % path, # AIX
|
||||||
|
"(/bin/csum -h MD5 %s 2>/dev/null)" % path # AIX also
|
||||||
|
]
|
||||||
|
|
||||||
|
cmd = " || ".join(md5s)
|
||||||
|
cmd = "%s; %s || (echo \"${rc} %s\")" % (test, cmd, path)
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
def build_module_command(self, env_string, shebang, cmd, rm_tmp=None):
|
||||||
|
cmd_parts = [env_string.strip(), shebang.replace("#!", "").strip(), cmd]
|
||||||
|
new_cmd = " ".join(cmd_parts)
|
||||||
|
if rm_tmp:
|
||||||
|
new_cmd = '%s; rm -rf %s >/dev/null 2>&1' % (new_cmd, rm_tmp)
|
||||||
|
return new_cmd
|
@ -0,0 +1,13 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'FIXME';
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,13 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'FIXME';
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,13 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'FIXME';
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,13 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'FIXME';
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,13 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'FIXME';
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name fixme -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,17 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'pong';
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'data')
|
||||||
|
{
|
||||||
|
$data = $params.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name ping -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,33 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
$params = '{}' | ConvertFrom-Json;
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$src = '';
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'src')
|
||||||
|
{
|
||||||
|
$src = $params.src;
|
||||||
|
}
|
||||||
|
Else
|
||||||
|
{
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'path')
|
||||||
|
{
|
||||||
|
$src = $params.path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
If (-not $src)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$bytes = [System.IO.File]::ReadAllBytes($src);
|
||||||
|
$content = [System.Convert]::ToBase64String($bytes);
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name content -Value $content;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name encoding -Value 'base64';
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,51 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
$params = '{}' | ConvertFrom-Json;
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$path = '';
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'path')
|
||||||
|
{
|
||||||
|
$path = $params.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
$get_md5 = $TRUE;
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'get_md5')
|
||||||
|
{
|
||||||
|
$get_md5 = $params.get_md5;
|
||||||
|
}
|
||||||
|
|
||||||
|
$stat = '{}' | ConvertFrom-Json;
|
||||||
|
If (Test-Path $path)
|
||||||
|
{
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name exists -Value $TRUE;
|
||||||
|
$info = Get-Item $path;
|
||||||
|
If ($info.Directory) # Only files have the .Directory attribute.
|
||||||
|
{
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name isdir -Value $FALSE;
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name size -Value $info.Length;
|
||||||
|
}
|
||||||
|
Else
|
||||||
|
{
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name isdir -Value $TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Else
|
||||||
|
{
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name exists -Value $FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
If ($get_md5 -and $stat.exists -and -not $stat.isdir)
|
||||||
|
{
|
||||||
|
$path_md5 = (Get-FileHash -Path $path -Algorithm MD5).Hash.ToLower();
|
||||||
|
$stat | Add-Member -MemberType NoteProperty -Name md5 -Value $path_md5;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name stat -Value $stat;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name changed -Value $FALSE;
|
||||||
|
echo $result | ConvertTo-Json;
|
@ -0,0 +1,87 @@
|
|||||||
|
#!powershell
|
||||||
|
# WANT_JSON
|
||||||
|
|
||||||
|
If ($args.Length -gt 0)
|
||||||
|
{
|
||||||
|
$params = Get-Content $args[0] | ConvertFrom-Json;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = 'pong';
|
||||||
|
If (($params | Get-Member | Select-Object -ExpandProperty Name) -contains 'data')
|
||||||
|
{
|
||||||
|
$data = $params.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = '{}' | ConvertFrom-Json;
|
||||||
|
$result | Add-Member -MemberType NoteProperty -Name ping -Value $data;
|
||||||
|
echo $result | ConvertTo-Json;
|
||||||
|
|
||||||
|
# _______ _ _
|
||||||
|
# |__ __| | (_)
|
||||||
|
# | | | |__ _ ___
|
||||||
|
# | | | '_ \| / __|
|
||||||
|
# | | | | | | \__ \
|
||||||
|
# __|_| |_| |_|_|___/
|
||||||
|
# |_ _|
|
||||||
|
# | | ___
|
||||||
|
# | | / __|
|
||||||
|
# _| |_\__ \
|
||||||
|
# |___/\|___/
|
||||||
|
# / \
|
||||||
|
# / /\ \
|
||||||
|
# / ____ \
|
||||||
|
# /_/ \_\
|
||||||
|
# | |
|
||||||
|
# | | __ _ _ __ __ _ ___
|
||||||
|
# | | / _` | '__/ _` |/ _ \
|
||||||
|
# | |___| (_| | | | (_| | __/
|
||||||
|
# |______\__,_|_| \__, |\___|
|
||||||
|
# __/ |
|
||||||
|
# ____ _ |___/
|
||||||
|
# | _ \| | | |
|
||||||
|
# | |_) | | ___ ___| | __
|
||||||
|
# | _ <| |/ _ \ / __| |/ /
|
||||||
|
# | |_) | | (_) | (__| <
|
||||||
|
# |____/|_|\___/ \___|_|\_\
|
||||||
|
# / __ \ / _|
|
||||||
|
# | | | | |_
|
||||||
|
# | | | | _|
|
||||||
|
# | |__| | |
|
||||||
|
# \____/|_| __ __
|
||||||
|
# / ____| | / _|/ _|
|
||||||
|
# | (___ | |_ _ _| |_| |_
|
||||||
|
# \___ \| __| | | | _| _|
|
||||||
|
# ____) | |_| |_| | | | |
|
||||||
|
# |_____/ \__|\__,_|_| |_|
|
||||||
|
# | | | |
|
||||||
|
# | |_ _ ___| |_
|
||||||
|
# _ | | | | / __| __|
|
||||||
|
# | |__| | |_| \__ \ |_
|
||||||
|
# \____/_\__,_|___/\__|
|
||||||
|
# |__ __|
|
||||||
|
# | | ___
|
||||||
|
# | |/ _ \
|
||||||
|
# | | (_) |
|
||||||
|
# __|_|\___/ _
|
||||||
|
# | \/ | | |
|
||||||
|
# | \ / | __ _| | _____
|
||||||
|
# | |\/| |/ _` | |/ / _ \
|
||||||
|
# | | | | (_| | < __/
|
||||||
|
# |_|__|_|\__,_|_|\_\___|
|
||||||
|
# |__ __| |
|
||||||
|
# | | | |__ ___
|
||||||
|
# | | | '_ \ / _ \
|
||||||
|
# | | | | | | __/
|
||||||
|
# __|_|_ |_| |_|\___|
|
||||||
|
# | ____(_) |
|
||||||
|
# | |__ _| | ___
|
||||||
|
# | __| | | |/ _ \
|
||||||
|
# | | | | | __/
|
||||||
|
# |_|__ |_|_|\___|
|
||||||
|
# | _ \(_)
|
||||||
|
# | |_) |_ __ _ __ _ ___ _ __
|
||||||
|
# | _ <| |/ _` |/ _` |/ _ \ '__|
|
||||||
|
# | |_) | | (_| | (_| | __/ |
|
||||||
|
# |____/|_|\__, |\__, |\___|_|
|
||||||
|
# __/ | __/ |
|
||||||
|
# |___/ |___/
|
Loading…
Reference in New Issue