<!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 = "/Applications/Server.app/Contents/ServerRoot"
+CALENDARSERVER_CONFIG = "%s/usr/sbin/calendarserver_config" % (SERVER_APP_ROOT,)
</ins><span class="cx">
</span><ins>+
+def serverRootLocation():
+ """
+ Return the ServerRoot value from the servermgr_calendar.plist. If not
+ present, return the default.
+ """
+ plist = "/Library/Preferences/com.apple.servermgr_calendar.plist"
+ serverRoot = u"/Library/Server/Calendar and Contacts"
+ if os.path.exists(plist):
+ serverRoot = plistlib.readPlist(plist).get("ServerRoot", 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("Usage: %s [-hv] old-ip new-ip [old-hostname new-hostname]" % (name,))
</span><span class="cx"> print(" Options:")
</span><span class="cx"> print(" -h - print this message and exit")
</span><del>- print(" -v - print additional information when running")
</del><ins>+ print(" -f <file> - path to config file")
</ins><span class="cx"> print(" Arguments:")
</span><span class="cx"> print(" old-ip - current IPv4 address of the server")
</span><span class="cx"> print(" new-ip - new IPv4 address of the server")
</span><span class="lines">@@ -32,7 +51,21 @@
</span><span class="cx"> print(" new-hostname - new FQDN for the server")
</span><span class="cx">
</span><span class="cx">
</span><ins>+def log(msg):
+ serverRoot = serverRootLocation()
+ logDir = os.path.join(serverRoot, "Logs")
+ logFile = os.path.join(logDir, "changeip.log")
</ins><span class="cx">
</span><ins>+ try:
+ timestamp = datetime.datetime.now().strftime("%b %d %H:%M:%S")
+ msg = "changeip_calendar: %s %s" % (timestamp, msg)
+ with open(logFile, 'a') as output:
+ output.write("%s\n" % (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:], "hvf:", [
</del><ins>+ sys.argv[1:], "hf:", [
</ins><span class="cx"> "help",
</span><del>- "verbose",
</del><span class="cx"> "config=",
</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 = "/Library/Server/Calendar and Contacts/Config/caldavd-system.plist"
</del><ins>+ configFile = None
</ins><span class="cx">
</span><span class="cx"> for opt, arg in optargs:
</span><span class="cx"> if opt in ("-h", "--help"):
</span><span class="cx"> usage()
</span><span class="cx"> sys.exit(1)
</span><span class="cx">
</span><del>- elif opt in ("-v", "--verbose"):
- verbose = True
-
</del><span class="cx"> elif opt in ("-f", "--config"):
</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("Calendar Server: updating %s" % (configFile,))
</del><ins>+ log("args: {}".format(args))
</ins><span class="cx">
</span><del>- try:
- plist = readPlist(configFile)
- except IOError:
- print("Error: could not open %s" % (configFile,))
- sys.exit(1)
- except Exception, e:
- print("Error: could not parse %s" % (configFile,))
- raise e
</del><ins>+ config = readConfig(configFile=configFile)
</ins><span class="cx">
</span><del>- writePlist(plist, "%s.changeip.bak" % (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("Calendar Server: done")
</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("-f {}".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("Sending to calendarserver_config: {}".format(commandString))
+
+ output, error = child.communicate(input=commandString)
+ log("Output from calendarserver_config: {}".format(output))
+ if child.returncode:
+ log(
+ "Error from calendarserver_config: {}, {}".format(
+ child.returncode, error
+ )
+ )
+ return None
+ else:
+ return plistlib.readPlistFromString(output)["result"]
+
+
+def readConfig(configFile=None):
+ """
+ Ask calendarserver_config for the current configuration
+ """
+
+ command = {
+ "command": "readConfig"
+ }
+ return sendCommand(command)
+
+
+
+def writeConfig(valuesDict, configFile=None):
+ """
+ Ask calendarserver_config to update the configuration
+ """
+ command = {
+ "command": "writeConfig",
+ "Values": valuesDict,
+ }
+ return sendCommand(command)
+
+
+def updateConfig(
+ config,
+ oldIP, newIP,
+ oldHostname, newHostname,
+ configFile=None
+):
+
</ins><span class="cx"> keys = (
</span><del>- ("Authentication", "Wiki", "Hostname"),
- ("BindAddresses",),
</del><span class="cx"> ("Scheduling", "iMIP", "Receiving", "Server"),
</span><span class="cx"> ("Scheduling", "iMIP", "Sending", "Server"),
</span><span class="cx"> ("Scheduling", "iMIP", "Sending", "Address"),
</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("Changed %s -> %s" % (value, newValue))
</del><ins>+ if value != newValue:
+ log("Changed %s -> %s" % (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>- "Authentication" : {
- "Wiki" : {
- "Hostname" : "original_hostname",
- "Other" : "should_be_untouched",
- },
- },
- "Untouched" : "dont_change_me",
- "BindAddresses" : [
- "10.1.1.1",
- "192.168.1.1",
- "original_hostname",
- ],
- "ServerHostName" : "",
- "Scheduling" : {
- "iMIP" : {
- "Receiving" : {
- "Server" : "original_hostname",
</del><ins>+ "Untouched": "dont_change_me",
+ "ServerHostName": "",
+ "Scheduling": {
+ "iMIP": {
+ "Receiving": {
+ "Server": "original_hostname",
</ins><span class="cx"> },
</span><del>- "Sending" : {
- "Server" : "original_hostname",
- "Address" : "user@original_hostname",
</del><ins>+ "Sending": {
+ "Server": "original_hostname",
+ "Address": "user@original_hostname",
</ins><span class="cx"> },
</span><span class="cx"> },
</span><span class="cx"> },
</span><span class="cx"> }
</span><span class="cx">
</span><del>- updatePlist(plist, "10.1.1.1", "10.1.1.2", "original_hostname",
- "new_hostname")
</del><ins>+ updateConfig(
+ plist, "10.1.1.1", "10.1.1.2", "original_hostname", "new_hostname"
+ )
</ins><span class="cx">
</span><del>- self.assertEquals(plist,
</del><ins>+ self.assertEquals(
+ plist,
</ins><span class="cx"> {
</span><del>- "Authentication" : {
- "Wiki" : {
- "Hostname" : "new_hostname",
- "Other" : "should_be_untouched",
- },
- },
- "Untouched" : "dont_change_me",
- "BindAddresses" : [
- "10.1.1.2",
- "192.168.1.1",
- "new_hostname",
- ],
- "ServerHostName" : "",
- "Scheduling" : {
- "iMIP" : {
- "Receiving" : {
- "Server" : "new_hostname",
</del><ins>+ "Untouched": "dont_change_me",
+ "ServerHostName": "",
+ "Scheduling": {
+ "iMIP": {
+ "Receiving": {
+ "Server": "new_hostname",
</ins><span class="cx"> },
</span><del>- "Sending" : {
- "Server" : "new_hostname",
- "Address" : "user@new_hostname",
</del><ins>+ "Sending": {
+ "Server": "new_hostname",
+ "Address": "user@new_hostname",
</ins><span class="cx"> },
</span><span class="cx"> },
</span><span class="cx"> },
</span></span></pre>
</div>
</div>
</body>
</html>