Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
agent
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
7
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1d60a2b2
authored
Sep 18, 2014
by
Guba Sándor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
re-refactor Context
parent
56441d4b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
131 additions
and
160 deletions
+131
-160
agent.py
+9
-37
context.py
+118
-13
linux/_linuxcontext.py
+3
-2
win32/_win32context.py
+0
-0
win32/network.py
+1
-108
No files found.
agent.py
View file @
1d60a2b2
...
@@ -25,12 +25,15 @@ from twisted.internet.task import LoopingCall
...
@@ -25,12 +25,15 @@ from twisted.internet.task import LoopingCall
import
uptime
import
uptime
import
logging
import
logging
from
os.path
import
exists
from
inspect
import
getargspec
,
isfunction
from
inspect
import
getargspec
,
isfunction
from
utils
import
SerialLineReceiverBase
from
utils
import
SerialLineReceiverBase
from
context
import
Context
# Note: Import everything because later we need to use the BaseContext (relative
# import error.
from
context
import
*
Context
=
get_context
()
logging
.
basicConfig
()
logging
.
basicConfig
()
logger
=
logging
.
getLogger
()
logger
=
logging
.
getLogger
()
...
@@ -145,43 +148,12 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -145,43 +148,12 @@ class SerialLineReceiver(SerialLineReceiverBase):
pass
pass
def
_get_virtio_device
():
path
=
None
GUID
=
'{6FDE7521-1B65-48ae-B628-80BE62016026}'
from
infi.devicemanager
import
DeviceManager
dm
=
DeviceManager
()
dm
.
root
.
rescan
()
# Search Virtio-Serial by name TODO: search by class_guid
for
i
in
dm
.
all_devices
:
if
i
.
has_property
(
"description"
):
if
"virtio-serial"
.
upper
()
in
i
.
description
.
upper
():
path
=
(
"
\\\\
?
\\
"
+
i
.
children
[
0
]
.
instance_id
.
lower
()
.
replace
(
'
\\
'
,
'#'
)
+
"#"
+
GUID
.
lower
()
)
return
path
def
main
():
def
main
():
port
=
None
# Get proper serial class and port name
if
system
==
'Windows'
:
(
serial
,
port
)
=
get_serial
()
port
=
_get_virtio_device
()
if
port
:
from
context
import
SerialPort
else
:
from
twisted.internet.serial
import
SerialPort
import
pythoncom
pythoncom
.
CoInitialize
()
port
=
r'\\.\COM1'
else
:
port
=
"/dev/virtio-ports/agent"
if
exists
(
port
):
from
context
import
SerialPort
else
:
from
twisted.internet.serial
import
SerialPort
port
=
'/dev/ttyS0'
logger
.
info
(
"Opening port
%
s"
,
port
)
logger
.
info
(
"Opening port
%
s"
,
port
)
SerialPort
(
SerialLineReceiver
(),
port
,
reactor
)
# Open serial connection
serial
(
SerialLineReceiver
(),
port
,
reactor
)
try
:
try
:
from
notify
import
register_publisher
from
notify
import
register_publisher
register_publisher
(
reactor
)
register_publisher
(
reactor
)
...
...
context.py
View file @
1d60a2b2
import
platform
import
platform
from
os.path
import
exists
""" This is the defautl context file. It replaces the Context class
""" This is the defautl context file. It replaces the Context class
to the platform specific one.
to the platform specific one.
"""
"""
system
=
platform
.
system
()
if
system
==
"Windows"
:
def
_get_virtio_device
():
from
windows._win32context
import
Context
path
=
None
from
win32.win32virtio
import
SerialPort
GUID
=
'{6FDE7521-1B65-48ae-B628-80BE62016026}'
elif
system
==
"Linux"
:
from
infi.devicemanager
import
DeviceManager
from
linux._linuxcontext
import
Context
dm
=
DeviceManager
()
from
linux.posixvirtio
import
SerialPort
dm
.
root
.
rescan
()
# Search Virtio-Serial by name TODO: search by class_guid
for
i
in
dm
.
all_devices
:
if
i
.
has_property
(
"description"
):
if
"virtio-serial"
.
upper
()
in
i
.
description
.
upper
():
path
=
(
"
\\\\
?
\\
"
+
i
.
children
[
0
]
.
instance_id
.
lower
()
.
replace
(
'
\\
'
,
'#'
)
+
"#"
+
GUID
.
lower
()
)
return
path
else
:
raise
NotImplementedError
(
"Platform
%
s is not supported."
,
system
)
def
get_context
():
system
=
platform
.
system
()
if
system
==
"Windows"
:
from
win32._win32context
import
Context
elif
system
==
"Linux"
:
from
linux._linuxcontext
import
Context
else
:
raise
NotImplementedError
(
"Platform
%
s is not supported."
,
system
)
return
Context
class
BaseContext
():
pass
Context
def
get_serial
():
SerialPort
system
=
platform
.
system
()
port
=
None
if
system
==
'Windows'
:
port
=
_get_virtio_device
()
if
port
:
from
win32.win32virtio
import
SerialPort
else
:
from
twisted.internet.serial
import
SerialPort
import
pythoncom
pythoncom
.
CoInitialize
()
port
=
r'\\.\COM1'
elif
system
==
"Linux"
:
port
=
"/dev/virtio-ports/agent"
if
exists
(
port
):
from
linux.posixvirtio
import
SerialPort
else
:
from
twisted.internet.serial
import
SerialPort
port
=
'/dev/ttyS0'
else
:
raise
NotImplementedError
(
"Platform
%
s is not supported."
,
system
)
return
(
SerialPort
,
port
)
class
BaseContext
(
object
):
@staticmethod
def
change_password
(
password
):
pass
@staticmethod
def
restart_networking
():
pass
@staticmethod
def
change_ip
(
interfaces
,
dns
):
pass
@staticmethod
def
set_time
(
time
):
pass
@staticmethod
def
set_hostname
(
hostname
):
pass
@staticmethod
def
mount_store
(
host
,
username
,
password
):
pass
@staticmethod
def
get_keys
():
pass
@staticmethod
def
add_keys
(
keys
):
pass
@staticmethod
def
del_keys
(
keys
):
pass
@staticmethod
def
cleanup
():
pass
@staticmethod
def
start_access_server
():
pass
@staticmethod
def
append
(
data
,
filename
,
chunk_number
,
uuid
):
pass
@staticmethod
def
update
(
filename
,
executable
,
checksum
,
uuid
):
pass
@staticmethod
def
ipaddresses
():
pass
@staticmethod
def
get_agent_version
():
try
:
with
open
(
'version.txt'
)
as
f
:
return
f
.
readline
()
except
IOError
:
return
None
@staticmethod
def
send_expiration
(
url
):
import
notify
notify
.
notify
(
url
)
linux/_linuxcontext.py
View file @
1d60a2b2
...
@@ -30,7 +30,8 @@ from hashlib import md5
...
@@ -30,7 +30,8 @@ from hashlib import md5
from
ssh
import
PubKey
from
ssh
import
PubKey
from
network
import
change_ip_ubuntu
,
change_ip_rhel
from
.network
import
change_ip_ubuntu
,
change_ip_rhel
from
context
import
BaseContext
from
twisted.internet
import
reactor
from
twisted.internet
import
reactor
...
@@ -58,7 +59,7 @@ if system == 'Linux':
...
@@ -58,7 +59,7 @@ if system == 'Linux':
distro
=
distros
[
platform
.
linux_distribution
()[
0
]]
distro
=
distros
[
platform
.
linux_distribution
()[
0
]]
class
Context
(
objec
t
):
class
Context
(
BaseContex
t
):
# http://stackoverflow.com/questions/12081310/
# http://stackoverflow.com/questions/12081310/
# python-module-to-change-system-date-and-time
# python-module-to-change-system-date-and-time
...
...
win32/_win32context.py
View file @
1d60a2b2
This diff is collapsed.
Click to expand it.
win32/network.py
View file @
1d60a2b2
import
netifaces
from
netaddr
import
IPNetwork
,
IPAddress
from
netaddr
import
IPNetwork
,
IPAddress
import
fileinput
import
logging
import
logging
from
subprocess
import
check_output
,
CalledProcessError
from
subprocess
import
check_output
,
CalledProcessError
...
@@ -9,84 +7,6 @@ logger = logging.getLogger()
...
@@ -9,84 +7,6 @@ logger = logging.getLogger()
interfaces_file
=
'/etc/network/interfaces'
interfaces_file
=
'/etc/network/interfaces'
ifcfg_template
=
'/etc/sysconfig/network-scripts/ifcfg-
%
s'
ifcfg_template
=
'/etc/sysconfig/network-scripts/ifcfg-
%
s'
def
get_interfaces_linux
(
interfaces
):
for
ifname
in
netifaces
.
interfaces
():
mac
=
netifaces
.
ifaddresses
(
ifname
)[
17
][
0
][
'addr'
]
conf
=
interfaces
.
get
(
mac
.
upper
())
if
conf
:
yield
ifname
,
conf
def
remove_interfaces_ubuntu
(
devices
):
delete_device
=
False
for
line
in
fileinput
.
input
(
interfaces_file
,
inplace
=
True
):
line
=
line
.
rstrip
()
words
=
line
.
split
()
if
line
.
startswith
(
'#'
)
or
line
==
''
or
line
.
isspace
()
or
not
words
:
# keep line
print
line
continue
if
(
words
[
0
]
in
(
'auto'
,
'allow-hotplug'
)
and
words
[
1
]
.
split
(
':'
)[
0
]
in
devices
):
# remove line
continue
if
words
[
0
]
==
'iface'
:
if
words
[
1
]
.
split
(
':'
)[
0
]
in
devices
:
# remove line
delete_device
=
True
continue
else
:
delete_device
=
False
if
line
[
0
]
in
(
' '
,
'
\t
'
)
and
delete_device
:
# remove line
continue
# keep line
print
line
def
change_ip_ubuntu
(
interfaces
,
dns
):
data
=
list
(
get_interfaces_linux
(
interfaces
))
remove_interfaces_ubuntu
(
dict
(
data
)
.
keys
())
with
open
(
interfaces_file
,
'a'
)
as
f
:
for
ifname
,
conf
in
data
:
ipv4_alias_counter
=
ipv6_alias_counter
=
0
f
.
write
(
'auto
%
s
\n
'
%
ifname
)
for
i
in
conf
[
'addresses'
]:
ip_with_prefix
=
IPNetwork
(
i
)
prefixlen
=
ip_with_prefix
.
prefixlen
ip
=
ip_with_prefix
.
ip
alias
=
ifname
if
ip
.
version
==
6
:
if
ipv6_alias_counter
>
0
:
alias
=
'
%
s:
%
d'
%
(
ifname
,
ipv6_alias_counter
)
ipv6_alias_counter
+=
1
else
:
if
ipv4_alias_counter
>
0
:
alias
=
'
%
s:
%
d'
%
(
ifname
,
ipv4_alias_counter
)
ipv4_alias_counter
+=
1
f
.
write
(
'iface
%(ifname)
s
%(proto)
s static
\n
'
' address
%(ip)
s
\n
'
' netmask
%(prefixlen)
d
\n
'
' gateway
%(gw)
s
\n
'
' dns-nameservers
%(dns)
s
\n
'
%
{
'ifname'
:
alias
,
'proto'
:
'inet6'
if
ip
.
version
==
6
else
'inet'
,
'ip'
:
ip
,
'prefixlen'
:
prefixlen
,
'gw'
:
conf
[
'gw6'
if
ip
.
version
==
6
else
'gw4'
],
'dns'
:
dns
})
# example:
# example:
# change_ip_ubuntu({
# change_ip_ubuntu({
# u'02:00:00:02:A3:E8': {
# u'02:00:00:02:A3:E8': {
...
@@ -97,33 +17,6 @@ def change_ip_ubuntu(interfaces, dns):
...
@@ -97,33 +17,6 @@ def change_ip_ubuntu(interfaces, dns):
# '8.8.8.8')
# '8.8.8.8')
def
change_ip_rhel
(
interfaces
,
dns
):
for
ifname
,
conf
in
get_interfaces_linux
(
interfaces
):
with
open
(
ifcfg_template
%
ifname
,
'w'
)
as
f
:
f
.
write
(
'DEVICE=
%
s
\n
'
'BOOTPROTO=none
\n
'
'USERCTL=no
\n
'
'ONBOOT=yes
\n
'
%
ifname
)
for
i
in
conf
[
'addresses'
]:
ip_with_prefix
=
IPNetwork
(
i
)
ip
=
ip_with_prefix
.
ip
if
ip
.
version
==
6
:
f
.
write
(
'IPV6INIT=yes
\n
'
'IPV6ADDR=
%(ip)
s/
%(prefixlen)
d
\n
'
'IPV6_DEFAULTGW=
%(gw)
s
\n
'
%
{
'ip'
:
ip
,
'prefixlen'
:
ip_with_prefix
.
prefixlen
,
'gw'
:
conf
[
'gw6'
]})
else
:
f
.
write
(
'NETMASK=
%(netmask)
s
\n
'
'IPADDR=
%(ip)
s
\n
'
'GATEWAY=
%(gw)
s
\n
'
%
{
'ip'
:
ip
,
'netmask'
:
str
(
ip_with_prefix
.
netmask
),
'gw'
:
conf
[
'gw4'
]})
def
get_interfaces_windows
(
interfaces
):
def
get_interfaces_windows
(
interfaces
):
import
wmi
import
wmi
nics
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
nics
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
...
@@ -133,7 +26,7 @@ def get_interfaces_windows(interfaces):
...
@@ -133,7 +26,7 @@ def get_interfaces_windows(interfaces):
yield
nic
,
conf
yield
nic
,
conf
def
change_ip
_windows
(
interfaces
,
dns
):
def
change_ip
(
interfaces
,
dns
):
for
nic
,
conf
in
get_interfaces_windows
(
interfaces
):
for
nic
,
conf
in
get_interfaces_windows
(
interfaces
):
link_local
=
IPNetwork
(
'fe80::/16'
)
link_local
=
IPNetwork
(
'fe80::/16'
)
new_addrs
=
[
IPNetwork
(
ip
)
for
ip
in
conf
[
'addresses'
]]
new_addrs
=
[
IPNetwork
(
ip
)
for
ip
in
conf
[
'addresses'
]]
...
...
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