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
262b6add
authored
Mar 08, 2014
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code refactoring
parent
e10b401d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
43 deletions
+55
-43
agent.py
+55
-43
No files found.
agent.py
View file @
262b6add
...
@@ -7,20 +7,48 @@ from twisted.internet.serialport import SerialPort
...
@@ -7,20 +7,48 @@ from twisted.internet.serialport import SerialPort
import
psutil
import
psutil
import
uptime
import
uptime
import
subprocess
import
subprocess
import
fileinput
#import netifaces
#import netifaces
import
platform
import
platform
from
datetime
import
datetime
from
datetime
import
datetime
from
utils
import
SerialLineReceiverBase
from
utils
import
SerialLineReceiverBase
fstab_template
=
(
'sshfs#
%(username)
s@
%(host)
s:home /home/cloud/sshfs '
'fuse defaults,idmap=user,reconnect,_netdev,uid=1000,'
'gid=1000,allow_other,StrictHostKeyChecking=no,'
'IdentityFile=/home/cloud/.ssh/id_rsa 0 0
\n
'
)
system
=
platform
.
system
()
# http://stackoverflow.com/questions/12081310/
# python-module-to-change-system-date-and-time
def
linux_set_time
(
time
):
import
ctypes
import
ctypes.util
CLOCK_REALTIME
=
0
class
timespec
(
ctypes
.
Structure
):
_fields_
=
[(
"tv_sec"
,
ctypes
.
c_long
),
(
"tv_nsec"
,
ctypes
.
c_long
)]
librt
=
ctypes
.
CDLL
(
ctypes
.
util
.
find_library
(
"rt"
))
ts
=
timespec
()
ts
.
tv_sec
=
int
(
time
)
ts
.
tv_nsec
=
0
librt
.
clock_settime
(
CLOCK_REALTIME
,
ctypes
.
byref
(
ts
))
class
Context
(
object
):
class
Context
(
object
):
@staticmethod
@staticmethod
def
change_password
(
new_password
):
def
change_password
(
new_password
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
proc
=
subprocess
.
Popen
([
'/usr/sbin/chpasswd'
],
'/usr/sbin/chpasswd'
],
stdin
=
subprocess
.
PIPE
)
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
'cloud:
%
s
\n
'
%
new_password
)
proc
.
communicate
(
'cloud:
%
s
\n
'
%
new_password
)
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
...
@@ -31,20 +59,13 @@ class Context(object):
...
@@ -31,20 +59,13 @@ class Context(object):
@staticmethod
@staticmethod
def
restart_networking
():
def
restart_networking
():
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
interfaces
=
'''
with
open
(
'/etc/network/interfaces'
,
'w'
)
as
f
:
auto lo
f
.
write
(
'auto lo
\n
'
iface lo inet loopback
'iface lo inet loopback
\n
'
auto eth0
'auto eth0
\n
'
iface eth0 inet dhcp
'iface eth0 inet dhcp
\n
'
)
'''
subprocess
.
call
([
'/etc/init.d/networking'
,
'restart'
])
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'/etc/network/interfaces'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
interfaces
)
subprocess
.
call
([
'/usr/bin/sudo'
,
'/etc/init.d/networking'
,
'restart'
])
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
import
wmi
import
wmi
w
=
wmi
.
WMI
()
w
=
wmi
.
WMI
()
...
@@ -53,10 +74,12 @@ class Context(object):
...
@@ -53,10 +74,12 @@ class Context(object):
@staticmethod
@staticmethod
def
set_time
(
new_time
):
def
set_time
(
new_time
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
subprocess
.
call
([
'/usr/bin/sudo'
,
linux_set_time
(
float
(
new_time
))
'/etc/init.d/openntpd'
,
'restart'
])
try
:
subprocess
.
call
([
'/etc/init.d/openntpd'
,
'restart'
])
except
:
pass
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
import
win32api
import
win32api
t
=
datetime
.
utcfromtimestamp
(
float
(
new_time
))
t
=
datetime
.
utcfromtimestamp
(
float
(
new_time
))
...
@@ -65,37 +88,27 @@ class Context(object):
...
@@ -65,37 +88,27 @@ class Context(object):
@staticmethod
@staticmethod
def
set_hostname
(
new_hostname
):
def
set_hostname
(
new_hostname
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
# /etc/hostname
with
open
(
'/etc/hostname'
,
'w'
)
as
f
:
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
f
.
write
(
new_hostname
)
'/etc/hostname'
],
with
open
(
'/etc/hosts'
,
'w'
)
as
f
:
stdin
=
subprocess
.
PIPE
)
f
.
write
(
'127.0.0.1 localhost'
proc
.
communicate
(
new_hostname
)
'127.0.1.1
%
s
\n
'
%
new_hostname
)
# /etc/hosts
# set hostname
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'-a'
,
subprocess
.
call
([
'/bin/hostname'
,
new_hostname
])
'/etc/hosts'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
'127.0.1.1
%
s
\n
'
%
new_hostname
)
subprocess
.
call
([
'/usr/bin/sudo'
,
'/bin/hostname'
,
new_hostname
])
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
import
wmi
import
wmi
wmi
.
WMI
()
.
Win32_ComputerSystem
()[
0
]
.
Rename
(
new_hostname
)
wmi
.
WMI
()
.
Win32_ComputerSystem
()[
0
]
.
Rename
(
new_hostname
)
@staticmethod
@staticmethod
def
mount_store
(
host
,
username
,
password
,
key
):
def
mount_store
(
host
,
username
,
password
,
key
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
data
=
(
'sshfs#
%(username)
s@
%(host)
s:home /home/cloud/sshfs '
for
line
in
fileinput
.
input
(
'/etc/fstab'
,
inplace
=
1
):
'fuse defaults,idmap=user,reconnect,_netdev,uid=1000,'
if
line
.
startswith
(
'sshfs#'
):
'gid=1000,allow_other,StrictHostKeyChecking=no,'
line
=
''
'IdentityFile=/home/cloud/.ssh/id_rsa 0 0
\n
'
)
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'-a'
,
with
open
(
'/etc/fstab'
,
'a'
)
as
f
:
'/etc/fstab'
],
f
.
write
(
fstab_template
%
{
'host'
:
host
,
'username'
:
username
,
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
data
%
{
'host'
:
host
,
'username'
:
username
,
'password'
:
password
})
'password'
:
password
})
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
...
@@ -189,7 +202,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -189,7 +202,6 @@ class SerialLineReceiver(SerialLineReceiverBase):
def
main
():
def
main
():
system
=
platform
.
system
()
if
system
==
'Windows'
:
if
system
==
'Windows'
:
port
=
r'\\.\COM1'
port
=
r'\\.\COM1'
else
:
else
:
...
...
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