]>
Commit | Line | Data |
---|---|---|
a10ce0b8 SS |
1 | #!/usr/bin/python |
2 | # | |
3 | ############################################################################### | |
4 | # # | |
5 | # pyPDNS - A PDNS administration tool, written in pure python. # | |
9deb9e05 | 6 | # Copyright (C) 2012 IPFire development team # |
a10ce0b8 SS |
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 | ||
28 | # Initialize DNS module. | |
29 | dns = backend.DNS(backend.DB) | |
30 | ||
31 | # Create main class for the CLI. | |
32 | class Cli(object): | |
33 | def __init__(self): | |
34 | self.parser = argparse.ArgumentParser( | |
9deb9e05 | 35 | description = _("CLI tool to adminstrate authoritative PowerDNS servers."), |
a10ce0b8 SS |
36 | ) |
37 | ||
38 | # Add entry for version displaying. | |
9deb9e05 | 39 | self.parser.add_argument('--version', action='version', version='pyPDNS 0.1.1') |
a10ce0b8 SS |
40 | |
41 | # Add sub-commands. | |
42 | self.sub_commands = self.parser.add_subparsers() | |
43 | ||
44 | self.parse_command_show() | |
45 | #self.parse_command_add() | |
46 | #self.parse_command_remove() | |
47 | ||
48 | # Finally parse all arguments from the command line and save 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", const="show_all") | |
70 | ||
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") | |
77 | ||
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") | |
82 | ||
83 | def run(self): | |
84 | action = self.args.action | |
85 | ||
86 | try: | |
87 | func = self.action2func[action] | |
88 | except KeyError: | |
89 | raise Exception, "Unhandled action: %s" % action | |
90 | ||
91 | return func() | |
92 | ||
93 | def handle_show_domains(self): | |
94 | # Get all domains and print them | |
95 | domains = dns.get_domains() | |
96 | if domains: | |
97 | for domain in domains: | |
98 | print domain.id, domain.name | |
99 | ||
100 | # If there hasn't been any domain configured, print an error message. | |
101 | else: | |
102 | print "No domain configured, yet." | |
103 | ||
104 | def handle_show_records(self): | |
105 | if self.args.domain: | |
106 | # Check if the given domain name really exists. | |
107 | domain = dns.get_domain(self.args.domain) | |
108 | if domain: | |
109 | # Get all records and print them. | |
110 | if domain.records: | |
111 | print " Showing records for: %s \n" % self.args.domain | |
112 | ||
113 | soa = domain.SOA | |
114 | ||
115 | if soa: | |
116 | FORMAT = " %-8s: %s" | |
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) | |
124 | ||
125 | print "" #Just an emtpy line. | |
126 | ||
127 | else: | |
128 | print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n" | |
129 | ||
130 | # Define layout for records table. | |
131 | FORMAT = " %-24s %-8s %-34s %-10s" | |
132 | ||
133 | # Define tables headline for records and print it. | |
134 | title = FORMAT % (_("Name"), _("Type"), _("Content"), _("TTL")) | |
135 | print title | |
136 | print "=" * len(title) # spacing line | |
137 | ||
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) | |
142 | ||
143 | # If there aren't any records yet, print a short notice about that. | |
144 | else: | |
145 | print " Domain has no records, yet." | |
146 | ||
147 | # If given domain doesn't exist, print an error message. | |
148 | else: | |
149 | print " Invalid domain. Use 'pdns show domains' to get a list from all available domains." | |
150 | ||
151 | # If no domain name has been specified, also print an error message. | |
152 | else: | |
153 | print " No domain name specified to show records." | |
154 | print " Use 'pdns show domains' to get a list from all available domains." | |
155 | ||
156 | def handle_show_all(self): | |
157 | # Get all domains and print them | |
158 | domains = dns.get_domains() | |
159 | if domains: | |
160 | for domain in domains: | |
161 | print domain.name | |
162 | print "=" * 80 | |
163 | ||
164 | # Print all records. | |
165 | if domain.records: | |
166 | for record in domain.records: | |
167 | if record.type == "SOA": | |
168 | FORMAT = ("%-30s %s") | |
169 | print FORMAT % (record.dnsname, record.type) | |
170 | else: | |
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. | |
174 | else: | |
175 | print "Domain has no records, yet." | |
176 | ||
177 | # Just an emty line. | |
178 | print "" | |
179 | ||
180 | # If there hasn't been any domain configured, print an error message. | |
181 | else: | |
182 | print "No domain configured, yet." | |
183 | ||
184 | ||
185 | ||
186 | ||
187 | cli = Cli() | |
188 | cli.run() |