Projet

Général

Profil

0002-use-hobo_natch-in-agent-Service-classes-fixes-9821.patch

Benjamin Dauvergne, 01 février 2016 14:25

Télécharger (5,11 ko)

Voir les différences:

Subject: [PATCH 2/2] use hobo_natch in agent Service classes (fixes #9821)

 hobo/agent/common/management/hobo_batch_client.py | 51 +++++++++++++++++++++++
 hobo/agent/worker/services.py                     | 37 ++++++++++++----
 2 files changed, 79 insertions(+), 9 deletions(-)
 create mode 100644 hobo/agent/common/management/hobo_batch_client.py
hobo/agent/common/management/hobo_batch_client.py
1
import os
2
import subprocess
3
import struct
4

  
5

  
6
class BatchClient(object):
7
    def write_buffer(self, b):
8
        self.process.stdin.write(b)
9

  
10
    def write_integer(self, i):
11
        self.write_buffer(struct.pack('!I', i))
12

  
13
    def write_string(self, s):
14
        self.write_integer(len(s))
15
        self.write_buffer(s)
16

  
17
    def read_string(self):
18
        length = self.read_integer()
19
        return self.read_buffer(length)
20

  
21
    def read_buffer(self, length):
22
        s = ''
23
        while len(s) != length:
24
            b = self.process.stdout.read(length - len(s))
25
            if not b:
26
                raise IOError('EOF')
27
            s += b
28
        return s
29

  
30
    def read_fmt(self, fmt):
31
        n = struct.calcsize(fmt)
32
        s = self.read_buffer(n)
33
        return struct.unpack(fmt, s)
34

  
35
    def read_integer(self):
36
        return self.read_fmt('!I')[0]
37

  
38
    def __init__(self, command, environment=None):
39
        env = os.environ.copy()
40
        env.update(environment or {})
41
        self.process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
42

  
43
    def execute(self, args, stdin=None):
44
        self.write_integer(len(args))
45
        for arg in args:
46
            self.write_string(arg)
47
        self.write_string(stdin or '')
48
        self.process.stdin.flush()
49
        result = self.read_integer()
50
        output = self.read_string()
51
        return result, output
hobo/agent/worker/services.py
1 1
# hobo - portal to configure and deploy applications
2
                irint action, data
2 3
# Copyright (C) 2015  Entr'ouvert
3 4
#
4 5
# This program is free software: you can redistribute it and/or modify it
......
14 15
# You should have received a copy of the GNU Affero General Public License
15 16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17

  
18
import time
17 19
import sys
18 20
import ConfigParser
19 21
import fnmatch
......
27 29

  
28 30
from . import settings
29 31

  
32
from hobo.agent.common.management.hobo_batch_client import BatchClient
33

  
30 34

  
31 35
class BaseService(object):
36
    batch_support = True
37
    _batch_process = None
38

  
32 39
    def __init__(self, base_url, title, secret_key, **kwargs):
33 40
        self.base_url = base_url
34 41
        self.title = title
......
74 81
        stdout = cmd_process.communicate(input=json.dumps(environment))
75 82

  
76 83
    @classmethod
84
    def get_batch_process(cls):
85
        if not cls._batch_process or cls._batch_process.process.returncode:
86
            cls._batch_process = BatchClient(cls.service_manage_cmd + ' hobo_batch')
87
        return cls._batch_process
88

  
89
    @classmethod
77 90
    def notify(cls, data):
78 91
        for audience in data.get('audience', []):
79 92
            if cls.is_for_us(audience):
......
82 95
            return
83 96
        if not os.path.exists(cls.service_manage_try_cmd):
84 97
            return
85
        cmd = cls.service_manage_cmd + ' hobo_notify -'
86
        try:
87
            cmd_process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
88
                                           stdout=subprocess.PIPE, stderr=subprocess.PIPE)
89
        except OSError:
90
            return
91
        stdout, stderr = cmd_process.communicate(input=json.dumps(data))
92
        if cmd_process.returncode != 0:
93
            raise RuntimeError('command "%s" failed: %r %r' % (cmd, stdout, stderr))
98
        if cls.batch_support:
99
            batch = cls.get_batch_process()
100
            returncode, stdout = batch.execute(['hobo_notify', '-'], json.dumps(data))
101
            if returncode != 0:
102
                raise RuntimeError('command "hobo_notify -" failed: %r %r' % (returncode, stdout))
103
        else:
104
            cmd = cls.service_manage_cmd + ' hobo_notify -'
105
            try:
106
                cmd_process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
107
                                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
108
            except OSError:
109
                return
110
            stdout, stderr = cmd_process.communicate(input=json.dumps(data))
111
            if cmd_process.returncode != 0:
112
                raise RuntimeError('command "%s" failed: %r %r' % (cmd, stdout, stderr))
94 113

  
95 114

  
96 115
class Passerelle(BaseService):
97
-