Merge lp:~alecu/ubuntuone-client/use-staggered-sso-ports into lp:ubuntuone-client

Proposed by Alejandro J. Cura
Status: Merged
Approved by: Natalia Bidart
Approved revision: 1070
Merged at revision: 1073
Proposed branch: lp:~alecu/ubuntuone-client/use-staggered-sso-ports
Merge into: lp:ubuntuone-client
Diff against target: 292 lines (+103/-36)
6 files modified
bin/ubuntuone-syncdaemon (+34/-14)
tests/platform/windows/test_ipc.py (+31/-3)
tests/platform/windows/test_tools.py (+3/-3)
ubuntuone/platform/linux/__init__.py (+5/-1)
ubuntuone/platform/windows/__init__.py (+6/-7)
ubuntuone/platform/windows/ipc.py (+24/-8)
To merge this branch: bzr merge lp:~alecu/ubuntuone-client/use-staggered-sso-ports
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+68922@code.launchpad.net

Commit message

Calculate the pb port number from the user id and use tcpactivation to only start one instance.

Description of the change

Calculate the pb port number from the user id and use tcpactivation to only start one instance.

To post a comment you must log in.
1067. By Alejandro J. Cura

merged trunk in

Revision history for this message
Roberto Alsina (ralsina) wrote :

+1 IRL and code review

review: Approve
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

Text conflict in ubuntuone/platform/windows/__init__.py
1 conflicts encountered.

review: Needs Fixing
1068. By Alejandro J. Cura

merged trunk in

1069. By Alejandro J. Cura

merged with trunk

1070. By Alejandro J. Cura

ugly workaround for the circular import on the windows platform modules (see LP: #817559)

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

All good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/ubuntuone-syncdaemon'
2--- bin/ubuntuone-syncdaemon 2011-07-26 02:26:33 +0000
3+++ bin/ubuntuone-syncdaemon 2011-07-28 14:37:29 +0000
4@@ -45,17 +45,28 @@
5
6 from ubuntuone.syncdaemon.main import Main
7
8-from twisted.internet import reactor
9+from twisted.internet import reactor, defer
10 from ubuntuone.platform.xdg_base_directory import (
11 xdg_cache_home,
12 xdg_data_home,
13 )
14
15
16+class DeathException(Exception):
17+ """The process has commited seppuku."""
18+
19+
20 def die(msg):
21+ """Write the error message an die."""
22 logger.root_logger.warning(msg)
23 sys.stderr.write(msg + '\n')
24- sys.exit(1)
25+ raise DeathException()
26+
27+
28+def check_death(failure):
29+ """Stop the reactor and exit the process."""
30+ failure.trap(DeathException)
31+ reactor.callWhenRunning(reactor.stop)
32
33
34 def main(argv):
35@@ -69,7 +80,24 @@
36 configs.extend(get_config_files())
37 (parser, options, argv) = config.configglue(file(configs[0]), *configs[1:],
38 args=args, usage=usage)
39-
40+ d = async_main(parser, options, argv)
41+ d.addErrback(check_death)
42+
43+ if options.debug_lsprof_file:
44+ try:
45+ from bzrlib.lsprof import profile
46+ ret, stats = profile(reactor.run)
47+ stats.save(options.debug_lsprof_file)
48+ except ImportError:
49+ logger.root_logger.warning('bzrlib.lsprof not available')
50+ reactor.run()
51+ else:
52+ reactor.run()
53+
54+
55+@defer.inlineCallbacks
56+def async_main(parser, options, argv):
57+ """The client entry point that can yield."""
58 logger.init()
59 if options.debug:
60 logger.set_debug('stdout file')
61@@ -92,7 +120,9 @@
62 die('Files synchronization is disabled.')
63
64 # check if there is another instance running
65- if is_already_running():
66+ is_running = yield is_already_running()
67+
68+ if is_running:
69 die('Another instance is running')
70
71 # check if we are using xdg_data_home and it doesn't exists
72@@ -173,16 +203,6 @@
73 guppy.heapy.RM.on()
74
75 main.start()
76- if options.debug_lsprof_file:
77- try:
78- from bzrlib.lsprof import profile
79- ret, stats = profile(reactor.run)
80- stats.save(options.debug_lsprof_file)
81- except ImportError:
82- logger.root_logger.warning('bzrlib.lsprof not available')
83- reactor.run()
84- else:
85- reactor.run()
86
87
88 if __name__ == '__main__':
89
90=== modified file 'tests/platform/windows/test_ipc.py'
91--- tests/platform/windows/test_ipc.py 2011-07-27 20:38:16 +0000
92+++ tests/platform/windows/test_ipc.py 2011-07-28 14:37:29 +0000
93@@ -61,7 +61,7 @@
94 SharesClient
95 )
96
97-TEST_HOST_PORT = "127.0.0.1", 40404
98+TEST_PORT = 40404
99
100
101 class FakeActivationClient(object):
102@@ -95,7 +95,7 @@
103
104 def setUp(self):
105 """Initialize this test instance."""
106- self.patch(ipc, "HOST_PORT", TEST_HOST_PORT)
107+ self.patch(ipc, "get_sd_pb_port", lambda: TEST_PORT)
108 self.patch(ipc, "ActivationClient", FakeActivationClient)
109
110
111@@ -756,7 +756,7 @@
112 yield ipc_client_connect(fake_factory)
113 self.assertEqual(len(fake_reactor.connections), 1)
114 c = fake_reactor.connections[0]
115- self.assertEqual(c.port, TEST_HOST_PORT[1])
116+ self.assertEqual(c.port, TEST_PORT)
117 self.assertEqual(c.factory, fake_factory)
118
119
120@@ -3377,3 +3377,31 @@
121 """Test the connect method when autoconnecting fails."""
122 d = self.ipc.connect(autoconnecting=True)
123 yield self.assertFailure(d, ipc.NoAccessToken)
124+
125+
126+class IPCPortTestCase(TestCase):
127+ """Tests for the ipc port setup."""
128+
129+ def test_get_sd_pb_port(self):
130+ """A test for the get_sd_pb_port function."""
131+ sso_port = 50001
132+ self.patch(ipc, "get_sso_pb_port", lambda: sso_port)
133+ result = ipc.get_sd_pb_port()
134+ expected = sso_port + ipc.SD_SSO_PORT_OFFSET
135+ self.assertEqual(result, expected)
136+
137+ @defer.inlineCallbacks
138+ def test_is_already_running_no(self):
139+ """Test the is_already_running function."""
140+ self.patch(ipc.ActivationInstance, "get_port",
141+ lambda _: defer.succeed(TEST_PORT))
142+ is_running = yield ipc.is_already_running()
143+ self.assertFalse(is_running, "Should not be running.")
144+
145+ @defer.inlineCallbacks
146+ def test_is_already_running_yes(self):
147+ """Test the is_already_running function."""
148+ self.patch(ipc.ActivationInstance, "get_port",
149+ lambda _: defer.fail(ipc.AlreadyStartedError()))
150+ is_running = yield ipc.is_already_running()
151+ self.assertTrue(is_running, "Should be running by now.")
152
153=== modified file 'tests/platform/windows/test_tools.py'
154--- tests/platform/windows/test_tools.py 2011-07-27 20:38:16 +0000
155+++ tests/platform/windows/test_tools.py 2011-07-28 14:37:29 +0000
156@@ -43,7 +43,7 @@
157 SyncDaemonTool,
158 )
159
160-TEST_TOOLS_HOST_PORT = "127.0.0.1", 40405
161+TEST_TOOLS_PORT = 40405
162
163
164 class SaveProtocolServerFactory(PBServerFactory):
165@@ -136,9 +136,9 @@
166 self.folders, self.files)
167 self.server_factory = SaveProtocolServerFactory(self.syncdaemon_root)
168 # pylint: disable=E1101
169- self.patch(ipc, "HOST_PORT", TEST_TOOLS_HOST_PORT)
170+ self.patch(ipc, "get_sd_pb_port", lambda: TEST_TOOLS_PORT)
171 self.patch(ipc.ActivationClient, "get_active_port",
172- lambda _: defer.succeed(TEST_TOOLS_HOST_PORT[1]))
173+ lambda _: defer.succeed(TEST_TOOLS_PORT))
174 self.listener = ipc_server_listen(self.server_factory)
175 self.sdtool = SyncDaemonTool()
176
177
178=== modified file 'ubuntuone/platform/linux/__init__.py'
179--- ubuntuone/platform/linux/__init__.py 2011-07-27 20:38:16 +0000
180+++ ubuntuone/platform/linux/__init__.py 2011-07-28 14:37:29 +0000
181@@ -24,6 +24,7 @@
182
183 import dbus
184 from dbus.mainloop.glib import DBusGMainLoop
185+from twisted.internet import defer
186
187 from ubuntuone.platform.linux.os_helper import (
188 access,
189@@ -101,4 +102,7 @@
190 bus = dbus.SessionBus()
191 request = bus.request_name(dbus_interface.DBUS_IFACE_NAME,
192 dbus.bus.NAME_FLAG_DO_NOT_QUEUE)
193- return request == dbus.bus.REQUEST_NAME_REPLY_EXISTS
194+ if request == dbus.bus.REQUEST_NAME_REPLY_EXISTS:
195+ return defer.succeed(True)
196+ else:
197+ return defer.succeed(False)
198
199=== modified file 'ubuntuone/platform/windows/__init__.py'
200--- ubuntuone/platform/windows/__init__.py 2011-07-27 20:38:16 +0000
201+++ ubuntuone/platform/windows/__init__.py 2011-07-28 14:37:29 +0000
202@@ -63,6 +63,12 @@
203 from ubuntuone.platform.windows.notification import Notification
204
205
206+def is_already_running():
207+ """An ugly workaround for circular imports, see bug #817559."""
208+ from ubuntuone.platform.windows.ipc import is_already_running
209+ return is_already_running()
210+
211+
212 class ExternalInterface(object):
213 """An ExternalInterface implemented with a IPC interface."""
214
215@@ -92,10 +98,3 @@
216
217 def connect(self, *args, **kwargs):
218 self.dbus_iface.connect(*args, **kwargs)
219-
220-
221-def is_already_running():
222- """Return if the sd is running by asking the named pipe."""
223- #TODO: Do not start two instances of this process
224- # https://launchpad.net/bugs/803672
225- return False
226
227=== modified file 'ubuntuone/platform/windows/ipc.py'
228--- ubuntuone/platform/windows/ipc.py 2011-07-21 14:54:14 +0000
229+++ ubuntuone/platform/windows/ipc.py 2011-07-28 14:37:29 +0000
230@@ -29,8 +29,13 @@
231 Referenceable,
232 Root,
233 )
234-from ubuntu_sso.main.windows import get_activation_cmdline
235-from ubuntu_sso.utils.tcpactivation import ActivationClient, ActivationConfig
236+from ubuntu_sso.main.windows import get_activation_cmdline, get_sso_pb_port
237+from ubuntu_sso.utils.tcpactivation import (
238+ ActivationClient,
239+ ActivationConfig,
240+ ActivationInstance,
241+ AlreadyStartedError,
242+)
243 from ubuntuone.syncdaemon.interfaces import IMarker
244 from ubuntuone.platform.windows import CredentialsManagementTool
245 from ubuntuone.platform.windows.network_manager import NetworkManager
246@@ -51,19 +56,19 @@
247
248 logger = logging.getLogger("ubuntuone.SyncDaemon.Pb")
249 LOCALHOST = "127.0.0.1"
250-HOST_PORT = LOCALHOST, 50002
251+SD_SSO_PORT_OFFSET = 1
252 SD_SERVICE_NAME = "ubuntuone-syncdaemon"
253 CLIENT_NOT_PROCESSED = -1
254
255
256-def get_sd_pb_hostport():
257+def get_sd_pb_port():
258 """Returns the host and port for this user."""
259- return HOST_PORT
260+ return get_sso_pb_port() + SD_SSO_PORT_OFFSET
261
262
263 def get_activation_config():
264 """Get the configuration to activate the sso service."""
265- _, port = get_sd_pb_hostport()
266+ port = get_sd_pb_port()
267 service_name = SD_SERVICE_NAME
268 cmdline = get_activation_cmdline(service_name)
269 return ActivationConfig(service_name, cmdline, port)
270@@ -71,9 +76,20 @@
271
272 def ipc_server_listen(server_factory):
273 """Connect the IPC server factory."""
274- host, port = get_sd_pb_hostport()
275+ port = get_sd_pb_port()
276 # pylint: disable=E1101
277- return reactor.listenTCP(port, server_factory, interface=host)
278+ return reactor.listenTCP(port, server_factory, interface=LOCALHOST)
279+
280+
281+@defer.inlineCallbacks
282+def is_already_running():
283+ """Return if the sd is running by trying to get the port."""
284+ ai = ActivationInstance(get_activation_config())
285+ try:
286+ yield ai.get_port()
287+ defer.returnValue(False)
288+ except AlreadyStartedError:
289+ defer.returnValue(True)
290
291
292 @defer.inlineCallbacks

Subscribers

People subscribed via source and target branches