]> git.ipfire.org Git - people/stevee/pypdns.git/blob - cli.py
8bd1cf4a1d7b2dd456764808a5935be9d52e9aaa
[people/stevee/pypdns.git] / cli.py
1 #!/usr/bin/python
2 #
3 ###############################################################################
4 # #
5 # pyPDNS - A PDNS administration tool, written in pure python. #
6 # Copyright (C) 2012 IPFire development team #
7 # #
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. #
12 # #
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. #
17 # #
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/>. #
20 # #
21 ###############################################################################
22
23 import backend
24 import argparse
25
26 from i18n import _
27 from backend import *
28
29 # Create main class for the CLI.
30 class Cli(object):
31 def __init__(self):
32 self.parser = argparse.ArgumentParser(
33 description = _("CLI tool to adminstrate authoritative PowerDNS servers."),
34 )
35
36 # Add entry for version displaying.
37 self.parser.add_argument('--version', action='version', version='pyPDNS 0.1.1')
38
39 # Add sub-commands.
40 self.sub_commands = self.parser.add_subparsers()
41
42 self.parse_command_show()
43 #self.parse_command_add()
44 #self.parse_command_remove()
45
46 # Finally parse all arguments from the command line and save them.
47 self.args = self.parser.parse_args()
48
49 # Map return values from action to functions to proceed.
50 self.action2func = {
51 "show_all" : self.handle_show_all,
52 "show_domains" : self.handle_show_domains,
53 "show_records" : self.handle_show_records,
54 }
55
56 def parse_command_show(self):
57 # Initialize the "show" command.
58 sub_show = self.sub_commands.add_parser("show",
59 help=_("Show domains and records."))
60
61 # Initialize subcommands for show.
62 sub_show_subcommands = sub_show.add_subparsers()
63
64 # Impement "show all".
65 sub_show_records = sub_show_subcommands.add_parser("all",
66 help="Show all domains and records.")
67 sub_show_records.add_argument("action", action="store_const", const="show_all")
68
69 # Implement "show records" and pick up given domain.
70 sub_show_records = sub_show_subcommands.add_parser("records",
71 help="Show records from a given domain.")
72 sub_show_records.add_argument("domain", nargs="?",
73 help="Show records from a given domain.")
74 sub_show_records.add_argument("action", action="store_const", const="show_records")
75
76 # Implement "show domains".
77 sub_show_domains = sub_show_subcommands.add_parser("domains",
78 help="Show all configured domains.")
79 sub_show_domains.add_argument("action", action="store_const", const="show_domains")
80
81 def run(self):
82 action = self.args.action
83
84 try:
85 func = self.action2func[action]
86
87 except KeyError:
88 raise Exception, "Unhandled action: %s" % action
89
90 else:
91 # Initialize DNS module.
92 self.dns = DNS(DB)
93
94 return func()
95
96 def handle_show_domains(self):
97 # Get all domains and print them
98 domains = self.dns.get_domains()
99 if domains:
100 for domain in domains:
101 print domain.id, domain.name
102
103 # If there hasn't been any domain configured, print an error message.
104 else:
105 print "No domain configured, yet."
106
107 def handle_show_records(self):
108 if self.args.domain:
109 # Check if the given domain name really exists.
110 domain = self.dns.get_domain(self.args.domain)
111 if domain:
112 # Get all records and print them.
113 if domain.records:
114 print " Showing records for: %s \n" % self.args.domain
115
116 soa = domain.SOA
117
118 if soa:
119 FORMAT = " %-8s: %s"
120 print FORMAT % ("MName", soa.mname)
121 print FORMAT % ("E-Mail",soa.email)
122 print FORMAT % ("Serial", soa.serial)
123 print FORMAT % ("Refresh", soa.refresh)
124 print FORMAT % ("Retry", soa.retry)
125 print FORMAT % ("Expire", soa.expire)
126 print FORMAT % ("Minimum", soa.minimum)
127
128 print "" #Just an emtpy line.
129
130 else:
131 print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n"
132
133 # Define layout for records table.
134 FORMAT = " %-24s %-8s %-34s %-10s"
135
136 # Define tables headline for records and print it.
137 title = FORMAT % (_("Name"), _("Type"), _("Content"), _("TTL"))
138 print title
139 print "=" * len(title) # spacing line
140
141 # Display all remaining records, except SOA records.
142 for record in domain.records:
143 if not record.type == "SOA":
144 print FORMAT % (record.dnsname, record.type, record.content, record.ttl)
145
146 # If there aren't any records yet, print a short notice about that.
147 else:
148 print " Domain has no records, yet."
149
150 # If given domain doesn't exist, print an error message.
151 else:
152 print " Invalid domain. Use 'pdns show domains' to get a list from all available domains."
153
154 # If no domain name has been specified, also print an error message.
155 else:
156 print " No domain name specified to show records."
157 print " Use 'pdns show domains' to get a list from all available domains."
158
159 def handle_show_all(self):
160 # Get all domains and print them
161 domains = self.dns.get_domains()
162 if domains:
163 for domain in domains:
164 print domain.name
165 print "=" * 80
166
167 # Print all records.
168 if domain.records:
169 for record in domain.records:
170 if record.type == "SOA":
171 FORMAT = ("%-30s %s")
172 print FORMAT % (record.dnsname, record.type)
173 else:
174 FORMAT = ("%-30s %-6s %s")
175 print FORMAT % (record.dnsname, record.type, record.content)
176 # Print a notice if the domain hasn't any records yet.
177 else:
178 print "Domain has no records, yet."
179
180 # Just an emty line.
181 print ""
182
183 # If there hasn't been any domain configured, print an error message.
184 else:
185 print "No domain configured, yet."
186
187
188
189
190 cli = Cli()
191 cli.run()