]>
git.ipfire.org Git - people/stevee/pypdns.git/blob - cli.py
3 ###############################################################################
5 # pyPDNS - A PDNS administration tool, written in pure python. #
6 # Copyright (C) 2012 IPFire development team #
8 # This program is free software: you can redistribute it and/or modify #
9 # it under the terms of the GNU General Public License as published by #
10 # the Free Software Foundation, either version 3 of the License, or #
11 # (at your option) any later version. #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
21 ###############################################################################
28 # Initialize DNS module.
29 dns
= backend
.DNS(backend
.DB
)
31 # Create main class for the CLI.
34 self
.parser
= argparse
.ArgumentParser(
35 description
= _("CLI tool to adminstrate authoritative PowerDNS servers."),
38 # Add entry for version displaying.
39 self
.parser
.add_argument('--version', action
='version', version
='pyPDNS 0.1.1')
42 self
.sub_commands
= self
.parser
.add_subparsers()
44 self
.parse_command_show()
45 #self.parse_command_add()
46 #self.parse_command_remove()
48 # Finally parse all arguments from the command line and save them.
49 self
.args
= self
.parser
.parse_args()
51 # Map return values from action to functions to proceed.
53 "show_all" : self
.handle_show_all
,
54 "show_domains" : self
.handle_show_domains
,
55 "show_records" : self
.handle_show_records
,
58 def parse_command_show(self
):
59 # Initialize the "show" command.
60 sub_show
= self
.sub_commands
.add_parser("show",
61 help=_("Show domains and records."))
63 # Initialize subcommands for show.
64 sub_show_subcommands
= sub_show
.add_subparsers()
66 # Impement "show all".
67 sub_show_records
= sub_show_subcommands
.add_parser("all",
68 help="Show all domains and records.")
69 sub_show_records
.add_argument("action", action
="store_const", const
="show_all")
71 # Implement "show records" and pick up given domain.
72 sub_show_records
= sub_show_subcommands
.add_parser("records",
73 help="Show records from a given domain.")
74 sub_show_records
.add_argument("domain", nargs
="?",
75 help="Show records from a given domain.")
76 sub_show_records
.add_argument("action", action
="store_const", const
="show_records")
78 # Implement "show domains".
79 sub_show_domains
= sub_show_subcommands
.add_parser("domains",
80 help="Show all configured domains.")
81 sub_show_domains
.add_argument("action", action
="store_const", const
="show_domains")
84 action
= self
.args
.action
87 func
= self
.action2func
[action
]
89 raise Exception, "Unhandled action: %s" % action
93 def handle_show_domains(self
):
94 # Get all domains and print them
95 domains
= dns
.get_domains()
97 for domain
in domains
:
98 print domain
.id, domain
.name
100 # If there hasn't been any domain configured, print an error message.
102 print "No domain configured, yet."
104 def handle_show_records(self
):
106 # Check if the given domain name really exists.
107 domain
= dns
.get_domain(self
.args
.domain
)
109 # Get all records and print them.
111 print " Showing records for: %s \n" % self
.args
.domain
117 print FORMAT
% ("MName", soa
.mname
)
118 print FORMAT
% ("E-Mail",soa
.email
)
119 print FORMAT
% ("Serial", soa
.serial
)
120 print FORMAT
% ("Refresh", soa
.refresh
)
121 print FORMAT
% ("Retry", soa
.retry
)
122 print FORMAT
% ("Expire", soa
.expire
)
123 print FORMAT
% ("Minimum", soa
.minimum
)
125 print "" #Just an emtpy line.
128 print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n"
130 # Define layout for records table.
131 FORMAT
= " %-24s %-8s %-34s %-10s"
133 # Define tables headline for records and print it.
134 title
= FORMAT
% (_("Name"), _("Type"), _("Content"), _("TTL"))
136 print "=" * len(title
) # spacing line
138 # Display all remaining records, except SOA records.
139 for record
in domain
.records
:
140 if not record
.type == "SOA":
141 print FORMAT
% (record
.dnsname
, record
.type, record
.content
, record
.ttl
)
143 # If there aren't any records yet, print a short notice about that.
145 print " Domain has no records, yet."
147 # If given domain doesn't exist, print an error message.
149 print " Invalid domain. Use 'pdns show domains' to get a list from all available domains."
151 # If no domain name has been specified, also print an error message.
153 print " No domain name specified to show records."
154 print " Use 'pdns show domains' to get a list from all available domains."
156 def handle_show_all(self
):
157 # Get all domains and print them
158 domains
= dns
.get_domains()
160 for domain
in domains
:
166 for record
in domain
.records
:
167 if record
.type == "SOA":
168 FORMAT
= ("%-30s %s")
169 print FORMAT
% (record
.dnsname
, record
.type)
171 FORMAT
= ("%-30s %-6s %s")
172 print FORMAT
% (record
.dnsname
, record
.type, record
.content
)
173 # Print a notice if the domain hasn't any records yet.
175 print "Domain has no records, yet."
180 # If there hasn't been any domain configured, print an error message.
182 print "No domain configured, yet."