Browse Source

SLIM enhancement

Bugfixe in start
implements stop
add info in instance details
Yann Weber 8 years ago
parent
commit
42bc342ffd
2 changed files with 46 additions and 5 deletions
  1. 7
    2
      progs/Makefile.am
  2. 39
    3
      progs/slim/slim.py

+ 7
- 2
progs/Makefile.am View File

1
 SUBDIRS=slim
1
 SUBDIRS=slim
2
 
2
 
3
 lodel2_scripts_dir = $(datadir)/lodel2/scripts
3
 lodel2_scripts_dir = $(datadir)/lodel2/scripts
4
-lodel2_scripts__DATA = create_instance
4
+lodel2_scripts__DATA = create_instance mass_deploy
5
 CLEANFILES = $(lodel2_scripts__DATA)
5
 CLEANFILES = $(lodel2_scripts__DATA)
6
 
6
 
7
 do_subst = sed -e 's,\[@\]PKGPYTHONDIR\[@\],$(pkgpythondir),g' 
7
 do_subst = sed -e 's,\[@\]PKGPYTHONDIR\[@\],$(pkgpythondir),g' 
8
 
8
 
9
+#There is clearly a way to factorise those rules
10
+mass_deploy: mass_deploy.sh
11
+	$(do_subst) < $(srcdir)/mass_deploy.sh > mass_deploy
12
+	chmod +x mass_deploy
13
+
9
 create_instance: create_instance.sh
14
 create_instance: create_instance.sh
10
 	$(do_subst) < $(srcdir)/create_instance.sh > create_instance
15
 	$(do_subst) < $(srcdir)/create_instance.sh > create_instance
11
 	chmod +x create_instance
16
 	chmod +x create_instance
12
 
17
 
13
 install-data-hook:
18
 install-data-hook:
14
-	chmod +x $(datadir)/lodel2/scripts/create_instance
19
+	chmod +x $(datadir)/lodel2/scripts/create_instance $(datadir)/lodel2/scripts/mass_deploy

+ 39
- 3
progs/slim/slim.py View File

10
 import re
10
 import re
11
 import json
11
 import json
12
 import configparser
12
 import configparser
13
+import signal
13
 import subprocess
14
 import subprocess
14
 
15
 
15
 logging.basicConfig(level=logging.INFO)
16
 logging.basicConfig(level=logging.INFO)
176
 def get_pids():
177
 def get_pids():
177
     if not os.path.isfile(PID_FILE):
178
     if not os.path.isfile(PID_FILE):
178
         return dict()
179
         return dict()
179
-    with open(PID_FILE, 'r') as pdf:
180
+    with open(PID_FILE, 'r') as pfd:
180
         return json.load(pfd)
181
         return json.load(pfd)
181
 
182
 
182
 ##@brief Save a dict of pid
183
 ##@brief Save a dict of pid
196
 
197
 
197
 ##@brief Start an instance
198
 ##@brief Start an instance
198
 #@param names list : instance name list
199
 #@param names list : instance name list
199
-def start_instance(names):
200
+def start_instances(names):
200
     pids = get_pids()
201
     pids = get_pids()
201
     store_datas = get_store_datas()
202
     store_datas = get_store_datas()
202
     
203
     
209
         curexec = subprocess.Popen(args)
210
         curexec = subprocess.Popen(args)
210
         pids[name] = curexec.pid
211
         pids[name] = curexec.pid
211
         logging.info("Instance '%s' started. PID %d" % (name, curexec.pid))
212
         logging.info("Instance '%s' started. PID %d" % (name, curexec.pid))
213
+    save_pids(pids)
214
+
215
+def stop_instances(names):
216
+    pids = get_pids()
217
+    store_datas = get_store_datas()
218
+    for name in names:
219
+        if name not in pids:
220
+            logging.warning("The instance %s is not running" % name)
221
+            continue
222
+        pid = pids[name]
223
+        os.kill(pid, signal.SIGINT)
212
 
224
 
213
 ##@brief Check if instance are specified
225
 ##@brief Check if instance are specified
214
 def get_specified(args):
226
 def get_specified(args):
254
 def details_instance(name, verbosity, batch):
266
 def details_instance(name, verbosity, batch):
255
     validate_names([name])
267
     validate_names([name])
256
     store_datas = get_store_datas()
268
     store_datas = get_store_datas()
269
+    pids = get_pids()
257
     if not batch:
270
     if not batch:
258
         msg = "\t- '%s'" % name
271
         msg = "\t- '%s'" % name
272
+        if name in pids:
273
+            msg += ' [Run PID %d] ' % pids[name]
259
         if verbosity > 0:
274
         if verbosity > 0:
260
             msg += ' path = "%s"' % store_datas[name]['path']
275
             msg += ' path = "%s"' % store_datas[name]['path']
261
         if verbosity > 1:
276
         if verbosity > 1:
308
     actions.add_argument('-d', '--delete', action='store_const',
323
     actions.add_argument('-d', '--delete', action='store_const',
309
         default=False, const=True,
324
         default=False, const=True,
310
         help="Delete an instance with given name (see -n --name)")
325
         help="Delete an instance with given name (see -n --name)")
326
+    actions.add_argument('-p', '--purge', action='store_const',
327
+        default=False, const=True,
328
+        help="Delete ALL instances")
311
     actions.add_argument('-s', '--set-option', action='store_const',
329
     actions.add_argument('-s', '--set-option', action='store_const',
312
         default=False, const=True,
330
         default=False, const=True,
313
         help="Use this flag to set options on instance")
331
         help="Use this flag to set options on instance")
354
             exit(1)
372
             exit(1)
355
         for name in args.name:
373
         for name in args.name:
356
             new_instance(name)
374
             new_instance(name)
375
+    elif args.purge:
376
+        print("Are you sure ? Yes/no ",)
377
+        rep = sys.stdin.read()
378
+        if rep == 'Yes':
379
+            store = get_store_datas()
380
+            for name in store:
381
+                delete_instance(name)
382
+        elif rep.lower() != 'no':
383
+            print("Expect exactly 'Yes' to confirm...")
384
+        exit()
357
     elif args.delete:
385
     elif args.delete:
358
         #Instance deletion
386
         #Instance deletion
387
+        if args.all:
388
+            parser.print_help()
389
+            print("\n use -p --purge instead of --delete --all",
390
+                file=sys.stderr)
391
+            exit(1)
359
         if args.name is None:
392
         if args.name is None:
360
             parser.print_help()
393
             parser.print_help()
361
             print("\nAn instance name expected when creating an instance !",
394
             print("\nAn instance name expected when creating an instance !",
415
             set_conf(name, args)
448
             set_conf(name, args)
416
     elif args.start:
449
     elif args.start:
417
         names = get_specified(args)
450
         names = get_specified(args)
418
-        start_instance(names)
451
+        start_instances(names)
452
+    elif args.stop:
453
+        names = get_specified(args)
454
+        stop_instances(names)
419
         
455
         

Loading…
Cancel
Save