Guest User

Supervisord Docker initsys

a guest
Feb 17th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.75 KB | None | 0 0
  1. root@moo# cat run_nginx.sh
  2. #!/bin/bash
  3.  
  4. ME=$(basename $0 | sed 's/\.sh//' | sed 's/run\_//')
  5. ID=`docker run \
  6.   --net=host \
  7.   -v /data/stuff/conf/nginx/sites-available:/etc/nginx/sites-enabled \
  8.   -v /data/stuff/conf/nginx/nginx.conf:/etc/nginx/nginx.conf \
  9.   -v /data/stuff:/data \
  10.   -d \
  11.   192.168.1.1:5000/jessie-stack:latest \
  12.   /usr/local/openresty/nginx/sbin/nginx \
  13.     -c /etc/nginx/nginx.conf`
  14. trap "echo Stopping $ID; docker stop $ID; /root/ops/services/scripts/systemd-log-stop.pl $ME" SIGINT
  15. echo "[supervisor - shell] Executed PID: $$"
  16. /root/ops/services/scripts/systemd-log.pl $ME $ID
  17. docker logs -f $ID
  18.  
  19. root@moo:~/# cat /etc/supervisor/conf.d/nginx.conf    
  20. [program:nginx]
  21. command=/root/ops/services/scripts/run_nginx.sh
  22. user=root
  23. redirect_stderr=true
  24. autostart=true
  25. autorestart=true
  26. stopsignal=INT
  27. stopasgroup=true
  28.  
  29. root@moo:~/# cat systemd-log-stop.pl
  30. #!/usr/bin/perl
  31.  
  32. $service = $ARGV[0];
  33.  
  34. if($service eq "") { exit };
  35.  
  36. &main;
  37.  
  38. sub main {
  39.  $initsys = `ps aux | grep systemd | grep -v grep | wc -l`;
  40.  if($initsys >= 5) {
  41.   $ret = `systemctl stop $service-log`;
  42.   print $ret;
  43.  }
  44. }
  45.  
  46. root@moo:~/ops/services/scripts# cat systemd-log.pl
  47. #!/usr/bin/perl
  48.  
  49. $service = $ARGV[0];
  50. $id = $ARGV[1];
  51.  
  52. if($service eq "" || $id eq "") { exit };
  53.  
  54. &main;
  55.  
  56. sub main {
  57.  $initsys = `ps aux | grep systemd | grep -v grep | wc -l`;
  58.  if($initsys >= 5) {
  59.   $service = $service."-log";
  60.   &create_service;
  61.  }
  62. }
  63.  
  64. sub create_service {
  65.  open(FILE, ">/etc/systemd/system/$service.service");
  66.  
  67.  print FILE <<EOF;
  68. [Unit]
  69. Description=$service
  70.  
  71. [Service]
  72. ExecStart=/usr/bin/docker logs -f $id
  73.  
  74. [Install]
  75. WantedBy=multi-user.target
  76. EOF
  77.  
  78.  close(FILE);
  79.  
  80.  $ret = `systemctl daemon-reload`;
  81.  $ret = `systemctl start $service`;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment