forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaremetal-vr.py
More file actions
executable file
·167 lines (140 loc) · 5.8 KB
/
baremetal-vr.py
File metadata and controls
executable file
·167 lines (140 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#Licensed to the Apache Software Foundation (ASF) under one
#or more contributor license agreements. See the NOTICE file
#distributed with this work for additional information
#regarding copyright ownership. The ASF licenses this file
#to you under the Apache License, Version 2.0 (the
#"License"); you may not use this file except in compliance
#with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing,
#software distributed under the License is distributed on an
#"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#KIND, either express or implied. See the License for the
#specific language governing permissions and limitations
#under the License.
import subprocess
import hmac
import hashlib
import base64
import traceback
import logging
import re
import urllib.request, urllib.parse, urllib.error
from flask import Flask
app = Flask(__name__)
logger = logging.getLogger('baremetal-vr')
hdlr = logging.FileHandler('/var/log/baremetal-vr.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
class ShellCmd(object):
'''
classdocs
'''
def __init__(self, cmd, workdir=None, pipe=True):
'''
Constructor
'''
self.cmd = cmd
if pipe:
self.process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, executable='/bin/sh', cwd=workdir)
else:
self.process = subprocess.Popen(cmd, shell=True, executable='/bin/sh', cwd=workdir)
self.stdout = None
self.stderr = None
self.return_code = None
def __call__(self, is_exception=True):
(self.stdout, self.stderr) = self.process.communicate()
if is_exception and self.process.returncode != 0:
err = []
err.append('failed to execute shell command: %s' % self.cmd)
err.append('return code: %s' % self.process.returncode)
err.append('stdout: %s' % self.stdout.decode())
err.append('stderr: %s' % self.stderr.decode())
raise Exception('\n'.join(err))
self.return_code = self.process.returncode
return self.stdout
def shell(cmd):
return ShellCmd(cmd)()
class Server(object):
CMDLINE = '/var/cache/cloud/cmdline'
def __init__(self):
self.apikey = None
self.secretkey = None
self.mgmtIp = None
self.mgmtPort = None
def _get_credentials(self):
if not self.apikey or not self.secretkey:
with open(self.CMDLINE, 'r') as fd:
cmdline = fd.read()
for p in cmdline.split():
if 'baremetalnotificationsecuritykey' in p:
self.secretkey = p.split("=")[1]
if 'baremetalnotificationapikey' in p:
self.apikey = p.split("=")[1]
if not self.apikey:
raise Exception('cannot find baremetalnotificationapikey in %s' % Server.CMDLINE)
if not self.secretkey:
raise Exception('cannot find baremetalnotificationsecuritykey in %s' % Server.CMDLINE)
return self.apikey, self.secretkey
def _get_mgmt_ip(self):
if not self.mgmtIp:
with open(self.CMDLINE, 'r') as fd:
cmdline = fd.read()
for p in cmdline.split():
if 'host' in p:
self.mgmtIp = p.split("=")[1]
break
if not self.mgmtIp:
raise Exception('cannot find host in %s' % Server.CMDLINE)
return self.mgmtIp
def _get_mgmt_port(self):
if not self.mgmtPort:
with open(self.CMDLINE, 'r') as fd:
cmdline = fd.read()
for p in cmdline.split():
if 'port' in p:
self.mgmtPort = p.split("=")[1]
break
if not self.mgmtIp:
raise Exception('cannot find port in %s' % Server.CMDLINE)
return self.mgmtPort
def _make_sign(self, mac):
apikey, secretkey = self._get_credentials()
reqs = {
"apiKey": apikey,
"command": 'notifyBaremetalProvisionDone',
"mac": mac
}
request = list(zip(list(reqs.keys()), list(reqs.values())))
request.sort(key=lambda x: str.lower(x[0]))
hashStr = "&".join(["=".join([str.lower(r[0]), str.lower(urllib.parse.quote_plus(str(r[1]))).replace("+", "%20").replace('=', '%3d')]) for r in request])
sig = urllib.parse.quote_plus(base64.encodestring(hmac.new(secretkey, hashStr, hashlib.sha1).digest()).strip())
return sig
def notify_provisioning_done(self, mac):
sig = self._make_sign(mac)
cmd = 'http://%s:%s/client/api?command=notifyBaremetalProvisionDone&mac=%s&apiKey=%s&signature=%s' % (self._get_mgmt_ip(), self._get_mgmt_port(), mac, self.apikey, sig)
shell("curl -X GET '%s'" % cmd)
return ''
server = None
@app.route('/baremetal/provisiondone/<mac>', methods=['GET'])
def notify_provisioning_done(mac):
try:
if not is_a_mac(mac):
raise "there is an issue with that '%s'. Not a mac?" % mac
return server.notify_provisioning_done(mac)
except:
logger.warn(traceback.format_exc())
return ''
def is_a_mac(mac):
if re.match("[0-9a-f]{2}([-:]?)[0-9a-f]{2}(\\1[0-9a-f]{2}){4}$", mac.lower()):
return True
else:
return False
if __name__ == '__main__':
server = Server()
shell("iptables-save | grep -- '-A INPUT -i eth0 -p tcp -m tcp --dport 10086 -j ACCEPT' > /dev/null || iptables -I INPUT -i eth0 -p tcp -m tcp --dport 10086 -j ACCEPT")
app.run(host='0.0.0.0', port=10086)