1
|
|
|
# encoding: utf-8 |
2
|
|
|
""" |
3
|
|
|
parse_process.py |
4
|
|
|
|
5
|
|
|
Created by Thomas Mangin on 2015-06-05. |
6
|
|
|
Copyright (c) 2009-2017 Exa Networks. All rights reserved. |
7
|
|
|
License: 3-clause BSD. (See the COPYRIGHT file) |
8
|
|
|
""" |
9
|
|
|
|
10
|
|
|
import os |
11
|
|
|
import sys |
12
|
|
|
import uuid |
13
|
|
|
|
14
|
|
|
from exabgp.configuration.core import Section |
15
|
|
|
|
16
|
|
|
from exabgp.configuration.process.parser import encoder |
17
|
|
|
from exabgp.configuration.process.parser import run |
18
|
|
|
|
19
|
|
|
from exabgp.configuration.parser import boolean |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class ParseProcess(Section): |
23
|
|
|
syntax = 'process name-of-process {\n' ' run /path/to/command with its args;\n' ' encoder text|json;\n' '}' |
24
|
|
|
known = { |
25
|
|
|
'encoder': encoder, |
26
|
|
|
'respawn': boolean, |
27
|
|
|
'run': run, |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
action = { |
31
|
|
|
'encoder': 'set-command', |
32
|
|
|
'respawn': 'set-command', |
33
|
|
|
'run': 'set-command', |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
default = { |
37
|
|
|
'respawn': True, |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
name = 'process' |
41
|
|
|
|
42
|
|
|
def __init__(self, tokeniser, scope, error, logger): |
43
|
|
|
Section.__init__(self, tokeniser, scope, error, logger) |
44
|
|
|
self.processes = {} |
45
|
|
|
self._processes = [] |
46
|
|
|
self.named = '' |
47
|
|
|
|
48
|
|
|
def clear(self): |
49
|
|
|
self.processes = {} |
50
|
|
|
self._processes = [] |
51
|
|
|
|
52
|
|
|
def pre(self): |
53
|
|
|
self.named = self.tokeniser.line[1] |
54
|
|
|
if self.named in self._processes: |
55
|
|
|
return self.error.set('a process section called "%s" already exists' % self.named) |
56
|
|
|
self._processes.append(self.named) |
57
|
|
|
return True |
58
|
|
|
|
59
|
|
|
def post(self): |
60
|
|
|
known = self.known.keys() |
61
|
|
|
configured = self.scope.get().keys() |
62
|
|
|
for default in self.default: |
63
|
|
|
if default not in configured: |
64
|
|
|
self.scope.set(default, self.default[default]) |
65
|
|
|
difference = set(known).difference(configured) |
66
|
|
|
if difference: |
67
|
|
|
return self.error.set('unset process sections: %s' % ', '.join(difference)) |
68
|
|
|
self.processes.update({self.named: self.scope.pop()}) |
69
|
|
|
return True |
70
|
|
|
|
71
|
|
|
def add_api(self): |
72
|
|
|
if not os.environ.get('exabgp_cli_pipe', ''): |
73
|
|
|
return |
74
|
|
|
name = 'api-internal-cli-%x' % uuid.uuid1().fields[0] |
75
|
|
|
api = { |
76
|
|
|
name: { |
77
|
|
|
'run': [sys.executable, os.path.join(os.environ.get('PWD', ''), sys.argv[0])], |
78
|
|
|
'encoder': 'text', |
79
|
|
|
'respawn': True, |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
self._processes.append(name) |
83
|
|
|
self.processes.update(api) |
84
|
|
|
|