~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to daemons/poppy-sftp.tac

  • Committer: Ian Booth
  • Date: 2011-05-09 14:31:50 UTC
  • mto: This revision was merged to the branch mainline in revision 13063.
  • Revision ID: ian.booth@canonical.com-20110509143150-aw3y0f98mqdwpql8
Move gpghandler job to poppy-sftp.tac

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
#     twistd -noy sftp.tac
6
6
# or similar.  Refer to the twistd(1) man page for details.
7
7
 
 
8
import atexit
8
9
import logging
9
10
 
10
11
from twisted.application import service
11
12
from twisted.conch.interfaces import ISession
12
13
from twisted.conch.ssh import filetransfer
13
14
from twisted.cred.portal import IRealm, Portal
 
15
from twisted.internet import task
 
16
from twisted.internet.error import AlreadyCancelled
14
17
from twisted.protocols.policies import TimeoutFactory
15
18
from twisted.python import components
16
19
from twisted.web.xmlrpc import Proxy
17
20
 
 
21
from zope.component import getUtility
 
22
from zope.component.interfaces import ComponentLookupError
18
23
from zope.interface import implements
19
24
 
20
25
from canonical.config import config
21
26
from canonical.launchpad.daemons import readyservice
 
27
from canonical.launchpad.interfaces.gpghandler import IGPGHandler
22
28
from canonical.launchpad.scripts import execute_zcml_for_scripts
23
29
 
24
30
from lp.poppy import get_poppy_root
69
75
    return SFTPServer(avatar, get_poppy_root())
70
76
 
71
77
 
 
78
class GPGHandlerJob:
 
79
    """Manages the twisted job to touch the files in the gpgconfig directory."""
 
80
    def __init__(self):
 
81
        self._gpghandler_job = None
 
82
        # stop the GPGHandler job on normal termination.
 
83
        atexit.register(self._stopGPGHandlerJob)
 
84
        # start the GPGHandler job
 
85
        self._scheduleGPGHandlerJob()
 
86
 
 
87
    def _scheduleGPGHandlerJob(self, touch_interval=12 * 3600):
 
88
        # Create a job to touch the GPGHandler home directory every so often
 
89
        # so that it does not get cleaned up by any reaper scripts which look
 
90
        # at time last modified.
 
91
 
 
92
        self._stopGPGHandlerJob()
 
93
        try:
 
94
            self._gpghandler_job = task.LoopingCall(
 
95
                getUtility(IGPGHandler).touchConfigurationDirectory)
 
96
            return self._gpghandler_job.start(touch_interval)
 
97
        except ComponentLookupError:
 
98
            # No GPGHandler so no need to start the job.
 
99
            pass
 
100
 
 
101
    def _stopGPGHandlerJob(self):
 
102
        try:
 
103
            if self._gpghandler_job and self._gpghandler_job.running:
 
104
                self._gpghandler_job.stop()
 
105
        except AlreadyCancelled:
 
106
            # So we're already cancelled, meh.
 
107
            pass
 
108
 
 
109
 
72
110
# Connect Python logging to Twisted's logging.
73
111
from lp.services.twistedsupport.loggingsupport import set_up_tacfile_logging
74
112
set_up_tacfile_logging("poppy-sftp", logging.INFO)
109
147
# We need Zope for looking up the GPG utilities.
110
148
execute_zcml_for_scripts()
111
149
 
 
150
# Set up the GPGHandler job
 
151
gpgHandlerJob = GPGHandlerJob()
 
152
 
112
153
# Service that announces when the daemon is ready
113
154
readyservice.ReadyService().setServiceParent(application)