Skip to content

Commit

Permalink
run cmd to log from single point, check module is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalekremharmansa committed Oct 28, 2020
1 parent c8eb492 commit 6446b93
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
10 changes: 10 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from microsoftdnsserver.command_runner.powershell_runner import PowerShellRunner
from microsoftdnsserver.dns.dnsserver import DnsServerModule

if __name__ == '__main__':
runner = PowerShellRunner()
dns = DnsServerModule(runner)

# dns.addARecord("zone", "bilalekrem.com", "192.168.100.150", ttl='10s')

dns.addTxtRecord("bilalekrem.com", "bilalekrem.com", "testrecord", ttl='10s')
52 changes: 42 additions & 10 deletions microsoftdnsserver/dns/dnsserver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

from microsoftdnsserver.command_runner.powershell_runner import PowerShellCommand
from .base import DNSService
from ..util.dns_server_utils import formatTtl
from ..util import dns_server_utils

class DnsServerModule(DNSService):
"""
Expand All @@ -12,8 +14,22 @@ class DnsServerModule(DNSService):
def __init__(self, runner):
super().__init__()
self.runner = runner

self.logger = None
pass

def _initLogger(self):
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)

logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

self.logger = logger


def getDNSRecords(self, zone, name=None, recordType=None):
""" uses Get-DnsServerResourceRecord cmdlet to get records in a zone """

Expand All @@ -27,7 +43,7 @@ def getDNSRecords(self, zone, name=None, recordType=None):
args['RRType'] = recordType

command = PowerShellCommand('Get-DnsServerResourceRecord', **args)
result = self.runner.run(command)
self.run(command)


def addARecord(self, zone, name, ip, ttl='1h', ageRecord=False):
Expand All @@ -39,16 +55,17 @@ def addARecord(self, zone, name, ip, ttl='1h', ageRecord=False):
ZoneName=zone,
Name=name,
IPv4Address=ip,
TimeToLive=formatTtl(ttl)
TimeToLive=dns_server_utils.formatTtl(ttl)
)

result = self.runner.run(command)
self.run(command)

def removeARecord(self, zone, name, recordData=None):
""" uses Remove-DnsServerResourceRecord cmdlet to remove a record in a zone """

args = {
'Zone': zone
'ZoneName': zone,
'RRType': 'A'
}
if name:
args['Name'] = name
Expand All @@ -58,7 +75,7 @@ def removeARecord(self, zone, name, recordData=None):
flags = ['Force']

command = PowerShellCommand('Remove-DnsServerResourceRecord', *flags, **args)
result = self.runner.run(command)
self.run(command)

# ---

Expand All @@ -72,16 +89,17 @@ def addTxtRecord(self, zone, name, content, ttl='1h'):
ZoneName=zone,
Name=name,
DescriptiveText=content,
TimeToLive=formatTtl(ttl)
TimeToLive=dns_server_utils.formatTtl(ttl)
)

result = self.runner.run(command)
self.run(command)

def removeARecord(self, zone, name, recordData=None):
def removeTxtRecord(self, zone, name, recordData=None):
""" uses Remove-DnsServerResourceRecord cmdlet to remove txt record in a zone """

args = {
'Zone': zone
'ZoneName': zone,
'RRType': 'Txt'
}
if name:
args['Name'] = name
Expand All @@ -91,4 +109,18 @@ def removeARecord(self, zone, name, recordData=None):
flags = ['Force']

command = PowerShellCommand('Remove-DnsServerResourceRecord', *flags, **args)
self.run(command)

# --

def run(self, command):
result = self.runner.run(command)

if not result.success:
self.logger.error("Command failed [%s]" % command.prepareCommand())

def isDnsServerModuleInstalled(self):
cmdlet = "Get-Module DNSServer -ListAvailable"
result = self.runner.run(cmdlet)

return result.success and len(result.out) > 0

0 comments on commit 6446b93

Please sign in to comment.