Mercurial > hg > silvermirror
comparison silvermirror/unify.py @ 35:e86d475435ee
use argparse and try to diagnose strange error
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sun, 04 May 2014 23:14:15 -0700 |
parents | 4f1e45a8656c |
children | d081ca2e8696 |
comparison
equal
deleted
inserted
replaced
34:f0b30886801f | 35:e86d475435ee |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 """ | |
4 unify virtual filesystems | |
5 """ | |
6 | |
7 import argparse | |
3 import getpass | 8 import getpass |
4 import os | 9 import os |
5 import socket | 10 import socket |
6 import subprocess | 11 import subprocess |
7 import sys | 12 import sys |
8 | |
9 from martini.config import ConfigMunger | 13 from martini.config import ConfigMunger |
10 from optparse import OptionParser | |
11 from pkg_resources import iter_entry_points | 14 from pkg_resources import iter_entry_points |
12 from pprint import pprint | 15 from pprint import pprint |
13 from utils import home | 16 from utils import home |
14 from utils import ip_addresses | 17 from utils import ip_addresses |
15 | 18 |
59 ### | 62 ### |
60 config = { 'main': main, 'resources': config } | 63 config = { 'main': main, 'resources': config } |
61 return config | 64 return config |
62 | 65 |
63 def unify(conf, _resources, test=False, verbose=True, notification_prefix='\n*** SilverMirror; '): | 66 def unify(conf, _resources, test=False, verbose=True, notification_prefix='\n*** SilverMirror; '): |
67 """unify virtual filesystems given configuration""" | |
68 | |
64 # TODO: -> OO | 69 # TODO: -> OO |
65 | 70 |
66 # log function | 71 # log function |
67 def log(message): | 72 def log(message): |
68 if verbose: | 73 if verbose: |
78 _hosts = [] | 83 _hosts = [] |
79 for host in hosts: | 84 for host in hosts: |
80 s = socket.socket() | 85 s = socket.socket() |
81 s.settimeout(conf['main']['timeout']) | 86 s.settimeout(conf['main']['timeout']) |
82 if test: | 87 if test: |
83 print 'Resolving %s' % host | 88 print ('Resolving %s' % host) |
84 try: | 89 try: |
85 s.connect((host, 22)) | 90 s.connect((host, 22)) |
86 s.close() | 91 s.close() |
87 except (socket.gaierror, socket.timeout, socket.error): | 92 except (socket.gaierror, socket.timeout, socket.error): |
88 continue | 93 continue |
113 break | 118 break |
114 if test: | 119 if test: |
115 log("Resources:\n") | 120 log("Resources:\n") |
116 pprint(resources) | 121 pprint(resources) |
117 | 122 |
118 ### choose reflector backend | 123 # choose reflector backend |
119 reflectors = dict([(i.name, i.load()) for i in iter_entry_points('silvermirror.reflectors')]) | 124 reflectors = dict([(i.name, i.load()) for i in iter_entry_points('silvermirror.reflectors')]) |
120 reflector = reflectors['unison']() # only one right now | 125 reflector = reflectors['unison']() # only one right now |
121 | 126 |
122 ### sync with hosts | 127 # sync with hosts |
123 os.chdir(conf['main']['basedir']) | 128 try: |
124 for index, resource in enumerate(resources): | 129 os.chdir(conf['main']['basedir']) |
130 for index, resource in enumerate(resources): | |
125 | 131 |
126 # echo resource | 132 # echo resource |
127 log("syncing:'%s' [%d/%d]" % (resource, index+1, len(resources))) | 133 log("syncing:'%s' [%d/%d]" % (resource, index+1, len(resources))) |
128 | 134 |
129 # loop over hosts | 135 # loop over hosts |
130 for host in hosts: | 136 for host in hosts: |
131 reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test) | 137 reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test) |
132 os.chdir(cwd) | 138 finally: |
139 os.chdir(cwd) | |
133 | 140 |
134 def main(args=sys.argv[1:]): | 141 def main(args=sys.argv[1:]): |
142 """CLI""" | |
135 | 143 |
136 ### command line options | 144 # parse command line |
137 parser = OptionParser() | 145 parser = argparse.ArgumentParser(description=__doc__) |
138 parser.add_option('-c', '--config') | 146 parser.add_argument('-c', '--config') |
139 parser.add_option('-H', '--host', dest='hosts', | 147 parser.add_argument('-H', '--host', dest='hosts', action='append', |
140 action='append', default=None) | 148 help="hosts to sync with") |
141 parser.add_option('--no-password', dest='password', | 149 parser.add_argument('--no-password', dest='password', |
142 action='store_false', default=True) | 150 action='store_false', default=True) |
143 parser.add_option('--test', dest='test', | 151 parser.add_argument('--test', dest='test', |
144 action='store_true', default=False) | 152 action='store_true', default=False) |
145 (options, args) = parser.parse_args() | 153 parser.add_argument('-v', '--verbose', |
154 action='store_true', default=False) | |
155 parser.add_argument('resources', nargs='*', | |
156 help="resources to sync, or all if omitted") | |
157 options = parser.parse_args(args) | |
146 | 158 |
147 | 159 |
148 ### configuration | 160 # configuration |
149 user_conf = os.path.join(home(), '.silvermirror') | 161 user_conf = os.path.join(home(), '.silvermirror') |
150 if options.config: | 162 if options.config: |
151 assert os.path.exists(options.config) | 163 if not os.path.exists(options.config): |
164 parser.error("Configuration file '" + options.config + "' does not exist") | |
152 conf = read_config(options.config) | 165 conf = read_config(options.config) |
153 else: | 166 else: |
154 for i in user_conf, '/etc/silvermirror': | 167 for i in (user_conf, '/etc/silvermirror'): |
155 if os.path.exists(i): | 168 if os.path.exists(i): |
156 conf = read_config(i) | 169 conf = read_config(i) |
157 break | 170 break |
158 else: | 171 else: |
159 conf = make_config(user_conf) | 172 conf = make_config(user_conf) |