Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

More flexible options #6

Open
wants to merge 7 commits into from

2 participants

@xiongchiamiov

Allow users to either specify options in an ini-style config file or use command-line parameters. The former is important when the script is installed through pip (and thus settings.py ends up in some system library somewhere), and the latter is useful so users can override settings programmatically.

@xiongchiamiov xiongchiamiov referenced this pull request in xiongchiamiov/xiongchiamiov.github.com
Open

Build a better portfolio #5

@redsnapper8t8

@xiongchiamiov , I think PEP8 is pretty clear about import order.
http://www.python.org/dev/peps/pep-0008/#id15
Imports should be grouped in the following order:
standard library imports
related third party imports
local application/library specific imports

You're correct; I've never paid much attention to PEP8. How do you feel about this?

import time
import socket
from traceback import format_exc
from StringIO import StringIO

from lxml import etree

That is, imports before froms, groupings within those alphabetical, and the broader grouping as specified by PEP8?

Or I can just rebase out this commit; it's not really important.

@redsnapper8t8

@xiongchiamiov , sorry for not responding sooner. I'm looking to clear some time to polish up the app as soon as I get some free time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
19 README.rst
@@ -77,19 +77,10 @@ from github ::
or simply download to a local directory and run from there.
-Once installed you need to edit settings.py ::
+PyVarnish settings can be specified in a config file; copy
+``config.ini.example`` to ``~/.config/pyvarnish/config.ini`` and change the
+values as you like.
-
- VARNISH_SERVERS = ('server1', "server2",)
- SSH_CONFIG = "/home/username/.ssh/config"
- DEBUG = True
- CARBON_SERVER = 'localhost'
- CARBON_PORT = 2003
-
-
-* Replace server1, server2 etc with the hostnames of your own varnish servers
-* SSH_CONFIG needs to refer to your .ssh/config file
-* CARBON_SERVER and CARBON_PORT should refer to your graphite server.
-
-Then create a script that runs from crontab to run parse_stats.py
+Alternatively, PyVarnish takes command-line parameters that override settings
+defined in the config file. See ``--help`` for more information.
View
7 config.ini.example
@@ -0,0 +1,7 @@
+[DEFAULT]
+#varnish_servers = localhost
+#ssh_config = /home/user/.ssh/config
+#debug = true
+#carbon_server = localhost
+#carbon_port = 2003
+
View
2  pyvarnish/__init__.py
@@ -1,3 +1,5 @@
__author__ = 'john'
VERSION = (0, 6, 2)
__version__ = '.'.join(map(str, VERSION))
+__description__ = 'Varnish Management'
+
View
35 pyvarnish/parse_stats.py
@@ -2,13 +2,14 @@
#!/usr/bin/env python
import time
-from traceback import format_exc
-from StringIO import StringIO
import socket
+from argparse import ArgumentParser
from lxml import etree
+from traceback import format_exc
+from StringIO import StringIO
-from pyvarnish.settings import CARBON_SERVER, CARBON_PORT, VARNISH_SERVERS, DEBUG
+import pyvarnish
from pyvarnish.remote import Varnish_admin
@@ -57,13 +58,37 @@ def msg(message):
def main():
-
+ from pyvarnish.settings import VARNISH_SERVERS, SSH_CONFIG, DEBUG, \
+ CARBON_SERVER, CARBON_PORT
+ parser = ArgumentParser(description=pyvarnish.__description__)
+
+ parser.add_argument('--varnish-servers', nargs='+', metavar='SERVER')
+ parser.add_argument('--carbon-server', action='store', metavar='SERVER')
+ parser.add_argument('--carbon-port', action='store', metavar='PORT')
+ parser.add_argument('--ssh-config', action='store',
+ metavar='SSH_CONFIG_FILE', type=int)
+ parser.add_argument('-d', '--debug', action='store_true', default=DEBUG)
+ parser.add_argument('--version', action='version',
+ version='pyvarnish %s' % pyvarnish.__version__)
+
+ args = parser.parse_args()
+ if args.varnish_servers:
+ VARNISH_SERVERS = args.varnish_servers
+ if args.carbon_server:
+ CARBON_SERVER = args.carbon_server
+ if args.carbon_port:
+ CARBON_PORT = args.carbon_port
+ if args.ssh_config:
+ SSH_CONFIG = args.ssh_config
+ # argparse always assigns this a value, even if we don't use the flag.
+ DEBUG = args.debug
+
for server in VARNISH_SERVERS:
if DEBUG:
print 'Connecting to Varnish server at %s...' % server
try:
#connect to server
- vserver = Varnish_admin(server)
+ vserver = Varnish_admin(server, SSH_CONFIG)
data = vserver.runcmd("varnishstat -x")
except Exception:
if DEBUG:
View
10 pyvarnish/remote.py
@@ -5,13 +5,12 @@
from paramiko import SSHClient, SSHConfig, AutoAddPolicy
-from pyvarnish.settings import SSH_CONFIG
-
class Varnish_admin():
- def __init__(self, server=''):
+ def __init__(self, server, ssh_config_file):
self.server = server
+ self.ssh_config_file = ssh_config_file
self.conf = {
'hostname': server,
'port': 22,
@@ -24,10 +23,9 @@ def __init__(self, server=''):
def config(self):
sshconfig = SSHConfig()
try:
- sshconfig.parse(open(SSH_CONFIG))
+ sshconfig.parse(open(self.ssh_config_file))
except IOError:
- print "your app needs to have a valid " \
- "ssh config file location in settings.py"
+ print 'SSH config file location invalid.'
sys.exit(1)
conf = sshconfig.lookup(self.server)
if 'port' in conf:
View
33 pyvarnish/settings.py
@@ -1,10 +1,31 @@
# -*- coding: utf-8 -*-
+import os
+try:
+ from ConfigParser import SafeConfigParser, NoOptionError, NoSectionError
+except ImportError:
+ # Renamed in py3k
+ from configparser import SafeConfigParser, NoOptionError, NoSectionError
+
__author__ = 'John Moylan'
-VARNISH_SERVERS = ('server1', "server2",)
-SSH_CONFIG = "/home/username/.ssh/config"
-DEBUG = True
-CARBON_SERVER = 'localhost'
-CARBON_PORT = 2003
+defaults = {
+ 'varnish_servers': 'localhost',
+ 'ssh_config': os.path.expanduser('~/.ssh/config'),
+ 'debug': 'True',
+ 'carbon_server': 'localhost',
+ 'carbon_port': '2003',
+}
+
+config = SafeConfigParser(defaults)
+# Loosely follow the XDG Base Directory Specification:
+# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
+configPath = os.path.expanduser('~/.config/pyvarnish/config.ini')
+config.read(configPath)
+
+VARNISH_SERVERS = config.get('DEFAULT', 'varnish_servers').split(',')
+VARNISH_SERVERS = [server.strip() for server in VARNISH_SERVERS]
+SSH_CONFIG = config.get('DEFAULT', 'ssh_config')
+DEBUG = config.getboolean('DEFAULT', 'debug')
+CARBON_SERVER = config.get('DEFAULT', 'carbon_server')
+CARBON_PORT = config.getint('DEFAULT', 'carbon_port')
-
View
6 setup.py
@@ -1,10 +1,10 @@
from setuptools import setup, find_packages
-from pyvarnish import __author__, __version__
+from pyvarnish import __author__, __description__, __version__
setup(
name='pyvarnish',
version=__version__,
- description='Varnish Management',
+ description=__description__,
long_description=open('README.rst').read(),
author=__author__,
author_email='john@8t8.eu',
@@ -22,4 +22,4 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
]
-)
+)
Something went wrong with that request. Please try again.