Browse Source

Come back

prieto 8 years ago
parent
commit
30a8b8238a
1 changed files with 52 additions and 46 deletions
  1. 52
    46
      progs/slim/slim.py

+ 52
- 46
progs/slim/slim.py View File

@@ -13,7 +13,6 @@ import configparser
13 13
 import signal
14 14
 import subprocess
15 15
 from lodel import buildconf
16
-from lodel.context import LodelContext
17 16
 
18 17
 logging.basicConfig(level=logging.INFO)
19 18
 
@@ -166,31 +165,19 @@ lower&upper alphanum and '_'. Name '%s' is invalid" % name)
166 165
 ##@brief Create a new instance
167 166
 #@param name str : the instance name
168 167
 def new_instance(name):
169
-    if LodelContext._type == LodelContext.MONOSITE:
170
-        store_datas = get_store_datas()
171
-        if len(store_datas) > 0:
172
-            logging.error("We are in monosite mode and a site already exists")
173
-            exit(1)
174
-        if name is not None:
175
-            logging.error("We are in monosite mode, we can't have a site name")
176
-            exit(1)
177
-        instance_path = INSTANCES_ABSPATH
178
-    else:
179
-        name_is_valid(name)
180
-        store_datas = get_store_datas()
181
-        if name in store_datas:
182
-            logging.error("A site named '%s' already exists" % name)
183
-            exit(1)
184
-        instance_path = os.path.join(INSTANCES_ABSPATH, name)
185
-        
168
+    name_is_valid(name)
169
+    store_datas = get_store_datas()
170
+    if name in store_datas:
171
+        logging.error("An instance named '%s' already exists" % name)
172
+        exit(1)
186 173
     if not os.path.isdir(INSTANCES_ABSPATH):
187
-        logging.info("Sites directory '%s' doesn't exist, creating it" % INSTANCES_ABSPATH)
174
+        logging.info("Instances directory '%s' don't exists, creating it")
188 175
         os.mkdir(INSTANCES_ABSPATH)
189
-    sitename = name if name is not None else 'monosite'
176
+    instance_path = os.path.join(INSTANCES_ABSPATH, name)
190 177
     creation_cmd = '{script} "{name}" "{path}" "{install_tpl}" \
191
-    "{emfile}"'.format(
178
+"{emfile}"'.format(
192 179
         script = CREATION_SCRIPT,
193
-        name = sitename,
180
+        name = name,
194 181
         path = instance_path,
195 182
         install_tpl = INSTALL_TPL,
196 183
         emfile = EMFILE)
@@ -201,19 +188,18 @@ def new_instance(name):
201 188
     #storing new instance
202 189
     store_datas[name] = {'path': instance_path}
203 190
     save_datas(store_datas)
204
-    lodelcontext = LodelContext.new(name)
205 191
 
206 192
 ##@brief Delete an instance
207 193
 #@param name str : the instance name
208
-def delete_instance(name=None):
194
+def delete_instance(name):
195
+    pids = get_pids()
209 196
     if name in pids:
210
-        logging.error("The site '%s' is started. Stop it before deleting \
197
+        logging.error("The instance '%s' is started. Stop it before deleting \
211 198
 it" % name)
212 199
         return
213 200
     store_datas = get_store_datas()
214
-    logging.warning("Deleting site %s" % name)
215
-    LodelContext.remove(name)
216
-    logging.info("Deleting site's folder %s" % store_datas[name]['path'])
201
+    logging.warning("Deleting instance %s" % name)
202
+    logging.info("Deleting instance folder %s" % store_datas[name]['path'])
217 203
     shutil.rmtree(store_datas[name]['path'])
218 204
     logging.debug("Deleting instance from json store file")
219 205
     del(store_datas[name])
@@ -238,18 +224,35 @@ def validate_names(names):
238 224
             print("\t%s" % name, file=sys.stderr)
239 225
         exit(1)
240 226
 
227
+##@brief Returns the PID dict
228
+#@return a dict with instance name as key an PID as value
229
+def get_pids():
230
+    if not os.path.isfile(PID_FILE) or os.stat(PID_FILE).st_size == 0:
231
+        return dict()
232
+    with open(PID_FILE, 'r') as pfd:
233
+        return json.load(pfd)
234
+
235
+##@brief Save a dict of pid
236
+#@param pid_dict dict : key is instance name values are pid
237
+def save_pids(pid_dict):
238
+    with open(PID_FILE, 'w+') as pfd:
239
+        json.dump(pid_dict, pfd)
240
+
241
+##@brief Given an instance name returns its PID
242
+#@return False or an int
243
+def get_pid(name):
244
+    pid_datas = get_pids()
245
+    if name not in pid_datas:
246
+        return False
247
+    else:
248
+        pid = pid_datas[name]
249
+        if not is_running(name, pid):
250
+            return False
251
+        return pid
252
+
241 253
 ##@brief Start an instance
242 254
 #@param names list : instance name list
243 255
 def start_instances(names, foreground):
244
-    from lodel.context import LodelContext
245
-    for name in names:
246
-        if name in LodelContext._contexts:
247
-            logging.warning("The instance %s is already activated" % name)
248
-            continue
249
-        LodelContext.from_path(name)
250
-        logging.info("Instance '%s' started.")
251
-
252
-    '''
253 256
     pids = get_pids()
254 257
     store_datas = get_store_datas()
255 258
     
@@ -271,7 +274,6 @@ def start_instances(names, foreground):
271 274
             pids[name] = curexec.pid
272 275
             logging.info("Instance '%s' started. PID %d" % (name, curexec.pid))
273 276
     save_pids(pids)
274
-    '''
275 277
 
276 278
 ##@brief Stop an instance given its name
277 279
 #@param names list : names list
@@ -295,14 +297,18 @@ with pid %d found" % (name, pids[name]))
295 297
 #
296 298
 #If not running clean the pid list
297 299
 #@return bool
298
-def is_running(name):
299
-    if name is None:
300
-        return LodelContext._current is not None
301
-    else:
302
-        if LodelContext._type == LodelContext.MONOSITE
303
-            logging.warning("In monosite mode no name is allowed")
304
-        else:
305
-            return LodelContext.__current == LodelContext._contexts[name]
300
+def is_running(name, pid):
301
+    try:
302
+        os.kill(pid, 0)
303
+        return True
304
+    except (OSError,ProcessLookupError):
305
+        pid_datas = get_pids()
306
+        logging.warning("Instance '%s' was marked as running, but not \
307
+process with pid %d found. Cleaning pid list" % (name, pid))
308
+        del(pid_datas[name])
309
+        save_pids(pid_datas)
310
+    return False
311
+        
306 312
 
307 313
 ##@brief Check if instance are specified
308 314
 def get_specified(args):

Loading…
Cancel
Save