Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE3
/
agentdriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
0a1798be
authored
Jun 25, 2024
by
Szeberényi Imre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
python 3.6 on mega7
parent
859ecd46
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
107 additions
and
29 deletions
+107
-29
agentcelery.py
+4
-3
agentdriver.py
+2
-1
miscellaneous/agentdriver.conf
+3
-3
protocol.py
+82
-11
requirements/base.txt
+3
-3
utils.py
+13
-8
No files found.
agentcelery.py
View file @
0a1798be
...
...
@@ -14,8 +14,9 @@ HOSTNAME = gethostname().split('.')[0]
AMQP_URI
=
getenv
(
'AMQP_URI'
)
celery
=
Celery
(
'agent'
,
broker
=
AMQP_URI
)
celery
.
conf
.
update
(
CELERY_RESULT_BACKEND
=
'amqp://'
,
CELERY_TASK_RESULT_EXPIRES
=
30000
,
celery
.
conf
.
update
(
CELERY_RESULT_BACKEND
=
'amqp'
,
CELERY_TASK_RESULT_EXPIRES
=
300
,
CELERYD_PREFETCH_MULTIPLIER
=
32
,
CELERY_QUEUES
=
(
Queue
(
HOSTNAME
+
'.agent'
,
Exchange
(
'agent'
,
type
=
'direct'
),
routing_key
=
'agent'
),
))
...
...
@@ -33,7 +34,7 @@ def send_command(vm, command, *args, **kwargs):
if
command
==
'append'
and
'vio-cloud'
not
in
conn
.
transport
.
addr
:
continue
logger
.
info
(
'
%
s(
%
s,
%
s)'
,
command
,
vm
,
', '
.
join
(
[
str
(
x
)[:
100
]
for
x
in
list
(
kwargs
.
values
())]
))
', '
.
join
(
map
(
lambda
x
:
str
(
x
)[:
100
],
kwargs
.
values
())
))
conn
.
send_command
(
command
=
command
,
args
=
kwargs
)
if
uuid
:
...
...
agentdriver.py
View file @
0a1798be
...
...
@@ -13,7 +13,7 @@ level = environ.get('LOGLEVEL', 'INFO')
logger
.
setLevel
(
level
)
SOCKET_DIR
=
getenv
(
'SOCKET_DIR'
,
'/var/lib/libvirt/'
)
SOCKET_DIR
=
getenv
(
'SOCKET_DIR'
,
'/var/lib/libvirt/
serial
'
)
old_install_platform_tweaks
=
Worker
.
install_platform_tweaks
...
...
@@ -26,6 +26,7 @@ Worker.install_platform_tweaks = install_platform_tweaks
def
reactor_started
():
logger
.
info
(
"reactor_started"
)
reactor
.
running_tasks
=
{}
reactor
.
ended_tasks
=
{}
for
f
in
listdir
(
SOCKET_DIR
):
...
...
miscellaneous/agentdriver.conf
View file @
0a1798be
...
...
@@ -10,8 +10,8 @@ setuid cloud
script
cd
/
home
/
kohlkriszto
/
Documents
/
agentdriver
. /
home
/
kohlkriszto
/
Documents
/
agentdriver_venv
/
bin
/
activate
. /
home
/
kohlkriszto
/
Documents
/
agentdriver_venv
/
bin
/
postactivate
cd
/
home
/
cloud
/
agentdriver
. /
home
/
cloud
/.
virtualenvs
/
agentdriver
/
local
/
bin
/
activate
. /
home
/
cloud
/.
virtualenvs
/
agentdriver
/
local
/
bin
/
postactivate
python
agentdriver
.
py
end
script
protocol.py
View file @
0a1798be
#!/usr/bin/env python
from
twisted.internet
import
protocol
,
reactor
from
twisted.internet
import
protocol
,
reactor
,
inotify
import
pickle
import
logging
import
time
import
struct
from
os
import
getenv
import
gc
from
utils
import
SerialLineReceiverBase
...
...
@@ -15,15 +16,22 @@ logger = logging.getLogger()
reactor
.
connections
=
{}
def
numObjsByName
(
name
):
num
=
0
for
ob
in
gc
.
get_objects
():
if
isinstance
(
ob
,
name
):
num
+=
1
return
num
class
GraphiteClientProtocol
(
protocol
.
Protocol
):
def
connectionMade
(
self
):
logger
.
info
(
"Monitor connection
%
s"
,
self
.
name
)
timestamp
=
time
.
time
()
data_list
=
[]
for
key
,
value
in
list
(
self
.
data
.
items
()
):
for
key
,
value
in
self
.
data
.
items
(
):
if
not
isinstance
(
value
,
dict
):
continue
for
k
,
v
in
list
(
value
.
items
()
):
for
k
,
v
in
value
.
items
(
):
data_list
.
append
((
'agent.
%
s.
%
s.
%
s'
%
(
self
.
name
,
key
,
k
),
(
timestamp
,
float
(
v
))))
...
...
@@ -41,18 +49,38 @@ class GraphiteClientFactory(protocol.ClientFactory):
def
inotify_handler
(
self
,
file
,
mask
):
file
=
file
.
asTextMode
(
encoding
=
'utf-8'
)
if
file
.
basename
()
.
startswith
(
'cloud'
):
return
vm
=
file
.
basename
()
.
replace
(
'vio-'
,
''
)
logger
.
info
(
'inotify:
%
s (
%
s)'
,
vm
,
file
.
path
)
for
conn
in
reactor
.
connections
.
get
(
vm
,
[]):
if
file
.
path
==
conn
.
transport
.
addr
:
return
if
mask
:
logger
.
info
(
"event
%
s (
%
s) on
%
s"
%
(
', '
.
join
(
inotify
.
humanReadableMask
(
mask
)),
mask
,
file
))
if
vm
in
reactor
.
running_tasks
:
for
addr
in
reactor
.
running_tasks
[
vm
]
.
get
(
'started'
,
None
)
.
copy
()
:
if
file
.
path
==
addr
:
if
mask
and
mask
==
inotify
.
IN_DELETE
and
reactor
.
running_tasks
[
vm
][
'started'
][
addr
]:
_p
=
reactor
.
running_tasks
[
vm
][
'started'
][
addr
]
logger
.
info
(
"DELETE
%
s"
,
_p
)
logger
.
info
(
'NumOBJSerialLineReceiverFactory1:
%
s'
,
numObjsByName
(
SerialLineReceiverFactory
))
del
_p
reactor
.
running_tasks
[
vm
][
'started'
]
.
pop
(
addr
)
logger
.
info
(
'NumOBJSerialLineReceiverFactory2:
%
s'
,
numObjsByName
(
SerialLineReceiverFactory
))
logger
.
info
(
'reacror_running1
%
s'
,
reactor
.
running_tasks
)
return
elif
reactor
.
running_tasks
[
vm
][
'started'
][
addr
]:
return
serial
=
SerialLineReceiverFactory
(
vm
)
logger
.
info
(
"connecting to
%
s (
%
s)"
,
vm
,
file
.
path
)
reactor
.
connectUNIX
(
file
.
path
,
serial
)
ic
=
reactor
.
connectUNIX
(
file
.
path
,
serial
,
10
)
logger
.
info
(
'IConnector state:
%
s'
,
ic
.
state
)
logger
.
info
(
'reacror_running2
%
s'
,
reactor
.
running_tasks
)
# logger.info('NumOBJSerialLineReceiverFactory2: %s' , numObjsByName(SerialLineReceiverFactory))
class
SerialLineReceiver
(
SerialLineReceiverBase
):
def
send_to_graphite
(
self
,
data
):
logger
.
info
(
"Send_TO_Graphite"
)
client
=
GraphiteClientFactory
()
client
.
protocol
.
data
=
data
client
.
protocol
.
name
=
self
.
factory
.
vm
...
...
@@ -61,6 +89,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
client
)
def
handle_command
(
self
,
command
,
args
):
logger
.
info
(
'serial_command:
%
s
%
s'
,
command
,
args
)
if
command
==
'agent_stopped'
:
agent_stopped
.
apply_async
(
queue
=
'localhost.man'
,
args
=
(
self
.
factory
.
vm
,
))
...
...
@@ -77,6 +106,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
args
=
args
)
def
handle_response
(
self
,
response
,
args
):
logger
.
info
(
'handle_reponse:
%
s
%
s'
,
response
,
args
)
vm
=
self
.
factory
.
vm
if
response
==
'status'
:
self
.
send_to_graphite
(
args
)
...
...
@@ -89,17 +119,25 @@ class SerialLineReceiver(SerialLineReceiverBase):
reactor
.
ended_tasks
[
vm
][
uuid
]
=
args
event
.
set
()
def
connectionMade
(
self
):
logger
.
info
(
"connected to
%
s (
%
s)"
,
self
.
factory
.
vm
,
self
.
transport
.
addr
)
logger
.
info
(
"connected to
%
s (
%
s)"
,
self
.
factory
.
vm
,
self
.
transport
.
addr
)
logger
.
info
(
"reactor connections:
%
s"
,
reactor
.
connections
)
if
self
.
factory
.
vm
not
in
reactor
.
connections
:
reactor
.
connections
[
self
.
factory
.
vm
]
=
set
()
logger
.
info
(
"reactor connections factory:
%
s"
,
reactor
.
connections
[
self
.
factory
.
vm
])
reactor
.
connections
[
self
.
factory
.
vm
]
.
add
(
self
)
logger
.
info
(
'NumOBJSerialLineReceiver:
%
s'
,
numObjsByName
(
SerialLineReceiver
))
def
connectionLost
(
self
,
reason
):
logger
.
info
(
"disconnected from
%
s (
%
s)"
,
self
.
factory
.
vm
,
self
.
transport
.
addr
)
logger
.
info
(
"disconnected from
%
s (
%
s)"
,
self
.
factory
.
vm
,
self
.
transport
.
addr
)
reactor
.
connections
[
self
.
factory
.
vm
]
.
remove
(
self
)
vm
=
self
.
factory
.
vm
# for addr in reactor.running_tasks[vm].get('started', None):
# if addr == self.transport.addr :
# reactor.running_tasks[vm]['started'][addr] = None
logger
.
info
(
"active connetions:
%
s"
,
reactor
.
running_tasks
[
vm
])
class
SerialLineReceiverFactory
(
protocol
.
ClientFactory
):
...
...
@@ -111,3 +149,36 @@ class SerialLineReceiverFactory(protocol.ClientFactory):
reactor
.
running_tasks
[
vm
]
=
{}
if
vm
not
in
reactor
.
ended_tasks
:
reactor
.
ended_tasks
[
vm
]
=
{}
def
startedConnecting
(
self
,
connector
):
vm
=
self
.
vm
addr
=
connector
.
address
logger
.
info
(
"startedConnecting to
%
s (
%
s)"
,
vm
,
addr
)
logger
.
info
(
"started connetions:
%
s"
,
reactor
.
running_tasks
[
vm
])
if
not
reactor
.
running_tasks
[
vm
]
.
get
(
'started'
,
None
):
reactor
.
running_tasks
[
vm
][
'started'
]
=
{}
reactor
.
running_tasks
[
vm
][
'started'
][
addr
]
=
self
logger
.
info
(
'NumOBJSerialLineReceiverFactory:
%
s'
,
numObjsByName
(
SerialLineReceiverFactory
))
logger
.
info
(
'NumOBJSerialLineReceiver:
%
s'
,
numObjsByName
(
SerialLineReceiver
))
logger
.
info
(
"NumConnetions:
%
s"
,
reactor
.
running_tasks
)
def
clientrConnectionLost
(
self
,
connector
):
vm
=
self
.
vm
addr
=
connector
.
address
logger
.
info
(
"clientConnectionLost with
%
s (
%
s)"
,
vm
,
addr
)
for
_addr
in
reactor
.
running_tasks
[
vm
]
.
get
(
'started'
,
None
):
if
_addr
==
addr
:
reactor
.
running_tasks
[
vm
][
'started'
]
.
pop
(
addr
)
logger
.
info
(
"active connetions:
%
s"
,
reactor
.
running_tasks
[
vm
])
def
clientrConnectionFailed
(
self
,
connector
,
reason
):
vm
=
self
.
vm
addr
=
connector
.
address
logger
.
info
(
"clientConnectionFailed with
%
s (
%
s)"
,
vm
,
connector
.
addr
)
for
_addr
in
reactor
.
running_tasks
[
vm
]
.
get
(
'started'
,
None
):
if
_addr
==
addr
:
reactor
.
running_tasks
[
vm
][
'started'
]
.
pop
(
addr
)
logger
.
info
(
"active connetions:
%
s"
,
reactor
.
running_tasks
[
vm
])
requirements/base.txt
View file @
0a1798be
celery
Twisted
threadpool
celery
==3.1.17
Twisted
==20.3.0
threadpool
==1.3.2
utils.py
View file @
0a1798be
...
...
@@ -2,19 +2,24 @@ from twisted.protocols.basic import LineReceiver
import
json
import
logging
logger
=
logging
.
getLogger
()
try
:
# Python 2: "unicode" is built-in
unicode
except
NameError
:
unicode
=
str
logger
=
logging
.
getLogger
()
class
SerialLineReceiverBase
(
LineReceiver
,
object
):
delimiter
=
'
\r
'
delimiter
=
b
'
\r
'
def
send_response
(
self
,
response
,
args
):
self
.
transport
.
write
(
json
.
dumps
({
'response'
:
response
,
'args'
:
args
})
+
'
\r\n
'
)
self
.
transport
.
write
(
str
.
encode
(
json
.
dumps
({
'response'
:
response
,
'args'
:
args
})
+
'
\r\n
'
)
)
def
send_command
(
self
,
command
,
args
):
self
.
transport
.
write
(
json
.
dumps
({
'command'
:
command
,
'args'
:
args
})
+
'
\r\n
'
)
self
.
transport
.
write
(
str
.
encode
(
json
.
dumps
({
'command'
:
command
,
'args'
:
args
})
+
'
\r\n
'
)
)
def
handle_command
(
self
,
command
,
args
):
raise
NotImplementedError
(
"Subclass must implement abstract method"
)
...
...
@@ -37,9 +42,9 @@ class SerialLineReceiverBase(LineReceiver, object):
logger
.
error
(
'[serial] invalid json:
%
s (
%
s)'
%
(
data
,
e
))
return
if
command
is
not
None
and
isinstance
(
command
,
str
):
if
command
is
not
None
and
isinstance
(
command
,
unicode
):
logger
.
debug
(
'received command:
%
s (
%
s)'
%
(
command
,
args
))
self
.
handle_command
(
command
,
args
)
elif
response
is
not
None
and
isinstance
(
response
,
str
):
elif
response
is
not
None
and
isinstance
(
response
,
unicode
):
logger
.
debug
(
'received reply:
%
s (
%
s)'
%
(
response
,
args
))
self
.
handle_response
(
response
,
args
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment