Alias setting for FSIJ.ORG works now.
authorNIIBE Yutaka <gniibe@fsij.org>
Fri, 16 May 2014 07:48:21 +0000 (16:48 +0900)
committerNIIBE Yutaka <gniibe@fsij.org>
Fri, 16 May 2014 07:48:21 +0000 (16:48 +0900)
* api-key-info.json.gpg: New.
* fsij_list.json.gpg: New.
* gandi_tool.py: Updated.
* get_list_by_sql.py: New.
* setup_forward.py: New.

api-key-info.asc [deleted file]
api-key-info.json.gpg [new file with mode: 0644]
fsij_list.json.gpg [new file with mode: 0644]
gandi_tool.py
get_list_by_sql.py [new file with mode: 0644]
setup_forward.py [new file with mode: 0644]

diff --git a/api-key-info.asc b/api-key-info.asc
deleted file mode 100644 (file)
index cf8d5d6..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
------BEGIN PGP MESSAGE-----
-Version: GnuPG v1.4.12 (GNU/Linux)
-
-hQEMA3mnkJMIQjnPAQf9F4hWbz6mCGL6PST67UMTuvyloHgorb4KWW/ZGtfvhIUz
-dCteANQvWtMNjGfWHYwakD9YOIHHdOguSHwmWIrVrUhzKiQwUA/rE81ML1Shd9E5
-QgizSrD3ghCHv7qHnn+7T88j2Tx33yNkDTiFfmgI68RIi8DVD+MtcIIxGQdjZQfG
-RUiGR+SGVuWcfDfbJW1AeiAXrGjXwJIwJt8s+SUptduvreoLjQzLOdJwtmJ9mXKO
-02MbmjcSPoiLjt5vTsIQQpSI40TViynjLvLkET2N+USTb+TrHzv+1Jo128/r/iji
-lA4KGmcAURx+8KjWoJeom//Teiht4xQq4y9rogIcjtJ+Ae92d0F8Lf1tvLNDLorq
-EZDrWV9MBIcjydMRXUQxYcNTLNqZEZtDbrGAxBSguAJ61eBzsLn4g/5E39R+ttqd
-AfOj09Li/eERFwWuXuxIpHjGvUnZyD0G8JHj1fnLM1ZNltvq1l8R6G7A4EtiM3UX
-jCOVXkpOVMa+KMIJsDpw
-=u34s
------END PGP MESSAGE-----
diff --git a/api-key-info.json.gpg b/api-key-info.json.gpg
new file mode 100644 (file)
index 0000000..a70504d
Binary files /dev/null and b/api-key-info.json.gpg differ
diff --git a/fsij_list.json.gpg b/fsij_list.json.gpg
new file mode 100644 (file)
index 0000000..2f1bf82
Binary files /dev/null and b/fsij_list.json.gpg differ
index 594dd0a..a5a5dfa 100644 (file)
@@ -1,31 +1,57 @@
-url='https://rpc.gandi.net/xmlrpc/'
-
 import xmlrpclib
-api = xmlrpclib.ServerProxy(url)
-
 import subprocess
-command=['gpg', '--decrypt', '-o', '-', 'api-key-info.asc' ]
-p = subprocess.Popen(command, stdout=subprocess.PIPE)
-keydata_string = p.communicate()[0]
-
 import json
-keydata = json.loads(keydata_string)
-apikey = keydata['key']
-# validation check: is it 24-char?
-
-version = api.version.info(apikey)
-print("API Version: %s", version)
-print(api.domain.info(apikey, 'fsij.org'))
-
-print(api.domain.forward.count(apikey, 'fsij.org'))
-print(api.domain.forward.list(apikey, 'fsij.org'))
-
-# >>> api.domain.forward.create(apikey, 'mydomain.net', 'admin',
-# ... {'destinations': ['stephanie@example.com']})
-# {'destinations': ['stephanie@example.com'], 'source': 'admin'}
-# 
-# >>> api.domain.forward.update(apikey, 'mydomain.net', 'admin', ... {'destinations': ['stephanie@example.com', 'steph@example.com']})
-# {'destinations': ['stephanie@example.com', 'steph@example.com'], 'source': 'admin'}
-# 
-# >>> api.domain.forward.delete(apikey, 'mydomain.net', 'admin')
-# True
+
+class gandi_tool(object):
+    def __init__(self, url, encrypted_key_file, domain):
+        self.api = xmlrpclib.ServerProxy(url)
+        command=['gpg', '--decrypt', '-o', '-',  encrypted_key_file]
+        p = subprocess.Popen(command, stdout=subprocess.PIPE)
+        keydata_string = p.communicate()[0]
+        keydata = json.loads(keydata_string)
+        self.apikey = keydata['key']   # validation check: is it 24-char?
+        self.domain = domain
+
+    def api_version(self):
+        return self.api.version.info(self.apikey)
+
+    def info(self):
+        return self.api.domain.info(self.apikey, self.domain)
+
+    def forward_count(self):
+        return self.api.domain.forward.count(self.apikey, self.domain)
+
+    def forward_list(self, opts=None):
+        if opts:
+            return self.api.domain.forward.list(self.apikey, self.domain, opts)
+        else:
+            return self.api.domain.forward.list(self.apikey, self.domain)
+
+    def forward_create(self, src, params):
+        return self.api.domain.forward.create(self.apikey, self.domain, src, params)
+
+    def forward_update(self, src, params):
+        return self.api.domain.forward.update(self.apikey, self.domain, src, params)
+
+    def forward_delete(self, src):
+        return self.api.domain.forward.delete(self.apikey, self.domain, src)
+
+
+if __name__ == "__main__":
+    url='https://rpc.gandi.net/xmlrpc/'
+    encrypted_key_file='api-key-info.asc'
+    domain='fsij.org'
+
+    g=gandi_tool(url, encrypted_key_file, domain)
+    print("API Version: %s" % g.api_version()['api_version'])
+    info=g.info()
+    print("Domain owner handle: %s" % info['contacts']['owner']['handle'])
+    print(g.forward_count())
+    print(g.forward_list())
+    if g.forward_list({ 'source' : 'gniibe' }):
+        print(g.forward_delete('gniibe'))
+    print(g.forward_create('gniibe', {'destinations': ['gniibe@gniibe.org']}))
+    print(g.forward_list({ 'source' : 'gniibe' }))
+    print(g.forward_update('gniibe', {'destinations': ['fsij@gniibe.org']}))
+    if g.forward_delete('gniibe'):
+        print 'Done'
diff --git a/get_list_by_sql.py b/get_list_by_sql.py
new file mode 100644 (file)
index 0000000..ce4b8f1
--- /dev/null
@@ -0,0 +1,61 @@
+import sqlite3
+
+DB='../fsij-data/fsij.db'
+conn = sqlite3.connect(DB)
+c = conn.cursor()
+
+# TABLE: auth_user
+# id, username, email
+
+# TABLE: auth_user_groups
+# id, user_id, group_id
+
+# TABLE: auth_group
+# id, name
+
+# TODO: should care about multiple forward destinations
+
+c.execute('''select username, auth_user.email from auth_user, auth_user_groups,
+  auth_group, membership_member
+  where auth_user_groups.group_id = auth_group.id
+        and auth_user.id = auth_user_groups.user_id
+        and auth_group.name = \'fsijMember\'
+        and auth_user.id = membership_member.user_id
+        and membership_member.member_type = \'M'
+''')
+
+individual_member_list = []
+for row in c:
+    individual_member_list.append(( row[0], [ row[1] ] ))
+
+c.execute('''select email from auth_user, auth_user_groups, auth_group
+  where auth_user_groups.group_id = auth_group.id
+        and auth_user.id = auth_user_groups.user_id
+        and auth_group.name = \'boardMember\'''')
+board_member_list = []
+for row in c:
+   board_member_list.append(row[0])
+
+c.execute('''select email from auth_user, auth_user_groups, auth_group
+  where auth_user_groups.group_id = auth_group.id
+        and auth_user.id = auth_user_groups.user_id
+        and auth_group.name = \'fsijMember\'''')
+fsij_member_list = []
+for row in c:
+   fsij_member_list.append(row[0])
+
+c.close()
+conn.close()
+
+if __name__ == "__main__":
+    print("++++++++++++++++++++++++++")
+    for row in individual_member_list:
+        print row
+
+    print("++++++++++++++++++++++++++")
+    for row in board_member_list:
+        print row
+
+    print("++++++++++++++++++++++++++")
+    for row in fsij_member_list:
+        print row
diff --git a/setup_forward.py b/setup_forward.py
new file mode 100644 (file)
index 0000000..21dcec6
--- /dev/null
@@ -0,0 +1,73 @@
+from time import sleep
+from gandi_tool import gandi_tool
+from get_list_by_sql import individual_member_list, board_member_list, fsij_member_list
+
+def string_repr(l):
+    return u'\n'.join(sorted(l))
+
+if __name__ == "__main__":
+    url='https://rpc.gandi.net/xmlrpc/'
+    encrypted_key_file='api-key-info.json.gpg'
+    domain='fsij.org'
+    encrypted_list_file='fsij_list.json.gpg'
+
+    import subprocess, json
+    command=['gpg', '--decrypt', '-o', '-', encrypted_list_file]
+    p = subprocess.Popen(command, stdout=subprocess.PIPE)
+    listdata_string = p.communicate()[0]
+    fsij_aliases = json.loads(listdata_string)
+
+    g=gandi_tool(url, encrypted_key_file, domain)
+    print("API Version: %s" % g.api_version()['api_version'])
+    info=g.info()
+    print("Domain owner handle: %s" % info['contacts']['owner']['handle'])
+    print("Current number of forwards %d" % g.forward_count())
+    for i in fsij_aliases.items():
+        l = g.forward_list({ 'source' : i[0]})
+        dest = i[1]
+        if l:
+            if string_repr(l[0]['destinations']) == string_repr(dest):
+                print("Skip: %s" % i[0])
+            else:
+                print("Update: %s" % i[0])
+                g.forward_update(i[0], {'destinations': dest})
+        else:
+            print("Create: %s" % i[0])
+            g.forward_create(i[0], {'destinations': dest})
+    for i in individual_member_list:
+        l = g.forward_list({ 'source' : i[0]})
+        dest = i[1]
+        if l:
+            if string_repr(l[0]['destinations']) == string_repr(dest):
+                print("Skip: %s" % i[0])
+            else:
+                print("Update: %s" % i[0])
+                g.forward_update(i[0], {'destinations': dest})
+        else:
+            print("Create: %s" % i[0])
+            g.forward_create(i[0], {'destinations': dest})
+    l = g.forward_list({ 'source' : 'board' })
+    dest = board_member_list
+    if l:
+        if string_repr(l[0]['destinations']) == string_repr(dest):
+            print("Skip: %s" % 'board')
+        else:
+            print("Update: %s" % 'board')
+            # g.forward_update('board', {'destinations': dest})
+    else:
+        print("Create: %s" % 'board')
+        g.forward_create('board', {'destinations': dest})
+    l = g.forward_list({ 'source' : 'members' })
+    dest = fsij_member_list
+    if l:
+        if string_repr(l[0]['destinations']) == string_repr(dest):
+            print("Skip: %s" % 'members')
+        else:
+            print("Update: %s" % 'members')
+            g.forward_update('members', {'destinations': dest})
+    else:
+        print("Create: %s" % 'members')
+        g.forward_create('members', {'destinations': dest})
+    print("Current number of forwards %d" % g.forward_count())
+    for entry in g.forward_list():
+        print("%s -> %s" % (entry['source'], entry['destinations']))