Commit a0f8465b by Czémán Arnold

Merge remote-tracking branch 'origin/master' into freebsd

 Conflicts:
	agent.py
parents f6960606 16b8fc3a
......@@ -5,3 +5,9 @@
version.txt
*,cover
.coverage
*.log
build
dist
*.exe
*.spec
......@@ -3,7 +3,6 @@
from os import environ, chdir
import platform
from shutil import copy
import subprocess
import sys
......@@ -13,7 +12,6 @@ if system == "Linux" or system == "FreeBSD":
try:
chdir(sys.path[0])
subprocess.call(('pip', 'install', '-r', 'requirements.txt'))
copy("/root/agent/misc/vm_renewal", "/usr/local/bin/")
except:
pass # hope it works
......@@ -56,6 +54,9 @@ class SerialLineReceiver(SerialLineReceiverBase):
return d
reactor.addSystemEventTrigger("before", "shutdown", shutdown)
def connectionLost(self, reason):
reactor.stop()
def connectionLost2(self, reason):
self.send_command(command='agent_stopped',
args={})
......@@ -74,9 +75,9 @@ class SerialLineReceiver(SerialLineReceiverBase):
def send_status(self):
import psutil
disk_usage = {(disk.device.replace('/', '_')):
psutil.disk_usage(disk.mountpoint).percent
for disk in psutil.disk_partitions()}
disk_usage = dict((disk.device.replace('/', '_'),
psutil.disk_usage(disk.mountpoint).percent)
for disk in psutil.disk_partitions())
args = {"cpu": dict(psutil.cpu_times()._asdict()),
"ram": dict(psutil.virtual_memory()._asdict()),
"swap": dict(psutil.swap_memory()._asdict()),
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import mkdir, environ, chdir
from os import mkdir, remove, chown
from pwd import getpwnam
import platform
from shutil import copy, rmtree, move
from shutil import rmtree, move
import subprocess
import sys
system = platform.system()
working_directory = sys.path[0]
try:
chdir(working_directory)
subprocess.call(('pip', 'install', '-r', 'requirements.txt'))
if system == 'Linux':
copy("/root/agent/misc/vm_renewal", "/usr/local/bin/")
except:
pass # hope it works
import logging
import fileinput
import tarfile
......@@ -35,10 +26,7 @@ from context import BaseContext
from twisted.internet import reactor
logging.basicConfig()
logger = logging.getLogger()
level = environ.get('LOGLEVEL', 'INFO')
logger.setLevel(level)
SSH_DIR = expanduser('~cloud/.ssh')
AUTHORIZED_KEYS = join(SSH_DIR, 'authorized_keys')
......@@ -51,18 +39,19 @@ mount_template_linux = (
distros = {'Scientific Linux': 'rhel',
'Oracle Linux Server': 'rhel',
'CentOS': 'rhel',
'CentOS Linux': 'rhel',
'Debian': 'debian',
'Ubuntu': 'debian'}
if system == 'Linux':
distro = distros[platform.linux_distribution()[0]]
distro = distros[platform.linux_distribution()[0]]
class Context(BaseContext):
# http://stackoverflow.com/questions/12081310/
# python-module-to-change-system-date-and-time
@staticmethod
def _linux_set_time(time):
import ctypes
import ctypes.util
......@@ -161,7 +150,6 @@ class Context(BaseContext):
@staticmethod
def _save_keys(keys):
print keys
try:
mkdir(SSH_DIR)
except OSError:
......@@ -170,6 +158,10 @@ class Context(BaseContext):
for key in keys:
f.write(unicode(key) + '\n')
uid = getpwnam("cloud").pw_uid
chown(SSH_DIR, uid, -1)
chown(AUTHORIZED_KEYS, uid, -1)
@staticmethod
def add_keys(keys):
new_keys = Context.get_keys()
......@@ -199,13 +191,16 @@ class Context(BaseContext):
@staticmethod
def cleanup():
filelist = ([
'/root/.bash_history'
'/home/cloud/.bash_history'
'/root/.ssh'
'/home/cloud/.ssh']
+ glob('/etc/ssh/ssh_host_*'))
'/root/.bash_history', '/home/cloud/.bash_history',
] + glob('/etc/ssh/ssh_host_*'))
dirlist = ('/root/.ssh', '/home/cloud/.ssh')
for d in dirlist:
rmtree(d, ignore_errors=True)
for f in filelist:
rmtree(f, ignore_errors=True)
if exists(f):
remove(f)
subprocess.call(('/usr/bin/ssh-keygen', '-A'))
......
......@@ -7,7 +7,9 @@ Virtio-Serial Port Protocol
"""
# system imports
import logging
import os
from time import sleep
# dependent on pyserial ( http://pyserial.sf.net/ )
# only tested w/ 1.18 (5 Dec 2002)
......@@ -15,6 +17,8 @@ import os
# twisted imports
from twisted.internet import abstract, fdesc
logger = logging.getLogger()
class SerialPort(abstract.FileDescriptor):
"""
......@@ -57,4 +61,8 @@ class SerialPort(abstract.FileDescriptor):
"""
abstract.FileDescriptor.connectionLost(self, reason)
os.close(self._serial)
self.protocol.connectionLost(reason)
sleep(2)
self._serial = os.open(
self.port, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
self.startReading()
logger.info("Reconnecting")
......@@ -145,9 +145,15 @@ def mount_smb(url):
data = urlsplit(url)
share = data.path.lstrip('/')
subprocess.call(('net', 'use', 'Z:', '/delete'))
subprocess.check_output((
'net', 'use', 'Z:', r'\\%s\%s' % (data.hostname, share),
data.password, '/user:%s' % data.username, '/PERSISTENT:YES'))
try:
p = subprocess.Popen((
'net', 'use', 'Z:', r'\\%s\%s' % (data.hostname, share),
data.password, '/user:%s' % data.username, '/PERSISTENT:YES'),
stderr=subprocess.PIPE, stdout=subprocess.PIPE,
stdin=subprocess.PIPE)
logger.info('mount_smb(): %s', p.communicate())
except:
logger.exception('Unhandled exception: ')
def file_already_exists(name, mode=0o644):
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import platform
system = platform.system()
working_directory = r"C:\circle"
from os import environ
from os.path import join
import logging
......@@ -25,10 +21,7 @@ from .network import change_ip_windows
from context import BaseContext
logging.basicConfig()
logger = logging.getLogger()
level = environ.get('LOGLEVEL', 'INFO')
logger.setLevel(level)
class Context(BaseContext):
......
from netaddr import IPNetwork, IPAddress
import logging
from subprocess import check_output, CalledProcessError
from subprocess import PIPE, Popen
logger = logging.getLogger()
......@@ -17,6 +17,23 @@ ifcfg_template = '/etc/sysconfig/network-scripts/ifcfg-%s'
# '8.8.8.8')
class IPAddress2(IPNetwork):
def key(self):
return self._module.version, self._value, self._prefixlen
def check_output2(cmd, shell=False):
try:
p = Popen(cmd, shell=shell,
stderr=PIPE, stdout=PIPE, stdin=PIPE)
stdout, stderr = p.communicate()
logger.info('%s: %s, %s', cmd, stdout, stderr)
return stdout
except:
logger.exception(
'Unhandled exception in %s: ', cmd)
def get_interfaces_windows(interfaces):
import wmi
nics = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)
......@@ -29,60 +46,63 @@ def get_interfaces_windows(interfaces):
def change_ip_windows(interfaces, dns):
for nic, conf in get_interfaces_windows(interfaces):
link_local = IPNetwork('fe80::/16')
new_addrs = [IPNetwork(ip) for ip in conf['addresses']]
new_addrs_str = set(str(ip) for ip in new_addrs)
old_addrs = [IPNetwork('%s/%s' % (ip, nic.IPSubnet[i]))
for i, ip in enumerate(nic.IPAddress)
if IPAddress(ip) not in link_local]
old_addrs_str = set(str(ip) for ip in old_addrs)
changed = (
new_addrs_str != old_addrs_str or
set(nic.DefaultIPGateway) != set([conf['gw4'], conf['gw6']]))
if changed or 1:
new_addrs = set([IPAddress2(ip) for ip in conf['addresses']])
old_addrs = set([IPAddress2('%s/%s' % (ip, nic.IPSubnet[i]))
for i, ip in enumerate(nic.IPAddress)
if IPAddress(ip) not in link_local])
addrs_add = new_addrs - old_addrs
addrs_del = old_addrs - new_addrs
changed = (addrs_add or addrs_del or
set(nic.DefaultIPGateway) != set(
[conf.get('gw4'), conf.get('gw6')]))
if changed:
logger.info('new config for <%s(%s)>: %s', nic.Description,
nic.MACAddress, ', '.join(new_addrs_str))
# IPv4
ipv4_addrs = [str(ip.ip) for ip in new_addrs
if ip.version == 4]
ipv4_masks = [str(ip.netmask) for ip in new_addrs
if ip.version == 4]
logger.debug('<%s>.EnableStatic(%s, %s) called', nic.Description,
ipv4_addrs, ipv4_masks)
retval = nic.EnableStatic(
IPAddress=ipv4_addrs, SubnetMask=ipv4_masks)
assert retval == (0, )
nic.SetGateways(DefaultIPGateway=[conf['gw4']])
assert retval == (0, )
# IPv6
for ip in new_addrs:
if ip.version == 6 and str(ip) not in old_addrs_str:
logger.debug('add %s (%s)', ip, nic.Description)
check_output(
nic.MACAddress, ', '.join(conf['addresses']))
for ip in addrs_add:
logger.info('add %s (%s)', ip, nic.Description)
if ip.version == 6:
cmd = (
'netsh interface ipv6 add address '
'interface=%s address=%s'
% (nic.InterfaceIndex, ip), shell=True)
% (nic.InterfaceIndex, ip))
else:
cmd = (
'netsh interface ipv4 add address '
'%s %s %s'
% (nic.InterfaceIndex, ip.ip, ip.netmask))
for ip in old_addrs:
if ip.version == 6 and str(ip) not in new_addrs_str:
logger.debug('delete %s (%s)', ip, nic.Description)
check_output(
'netsh interface ipv6 delete address '
'interface=%s address=%s'
% (nic.InterfaceIndex, ip.ip), shell=True)
try:
check_output('netsh interface ipv6 del route ::/0 interface=%s'
% nic.InterfaceIndex, shell=True)
except CalledProcessError:
pass
check_output('netsh interface ipv6 add route ::/0 interface=%s %s'
% (nic.InterfaceIndex, conf['gw6']), shell=True)
try:
check_output('netsh interface ipv4 add dnsserver %s '
'address=%s index=1'
% (nic.InterfaceIndex, dns), shell=True)
except CalledProcessError:
pass
check_output2(cmd, shell=True)
for ip in addrs_del:
proto = 'ipv6' if ip.version == 6 else 'ipv4'
logger.info('del %s (%s)', ip, nic.Description)
check_output2(
'netsh interface %s delete address '
'%s %s'
% (proto, nic.InterfaceIndex, ip.ip), shell=True)
# default gw4
if conf.get('gw4'):
check_output2(
'netsh interface ip del route 0.0.0.0/0 interface=%s'
% nic.InterfaceIndex, shell=True)
check_output2(
'netsh interface ip add route 0.0.0.0/0 interface=%s %s'
% (nic.InterfaceIndex, conf.get('gw4')), shell=True)
# default gw6
if conf.get('gw6'):
check_output2(
'netsh interface ipv6 del route ::/0 interface=%s'
% nic.InterfaceIndex, shell=True)
check_output2(
'netsh interface ipv6 add route ::/0 interface=%s %s'
% (nic.InterfaceIndex, conf.get('gw6')), shell=True)
# DNS
check_output2('netsh interface ipv4 add dnsserver %s '
'address=%s index=1'
% (nic.InterfaceIndex, dns), shell=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment