Skip to content

Commit

Permalink
Merge branch 'master' into setup.py
Browse files Browse the repository at this point in the history
  • Loading branch information
emcek committed Oct 15, 2018
2 parents 052fe32 + 5452e07 commit 6fe5854
Show file tree
Hide file tree
Showing 123 changed files with 2,032 additions and 404 deletions.
340 changes: 311 additions & 29 deletions README.md

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions examples/command/my_devices.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
LOGGER:
PATH: ./logs
RAW_LOG: True
DEBUG_LEVEL: TRACE
DATE_FORMAT: "%d %H:%M:%S"

DEVICES:
DEFAULT_CONNECTION:
CONNECTION_DESC:
io_type: terminal
variant: threaded

MyMachine:
DEVICE_CLASS: moler.device.unixremote.UnixLocal

RebexTestMachine:
DEVICE_CLASS: moler.device.unixremote.UnixRemote
STATE_PARAMS: # modify defaults
UNIX_LOCAL:
newline: \n # default (may be skipped)
UNIX_REMOTE:
newline: \r\n
CONNECTION_HOPS:
UNIX_LOCAL: # from state
UNIX_REMOTE: # to state
execute_command: ssh # via command
command_params: # with params
expected_prompt: demo@
host: test.rebex.net
login: demo
password: password
set_timeout: False # remote doesn't support: export TMOUT


31 changes: 31 additions & 0 deletions examples/command/unix_ls_on_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from moler.config import load_config
from moler.device.device import DeviceFactory

load_config(path='my_devices.yml')

remote_unix = DeviceFactory.get_device(name='RebexTestMachine') # it starts in local shell
remote_unix.goto_state(state="UNIX_REMOTE") # make it go to remote shell

ls_cmd = remote_unix.get_cmd(cmd_name="ls", cmd_params={"options": "-l"})
ls_cmd.connection.newline = '\r\n' # tweak since rebex remote console uses such one

remote_files = ls_cmd()

if 'readme.txt' in remote_files['files']:
print("readme.txt file:")
readme_file_info = remote_files['files']['readme.txt']
for attr in readme_file_info:
print(" {:<18}: {}".format(attr, readme_file_info[attr]))

# result:
"""
readme.txt file:
permissions : -rw-------
hard_links_count : 1
owner : demo
group : users
size_raw : 403
size_bytes : 403
date : Apr 08 2014
name : readme.txt
"""
25 changes: 25 additions & 0 deletions examples/command/unix_ping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import time
from moler.cmd.unix.ping import Ping
from moler.connection import get_connection

host = 'www.google.com'
terminal = get_connection(io_type='terminal', variant='threaded')
with terminal:
ping_cmd = Ping(connection=terminal.moler_connection,
destination=host, options="-w 6")
print("Start pinging {} ...".format(host))
ping_cmd.start()
print("Doing other stuff while pinging {} ...".format(host))
time.sleep(3)
ping_stats = ping_cmd.await_done(timeout=4)
print("ping {}: {}={}, {}={} [{}]".format(host,'packet_loss',
ping_stats['packet_loss'],
'time_avg',
ping_stats['time_avg'],
ping_stats['time_unit']))
# result:
"""
Start pinging www.google.com ...
Doing other stuff while pinging www.google.com ...
ping www.google.com: packet_loss=0, time_avg=50.000 [ms]
"""
36 changes: 36 additions & 0 deletions examples/command/unix_ping_and_ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from moler.config import load_config
from moler.device.device import DeviceFactory

load_config(path='my_devices.yml')

my_unix = DeviceFactory.get_device(name='MyMachine')
host = 'www.google.com'
ping_cmd = my_unix.get_cmd(cmd_name="ping", cmd_params={"destination": host, "options": "-w 6"})

remote_unix = DeviceFactory.get_device(name='RebexTestMachine')
remote_unix.goto_state(state="UNIX_REMOTE")
ls_cmd = remote_unix.get_cmd(cmd_name="ls", cmd_params={"options": "-l"})
ls_cmd.connection.newline = '\r\n' # tweak since rebex remote console uses such one

print("Start pinging {} ...".format(host))
ping_cmd.start() # run command in background
print("Let's check readme.txt at {} while pinging {} ...".format(remote_unix.name, host))

remote_files = ls_cmd() # foreground "run in the meantime"
file_info = remote_files['files']['readme.txt']
print("readme.txt file: owner={fi[owner]}, size={fi[size_bytes]}".format(fi=file_info))

ping_stats = ping_cmd.await_done(timeout=6) # await background command
print("ping {}: {}={}, {}={} [{}]".format(host,'packet_loss',
ping_stats['packet_loss'],
'time_avg',
ping_stats['time_avg'],
ping_stats['time_unit']))

# result:
"""
Start pinging www.google.com ...
Let's check readme.txt at RebexTestMachine while pinging www.google.com ...
readme.txt file: owner=demo, size=403
ping www.google.com: packet_loss=0, time_avg=49.686 [ms]
"""
25 changes: 25 additions & 0 deletions examples/command/unix_ps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from moler.cmd.unix.ps import Ps
from moler.connection import ObservableConnection, get_connection
from moler.io.raw.terminal import ThreadedTerminal

# v.1 - combine all manually
# moler_conn = ObservableConnection()
# terminal = ThreadedTerminal(moler_connection=moler_conn)
# v.2 - let factory combine
# terminal = get_connection(io_type='terminal', variant='threaded')
# v.3 - let factory select default variant
terminal = get_connection(io_type='terminal')
terminal.open()
ps_cmd = Ps(connection=terminal.moler_connection, options="-ef")

processes = ps_cmd()
for proc in processes:
if 'python' in proc['CMD']:
print("PID: {} CMD: {}".format(proc['PID'], proc['CMD']))
terminal.close()

# result:
"""
PID: 1817 CMD: /usr/bin/python /usr/share/system-config-printer/applet.py
PID: 21825 CMD: /usr/bin/python /home/gl/moler/examples/command/unix_ps.py
"""
17 changes: 17 additions & 0 deletions examples/command/unix_ps_on_device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from moler.config import load_config
from moler.device.device import DeviceFactory

load_config(path='my_devices.yml') # description of available devices
my_unix = DeviceFactory.get_device(name='MyMachine') # take specific device out of available ones
ps_cmd = my_unix.get_cmd(cmd_name="ps", # take command of that device
cmd_params={"options": "-ef"})

processes_info = ps_cmd() # run the command, it returns result
for proc_info in processes_info:
if 'python' in proc_info['CMD']:
print("PID: {info[PID]} CMD: {info[CMD]}".format(info=proc_info))

"""
PID: 1817 CMD: /usr/bin/python /usr/share/system-config-printer/applet.py
PID: 21825 CMD: /usr/bin/python /home/gl/moler/examples/command/unix_ps.py
"""
16 changes: 8 additions & 8 deletions moler/cmd/commandtextualgeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

class CommandTextualGeneric(Command):
_re_default_prompt = re.compile(r'^[^<]*[\$|%|#|>|~]\s*$') # When user provides no prompt
_default_new_line_chars = ("\n", "\r") # New line chars on device, not system with script!
_default_newline_chars = ("\n", "\r") # New line chars on device, not system with script!

def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
"""
:param connection: connection to device
:param prompt: expected prompt sending by device after command execution. Maybe String or compiled re
:param new_line_chars: new line chars on device
:param newline_chars: new line chars on device
"""
super(CommandTextualGeneric, self).__init__(connection=connection, runner=runner)
self.__command_string = None # String representing command on device
Expand All @@ -37,9 +37,9 @@ def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
self.break_on_timeout = True # If True then Ctrl+c on timeout
self._last_not_full_line = None # Part of line
self._re_prompt = CommandTextualGeneric._calculate_prompt(prompt) # Expected prompt on device
self._new_line_chars = new_line_chars # New line characters on device
if not self._new_line_chars:
self._new_line_chars = CommandTextualGeneric._default_new_line_chars
self._newline_chars = newline_chars # New line characters on device
if not self._newline_chars:
self._newline_chars = CommandTextualGeneric._default_newline_chars

@property
def command_string(self):
Expand Down Expand Up @@ -67,7 +67,7 @@ def has_endline_char(self, line):
:param line: String to check
:return: True if any new line char was found, False otherwise
"""
if line.endswith(self._new_line_chars):
if line.endswith(self._newline_chars):
return True
return False

Expand Down Expand Up @@ -125,7 +125,7 @@ def _strip_new_lines_chars(self, line):
:param line: line from device
:return: line without new lines chars
"""
for char in self._new_line_chars:
for char in self._newline_chars:
line = line.rstrip(char)
return line

Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class Bash(GenericUnixCommand):

def __init__(self, connection, prompt=None, new_line_chars=None, runner=None, bash="TERM=xterm-mono bash"):
super(Bash, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, prompt=None, newline_chars=None, runner=None, bash="TERM=xterm-mono bash"):
super(Bash, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.bash = bash
self.ret_required = False

Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Cat(GenericUnixCommand):
def __init__(self, connection, path, options=None, prompt=None, new_line_chars=None, runner=None):
super(Cat, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, path, options=None, prompt=None, newline_chars=None, runner=None):
super(Cat, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.path = path
self.options = options
self.current_ret["LINES"] = []
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

class Cd(GenericUnixCommand):

def __init__(self, connection, path=None, prompt=None, new_line_chars=None, runner=None):
super(Cd, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, path=None, prompt=None, newline_chars=None, runner=None):
super(Cd, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)

# Parameters defined by calling the command
self.path = path
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/chgrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@


class Chgrp(GenericUnixCommand):
def __init__(self, connection, files, group, options=None, prompt=None, new_line_chars=None, runner=None):
super(Chgrp, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, files, group, options=None, prompt=None, newline_chars=None, runner=None):
super(Chgrp, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.options = options
self.files = files
self.group = group
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/chmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Chmod(GenericUnixCommand):
def __init__(self, connection, permission, filename, options=None, prompt=None, new_line_chars=None, runner=None):
super(Chmod, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, permission, filename, options=None, prompt=None, newline_chars=None, runner=None):
super(Chmod, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.permission = permission
self.filename = filename
self.options = options
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/chown.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Chown(GenericUnixCommand):
def __init__(self, connection, param, filename, options=None, prompt=None, new_line_chars=None, runner=None):
super(Chown, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, param, filename, options=None, prompt=None, newline_chars=None, runner=None):
super(Chown, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.param = param
self.filename = filename
self.options = options
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Cp(GenericUnixCommand):
def __init__(self, connection, src, dst, options=None, prompt=None, new_line_chars=None, runner=None):
super(Cp, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, src, dst, options=None, prompt=None, newline_chars=None, runner=None):
super(Cp, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)

self.src = src
self.dst = dst
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@


class Date(GenericUnixCommand):
def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
super(Date, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
super(Date, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)

# Parameters defined by calling the command

Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/df.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

class Df(GenericUnixCommand):

def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
super(Df, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
super(Df, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self._converter_helper = ConverterHelper()

def build_command_string(self):
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/dmesg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


class Dmesg(GenericUnixCommand):
def __init__(self, connection, options=None, prompt=None, new_line_chars=None, runner=None):
super(Dmesg, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, options=None, prompt=None, newline_chars=None, runner=None):
super(Dmesg, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.options = options
self.current_ret["LINES"] = []

Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class Echo(GenericUnixCommand):
def __init__(self, connection, options=None, text=None, write_mode=">", output_file=None, prompt=None,
new_line_chars=None, runner=None):
super(Echo, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
newline_chars=None, runner=None):
super(Echo, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)

self.options = options
self.text = text
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/enter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class Enter(GenericUnixCommand):

def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
super(Enter, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
super(Enter, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.ret_required = False

def build_command_string(self):
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

class Env(GenericUnixCommand):

def __init__(self, connection, prompt=None, new_line_chars=None, runner=None):
super(Env, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
def __init__(self, connection, prompt=None, newline_chars=None, runner=None):
super(Env, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.ret_required = True

def build_command_string(self):
Expand Down
4 changes: 2 additions & 2 deletions moler/cmd/unix/ethtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

class Ethtool(GenericUnixCommand):

def __init__(self, connection, interface, prompt=None, new_line_chars=None, options=None, runner=None):
super(Ethtool, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars,
def __init__(self, connection, interface, prompt=None, newline_chars=None, options=None, runner=None):
super(Ethtool, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars,
runner=runner)
self.interface = interface
self.options = options
Expand Down
7 changes: 4 additions & 3 deletions moler/cmd/unix/exit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@


class Exit(GenericUnixCommand):
def __init__(self, connection, prompt=None, expected_prompt='>', new_line_chars=None, runner=None):
def __init__(self, connection, prompt=None, expected_prompt='>', newline_chars=None, runner=None, target_newline="\n"):
"""
:param connection:
:param prompt: Prompt of the starting shell
:param expected_prompt: Prompt of the target shell reached after exit command
:param new_line_chars:
:param newline_chars:
"""
super(Exit, self).__init__(connection=connection, prompt=prompt, new_line_chars=new_line_chars, runner=runner)
super(Exit, self).__init__(connection=connection, prompt=prompt, newline_chars=newline_chars, runner=runner)
self.ret_required = False
self.target_newline = target_newline

# Parameters defined by calling the command
self._re_expected_prompt = CommandTextualGeneric._calculate_prompt(expected_prompt) # Expected prompt on device
Expand Down
Loading

0 comments on commit 6fe5854

Please sign in to comment.