]>
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 ###############################################################################
29 # Create main class for the CLI.
32 self
.parser
= argparse
.ArgumentParser(
33 description
= _("CLI tool to adminstrate authoritative \
36 # Add entry for version displaying.
37 self
.parser
.add_argument('--version', action
='version',
38 version
='pyPDNS 0.1.1')
41 self
.sub_commands
= self
.parser
.add_subparsers()
43 self
.parse_command_show()
44 #self.parse_command_add()
45 #self.parse_command_remove()
47 # Finally parse all arguments from the command line and save
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",
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",
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",
87 action
= self
.args
.action
90 func
= self
.action2func
[action
]
93 raise Exception, "Unhandled action: %s" % action
96 # Initialize DNS module.
101 def handle_show_domains(self
):
103 Get all domains and print them.
105 domains
= self
.dns
.get_domains()
107 # Print the domains, if there are any configured.
109 print "No domain configured, yet."
112 print "Currently configured domains:"
113 for domain
in domains
:
114 print " - %s" % domain
.name
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 \
122 print " Use 'pdns show domains' to get a list \
123 from all available domains."
127 # Print message if domain doens't exist.
128 domain
= self
.dns
.get_domain(self
.args
.domain
)
131 print " No such domain, use 'pdns show \
132 domains' to get a list of all \
137 # Print message if no records have been configured yet.
138 if not domain
.has_records():
139 print " Domain has no records yet."
142 print " Showing records \
143 for: %s \n" % self
.args
.domain
147 # Check if the domain has a SOA record.
149 print " No Start of Authority \
150 (SOA record) created yet. \
151 Your domain will not work \
155 # Define table layout for SOA table..
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
)
165 print "" #Just an emtpy line.
167 # Define layout for records table.
168 FORMAT
= " %-24s %-8s %-34s %-10s"
170 # Define tables headline for records
172 title
= FORMAT
% (_("Name"), _("Type"), _("Content"), _("TTL"))
174 print "=" * len(title
) # spacing line
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
)
183 def handle_show_all(self
):
184 # Get all domains and print them
185 domains
= self
.dns
.get_domains()
187 # Print message if no domain has been configured.
189 print "No domain configured, yet."
193 for domain
in domains
:
199 # Print a message if domain has no records.
200 if not domain
.has_records():
201 print "Domain has no records yet."
204 for record
in domain
.records
:
205 if record
.type == "SOA":
206 FORMAT
= ("%-30s %s")
207 print FORMAT
% (record
.dnsname
, record
.type)
209 FORMAT
= ("%-30s %-6s %s")
210 print FORMAT
% (record
.dnsname
, record
.type, record
.content
)