git push origin masterMerge branch 'w0uld-argparse'

This commit is contained in:
James Kasten
2014-11-26 20:49:54 -08:00
5 changed files with 122 additions and 111 deletions
+29 -14
View File
@@ -29,21 +29,36 @@ conf layout.
## Command line usage ## Command line usage
``` ```
sudo ./letsencrypt.py (default authentication mode using pythondialog) options usage: sudo letsencrypt.py [-h] [-d DOMAIN [DOMAIN ...]] [-s SERVER] [-p PRIVKEY]
[-c CSR] [-b ROLLBACK] [-k] [-v] [-r] [-n] [-e] [-t]
[--test]
--text (text mode) An ACME client that can update Apache configurations.
--privkey= (specify privatekey file to use to generate the certificate)
--csr= (Use a specific CSR. If this is specified, privkey must also be specified optional arguments:
with the correct private key for the CSR) -h, --help show this help message and exit
--server (list the ACME CA server address) -d DOMAIN [DOMAIN ...], --domains DOMAIN [DOMAIN ...]
--revoke (revoke a certificate) -s SERVER, --server SERVER
--view-checkpoints (Used to view available checkpoints and see what configuration The ACME CA server address.
changes have been made) -p PRIVKEY, --privkey PRIVKEY
--rollback=X (Revert the configuration X number of checkpoints) Path to the private key file for certificate
--redirect (Automatically redirect all HTTP traffic to HTTPS for the newly generation.
authenticated vhost) -c CSR, --csr CSR Path to the certificate signing request file
--no-redirect (Skip the HTTPS redirect question, allowing both HTTP and HTTPS) corresponding to the private key file. The private key
--agree-eula (Skip the end user agreement screen) file argument is required if this argument is
specified.
-b ROLLBACK, --rollback ROLLBACK
Revert configuration <ROLLBACK> number of checkpoints.
-k, --revoke Revoke a certificate.
-v, --view-checkpoints
View checkpoints and associated configuration changes.
-r, --redirect Automatically redirect all HTTP traffic to HTTPS for
the newly authenticated vhost.
-n, --no-redirect Skip the HTTPS redirect question, allowing both HTTP
and HTTPS.
-e, --agree-eula Skip the end user license agreement screen.
-t, --text Use the text output instead of the curses UI.
--test Run in test mode.
``` ```
## More Information ## More Information
+2 -2
View File
@@ -45,8 +45,8 @@ class Client(object):
self.server = ca_server self.server = ca_server
self.csr_file = cert_signing_request self.csr_file = cert_signing_request.name
self.key_file = private_key self.key_file = private_key.name
# TODO: Figure out all exceptions from this function # TODO: Figure out all exceptions from this function
try: try:
+89 -95
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
"""Parse command line and call the appropriate functions.""" """Parse command line and call the appropriate functions."""
import getopt import argparse
import os import os
import sys import sys
@@ -10,116 +10,110 @@ from letsencrypt.client import client
from letsencrypt.client import display from letsencrypt.client import display
from letsencrypt.client import logger from letsencrypt.client import logger
logger.setLogger(logger.FileLogger(sys.stdout))
logger.setLogLevel(logger.INFO)
def main(): def main():
# Check to make sure user is root """Command line argument parsing and main script execution."""
if not os.geteuid() == 0: if not os.geteuid() == 0:
sys.exit("\nOnly root can run letsencrypt.\n") sys.exit(
# Parse options "{0}Root is required to run letsencrypt. Please use sudo.{0}"
try: .format(os.linesep))
opts, args = getopt.getopt(sys.argv[1:], "", ["text", "test",
"view-checkpoints",
"privkey=", "csr=",
"server=", "rollback=",
"revoke", "agree-eula",
"redirect",
"no-redirect",
"help"])
except getopt.GetoptError as err:
# print help info and exit
print str(err)
usage()
sys.exit(2)
server = None parser = argparse.ArgumentParser(
csr = None description="An ACME client that can update Apache configurations.")
privkey = None
curses = True
names = args
flag_revoke = False
redirect = None
eula = False
for o, a in opts: parser.add_argument("-d", "--domains", dest="domains", metavar="DOMAIN",
if o == "--text": nargs="+")
curses = False parser.add_argument("-s", "--server", dest="server",
elif o == "--csr": help="The ACME CA server address.")
csr = a parser.add_argument("-p", "--privkey", dest="privkey", type=file,
elif o == "--privkey": help="Path to the private key file for certificate "
privkey = a "generation.")
elif o == "--server": parser.add_argument("-c", "--csr", dest="csr", type=file,
server = a help="Path to the certificate signing request file "
elif o == "--rollback": "corresponding to the private key file. The "
logger.setLogger(logger.FileLogger(sys.stdout)) "private key file argument is required if this "
logger.setLogLevel(logger.INFO) "argument is specified.")
config = apache_configurator.ApacheConfigurator() parser.add_argument("-b", "--rollback", dest="rollback", type=int,
config.rollback_checkpoints(a) default=0,
config.restart() help="Revert configuration <ROLLBACK> number of "
sys.exit(0) "checkpoints.")
elif o == "--view-checkpoints": parser.add_argument("-k", "--revoke", dest="revoke", action="store_true",
logger.setLogger(logger.FileLogger(sys.stdout)) help="Revoke a certificate.")
logger.setLogLevel(logger.INFO) parser.add_argument("-v", "--view-checkpoints", dest="view_checkpoints",
config = apache_configurator.ApacheConfigurator() action="store_true",
config.display_checkpoints() help="View checkpoints and associated configuration "
sys.exit(0) "changes.")
elif o == "--revoke": parser.add_argument("-r", "--redirect", dest="redirect",
# Do Stuff action="store_const", const=True,
flag_revoke = True help="Automatically redirect all HTTP traffic to HTTPS "
elif o == "--redirect": "for the newly authenticated vhost.")
redirect = True parser.add_argument("-n", "--no-redirect", dest="redirect",
elif o == "--no-redirect": action="store_const", const=False,
redirect = False help="Skip the HTTPS redirect question, allowing both "
elif o == "--agree-eula": "HTTP and HTTPS.")
eula = True parser.add_argument("-e", "--agree-eula", dest="eula", action="store_true",
elif o == "--help": help="Skip the end user license agreement screen.")
print_options() parser.add_argument("-t", "--text", dest="curses", action="store_false",
elif o == "--test": help="Use the text output instead of the curses UI.")
# put any temporary tests in here parser.add_argument("--test", dest="test", action="store_true",
continue help="Run in test mode.")
if curses: args = parser.parse_args()
# Enforce --privkey is set along with --csr.
if args.csr and not args.privkey:
parser.print_usage()
parser.error("private key file (--privkey) must be specified along{}"
"with the certificate signing request file (--csr)"
.format(os.linesep))
if args.curses:
display.set_display(display.NcursesDisplay()) display.set_display(display.NcursesDisplay())
else: else:
display.set_display(display.FileDisplay(sys.stdout)) display.set_display(display.FileDisplay(sys.stdout))
if not server: if args.rollback > 0:
server = CONFIG.ACME_SERVER rollback(apache_configurator.ApacheConfigurator(), args.rollback)
sys.exit()
c = client.Client(server, csr, privkey, curses) if args.view_checkpoints:
if flag_revoke: view_checkpoints(apache_configurator.ApacheConfigurator())
c.list_certs_keys() sys.exit()
server = args.server is None and CONFIG.ACME_SERVER or args.server
acme = client.Client(server, args.csr, args.privkey, args.curses)
if args.revoke:
acme.list_certs_keys()
else: else:
c.authenticate(args, redirect, eula) acme.authenticate(args.domains, args.redirect, args.eula)
def usage(): def rollback(config, checkpoints):
s = "Available options: --text, --privkey=, --csr=, --server=, " """Revert configuration the specified number of checkpoints.
s += "--rollback=, --view-checkpoints, --revoke, --agree-eula, --redirect,"
s += " --no-redirect, --help" :param config: Configurator object
print s :type config: ApacheConfigurator
:param checkpoints: Number of checkpoints to revert.
:type checkpoints: int
"""
config.rollback_checkpoints(checkpoints)
config.restart()
def print_options(): def view_checkpoints(config):
print ("\nsudo ./letsencrypt.py " """View checkpoints and associated configuration changes.
"(default authentication mode using pythondialog)")
options = ("privkey= (specify key file to use to generate the " :param config: Configurator object
"certificate)", :type config: ApacheConfigurator
"csr= (Use a specific CSR. If this is specified, privkey "
"must also be specified with the correct" """
" private key for the CSR)", config.display_checkpoints()
"server (list the ACME CA server address)",
"revoke (revoke a certificate)",
"view-checkpoints (Used to view available checkpoints and "
"see what configuration changes have been made)",
"rollback=X (Revert the configuration X number of checkpoints)",
"redirect (Automatically redirect all HTTP traffic to "
"HTTPS for the newly authenticated vhost)",
"no-redirect (Skip the HTTPS redirect question, "
"allowing both HTTP and HTTPS)",
"agree-eula (Skip the end user agreement screen))")
for o in options:
print " --%s" % o
sys.exit(0)
if __name__ == "__main__": if __name__ == "__main__":
+1
View File
@@ -3,3 +3,4 @@ python2-pythondialog
jsonschema==2.4.0 jsonschema==2.4.0
python-augeas==0.5.0 python-augeas==0.5.0
requests==2.4.3 requests==2.4.3
argparse==1.2.2
+1
View File
@@ -3,6 +3,7 @@ from setuptools import setup
install_requires = [ install_requires = [
'argparse',
'jsonschema', 'jsonschema',
'M2Crypto', 'M2Crypto',
'pycrypto', 'pycrypto',