|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
|
|
3
|
|
|
import os |
|
4
|
|
|
import time |
|
5
|
|
|
import socket |
|
6
|
|
|
|
|
7
|
|
|
from pprint import pprint |
|
8
|
|
|
|
|
9
|
|
|
from heppy.EPP import REPP |
|
10
|
|
|
from heppy.Error import Error |
|
11
|
|
|
from heppy.Login import Login |
|
12
|
|
|
from heppy.Client import Client |
|
13
|
|
|
from heppy.Request import Request |
|
14
|
|
|
from heppy.Response import Response |
|
15
|
|
|
from heppy.SignalHandler import SignalHandler |
|
16
|
|
|
|
|
17
|
|
|
class Daemon: |
|
18
|
|
|
def __init__(self, config): |
|
19
|
|
|
self.config = config |
|
20
|
|
|
self.is_external = False |
|
21
|
|
|
self.client = None |
|
22
|
|
|
self.handler = SignalHandler({ |
|
23
|
|
|
'SIGINT': self.quit, |
|
24
|
|
|
'SIGTERM': self.quit, |
|
25
|
|
|
'SIGHUP': self.hello, |
|
26
|
|
|
'SIGUSR1': self.hello, |
|
27
|
|
|
'SIGUSR2': self.hello, |
|
28
|
|
|
}) |
|
29
|
|
|
self.login_query = None |
|
30
|
|
|
|
|
31
|
|
|
def quit(self): |
|
32
|
|
|
global quit |
|
33
|
|
|
quit() |
|
34
|
|
|
|
|
35
|
|
|
def hello(self): |
|
36
|
|
|
print "\nHELLO\n" |
|
37
|
|
|
|
|
38
|
|
|
def start(self,args = {}): |
|
39
|
|
|
self.connect() |
|
40
|
|
|
self.login(args) |
|
41
|
|
|
|
|
42
|
|
|
def connect(self): |
|
43
|
|
|
if self.client is not None: |
|
44
|
|
|
return |
|
45
|
|
|
if self.is_external: |
|
46
|
|
|
self.connect_external() |
|
47
|
|
|
else: |
|
48
|
|
|
self.connect_internal() |
|
49
|
|
|
|
|
50
|
|
|
def login(self, args = {}): |
|
51
|
|
|
try: |
|
52
|
|
|
query = self.get_login_query(args) |
|
53
|
|
|
print Request.prettifyxml(query) |
|
54
|
|
|
reply = self.request(query) |
|
55
|
|
|
print Request.prettifyxml(reply) |
|
56
|
|
|
except Error as e: |
|
57
|
|
|
Error.die(2, 'failed perform login request') |
|
58
|
|
|
error = None |
|
59
|
|
|
try: |
|
60
|
|
|
response = Response.parsexml(reply) |
|
61
|
|
|
data = response.data |
|
62
|
|
|
pprint(data) |
|
63
|
|
|
except Error as e: |
|
64
|
|
|
error = e.message |
|
65
|
|
|
data = e.data |
|
66
|
|
|
if error is not None and data['resultCode']!='2002': |
|
67
|
|
|
Error.die(2, 'bad login response', data) |
|
68
|
|
|
print 'LOGIN OK' |
|
69
|
|
|
|
|
70
|
|
|
def get_login_query(self, args = {}): |
|
71
|
|
|
if self.login_query is None: |
|
72
|
|
|
greeting = self.client.get_greeting() |
|
73
|
|
|
greetobj = Response.parsexml(greeting) |
|
74
|
|
|
pprint(greetobj.data) |
|
75
|
|
|
request = Login.build(self.config, greeting, args) |
|
76
|
|
|
self.login_query = str(request) |
|
77
|
|
|
return self.login_query |
|
78
|
|
|
|
|
79
|
|
|
def connect_internal(self): |
|
80
|
|
|
self.client = REPP(self.config) |
|
81
|
|
|
|
|
82
|
|
|
def connect_external(self): |
|
83
|
|
|
try: |
|
84
|
|
|
self.client = Client(self.config['local']['address']) |
|
85
|
|
|
self.client.connect() |
|
86
|
|
|
except socket.error as e: |
|
87
|
|
|
os.system(self.config['zdir'] + '/eppyd ' + self.config['path'] + ' &') |
|
88
|
|
|
time.sleep(2) |
|
89
|
|
|
self.client = Client(self.config['local']['address']) |
|
90
|
|
|
|
|
91
|
|
|
def stop(self, args = {}): |
|
92
|
|
|
Error.die(3, 'stop not implemented', config) |
|
93
|
|
|
|
|
94
|
|
|
def request(self, query): |
|
95
|
|
|
with self.handler.block_signals(): |
|
96
|
|
|
reply = self.client.request(query) |
|
97
|
|
|
return reply |
|
98
|
|
|
|
|
99
|
|
|
def smart_request(self, query): |
|
100
|
|
|
reply = self.request(query) |
|
101
|
|
|
response = Response.parsexml(reply) |
|
102
|
|
|
pprint(response.data) |
|
103
|
|
|
if response.data['result.code'] == '2002': |
|
104
|
|
|
self.login() |
|
105
|
|
|
reply = self.request(query) |
|
106
|
|
|
return reply |
|
107
|
|
|
|
|
108
|
|
|
|