]> git.ipfire.org Git - people/stevee/pypdns.git/blob - cli.py
Code rework of the CLI.
[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 \
34 PowerDNS servers."))
35
36 # Add entry for version displaying.
37 self.parser.add_argument('--version', action='version',
38 version='pyPDNS 0.1.1')
39
40 # Add sub-commands.
41 self.sub_commands = self.parser.add_subparsers()
42
43 self.parse_command_show()
44 #self.parse_command_add()
45 #self.parse_command_remove()
46
47 # Finally parse all arguments from the command line and save
48 # them.
49 self.args = self.parser.parse_args()
50
51 # Map return values from action to functions to proceed.
52 self.action2func = {
53 "show_all" : self.handle_show_all,
54 "show_domains" : self.handle_show_domains,
55 "show_records" : self.handle_show_records,
56 }
57
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."))
62
63 # Initialize subcommands for show.
64 sub_show_subcommands = sub_show.add_subparsers()
65
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",
70 const="show_all")
71
72 # Implement "show records" and pick up given domain.
73 sub_show_records = sub_show_subcommands.add_parser("records",
74 help="Show records from a given domain.")
75 sub_show_records.add_argument("domain", nargs="?",
76 help="Show records from a given domain.")
77 sub_show_records.add_argument("action", action="store_const",
78 const="show_records")
79
80 # Implement "show domains".
81 sub_show_domains = sub_show_subcommands.add_parser("domains",
82 help="Show all configured domains.")
83 sub_show_domains.add_argument("action", action="store_const",
84 const="show_domains")
85
86 def run(self):
87 action = self.args.action
88
89 try:
90 func = self.action2func[action]
91
92 except KeyError:
93 raise Exception, "Unhandled action: %s" % action
94
95 else:
96 # Initialize DNS module.
97 self.dns = DNS(DB)
98
99 return func()
100
101 def handle_show_domains(self):
102 """
103 Get all domains and print them.
104 """
105 domains = self.dns.get_domains()
106
107 # Print the domains, if there are any configured.
108 if not domains:
109 print "No domain configured, yet."
110
111 else:
112 print "Currently configured domains:"
113 for domain in domains:
114 print " - %s" % domain.name
115
116
117 def handle_show_records(self):
118 # Print message if domain doens't exist.
119 if not self.args.domain:
120 print " No domain name specified to show \
121 records."
122 print " Use 'pdns show domains' to get a list \
123 from all available domains."
124
125 return
126
127 # Print message if domain doens't exist.
128 domain = self.dns.get_domain(self.args.domain)
129
130 if not domain:
131 print " No such domain, use 'pdns show \
132 domains' to get a list of all \
133 available domains."
134
135 return
136
137 # Print message if no records have been configured yet.
138 if not domain.has_records():
139 print " Domain has no records yet."
140
141 else:
142 print " Showing records \
143 for: %s \n" % self.args.domain
144
145 soa = domain.SOA
146
147 # Check if the domain has a SOA record.
148 if not soa:
149 print " No Start of Authority \
150 (SOA record) created yet. \
151 Your domain will not work \
152 correctly. \n"
153
154 else:
155 # Define table layout for SOA table..
156 FORMAT = " %-8s: %s"
157 print FORMAT % ("MName", soa.mname)
158 print FORMAT % ("E-Mail",soa.email)
159 print FORMAT % ("Serial", soa.serial)
160 print FORMAT % ("Refresh", soa.refresh)
161 print FORMAT % ("Retry", soa.retry)
162 print FORMAT % ("Expire", soa.expire)
163 print FORMAT % ("Minimum", soa.minimum)
164
165 print "" #Just an emtpy line.
166
167 # Define layout for records table.
168 FORMAT = " %-24s %-8s %-34s %-10s"
169
170 # Define tables headline for records
171 # and print it.
172 title = FORMAT % (_("Name"), _("Type"), _("Content"), _("TTL"))
173 print title
174 print "=" * len(title) # spacing line
175
176 # Display all remaining records,
177 # except SOA records.
178 for record in domain.records:
179 if not record.type == "SOA":
180 print FORMAT % (record.dnsname, record.type, record.content, record.ttl)
181
182
183 def handle_show_all(self):
184 # Get all domains and print them
185 domains = self.dns.get_domains()
186
187 # Print message if no domain has been configured.
188 if not domains:
189 print "No domain configured, yet."
190
191 return
192
193 for domain in domains:
194 print domain.name
195
196 # Spacing line.
197 print "=" * 80
198
199 # Print a message if domain has no records.
200 if not domain.has_records():
201 print "Domain has no records yet."
202
203 # Print records.
204 for record in domain.records:
205 if record.type == "SOA":
206 FORMAT = ("%-30s %s")
207 print FORMAT % (record.dnsname, record.type)
208 else:
209 FORMAT = ("%-30s %-6s %s")
210 print FORMAT % (record.dnsname, record.type, record.content)
211
212 # Just an emty line.
213 print ""
214
215
216
217 cli = Cli()
218 cli.run()