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