Skip to content

Commit

Permalink
Adding source code
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-bednar committed Jan 6, 2016
1 parent fc0979e commit 8bfe271
Show file tree
Hide file tree
Showing 17 changed files with 1,852 additions and 1 deletion.
10 changes: 10 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Lukas Bednar

Contributors include::

Artyom Lukianov
Kobi Hakimi
Martin Pavlik
Meni Yakove
Michal Ovciarik
Ondra Machacek
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# python-rrmngmnt
REmote REsources MaNaGeMeNT
Remote Resources MaNaGeMeNT

This tool helps you manage remote machines and services running on that.
20 changes: 20 additions & 0 deletions rrmngmnt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from rrmngmnt.host import Host
from rrmngmnt.user import (
User,
RootUser,
Domain,
InternalDomain,
ADUser,
)
from rrmngmnt.db import Database


__all__ = [
Host,
User,
RootUser,
Domain,
InternalDomain,
ADUser,
Database,
]
21 changes: 21 additions & 0 deletions rrmngmnt/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import socket


def fqdn2ip(fqdn):
"""
translate fqdn to IP
:param fqdn: host name
:type fqdn: string
:return: IP
:rtype: string
"""
try:
return socket.gethostbyname(fqdn)
except (socket.gaierror, socket.herror) as ex:
args = list(ex.args)
message = "%s: %s" % (fqdn, args[1])
args[1] = message
ex.strerror = message
ex.args = tuple(args)
raise
54 changes: 54 additions & 0 deletions rrmngmnt/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from rrmngmnt.service import Service


class Database(Service):

def __init__(self, host, name, user):
"""
:param host: Remote resouce to DB machine
:type host: instance of Host
:param name: database name
:type name: str
:param user: user/role
:type user: instance of User
"""
super(Database, self).__init__(host)
self.name = name
self.user = user

def psql(self, sql, *args):
"""
Execute psql command on host
:param sql: sql command
:type sql: string
:param args: positional format arguments for command
:type args: list of arguments
:return: list of lines with records
:rtype: list(list(string, string, ...))
"""
separator = '__RECORD_SEPARATOR__'
sql = sql % tuple(args)
cmd = [
'export', 'PGPASSWORD=%s;' % self.user.password,
'psql', '-d', self.name, '-U', self.user.name, '-h', 'localhost',
'-R', separator, '-t', '-A', '-c', sql,
]

executor = self.host.executor()
with executor.session() as ss:
rc, out, err = ss.run_cmd(cmd)
if rc:
raise Exception(
"Failed to exec sql command: %s" % err
)
return [
a.strip().split('|') for a in out.strip().split(separator)
if a.strip()
]
# NOTE: I am considering to use Psycopg2 to access DB directly.
# I need to think whether it is better or not.
# We need to realize that connection can be forbiden from outside ...

def restart(self):
self.host.service('postgresql').restart()
32 changes: 32 additions & 0 deletions rrmngmnt/filesystem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from rrmngmnt.service import Service


class FileSystem(Service):
"""
Class for working with filesystem.
It has same interface as 'os' module.
"""
def _exec_file_test(self, op, path):
return self.host.executor().run_cmd(
['[', '-%s' % op, path, ']']
)[0] == 0

def exists(self, path):
return self._exec_file_test('e', path)

def isfile(self, path):
return self._exec_file_test('f', path)

def isdir(self, path):
return self._exec_file_test('d', path)

def remove(self, path):
return self.host.executor().run_cmd(
['rm', '-f', path]
)[0] == 0
unlink = remove

def rmdir(self, path):
return self.host.executor().run_cmd(
['rm', '-rf', path]
)[0] == 0
Loading

0 comments on commit 8bfe271

Please sign in to comment.