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
0211dd10
authored
Dec 12, 2013
by
Bach Dániel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix flake8 errors, fix set_hostname(), add mount_store()
parent
26201eeb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
4 deletions
+49
-4
agent.py
+49
-4
No files found.
agent.py
View file @
0211dd10
#!/usr/bin/env python
#!/usr/bin/env python
from
twisted.internet
import
protocol
,
reactor
,
defer
from
twisted.internet
import
reactor
,
defer
from
twisted.internet.task
import
LoopingCall
from
twisted.internet.task
import
LoopingCall
from
twisted.internet.serialport
import
SerialPort
from
twisted.internet.serialport
import
SerialPort
...
@@ -47,8 +47,8 @@ class Context(object):
...
@@ -47,8 +47,8 @@ class Context(object):
'restart'
])
'restart'
])
elif
system
==
'Windows'
:
elif
system
==
'Windows'
:
import
wmi
import
wmi
nic_configs
=
wmi
.
WMI
()
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
w
=
wmi
.
WMI
(
)
nic
=
nic_configs
[
0
]
nic
=
w
.
Win32_NetworkAdapterConfiguration
(
IPEnabled
=
True
)
[
0
]
assert
nic
.
EnableDHCP
()[
0
]
==
0
assert
nic
.
EnableDHCP
()[
0
]
==
0
@staticmethod
@staticmethod
...
@@ -67,15 +67,52 @@ class Context(object):
...
@@ -67,15 +67,52 @@ class Context(object):
def
set_hostname
(
new_hostname
):
def
set_hostname
(
new_hostname
):
system
=
platform
.
system
()
system
=
platform
.
system
()
if
system
==
'Linux'
:
if
system
==
'Linux'
:
pass
# /etc/hostname
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'/etc/hostname'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
new_hostname
)
# /etc/hosts
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'-a'
,
'/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
def
mount_store
(
host
,
username
,
password
,
key
):
system
=
platform
.
system
()
if
system
==
'Linux'
:
data
=
(
'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
'
)
proc
=
subprocess
.
Popen
([
'/usr/bin/sudo'
,
'/usr/bin/tee'
,
'-a'
,
'/etc/fstab'
],
stdin
=
subprocess
.
PIPE
)
proc
.
communicate
(
data
%
{
'host'
:
host
,
'username'
:
username
,
'password'
:
password
})
elif
system
==
'Windows'
:
data
=
(
'net use * /delete /yes
\r\n
'
'timeout 5
\r\n
'
'net use z:
\\
%(hostname)
s
\\
%(username)
s "
%(password)
s" '
'/user:
%(username)
s'
)
with
open
(
r'c:\Windows\System32\Repl\Import\Scripts'
r'
%
s.bat'
%
username
,
'w'
)
as
f
:
f
.
write
(
data
)
class
SerialLineReceiver
(
SerialLineReceiverBase
):
class
SerialLineReceiver
(
SerialLineReceiverBase
):
def
connectionMade
(
self
):
def
connectionMade
(
self
):
self
.
send_command
(
command
=
'agent_started'
,
self
.
send_command
(
command
=
'agent_started'
,
args
=
{})
args
=
{})
def
shutdown
():
def
shutdown
():
self
.
connectionLost2
(
'shutdown'
)
self
.
connectionLost2
(
'shutdown'
)
d
=
defer
.
Deferred
()
d
=
defer
.
Deferred
()
...
@@ -105,6 +142,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -105,6 +142,7 @@ class SerialLineReceiver(SerialLineReceiverBase):
args
=
args
)
args
=
args
)
def
send_ipaddresses
(
self
):
def
send_ipaddresses
(
self
):
import
netifaces
args
=
{}
args
=
{}
interfaces
=
netifaces
.
interfaces
()
interfaces
=
netifaces
.
interfaces
()
for
i
in
interfaces
:
for
i
in
interfaces
:
...
@@ -134,6 +172,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
...
@@ -134,6 +172,13 @@ class SerialLineReceiver(SerialLineReceiverBase):
Context
.
restart_networking
()
Context
.
restart_networking
()
elif
command
==
'set_time'
:
elif
command
==
'set_time'
:
Context
.
set_time
(
str
(
args
[
'time'
]))
Context
.
set_time
(
str
(
args
[
'time'
]))
elif
command
==
'set_hostname'
:
Context
.
set_hostname
(
str
(
args
[
'hostname'
]))
elif
command
==
'mount_store'
:
Context
.
mount_store
(
str
(
args
[
'host'
]),
str
(
args
[
'username'
]),
str
(
args
[
'password'
]),
str
(
args
[
'key'
]))
def
handle_response
(
self
,
response
,
args
):
def
handle_response
(
self
,
response
,
args
):
pass
pass
...
...
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