]>
git.ipfire.org Git - people/stevee/pypdns.git/blob - cli.py
8bd1cf4a1d7b2dd456764808a5935be9d52e9aaa
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 PowerDNS servers."),
36 # Add entry for version displaying.
37 self
.parser
.add_argument('--version', action
='version', version
='pyPDNS 0.1.1')
40 self
.sub_commands
= self
.parser
.add_subparsers()
42 self
.parse_command_show()
43 #self.parse_command_add()
44 #self.parse_command_remove()
46 # Finally parse all arguments from the command line and save them.
47 self
.args
= self
.parser
.parse_args()
49 # Map return values from action to functions to proceed.
51 "show_all" : self
.handle_show_all
,
52 "show_domains" : self
.handle_show_domains
,
53 "show_records" : self
.handle_show_records
,
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."))
61 # Initialize subcommands for show.
62 sub_show_subcommands
= sub_show
.add_subparsers()
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")
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")
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")
82 action
= self
.args
.action
85 func
= self
.action2func
[action
]
88 raise Exception, "Unhandled action: %s" % action
91 # Initialize DNS module.
96 def handle_show_domains(self
):
97 # Get all domains and print them
98 domains
= self
.dns
.get_domains()
100 for domain
in domains
:
101 print domain
.id, domain
.name
103 # If there hasn't been any domain configured, print an error message.
105 print "No domain configured, yet."
107 def handle_show_records(self
):
109 # Check if the given domain name really exists.
110 domain
= self
.dns
.get_domain(self
.args
.domain
)
112 # Get all records and print them.
114 print " Showing records for: %s \n" % self
.args
.domain
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
)
128 print "" #Just an emtpy line.
131 print " No Start of Authority (SOA record) created yet. Your domain will not work correctly. \n"
133 # Define layout for records table.
134 FORMAT
= " %-24s %-8s %-34s %-10s"
136 # Define tables headline for records and print it.
137 title
= FORMAT
% (_("Name"), _("Type"), _("Content"), _("TTL"))
139 print "=" * len(title
) # spacing line
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
)
146 # If there aren't any records yet, print a short notice about that.
148 print " Domain has no records, yet."
150 # If given domain doesn't exist, print an error message.
152 print " Invalid domain. Use 'pdns show domains' to get a list from all available domains."
154 # If no domain name has been specified, also print an error message.
156 print " No domain name specified to show records."
157 print " Use 'pdns show domains' to get a list from all available domains."
159 def handle_show_all(self
):
160 # Get all domains and print them
161 domains
= self
.dns
.get_domains()
163 for domain
in domains
:
169 for record
in domain
.records
:
170 if record
.type == "SOA":
171 FORMAT
= ("%-30s %s")
172 print FORMAT
% (record
.dnsname
, record
.type)
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.
178 print "Domain has no records, yet."
183 # If there hasn't been any domain configured, print an error message.
185 print "No domain configured, yet."