Skip to content

Commit fb1f3f0

Browse files
author
Frank.Zhang
committed
Add missing Baremetal security_group_agent java part
Change security_group_agent python side in line with default security group rules change in 4.2 Conflicts: plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalResourceBase.java
1 parent c055417 commit fb1f3f0

8 files changed

Lines changed: 1149 additions & 293 deletions

File tree

plugins/hypervisors/baremetal/resources/security_group_agent/security_group_agent/cs_sg_agent.py

Lines changed: 366 additions & 219 deletions
Large diffs are not rendered by default.

plugins/hypervisors/baremetal/resources/security_group_agent/security_group_agent/sglib.py

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
# Licensed to the Apache Software Foundation (ASF) under one
32
# or more contributor license agreements. See the NOTICE file
43
# distributed with this work for additional information
@@ -18,13 +17,55 @@
1817
#
1918
# Automatically generated by addcopyright.py at 01/29/2013
2019

21-
import sys, os, time, atexit
22-
import traceback
20+
#!/usr/bin/env python
21+
22+
import sys, os, time, atexit
23+
import traceback
2324
import subprocess
24-
from signal import SIGTERM
25+
from signal import SIGTERM,SIGKILL
2526
import cherrypy
2627
import copy
2728

29+
import weakref
30+
import threading
31+
import functools
32+
33+
_internal_lock = threading.RLock()
34+
_locks = weakref.WeakValueDictionary()
35+
36+
def _get_lock(name):
37+
with _internal_lock:
38+
lock = _locks.get(name, threading.RLock())
39+
if not name in _locks:
40+
_locks[name] = lock
41+
return lock
42+
43+
class NamedLock(object):
44+
def __init__(self, name):
45+
self.name = name
46+
self.lock = None
47+
48+
def __enter__(self):
49+
self.lock = _get_lock(self.name)
50+
self.lock.acquire()
51+
#logger.debug('%s got lock %s' % (threading.current_thread().name, self.name))
52+
53+
def __exit__(self, type, value, traceback):
54+
self.lock.release()
55+
#logger.debug('%s released lock %s' % (threading.current_thread().name, self.name))
56+
57+
58+
def lock(name='defaultLock'):
59+
def wrap(f):
60+
@functools.wraps(f)
61+
def inner(*args, **kwargs):
62+
with NamedLock(name):
63+
retval = f(*args, **kwargs)
64+
return retval
65+
return inner
66+
return wrap
67+
68+
2869
class Request(object):
2970
def __init__(self):
3071
self.headers = None
@@ -80,28 +121,28 @@ class Daemon(object):
80121
A generic daemon class.
81122
82123
Usage: subclass the Daemon class and override the run() method
83-
"""
84-
atexit_hooks = []
124+
"""
125+
atexit_hooks = []
85126

86127
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
87128
self.stdin = stdin
88129
self.stdout = stdout
89130
self.stderr = stderr
90131
self.pidfile = pidfile
91-
92-
@staticmethod
93-
def register_atexit_hook(hook):
94-
Daemon.atexit_hooks.append(hook)
95-
132+
96133
@staticmethod
97-
def _atexit():
98-
for hook in Daemon.atexit_hooks:
99-
try:
134+
def register_atexit_hook(hook):
135+
Daemon.atexit_hooks.append(hook)
136+
137+
@staticmethod
138+
def _atexit():
139+
for hook in Daemon.atexit_hooks:
140+
try:
100141
hook()
101-
except Exception:
142+
except Exception:
102143
content = traceback.format_exc()
103-
err = 'Exception when calling atexit hook[%s]\n%s' % (hook.__name__, content)
104-
#logger.error(err)
144+
err = 'Exception when calling atexit hook[%s]\n%s' % (hook.__name__, content)
145+
#logger.error(err)
105146

106147
def daemonize(self):
107148
"""
@@ -145,7 +186,7 @@ def daemonize(self):
145186

146187
# write pidfile
147188
Daemon.register_atexit_hook(self.delpid)
148-
atexit.register(Daemon._atexit)
189+
atexit.register(Daemon._atexit)
149190
pid = str(os.getpid())
150191
file(self.pidfile,'w').write("%s\n" % pid)
151192

@@ -173,12 +214,12 @@ def start(self):
173214
sys.exit(0)
174215

175216
# Start the daemon
176-
self.daemonize()
217+
self.daemonize()
177218
try:
178-
self.run()
179-
except Exception:
180-
content = traceback.format_exc()
181-
#logger.error(content)
219+
self.run()
220+
except Exception:
221+
content = traceback.format_exc()
222+
#logger.error(content)
182223
sys.exit(1)
183224

184225
def stop(self):
@@ -192,25 +233,29 @@ def stop(self):
192233
pf.close()
193234
except IOError:
194235
pid = None
195-
236+
196237
if not pid:
197238
message = "pidfile %s does not exist. Daemon not running?\n"
198239
sys.stderr.write(message % self.pidfile)
199240
return # not an error in a restart
200241

201242
# Try killing the daemon process
202-
try:
203-
while 1:
204-
os.kill(pid, SIGTERM)
205-
time.sleep(0.1)
206-
except OSError, err:
207-
err = str(err)
208-
if err.find("No such process") > 0:
209-
if os.path.exists(self.pidfile):
210-
os.remove(self.pidfile)
243+
wait_stop = 5
244+
start_time = time.time()
245+
while 1:
246+
if os.path.exists('/proc/' + str(pid)):
247+
curr_time = time.time()
248+
if (curr_time - start_time) > wait_stop:
249+
os.kill(pid, SIGKILL)
250+
else:
251+
os.kill(pid, SIGTERM)
252+
time.sleep(0.3)
211253
else:
212-
print str(err)
213-
sys.exit(1)
254+
if os.path.exists(self.pidfile):
255+
self.delpid()
256+
break
257+
258+
print "Stop Daemon Successfully"
214259

215260
def restart(self):
216261
"""

plugins/hypervisors/baremetal/src/com/cloud/baremetal/networkservice/BareMetalResourceBase.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import javax.ejb.Local;
3131
import javax.naming.ConfigurationException;
3232

33+
import com.cloud.agent.api.*;
34+
import com.cloud.utils.Pair;
35+
import org.apache.cloudstack.api.ApiConstants;
3336
import org.apache.log4j.Logger;
3437
import org.apache.cloudstack.api.ApiConstants;
3538

@@ -399,7 +402,20 @@ public PingCommand getCurrentStatus(long id) {
399402
return null;
400403
}
401404

402-
return new PingRoutingCommand(getType(), id, deltaSync(), getHostVmStateReport());
405+
if (hostId != null) {
406+
vmDao = ComponentContext.getComponent(VMInstanceDao.class);
407+
final List<? extends VMInstanceVO> vms = vmDao.listByHostId(hostId);
408+
if (vms.isEmpty()) {
409+
return new PingRoutingCommand(getType(), id, deltaSync(), getHostVmStateReport());
410+
} else {
411+
VMInstanceVO vm = vms.get(0);
412+
SecurityGroupHttpClient client = new SecurityGroupHttpClient();
413+
HashMap<String, Pair<Long, Long>> nwGrpStates = client.sync(vm.getInstanceName(), vm.getId(), vm.getPrivateIpAddress());
414+
return new PingRoutingWithNwGroupsCommand(getType(), id, null, getHostVmStateReport(), nwGrpStates);
415+
}
416+
} else {
417+
return new PingRoutingCommand(getType(), id, deltaSync(), getHostVmStateReport());
418+
}
403419
}
404420

405421
protected Answer execute(IpmISetBootDevCommand cmd) {

0 commit comments

Comments
 (0)