Package webf :: Module cli
[hide private]
[frames] | no frames]

Source Code for Module webf.cli

  1   
  2  # pylint: disable-msg=W0142 
  3  ''' 
  4   
  5  cli.py 
  6  ====== 
  7   
  8  Command-line tool for WebFaction's XML-RPC API http://api.webfaction.com/ 
  9   
 10  Author: Rob Cakebread <rob@cakebread.info> 
 11   
 12  License : BSD-2 
 13   
 14  ''' 
 15   
 16  __docformat__ = 'epytext' 
 17  __revision__ = '$Revision:  $'[11:-1].strip() 
 18   
 19   
 20  import sys 
 21  import logging 
 22  import optparse 
 23   
 24  from webf.__init__ import __version__ as VERSION 
 25  from webf import webflib 
 26   
 27   
28 -class WebFaction(object):
29 30 '''``WebFaction`` class''' 31
32 - def __init__(self):
33 '''Initialize attributes, set logger''' 34 self.options = None 35 self.log = logging.getLogger('webf') 36 self.log.addHandler(logging.StreamHandler()) 37 self.group_api = None
38
39 - def set_log_level(self):
40 '''Set log level according to command-line options''' 41 if self.options.debug: 42 self.log.setLevel(logging.DEBUG)
43
44 - def run(self):
45 ''' 46 Run WebFaction command 47 48 @rtype: int 49 @returns: 0 success or 1 failure 50 51 ''' 52 opt_parser = self.setup_opt_parser() 53 (self.options, remaining_args) = opt_parser.parse_args() 54 if remaining_args: 55 opt_parser.print_help() 56 self.set_log_level() 57 58 if self.options.webfaction_version: 59 return webfaction_version() 60 61 #Go through list of options in ``group_api`` and execute 62 #activated callable for enabled option 63 #Looks a bit magic, but saves a ton of if/then branching 64 for action in [p for p in self.group_api.option_list]: 65 action = str(action)[2:].replace('-', '_') 66 if getattr(self.options, action): 67 opts = getattr(self.options, action) 68 if not isinstance(opts, tuple): 69 opts = [opts] 70 server = webflib.WebFactionXmlRpc() 71 return getattr(server, action)(*opts) 72 else: 73 opt_parser.print_help() 74 return 1
75
76 - def setup_opt_parser(self):
77 ''' 78 Setup the optparser 79 80 @rtype: opt_parser.OptionParser 81 @return: Option parser 82 83 ''' 84 usage = 'usage: %prog [options]' 85 opt_parser = optparse.OptionParser(usage=usage) 86 group_api = optparse.OptionGroup(opt_parser, 87 'WebFaction Commands', 88 'Options for calling WebFaction XML-RPC API methods') 89 90 group_api.add_option('--create-app', 91 action='store', 92 dest='create_app', 93 nargs=4, 94 help='Create application', 95 metavar='NAME TYPE AUTOSTART EXTRA_INFO' 96 ) 97 98 group_api.add_option('--delete-app', 99 action='store', 100 dest='delete_app', 101 help='Delete application', 102 metavar='NAME' 103 ) 104 105 group_api.add_option('--create-cronjob', 106 action='store', 107 dest='create_cronjob', 108 help='Create cronjob', 109 metavar='CRONJOB' 110 ) 111 112 group_api.add_option('--delete-cronjob', 113 action='store', 114 dest='delete_cronjob', 115 help='Delete cronjob', 116 metavar='CRONJOB' 117 ) 118 119 group_api.add_option('--create-db', 120 action='store', 121 dest='create_db', 122 nargs=3, 123 help='Create Database', 124 metavar='NAME DB_TYPE PASSWORD' 125 ) 126 127 group_api.add_option('--delete-db', 128 action='store', 129 dest='delete_db', 130 nargs=2, 131 help='Delete Database', 132 metavar='NAME DB_TYPE' 133 ) 134 135 group_api.add_option('--create-domain', 136 action='store', 137 dest='create_domain', 138 help='Create domain', 139 nargs=2, 140 metavar='DOMAIN SUBDOMAIN' 141 ) 142 143 group_api.add_option('--create-website', 144 action='store', 145 dest='create_website', 146 help='Create website', 147 nargs=5, 148 metavar='NAME IP HTTPS SUBDOMAINS *SITE_APPS' 149 ) 150 151 group_api.add_option('--create-dns-override', 152 action='store', 153 dest='create_dns_override', 154 help='Create DNS override', 155 nargs=6, 156 metavar='DOMAIN A_IP CNAME MX_NAME MX_PRIORITY SPF_RECORD' 157 ) 158 159 group_api.add_option('--delete-dns-override', 160 action='store', 161 dest='delete_dns_override', 162 help='Delete DNS override', 163 nargs=6, 164 metavar='DOMAIN A_IP CNAME MX_NAME MX_PRIORITY SPF_RECORD' 165 ) 166 167 group_api.add_option('--create-email', 168 action='store', 169 dest='create_email', 170 help='Create email address', 171 nargs=6, 172 metavar='EMAIL_ADDRESS TARGETS AR_ON AR_SUBJECT AR_MSG AR_FROM' 173 ) 174 175 group_api.add_option('--delete-email', 176 action='store', 177 dest='delete_email', 178 help='Delete email address', 179 metavar='EMAIL_ADDRESS' 180 ) 181 182 group_api.add_option('--create-mailbox', 183 action='store', 184 dest='create_mailbox', 185 help='Create mailbox', 186 nargs=5, 187 metavar='MBOX_NAME SPAM_PROT SHARE SPAM_LEARN HAM_LEARN' 188 ) 189 190 group_api.add_option('--delete-mailbox', 191 action='store', 192 dest='delete_mailbox', 193 help='Delete mailbox', 194 metavar='MBOX_NAME' 195 ) 196 197 group_api.add_option('--set-apache-acl', 198 action='store', 199 dest='set_apache_acl', 200 nargs=3, 201 help='Set Apache ACL', 202 metavar='PATHS PERMS RECURSIVE' 203 ) 204 205 group_api.add_option('--system', 206 action='store', 207 dest='system', 208 help='Run system command.', 209 metavar='CMD' 210 ) 211 group_api.add_option('--write-file', 212 action='store', 213 dest='write_file', 214 help='Write file', 215 metavar='FILENAME STRING MODE' 216 ) 217 218 self.group_api = group_api 219 opt_parser.add_option_group(group_api) 220 #Common options 221 opt_parser.add_option('--version', action='store_true', dest= 222 'webfaction_version', default=False, help= 223 "Show this program's version and exit.") 224 225 opt_parser.add_option('--debug', action='store_true', dest= 226 'debug', default=False, help= 227 'Show debugging information') 228 229 return opt_parser
230 231
232 -def webfaction_version():
233 '''Print webfaction version''' 234 print VERSION
235
236 -def main():
237 '''Let's do it.''' 238 client = WebFaction() 239 return client.run()
240 241 242 if __name__ == '__main__': 243 sys.exit(main()) 244