<!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>[13964] CalendarServer/trunk/calendarserver/tools/config.py</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/13964">13964</a></dd>
<dt>Author</dt> <dd>sagen@apple.com</dd>
<dt>Date</dt> <dd>2014-09-15 13:03:36 -0700 (Mon, 15 Sep 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Adds --stop --start and --restart to calendarserver_config</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolsconfigpy">CalendarServer/trunk/calendarserver/tools/config.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolsconfigpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/config.py (13963 => 13964)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/config.py        2014-09-15 20:03:34 UTC (rev 13963)
+++ CalendarServer/trunk/calendarserver/tools/config.py        2014-09-15 20:03:36 UTC (rev 13964)
</span><span class="lines">@@ -25,6 +25,7 @@
</span><span class="cx"> import os
</span><span class="cx"> import plistlib
</span><span class="cx"> import signal
</span><ins>+import subprocess
</ins><span class="cx"> import sys
</span><span class="cx"> import xml
</span><span class="cx"> 
</span><span class="lines">@@ -78,6 +79,8 @@
</span><span class="cx"> ]
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
+
</ins><span class="cx"> def usage(e=None):
</span><span class="cx">     if e:
</span><span class="cx">         print(e)
</span><span class="lines">@@ -90,6 +93,9 @@
</span><span class="cx">     print(&quot;options:&quot;)
</span><span class="cx">     print(&quot;  -h --help: print this help and exit&quot;)
</span><span class="cx">     print(&quot;  -f --config: Specify caldavd.plist configuration path&quot;)
</span><ins>+    print(&quot;  -r --restart: Restart the calendar service&quot;)
+    print(&quot;  --start: Start the calendar service and agent&quot;)
+    print(&quot;  --stop: Stop the calendar service and agent&quot;)
</ins><span class="cx">     print(&quot;  -w --writeconfig: Specify caldavd.plist configuration path for writing&quot;)
</span><span class="cx"> 
</span><span class="cx">     if e:
</span><span class="lines">@@ -102,10 +108,13 @@
</span><span class="cx"> def main():
</span><span class="cx">     try:
</span><span class="cx">         (optargs, args) = getopt(
</span><del>-            sys.argv[1:], &quot;hf:w:&quot;, [
</del><ins>+            sys.argv[1:], &quot;hf:rw:&quot;, [
</ins><span class="cx">                 &quot;help&quot;,
</span><span class="cx">                 &quot;config=&quot;,
</span><span class="cx">                 &quot;writeconfig=&quot;,
</span><ins>+                &quot;restart&quot;,
+                &quot;start&quot;,
+                &quot;stop&quot;,
</ins><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx">     except GetoptError, e:
</span><span class="lines">@@ -113,6 +122,9 @@
</span><span class="cx"> 
</span><span class="cx">     configFileName = DEFAULT_CONFIG_FILE
</span><span class="cx">     writeConfigFileName = &quot;&quot;
</span><ins>+    doStop = False
+    doStart = False
+    doRestart = False
</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="lines">@@ -124,6 +136,15 @@
</span><span class="cx">         elif opt in (&quot;-w&quot;, &quot;--writeconfig&quot;):
</span><span class="cx">             writeConfigFileName = arg
</span><span class="cx"> 
</span><ins>+        if opt == &quot;--stop&quot;:
+            doStop = True
+
+        if opt == &quot;--start&quot;:
+            doStart = True
+
+        if opt in (&quot;-r&quot;, &quot;--restart&quot;):
+            doRestart = True
+
</ins><span class="cx">     try:
</span><span class="cx">         config.load(configFileName)
</span><span class="cx">     except ConfigurationError, e:
</span><span class="lines">@@ -139,13 +160,76 @@
</span><span class="cx">     if not writeConfigFileName:
</span><span class="cx">         writeConfigFileName = configFileName
</span><span class="cx"> 
</span><ins>+    if doStop:
+        setServiceState(&quot;org.calendarserver.agent&quot;, &quot;disable&quot;)
+        setServiceState(&quot;org.calendarserver.calendarserver&quot;, &quot;disable&quot;)
+        setEnabled(False)
+
+    if doStart:
+        setServiceState(&quot;org.calendarserver.agent&quot;, &quot;enable&quot;)
+        setServiceState(&quot;org.calendarserver.calendarserver&quot;, &quot;enable&quot;)
+        setEnabled(True)
+
+    if doStart or doStop:
+        setReverseProxies()
+        sys.exit(0)
+
+    if doRestart:
+        restartService(config.PIDFile)
+        sys.exit(0)
+
</ins><span class="cx">     writable = WritableConfig(config, writeConfigFileName)
</span><span class="cx">     writable.read()
</span><span class="cx"> 
</span><span class="cx">     processArgs(writable, args)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+def setServiceState(service, state):
+    &quot;&quot;&quot;
+    Invoke serverctl to enable/disable a service
+    &quot;&quot;&quot;
+    SERVERCTL = &quot;/Applications/Server.app/Contents/ServerRoot/usr/sbin/serverctl&quot;
+    child = subprocess.Popen(
+        args=[SERVERCTL, state, &quot;service={}&quot;.format(service)],
+        stdin=subprocess.PIPE,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+    )
+    output, error = child.communicate()
+    if child.returncode:
+        sys.stdout.write(
+            &quot;Error from serverctl: %d, %s&quot; % (child.returncode, error)
+        )
</ins><span class="cx"> 
</span><ins>+
+def setEnabled(enabled):
+    command = {
+        &quot;command&quot;: &quot;writeConfig&quot;,
+        &quot;Values&quot;: {
+            &quot;EnableCalDAV&quot;: enabled,
+            &quot;EnableCardDAV&quot;: enabled,
+        },
+    }
+
+    runner = Runner([command], quiet=True)
+    runner.run()
+
+
+def setReverseProxies():
+    &quot;&quot;&quot;
+    Invoke calendarserver_reverse_proxies
+    &quot;&quot;&quot;
+    SERVERCTL = &quot;/Applications/Server.app/Contents/ServerRoot/usr/libexec/calendarserver_reverse_proxies&quot;
+    child = subprocess.Popen(
+        args=[SERVERCTL, ],
+        stdin=subprocess.PIPE,
+        stdout=subprocess.PIPE,
+        stderr=subprocess.PIPE,
+    )
+    child.communicate()
+
+
+
</ins><span class="cx"> def processArgs(writable, args, restart=True):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Perform the read/write operations requested in the command line args.
</span><span class="lines">@@ -207,12 +291,13 @@
</span><span class="cx">     dictionaries with a &quot;command&quot; key, plus command-specific data.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx"> 
</span><del>-    def __init__(self, commands):
</del><ins>+    def __init__(self, commands, quiet=False):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         @param commands: the commands to run
</span><span class="cx">         @type commands: list of plist strings
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         self.commands = commands
</span><ins>+        self.quiet = quiet
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx">     def validate(self):
</span><span class="lines">@@ -291,7 +376,8 @@
</span><span class="cx">             respond(command, {&quot;error&quot;: str(e)})
</span><span class="cx">         else:
</span><span class="cx">             config.reload()
</span><del>-            self.command_readConfig(command)
</del><ins>+            if not self.quiet:
+                self.command_readConfig(command)
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>