Show More
@@ -1,412 +1,412 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """ |
|
3 | """ | |
4 | The ipcluster application. |
|
4 | The ipcluster application. | |
5 | """ |
|
5 | """ | |
6 |
|
6 | |||
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (C) 2008-2009 The IPython Development Team |
|
8 | # Copyright (C) 2008-2009 The IPython Development Team | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the BSD License. The full license is in |
|
10 | # Distributed under the terms of the BSD License. The full license is in | |
11 | # the file COPYING, distributed as part of this software. |
|
11 | # the file COPYING, distributed as part of this software. | |
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 |
|
13 | |||
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
15 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | import logging |
|
18 | import logging | |
19 | import os |
|
19 | import os | |
20 | import signal |
|
20 | import signal | |
21 | import sys |
|
21 | import sys | |
22 |
|
22 | |||
23 | if os.name=='posix': |
|
23 | if os.name=='posix': | |
24 | from twisted.scripts._twistd_unix import daemonize |
|
24 | from twisted.scripts._twistd_unix import daemonize | |
25 |
|
25 | |||
26 | from IPython.core import release |
|
26 | from IPython.core import release | |
27 | from IPython.external import argparse |
|
27 | from IPython.external import argparse | |
28 | from IPython.config.loader import ArgParseConfigLoader, NoConfigDefault |
|
28 | from IPython.config.loader import ArgParseConfigLoader, NoConfigDefault | |
29 | from IPython.utils.importstring import import_item |
|
29 | from IPython.utils.importstring import import_item | |
30 |
|
30 | |||
31 | from IPython.kernel.clusterdir import ( |
|
31 | from IPython.kernel.clusterdir import ( | |
32 | ApplicationWithClusterDir, ClusterDirError, PIDFileError |
|
32 | ApplicationWithClusterDir, ClusterDirError, PIDFileError | |
33 | ) |
|
33 | ) | |
34 |
|
34 | |||
35 | from twisted.internet import reactor |
|
35 | from twisted.internet import reactor | |
36 | from twisted.python import log |
|
36 | from twisted.python import log | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | # The ipcluster application |
|
40 | # The ipcluster application | |
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | # Exit codes for ipcluster |
|
44 | # Exit codes for ipcluster | |
45 |
|
45 | |||
46 | # This will be the exit code if the ipcluster appears to be running because |
|
46 | # This will be the exit code if the ipcluster appears to be running because | |
47 | # a .pid file exists |
|
47 | # a .pid file exists | |
48 | ALREADY_STARTED = 10 |
|
48 | ALREADY_STARTED = 10 | |
49 |
|
49 | |||
50 | # This will be the exit code if ipcluster stop is run, but there is not .pid |
|
50 | # This will be the exit code if ipcluster stop is run, but there is not .pid | |
51 | # file to be found. |
|
51 | # file to be found. | |
52 | ALREADY_STOPPED = 11 |
|
52 | ALREADY_STOPPED = 11 | |
53 |
|
53 | |||
54 |
|
54 | |||
55 | class IPClusterCLLoader(ArgParseConfigLoader): |
|
55 | class IPClusterCLLoader(ArgParseConfigLoader): | |
56 |
|
56 | |||
57 | def _add_arguments(self): |
|
57 | def _add_arguments(self): | |
58 | # This has all the common options that all subcommands use |
|
58 | # This has all the common options that all subcommands use | |
59 | parent_parser1 = argparse.ArgumentParser(add_help=False) |
|
59 | parent_parser1 = argparse.ArgumentParser(add_help=False) | |
60 | parent_parser1.add_argument('--ipython-dir', |
|
60 | parent_parser1.add_argument('--ipython-dir', | |
61 | dest='Global.ipython_dir',type=unicode, |
|
61 | dest='Global.ipython_dir',type=unicode, | |
62 | help='Set to override default location of Global.ipython_dir.', |
|
62 | help='Set to override default location of Global.ipython_dir.', | |
63 | default=NoConfigDefault, |
|
63 | default=NoConfigDefault, | |
64 | metavar='Global.ipython_dir') |
|
64 | metavar='Global.ipython_dir') | |
65 | parent_parser1.add_argument('--log-level', |
|
65 | parent_parser1.add_argument('--log-level', | |
66 | dest="Global.log_level",type=int, |
|
66 | dest="Global.log_level",type=int, | |
67 | help='Set the log level (0,10,20,30,40,50). Default is 30.', |
|
67 | help='Set the log level (0,10,20,30,40,50). Default is 30.', | |
68 | default=NoConfigDefault, |
|
68 | default=NoConfigDefault, | |
69 | metavar='Global.log_level') |
|
69 | metavar='Global.log_level') | |
70 |
|
70 | |||
71 | # This has all the common options that other subcommands use |
|
71 | # This has all the common options that other subcommands use | |
72 | parent_parser2 = argparse.ArgumentParser(add_help=False) |
|
72 | parent_parser2 = argparse.ArgumentParser(add_help=False) | |
73 | parent_parser2.add_argument('-p','--profile', |
|
73 | parent_parser2.add_argument('-p','--profile', | |
74 | dest='Global.profile',type=unicode, |
|
74 | dest='Global.profile',type=unicode, | |
75 | default=NoConfigDefault, |
|
75 | default=NoConfigDefault, | |
76 | help='The string name of the profile to be used. This determines ' |
|
76 | help='The string name of the profile to be used. This determines ' | |
77 | 'the name of the cluster dir as: cluster_<profile>. The default profile ' |
|
77 | 'the name of the cluster dir as: cluster_<profile>. The default profile ' | |
78 | 'is named "default". The cluster directory is resolve this way ' |
|
78 | 'is named "default". The cluster directory is resolve this way ' | |
79 | 'if the --cluster-dir option is not used.', |
|
79 | 'if the --cluster-dir option is not used.', | |
80 | default=NoConfigDefault, |
|
80 | default=NoConfigDefault, | |
81 | metavar='Global.profile') |
|
81 | metavar='Global.profile') | |
82 | parent_parser2.add_argument('--cluster-dir', |
|
82 | parent_parser2.add_argument('--cluster-dir', | |
83 | dest='Global.cluster_dir',type=unicode, |
|
83 | dest='Global.cluster_dir',type=unicode, | |
84 | default=NoConfigDefault, |
|
84 | default=NoConfigDefault, | |
85 | help='Set the cluster dir. This overrides the logic used by the ' |
|
85 | help='Set the cluster dir. This overrides the logic used by the ' | |
86 | '--profile option.', |
|
86 | '--profile option.', | |
87 | default=NoConfigDefault, |
|
87 | default=NoConfigDefault, | |
88 | metavar='Global.cluster_dir'), |
|
88 | metavar='Global.cluster_dir'), | |
89 | parent_parser2.add_argument('--working-dir', |
|
89 | parent_parser2.add_argument('--working-dir', | |
90 | dest='Global.working_dir',type=unicode, |
|
90 | dest='Global.working_dir',type=unicode, | |
91 | help='Set the working dir for the process.', |
|
91 | help='Set the working dir for the process.', | |
92 | default=NoConfigDefault, |
|
92 | default=NoConfigDefault, | |
93 | metavar='Global.working_dir') |
|
93 | metavar='Global.working_dir') | |
94 | parent_parser2.add_argument('--log-to-file', |
|
94 | parent_parser2.add_argument('--log-to-file', | |
95 | action='store_true', dest='Global.log_to_file', |
|
95 | action='store_true', dest='Global.log_to_file', | |
96 | default=NoConfigDefault, |
|
96 | default=NoConfigDefault, | |
97 | help='Log to a file in the log directory (default is stdout)' |
|
97 | help='Log to a file in the log directory (default is stdout)' | |
98 | ) |
|
98 | ) | |
99 |
|
99 | |||
100 | subparsers = self.parser.add_subparsers( |
|
100 | subparsers = self.parser.add_subparsers( | |
101 | dest='Global.subcommand', |
|
101 | dest='Global.subcommand', | |
102 | title='ipcluster subcommands', |
|
102 | title='ipcluster subcommands', | |
103 | description='ipcluster has a variety of subcommands. ' |
|
103 | description='ipcluster has a variety of subcommands. ' | |
104 | 'The general way of running ipcluster is "ipcluster <cmd> ' |
|
104 | 'The general way of running ipcluster is "ipcluster <cmd> ' | |
105 | ' [options]""', |
|
105 | ' [options]""', | |
106 | help='For more help, type "ipcluster <cmd> -h"') |
|
106 | help='For more help, type "ipcluster <cmd> -h"') | |
107 |
|
107 | |||
108 | parser_list = subparsers.add_parser( |
|
108 | parser_list = subparsers.add_parser( | |
109 | 'list', |
|
109 | 'list', | |
110 | help='List all clusters in cwd and ipython_dir.', |
|
110 | help='List all clusters in cwd and ipython_dir.', | |
111 | parents=[parent_parser1] |
|
111 | parents=[parent_parser1] | |
112 | ) |
|
112 | ) | |
113 |
|
113 | |||
114 | parser_create = subparsers.add_parser( |
|
114 | parser_create = subparsers.add_parser( | |
115 | 'create', |
|
115 | 'create', | |
116 | help='Create a new cluster directory.', |
|
116 | help='Create a new cluster directory.', | |
117 | parents=[parent_parser1, parent_parser2] |
|
117 | parents=[parent_parser1, parent_parser2] | |
118 | ) |
|
118 | ) | |
119 | parser_create.add_argument( |
|
119 | parser_create.add_argument( | |
120 | '--reset-config', |
|
120 | '--reset-config', | |
121 | dest='Global.reset_config', action='store_true', |
|
121 | dest='Global.reset_config', action='store_true', | |
122 | default=NoConfigDefault, |
|
122 | default=NoConfigDefault, | |
123 | help='Recopy the default config files to the cluster directory. ' |
|
123 | help='Recopy the default config files to the cluster directory. ' | |
124 | 'You will loose any modifications you have made to these files.' |
|
124 | 'You will loose any modifications you have made to these files.' | |
125 | ) |
|
125 | ) | |
126 |
|
126 | |||
127 | parser_start = subparsers.add_parser( |
|
127 | parser_start = subparsers.add_parser( | |
128 | 'start', |
|
128 | 'start', | |
129 | help='Start a cluster.', |
|
129 | help='Start a cluster.', | |
130 | parents=[parent_parser1, parent_parser2] |
|
130 | parents=[parent_parser1, parent_parser2] | |
131 | ) |
|
131 | ) | |
132 | parser_start.add_argument( |
|
132 | parser_start.add_argument( | |
133 | '-n', '--number', |
|
133 | '-n', '--number', | |
134 | type=int, dest='Global.n', |
|
134 | type=int, dest='Global.n', | |
135 | default=NoConfigDefault, |
|
135 | default=NoConfigDefault, | |
136 | help='The number of engines to start.', |
|
136 | help='The number of engines to start.', | |
137 | metavar='Global.n' |
|
137 | metavar='Global.n' | |
138 | ) |
|
138 | ) | |
139 | parser_start.add_argument('--clean-logs', |
|
139 | parser_start.add_argument('--clean-logs', | |
140 | dest='Global.clean_logs', action='store_true', |
|
140 | dest='Global.clean_logs', action='store_true', | |
141 | help='Delete old log flies before starting.', |
|
141 | help='Delete old log flies before starting.', | |
142 | default=NoConfigDefault |
|
142 | default=NoConfigDefault | |
143 | ) |
|
143 | ) | |
144 | parser_start.add_argument('--no-clean-logs', |
|
144 | parser_start.add_argument('--no-clean-logs', | |
145 | dest='Global.clean_logs', action='store_false', |
|
145 | dest='Global.clean_logs', action='store_false', | |
146 | help="Don't delete old log flies before starting.", |
|
146 | help="Don't delete old log flies before starting.", | |
147 | default=NoConfigDefault |
|
147 | default=NoConfigDefault | |
148 | ) |
|
148 | ) | |
149 | parser_start.add_argument('--daemon', |
|
149 | parser_start.add_argument('--daemon', | |
150 | dest='Global.daemonize', action='store_true', |
|
150 | dest='Global.daemonize', action='store_true', | |
151 | help='Daemonize the ipcluster program. This implies --log-to-file', |
|
151 | help='Daemonize the ipcluster program. This implies --log-to-file', | |
152 | default=NoConfigDefault |
|
152 | default=NoConfigDefault | |
153 | ) |
|
153 | ) | |
154 | parser_start.add_argument('--no-daemon', |
|
154 | parser_start.add_argument('--no-daemon', | |
155 | dest='Global.daemonize', action='store_false', |
|
155 | dest='Global.daemonize', action='store_false', | |
156 | help="Dont't daemonize the ipcluster program.", |
|
156 | help="Dont't daemonize the ipcluster program.", | |
157 | default=NoConfigDefault |
|
157 | default=NoConfigDefault | |
158 | ) |
|
158 | ) | |
159 |
|
159 | |||
160 | parser_start = subparsers.add_parser( |
|
160 | parser_start = subparsers.add_parser( | |
161 | 'stop', |
|
161 | 'stop', | |
162 | help='Stop a cluster.', |
|
162 | help='Stop a cluster.', | |
163 | parents=[parent_parser1, parent_parser2] |
|
163 | parents=[parent_parser1, parent_parser2] | |
164 | ) |
|
164 | ) | |
165 | parser_start.add_argument('--signal', |
|
165 | parser_start.add_argument('--signal', | |
166 | dest='Global.signal', type=int, |
|
166 | dest='Global.signal', type=int, | |
167 | help="The signal number to use in stopping the cluster (default=2).", |
|
167 | help="The signal number to use in stopping the cluster (default=2).", | |
168 | metavar="Global.signal", |
|
168 | metavar="Global.signal", | |
169 | default=NoConfigDefault |
|
169 | default=NoConfigDefault | |
170 | ) |
|
170 | ) | |
171 |
|
171 | |||
172 |
|
172 | |||
173 | default_config_file_name = u'ipcluster_config.py' |
|
173 | default_config_file_name = u'ipcluster_config.py' | |
174 |
|
174 | |||
175 |
|
175 | |||
176 | class IPClusterApp(ApplicationWithClusterDir): |
|
176 | class IPClusterApp(ApplicationWithClusterDir): | |
177 |
|
177 | |||
178 | name = u'ipcluster' |
|
178 | name = u'ipcluster' | |
179 | description = 'Start an IPython cluster (controller and engines).' |
|
179 | description = 'Start an IPython cluster (controller and engines).' | |
180 | config_file_name = default_config_file_name |
|
180 | config_file_name = default_config_file_name | |
181 | default_log_level = logging.INFO |
|
181 | default_log_level = logging.INFO | |
182 | auto_create_cluster_dir = False |
|
182 | auto_create_cluster_dir = False | |
183 |
|
183 | |||
184 | def create_default_config(self): |
|
184 | def create_default_config(self): | |
185 | super(IPClusterApp, self).create_default_config() |
|
185 | super(IPClusterApp, self).create_default_config() | |
186 | self.default_config.Global.controller_launcher = \ |
|
186 | self.default_config.Global.controller_launcher = \ | |
187 | 'IPython.kernel.launcher.LocalControllerLauncher' |
|
187 | 'IPython.kernel.launcher.LocalControllerLauncher' | |
188 | self.default_config.Global.engine_launcher = \ |
|
188 | self.default_config.Global.engine_launcher = \ | |
189 | 'IPython.kernel.launcher.LocalEngineSetLauncher' |
|
189 | 'IPython.kernel.launcher.LocalEngineSetLauncher' | |
190 | self.default_config.Global.n = 2 |
|
190 | self.default_config.Global.n = 2 | |
191 | self.default_config.Global.reset_config = False |
|
191 | self.default_config.Global.reset_config = False | |
192 | self.default_config.Global.clean_logs = True |
|
192 | self.default_config.Global.clean_logs = True | |
193 | self.default_config.Global.signal = 2 |
|
193 | self.default_config.Global.signal = 2 | |
194 | self.default_config.Global.daemonize = False |
|
194 | self.default_config.Global.daemonize = False | |
195 |
|
195 | |||
196 | def create_command_line_config(self): |
|
196 | def create_command_line_config(self): | |
197 | """Create and return a command line config loader.""" |
|
197 | """Create and return a command line config loader.""" | |
198 | return IPClusterCLLoader( |
|
198 | return IPClusterCLLoader( | |
199 | description=self.description, |
|
199 | description=self.description, | |
200 | version=release.version |
|
200 | version=release.version | |
201 | ) |
|
201 | ) | |
202 |
|
202 | |||
203 | def find_resources(self): |
|
203 | def find_resources(self): | |
204 | subcommand = self.command_line_config.Global.subcommand |
|
204 | subcommand = self.command_line_config.Global.subcommand | |
205 | if subcommand=='list': |
|
205 | if subcommand=='list': | |
206 | self.list_cluster_dirs() |
|
206 | self.list_cluster_dirs() | |
207 | # Exit immediately because there is nothing left to do. |
|
207 | # Exit immediately because there is nothing left to do. | |
208 | self.exit() |
|
208 | self.exit() | |
209 | elif subcommand=='create': |
|
209 | elif subcommand=='create': | |
210 | self.auto_create_cluster_dir = True |
|
210 | self.auto_create_cluster_dir = True | |
211 | super(IPClusterApp, self).find_resources() |
|
211 | super(IPClusterApp, self).find_resources() | |
212 | elif subcommand=='start' or subcommand=='stop': |
|
212 | elif subcommand=='start' or subcommand=='stop': | |
213 | self.auto_create_cluster_dir = False |
|
213 | self.auto_create_cluster_dir = False | |
214 | try: |
|
214 | try: | |
215 | super(IPClusterApp, self).find_resources() |
|
215 | super(IPClusterApp, self).find_resources() | |
216 | except ClusterDirError: |
|
216 | except ClusterDirError: | |
217 | raise ClusterDirError( |
|
217 | raise ClusterDirError( | |
218 | "Could not find a cluster directory. A cluster dir must " |
|
218 | "Could not find a cluster directory. A cluster dir must " | |
219 | "be created before running 'ipcluster start'. Do " |
|
219 | "be created before running 'ipcluster start'. Do " | |
220 | "'ipcluster create -h' or 'ipcluster list -h' for more " |
|
220 | "'ipcluster create -h' or 'ipcluster list -h' for more " | |
221 | "information about creating and listing cluster dirs." |
|
221 | "information about creating and listing cluster dirs." | |
222 | ) |
|
222 | ) | |
223 |
|
223 | |||
224 | def list_cluster_dirs(self): |
|
224 | def list_cluster_dirs(self): | |
225 | # Find the search paths |
|
225 | # Find the search paths | |
226 | cluster_dir_paths = os.environ.get('IPCLUSTER_DIR_PATH','') |
|
226 | cluster_dir_paths = os.environ.get('IPCLUSTER_DIR_PATH','') | |
227 | if cluster_dir_paths: |
|
227 | if cluster_dir_paths: | |
228 | cluster_dir_paths = cluster_dir_paths.split(':') |
|
228 | cluster_dir_paths = cluster_dir_paths.split(':') | |
229 | else: |
|
229 | else: | |
230 | cluster_dir_paths = [] |
|
230 | cluster_dir_paths = [] | |
231 | try: |
|
231 | try: | |
232 | ipython_dir = self.command_line_config.Global.ipython_dir |
|
232 | ipython_dir = self.command_line_config.Global.ipython_dir | |
233 | except AttributeError: |
|
233 | except AttributeError: | |
234 | ipython_dir = self.default_config.Global.ipython_dir |
|
234 | ipython_dir = self.default_config.Global.ipython_dir | |
235 | paths = [os.getcwd(), ipython_dir] + \ |
|
235 | paths = [os.getcwd(), ipython_dir] + \ | |
236 | cluster_dir_paths |
|
236 | cluster_dir_paths | |
237 | paths = list(set(paths)) |
|
237 | paths = list(set(paths)) | |
238 |
|
238 | |||
239 | self.log.info('Searching for cluster dirs in paths: %r' % paths) |
|
239 | self.log.info('Searching for cluster dirs in paths: %r' % paths) | |
240 | for path in paths: |
|
240 | for path in paths: | |
241 | files = os.listdir(path) |
|
241 | files = os.listdir(path) | |
242 | for f in files: |
|
242 | for f in files: | |
243 | full_path = os.path.join(path, f) |
|
243 | full_path = os.path.join(path, f) | |
244 | if os.path.isdir(full_path) and f.startswith('cluster_'): |
|
244 | if os.path.isdir(full_path) and f.startswith('cluster_'): | |
245 | profile = full_path.split('_')[-1] |
|
245 | profile = full_path.split('_')[-1] | |
246 | start_cmd = '"ipcluster start -n 4 -p %s"' % profile |
|
246 | start_cmd = '"ipcluster start -n 4 -p %s"' % profile | |
247 | print start_cmd + " ==> " + full_path |
|
247 | print start_cmd + " ==> " + full_path | |
248 |
|
248 | |||
249 | def pre_construct(self): |
|
249 | def pre_construct(self): | |
250 | # This is where we cd to the working directory. |
|
250 | # This is where we cd to the working directory. | |
251 | super(IPClusterApp, self).pre_construct() |
|
251 | super(IPClusterApp, self).pre_construct() | |
252 | config = self.master_config |
|
252 | config = self.master_config | |
253 | try: |
|
253 | try: | |
254 | daemon = config.Global.daemonize |
|
254 | daemon = config.Global.daemonize | |
255 | if daemon: |
|
255 | if daemon: | |
256 | config.Global.log_to_file = True |
|
256 | config.Global.log_to_file = True | |
257 | except AttributeError: |
|
257 | except AttributeError: | |
258 | pass |
|
258 | pass | |
259 |
|
259 | |||
260 | def construct(self): |
|
260 | def construct(self): | |
261 | config = self.master_config |
|
261 | config = self.master_config | |
262 | if config.Global.subcommand=='list': |
|
262 | if config.Global.subcommand=='list': | |
263 | pass |
|
263 | pass | |
264 | elif config.Global.subcommand=='create': |
|
264 | elif config.Global.subcommand=='create': | |
265 | self.log.info('Copying default config files to cluster directory ' |
|
265 | self.log.info('Copying default config files to cluster directory ' | |
266 | '[overwrite=%r]' % (config.Global.reset_config,)) |
|
266 | '[overwrite=%r]' % (config.Global.reset_config,)) | |
267 | self.cluster_dir_obj.copy_all_config_files(overwrite=config.Global.reset_config) |
|
267 | self.cluster_dir_obj.copy_all_config_files(overwrite=config.Global.reset_config) | |
268 | elif config.Global.subcommand=='start': |
|
268 | elif config.Global.subcommand=='start': | |
269 | self.start_logging() |
|
269 | self.start_logging() | |
270 | reactor.callWhenRunning(self.start_launchers) |
|
270 | reactor.callWhenRunning(self.start_launchers) | |
271 |
|
271 | |||
272 | def start_launchers(self): |
|
272 | def start_launchers(self): | |
273 | config = self.master_config |
|
273 | config = self.master_config | |
274 |
|
274 | |||
275 | # Create the launchers |
|
275 | # Create the launchers | |
276 | el_class = import_item(config.Global.engine_launcher) |
|
276 | el_class = import_item(config.Global.engine_launcher) | |
277 | self.engine_launcher = el_class( |
|
277 | self.engine_launcher = el_class( | |
278 | self.cluster_dir, config=config |
|
278 | self.cluster_dir, config=config | |
279 | ) |
|
279 | ) | |
280 | cl_class = import_item(config.Global.controller_launcher) |
|
280 | cl_class = import_item(config.Global.controller_launcher) | |
281 | self.controller_launcher = cl_class( |
|
281 | self.controller_launcher = cl_class( | |
282 | self.cluster_dir, config=config |
|
282 | self.cluster_dir, config=config | |
283 | ) |
|
283 | ) | |
284 |
|
284 | |||
285 | # Setup signals |
|
285 | # Setup signals | |
286 | signal.signal(signal.SIGINT, self.stop_launchers) |
|
286 | signal.signal(signal.SIGINT, self.stop_launchers) | |
287 |
|
287 | |||
288 | # Setup the observing of stopping |
|
288 | # Setup the observing of stopping | |
289 | d1 = self.controller_launcher.observe_stop() |
|
289 | d1 = self.controller_launcher.observe_stop() | |
290 | d1.addCallback(self.stop_engines) |
|
290 | d1.addCallback(self.stop_engines) | |
291 | d1.addErrback(self.err_and_stop) |
|
291 | d1.addErrback(self.err_and_stop) | |
292 | # If this triggers, just let them die |
|
292 | # If this triggers, just let them die | |
293 | # d2 = self.engine_launcher.observe_stop() |
|
293 | # d2 = self.engine_launcher.observe_stop() | |
294 |
|
294 | |||
295 | # Start the controller and engines |
|
295 | # Start the controller and engines | |
296 | d = self.controller_launcher.start( |
|
296 | d = self.controller_launcher.start( | |
297 | profile=None, cluster_dir=config.Global.cluster_dir |
|
297 | profile=None, cluster_dir=config.Global.cluster_dir | |
298 | ) |
|
298 | ) | |
299 | d.addCallback(lambda _: self.start_engines()) |
|
299 | d.addCallback(lambda _: self.start_engines()) | |
300 | d.addErrback(self.err_and_stop) |
|
300 | d.addErrback(self.err_and_stop) | |
301 |
|
301 | |||
302 | def err_and_stop(self, f): |
|
302 | def err_and_stop(self, f): | |
303 | log.msg('Unexpected error in ipcluster:') |
|
303 | log.msg('Unexpected error in ipcluster:') | |
304 | log.err(f) |
|
304 | log.err(f) | |
305 | reactor.stop() |
|
305 | reactor.stop() | |
306 |
|
306 | |||
307 | def stop_engines(self, r): |
|
307 | def stop_engines(self, r): | |
308 | return self.engine_launcher.stop() |
|
308 | return self.engine_launcher.stop() | |
309 |
|
309 | |||
310 | def start_engines(self): |
|
310 | def start_engines(self): | |
311 | config = self.master_config |
|
311 | config = self.master_config | |
312 | d = self.engine_launcher.start( |
|
312 | d = self.engine_launcher.start( | |
313 | config.Global.n, |
|
313 | config.Global.n, | |
314 | profile=None, cluster_dir=config.Global.cluster_dir |
|
314 | profile=None, cluster_dir=config.Global.cluster_dir | |
315 | ) |
|
315 | ) | |
316 | return d |
|
316 | return d | |
317 |
|
317 | |||
318 | def stop_launchers(self, signum, frame): |
|
318 | def stop_launchers(self, signum, frame): | |
319 | log.msg("Stopping cluster") |
|
319 | log.msg("Stopping cluster") | |
320 | d1 = self.engine_launcher.stop() |
|
320 | d1 = self.engine_launcher.stop() | |
321 | d2 = self.controller_launcher.stop() |
|
321 | d2 = self.controller_launcher.stop() | |
322 | # d1.addCallback(lambda _: self.controller_launcher.stop) |
|
322 | # d1.addCallback(lambda _: self.controller_launcher.stop) | |
323 | d1.addErrback(self.err_and_stop) |
|
323 | d1.addErrback(self.err_and_stop) | |
324 | d2.addErrback(self.err_and_stop) |
|
324 | d2.addErrback(self.err_and_stop) | |
325 | reactor.callLater(2.0, reactor.stop) |
|
325 | reactor.callLater(2.0, reactor.stop) | |
326 |
|
326 | |||
327 | def start_logging(self): |
|
327 | def start_logging(self): | |
328 | # Remove old log files |
|
328 | # Remove old log files | |
329 | if self.master_config.Global.clean_logs: |
|
329 | if self.master_config.Global.clean_logs: | |
330 | log_dir = self.master_config.Global.log_dir |
|
330 | log_dir = self.master_config.Global.log_dir | |
331 | for f in os.listdir(log_dir): |
|
331 | for f in os.listdir(log_dir): | |
332 | if f.startswith('ipengine' + '-') and f.endswith('.log'): |
|
332 | if f.startswith('ipengine' + '-') and f.endswith('.log'): | |
333 | os.remove(os.path.join(log_dir, f)) |
|
333 | os.remove(os.path.join(log_dir, f)) | |
334 | for f in os.listdir(log_dir): |
|
334 | for f in os.listdir(log_dir): | |
335 | if f.startswith('ipcontroller' + '-') and f.endswith('.log'): |
|
335 | if f.startswith('ipcontroller' + '-') and f.endswith('.log'): | |
336 | os.remove(os.path.join(log_dir, f)) |
|
336 | os.remove(os.path.join(log_dir, f)) | |
337 | super(IPClusterApp, self).start_logging() |
|
337 | super(IPClusterApp, self).start_logging() | |
338 |
|
338 | |||
339 | def start_app(self): |
|
339 | def start_app(self): | |
340 | """Start the application, depending on what subcommand is used.""" |
|
340 | """Start the application, depending on what subcommand is used.""" | |
341 | subcmd = self.master_config.Global.subcommand |
|
341 | subcmd = self.master_config.Global.subcommand | |
342 | if subcmd=='create' or subcmd=='list': |
|
342 | if subcmd=='create' or subcmd=='list': | |
343 | return |
|
343 | return | |
344 | elif subcmd=='start': |
|
344 | elif subcmd=='start': | |
345 | self.start_app_start() |
|
345 | self.start_app_start() | |
346 | elif subcmd=='stop': |
|
346 | elif subcmd=='stop': | |
347 | self.start_app_stop() |
|
347 | self.start_app_stop() | |
348 |
|
348 | |||
349 | def start_app_start(self): |
|
349 | def start_app_start(self): | |
350 | """Start the app for the start subcommand.""" |
|
350 | """Start the app for the start subcommand.""" | |
351 | config = self.master_config |
|
351 | config = self.master_config | |
352 | # First see if the cluster is already running |
|
352 | # First see if the cluster is already running | |
353 | try: |
|
353 | try: | |
354 | pid = self.get_pid_from_file() |
|
354 | pid = self.get_pid_from_file() | |
355 | except PIDFileError: |
|
355 | except PIDFileError: | |
356 | pass |
|
356 | pass | |
357 | else: |
|
357 | else: | |
358 | self.log.critical( |
|
358 | self.log.critical( | |
359 | 'Cluster is already running with [pid=%s]. ' |
|
359 | 'Cluster is already running with [pid=%s]. ' | |
360 | 'use "ipcluster stop" to stop the cluster.' % pid |
|
360 | 'use "ipcluster stop" to stop the cluster.' % pid | |
361 | ) |
|
361 | ) | |
362 | # Here I exit with a unusual exit status that other processes |
|
362 | # Here I exit with a unusual exit status that other processes | |
363 | # can watch for to learn how I existed. |
|
363 | # can watch for to learn how I existed. | |
364 | self.exit(ALREADY_STARTED) |
|
364 | self.exit(ALREADY_STARTED) | |
365 |
|
365 | |||
366 | # Now log and daemonize |
|
366 | # Now log and daemonize | |
367 | self.log.info( |
|
367 | self.log.info( | |
368 | 'Starting ipcluster with [daemon=%r]' % config.Global.daemonize |
|
368 | 'Starting ipcluster with [daemon=%r]' % config.Global.daemonize | |
369 | ) |
|
369 | ) | |
370 | if config.Global.daemonize: |
|
370 | if config.Global.daemonize: | |
371 | if os.name=='posix': |
|
371 | if os.name=='posix': | |
372 | daemonize() |
|
372 | daemonize() | |
373 |
|
373 | |||
374 | # Now write the new pid file AFTER our new forked pid is active. |
|
374 | # Now write the new pid file AFTER our new forked pid is active. | |
375 | self.write_pid_file() |
|
375 | self.write_pid_file() | |
376 | reactor.addSystemEventTrigger('during','shutdown', self.remove_pid_file) |
|
376 | reactor.addSystemEventTrigger('during','shutdown', self.remove_pid_file) | |
377 | reactor.run() |
|
377 | reactor.run() | |
378 |
|
378 | |||
379 | def start_app_stop(self): |
|
379 | def start_app_stop(self): | |
380 | """Start the app for the stop subcommand.""" |
|
380 | """Start the app for the stop subcommand.""" | |
381 | config = self.master_config |
|
381 | config = self.master_config | |
382 | try: |
|
382 | try: | |
383 | pid = self.get_pid_from_file() |
|
383 | pid = self.get_pid_from_file() | |
384 | except PIDFileError: |
|
384 | except PIDFileError: | |
385 | self.log.critical( |
|
385 | self.log.critical( | |
386 | 'Problem reading pid file, cluster is probably not running.' |
|
386 | 'Problem reading pid file, cluster is probably not running.' | |
387 | ) |
|
387 | ) | |
388 | # Here I exit with a unusual exit status that other processes |
|
388 | # Here I exit with a unusual exit status that other processes | |
389 | # can watch for to learn how I existed. |
|
389 | # can watch for to learn how I existed. | |
390 | self.exit(ALREADY_STOPPED) |
|
390 | self.exit(ALREADY_STOPPED) | |
391 | else: |
|
391 | else: | |
392 | if os.name=='posix': |
|
392 | if os.name=='posix': | |
393 | sig = config.Global.signal |
|
393 | sig = config.Global.signal | |
394 | self.log.info( |
|
394 | self.log.info( | |
395 | "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig) |
|
395 | "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig) | |
396 | ) |
|
396 | ) | |
397 | os.kill(pid, sig) |
|
397 | os.kill(pid, sig) | |
398 | elif os.name='nt': |
|
398 | elif os.name=='nt': | |
399 | # As of right now, we don't support daemonize on Windows, so |
|
399 | # As of right now, we don't support daemonize on Windows, so | |
400 | # stop will not do anything. Minimally, it should clean up the |
|
400 | # stop will not do anything. Minimally, it should clean up the | |
401 | # old .pid files. |
|
401 | # old .pid files. | |
402 | self.remove_pid_file() |
|
402 | self.remove_pid_file() | |
403 |
|
403 | |||
404 | def launch_new_instance(): |
|
404 | def launch_new_instance(): | |
405 | """Create and run the IPython cluster.""" |
|
405 | """Create and run the IPython cluster.""" | |
406 | app = IPClusterApp() |
|
406 | app = IPClusterApp() | |
407 | app.start() |
|
407 | app.start() | |
408 |
|
408 | |||
409 |
|
409 | |||
410 | if __name__ == '__main__': |
|
410 | if __name__ == '__main__': | |
411 | launch_new_instance() |
|
411 | launch_new_instance() | |
412 |
|
412 |
General Comments 0
You need to be logged in to leave comments.
Login now