<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[13254] CalendarServer/trunk/calendarserver/tools</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/13254">13254</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-04-10 16:32:58 -0700 (Thu, 10 Apr 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>changeip_calendar now uses calendarserver_config to do its bidding</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolschangeip_calendarpy">CalendarServer/trunk/calendarserver/tools/changeip_calendar.py</a></li>
<li><a href="#CalendarServertrunkcalendarservertoolstesttest_changeippy">CalendarServer/trunk/calendarserver/tools/test/test_changeip.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolschangeip_calendarpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/changeip_calendar.py (13253 => 13254)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/changeip_calendar.py        2014-04-10 23:10:17 UTC (rev 13253)
+++ CalendarServer/trunk/calendarserver/tools/changeip_calendar.py        2014-04-10 23:32:58 UTC (rev 13254)
</span><span class="lines">@@ -12,19 +12,38 @@
</span><span class="cx"> from __future__ import print_function
</span><span class="cx"> from __future__ import with_statement
</span><span class="cx"> 
</span><ins>+
+import datetime
+from getopt import getopt, GetoptError
</ins><span class="cx"> import os
</span><ins>+import plistlib
+import subprocess
</ins><span class="cx"> import sys
</span><del>-from getopt import getopt, GetoptError
</del><span class="cx"> 
</span><del>-from plistlib import readPlist, writePlist
</del><span class="cx"> 
</span><ins>+SERVER_APP_ROOT = &quot;/Applications/Server.app/Contents/ServerRoot&quot;
+CALENDARSERVER_CONFIG = &quot;%s/usr/sbin/calendarserver_config&quot; % (SERVER_APP_ROOT,)
</ins><span class="cx"> 
</span><ins>+
+def serverRootLocation():
+    &quot;&quot;&quot;
+    Return the ServerRoot value from the servermgr_calendar.plist.  If not
+    present, return the default.
+    &quot;&quot;&quot;
+    plist = &quot;/Library/Preferences/com.apple.servermgr_calendar.plist&quot;
+    serverRoot = u&quot;/Library/Server/Calendar and Contacts&quot;
+    if os.path.exists(plist):
+        serverRoot = plistlib.readPlist(plist).get(&quot;ServerRoot&quot;, serverRoot)
+    return serverRoot
+
+
+
</ins><span class="cx"> def usage():
</span><span class="cx">     name = os.path.basename(sys.argv[0])
</span><span class="cx">     print(&quot;Usage: %s [-hv] old-ip new-ip [old-hostname new-hostname]&quot; % (name,))
</span><span class="cx">     print(&quot;  Options:&quot;)
</span><span class="cx">     print(&quot;    -h           - print this message and exit&quot;)
</span><del>-    print(&quot;    -v           - print additional information when running&quot;)
</del><ins>+    print(&quot;    -f &lt;file&gt;    - path to config file&quot;)
</ins><span class="cx">     print(&quot;  Arguments:&quot;)
</span><span class="cx">     print(&quot;    old-ip       - current IPv4 address of the server&quot;)
</span><span class="cx">     print(&quot;    new-ip       - new IPv4 address of the server&quot;)
</span><span class="lines">@@ -32,7 +51,21 @@
</span><span class="cx">     print(&quot;    new-hostname - new FQDN for the server&quot;)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+def log(msg):
+    serverRoot = serverRootLocation()
+    logDir = os.path.join(serverRoot, &quot;Logs&quot;)
+    logFile = os.path.join(logDir, &quot;changeip.log&quot;)
</ins><span class="cx"> 
</span><ins>+    try:
+        timestamp = datetime.datetime.now().strftime(&quot;%b %d %H:%M:%S&quot;)
+        msg = &quot;changeip_calendar: %s %s&quot; % (timestamp, msg)
+        with open(logFile, 'a') as output:
+            output.write(&quot;%s\n&quot; % (msg,))
+    except IOError:
+        # Could not write to log
+        pass
+
+
</ins><span class="cx"> def main():
</span><span class="cx"> 
</span><span class="cx">     name = os.path.basename(sys.argv[0])
</span><span class="lines">@@ -44,9 +77,8 @@
</span><span class="cx"> 
</span><span class="cx">     try:
</span><span class="cx">         (optargs, args) = getopt(
</span><del>-            sys.argv[1:], &quot;hvf:&quot;, [
</del><ins>+            sys.argv[1:], &quot;hf:&quot;, [
</ins><span class="cx">                 &quot;help&quot;,
</span><del>-                &quot;verbose&quot;,
</del><span class="cx">                 &quot;config=&quot;,
</span><span class="cx">             ]
</span><span class="cx">         )
</span><span class="lines">@@ -54,17 +86,13 @@
</span><span class="cx">         usage()
</span><span class="cx">         sys.exit(1)
</span><span class="cx"> 
</span><del>-    verbose = False
-    configFile = &quot;/Library/Server/Calendar and Contacts/Config/caldavd-system.plist&quot;
</del><ins>+    configFile = None
</ins><span class="cx"> 
</span><span class="cx">     for opt, arg in optargs:
</span><span class="cx">         if opt in (&quot;-h&quot;, &quot;--help&quot;):
</span><span class="cx">             usage()
</span><span class="cx">             sys.exit(1)
</span><span class="cx"> 
</span><del>-        elif opt in (&quot;-v&quot;, &quot;--verbose&quot;):
-            verbose = True
-
</del><span class="cx">         elif opt in (&quot;-f&quot;, &quot;--config&quot;):
</span><span class="cx">             configFile = arg
</span><span class="cx"> 
</span><span class="lines">@@ -74,33 +102,79 @@
</span><span class="cx">     except ValueError:
</span><span class="cx">         oldHostname = newHostname = None
</span><span class="cx"> 
</span><del>-    if verbose:
-        print(&quot;Calendar Server: updating %s&quot; % (configFile,))
</del><ins>+    log(&quot;args: {}&quot;.format(args))
</ins><span class="cx"> 
</span><del>-    try:
-        plist = readPlist(configFile)
-    except IOError:
-        print(&quot;Error: could not open %s&quot; % (configFile,))
-        sys.exit(1)
-    except Exception, e:
-        print(&quot;Error: could not parse %s&quot; % (configFile,))
-        raise e
</del><ins>+    config = readConfig(configFile=configFile)
</ins><span class="cx"> 
</span><del>-    writePlist(plist, &quot;%s.changeip.bak&quot; % (configFile,))
</del><ins>+    updateConfig(
+        config,
+        oldIP, newIP,
+        oldHostname, newHostname
+    )
+    writeConfig(config)
</ins><span class="cx"> 
</span><del>-    updatePlist(plist, oldIP, newIP, oldHostname, newHostname, verbose=verbose)
-    writePlist(plist, configFile)
</del><span class="cx"> 
</span><del>-    if verbose:
-        print(&quot;Calendar Server: done&quot;)
</del><span class="cx"> 
</span><ins>+def sendCommand(commandDict, configFile=None):
</ins><span class="cx"> 
</span><ins>+    args = [CALENDARSERVER_CONFIG]
+    if configFile is not None:
+        args.append(&quot;-f {}&quot;.format(configFile))
</ins><span class="cx"> 
</span><del>-def updatePlist(plist, oldIP, newIP, oldHostname, newHostname, verbose=False):
</del><ins>+    child = subprocess.Popen(
+        args=args,
+        stdin=subprocess.PIPE,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+    )
</ins><span class="cx"> 
</span><ins>+    commandString = plistlib.writePlistToString(commandDict)
+    log(&quot;Sending to calendarserver_config: {}&quot;.format(commandString))
+
+    output, error = child.communicate(input=commandString)
+    log(&quot;Output from calendarserver_config: {}&quot;.format(output))
+    if child.returncode:
+        log(
+            &quot;Error from calendarserver_config: {}, {}&quot;.format(
+                child.returncode, error
+            )
+        )
+        return None
+    else:
+        return plistlib.readPlistFromString(output)[&quot;result&quot;]
+
+
+def readConfig(configFile=None):
+    &quot;&quot;&quot;
+    Ask calendarserver_config for the current configuration
+    &quot;&quot;&quot;
+
+    command = {
+        &quot;command&quot;: &quot;readConfig&quot;
+    }
+    return sendCommand(command)
+
+
+
+def writeConfig(valuesDict, configFile=None):
+    &quot;&quot;&quot;
+    Ask calendarserver_config to update the configuration
+    &quot;&quot;&quot;
+    command = {
+        &quot;command&quot;: &quot;writeConfig&quot;,
+        &quot;Values&quot;: valuesDict,
+    }
+    return sendCommand(command)
+
+
+def updateConfig(
+    config,
+    oldIP, newIP,
+    oldHostname, newHostname,
+    configFile=None
+):
+
</ins><span class="cx">     keys = (
</span><del>-        (&quot;Authentication&quot;, &quot;Wiki&quot;, &quot;Hostname&quot;),
-        (&quot;BindAddresses&quot;,),
</del><span class="cx">         (&quot;Scheduling&quot;, &quot;iMIP&quot;, &quot;Receiving&quot;, &quot;Server&quot;),
</span><span class="cx">         (&quot;Scheduling&quot;, &quot;iMIP&quot;, &quot;Sending&quot;, &quot;Server&quot;),
</span><span class="cx">         (&quot;Scheduling&quot;, &quot;iMIP&quot;, &quot;Sending&quot;, &quot;Address&quot;),
</span><span class="lines">@@ -111,12 +185,12 @@
</span><span class="cx">         newValue = value.replace(oldIP, newIP)
</span><span class="cx">         if oldHostname and newHostname:
</span><span class="cx">             newValue = newValue.replace(oldHostname, newHostname)
</span><del>-        if verbose and value != newValue:
-            print(&quot;Changed %s -&gt; %s&quot; % (value, newValue))
</del><ins>+        if value != newValue:
+            log(&quot;Changed %s -&gt; %s&quot; % (value, newValue))
</ins><span class="cx">         return newValue
</span><span class="cx"> 
</span><span class="cx">     for keyPath in keys:
</span><del>-        parent = plist
</del><ins>+        parent = config
</ins><span class="cx">         path = keyPath[:-1]
</span><span class="cx">         key = keyPath[-1]
</span><span class="cx"> 
</span><span class="lines">@@ -133,16 +207,17 @@
</span><span class="cx">                 if isinstance(value, list):
</span><span class="cx">                     newValue = []
</span><span class="cx">                     for item in value:
</span><del>-                        item = _replace(item, oldIP, newIP, oldHostname,
-                            newHostname)
</del><ins>+                        item = _replace(
+                            item, oldIP, newIP, oldHostname, newHostname
+                        )
</ins><span class="cx">                         newValue.append(item)
</span><span class="cx">                 else:
</span><del>-                    newValue = _replace(value, oldIP, newIP, oldHostname,
-                        newHostname)
</del><ins>+                    newValue = _replace(
+                        value, oldIP, newIP, oldHostname, newHostname
+                    )
</ins><span class="cx"> 
</span><span class="cx">                 parent[key] = newValue
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> if __name__ == '__main__':
</span><span class="cx">     main()
</span></span></pre></div>
<a id="CalendarServertrunkcalendarservertoolstesttest_changeippy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/test/test_changeip.py (13253 => 13254)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/test/test_changeip.py        2014-04-10 23:10:17 UTC (rev 13253)
+++ CalendarServer/trunk/calendarserver/tools/test/test_changeip.py        2014-04-10 23:32:58 UTC (rev 13254)
</span><span class="lines">@@ -15,65 +15,46 @@
</span><span class="cx"> ##
</span><span class="cx"> 
</span><span class="cx"> from twistedcaldav.test.util import TestCase
</span><del>-from calendarserver.tools.changeip_calendar import updatePlist
</del><ins>+from calendarserver.tools.changeip_calendar import updateConfig
</ins><span class="cx"> 
</span><ins>+
</ins><span class="cx"> class ChangeIPTestCase(TestCase):
</span><span class="cx"> 
</span><del>-    def test_updatePlist(self):
</del><ins>+    def test_updateConfig(self):
</ins><span class="cx"> 
</span><span class="cx">         plist = {
</span><del>-            &quot;Authentication&quot; : {
-                &quot;Wiki&quot; : {
-                    &quot;Hostname&quot; : &quot;original_hostname&quot;,
-                    &quot;Other&quot; : &quot;should_be_untouched&quot;,
-                },
-            },
-            &quot;Untouched&quot; : &quot;dont_change_me&quot;,
-            &quot;BindAddresses&quot; : [
-                &quot;10.1.1.1&quot;,
-                &quot;192.168.1.1&quot;,
-                &quot;original_hostname&quot;,
-            ],
-            &quot;ServerHostName&quot; : &quot;&quot;,
-            &quot;Scheduling&quot; : {
-                &quot;iMIP&quot; : {
-                    &quot;Receiving&quot; : {
-                        &quot;Server&quot; : &quot;original_hostname&quot;,
</del><ins>+            &quot;Untouched&quot;: &quot;dont_change_me&quot;,
+            &quot;ServerHostName&quot;: &quot;&quot;,
+            &quot;Scheduling&quot;: {
+                &quot;iMIP&quot;: {
+                    &quot;Receiving&quot;: {
+                        &quot;Server&quot;: &quot;original_hostname&quot;,
</ins><span class="cx">                     },
</span><del>-                    &quot;Sending&quot; : {
-                        &quot;Server&quot; : &quot;original_hostname&quot;,
-                        &quot;Address&quot; : &quot;user@original_hostname&quot;,
</del><ins>+                    &quot;Sending&quot;: {
+                        &quot;Server&quot;: &quot;original_hostname&quot;,
+                        &quot;Address&quot;: &quot;user@original_hostname&quot;,
</ins><span class="cx">                     },
</span><span class="cx">                 },
</span><span class="cx">             },
</span><span class="cx">         }
</span><span class="cx"> 
</span><del>-        updatePlist(plist, &quot;10.1.1.1&quot;, &quot;10.1.1.2&quot;, &quot;original_hostname&quot;,
-            &quot;new_hostname&quot;)
</del><ins>+        updateConfig(
+            plist, &quot;10.1.1.1&quot;, &quot;10.1.1.2&quot;, &quot;original_hostname&quot;, &quot;new_hostname&quot;
+        )
</ins><span class="cx"> 
</span><del>-        self.assertEquals(plist,
</del><ins>+        self.assertEquals(
+            plist,
</ins><span class="cx">             {
</span><del>-                &quot;Authentication&quot; : {
-                    &quot;Wiki&quot; : {
-                        &quot;Hostname&quot; : &quot;new_hostname&quot;,
-                        &quot;Other&quot; : &quot;should_be_untouched&quot;,
-                    },
-                },
-                &quot;Untouched&quot; : &quot;dont_change_me&quot;,
-                &quot;BindAddresses&quot; : [
-                    &quot;10.1.1.2&quot;,
-                    &quot;192.168.1.1&quot;,
-                    &quot;new_hostname&quot;,
-                ],
-                &quot;ServerHostName&quot; : &quot;&quot;,
-                &quot;Scheduling&quot; : {
-                    &quot;iMIP&quot; : {
-                        &quot;Receiving&quot; : {
-                            &quot;Server&quot; : &quot;new_hostname&quot;,
</del><ins>+                &quot;Untouched&quot;: &quot;dont_change_me&quot;,
+                &quot;ServerHostName&quot;: &quot;&quot;,
+                &quot;Scheduling&quot;: {
+                    &quot;iMIP&quot;: {
+                        &quot;Receiving&quot;: {
+                            &quot;Server&quot;: &quot;new_hostname&quot;,
</ins><span class="cx">                         },
</span><del>-                        &quot;Sending&quot; : {
-                            &quot;Server&quot; : &quot;new_hostname&quot;,
-                            &quot;Address&quot; : &quot;user@new_hostname&quot;,
</del><ins>+                        &quot;Sending&quot;: {
+                            &quot;Server&quot;: &quot;new_hostname&quot;,
+                            &quot;Address&quot;: &quot;user@new_hostname&quot;,
</ins><span class="cx">                         },
</span><span class="cx">                     },
</span><span class="cx">                 },
</span></span></pre>
</div>
</div>

</body>
</html>