From ee5314fec9aeccdf7334db9e314fced33ac89baf Mon Sep 17 00:00:00 2001 From: vanzhiganov Date: Wed, 25 May 2016 00:44:52 +0300 Subject: [PATCH] new release 3.1.0 --- extra/__init__.py | 0 extra/dnsmasq.conf | 1 + extra/dnsmasq.origin.conf | 2 + extra/dnsmasq.temp.conf | 1 + extra/ec2-firstboot.sh | 20 +++++ extra/ec2-firstlogin.sh | 49 +++++++++++ extra/old/__init__.py | 0 extra/old/client.py | 10 +++ extra/old/dnsmasq.py | 65 +++++++++++++++ extra/old/dnsmasq/__init__.py | 146 ++++++++++++++++++++++++++++++++ extra/old/logger.py | 43 ++++++++++ extra/old/nginx/__init__.py | 151 ++++++++++++++++++++++++++++++++++ setup.py | 2 +- 13 files changed, 489 insertions(+), 1 deletion(-) create mode 100644 extra/__init__.py create mode 100644 extra/dnsmasq.conf create mode 100644 extra/dnsmasq.origin.conf create mode 100644 extra/dnsmasq.temp.conf create mode 100755 extra/ec2-firstboot.sh create mode 100755 extra/ec2-firstlogin.sh create mode 100644 extra/old/__init__.py create mode 100644 extra/old/client.py create mode 100644 extra/old/dnsmasq.py create mode 100644 extra/old/dnsmasq/__init__.py create mode 100644 extra/old/logger.py create mode 100644 extra/old/nginx/__init__.py diff --git a/extra/__init__.py b/extra/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extra/dnsmasq.conf b/extra/dnsmasq.conf new file mode 100644 index 0000000..dd056e4 --- /dev/null +++ b/extra/dnsmasq.conf @@ -0,0 +1 @@ +dhcp-host=c6,10.0.3.101 \ No newline at end of file diff --git a/extra/dnsmasq.origin.conf b/extra/dnsmasq.origin.conf new file mode 100644 index 0000000..edf8d3e --- /dev/null +++ b/extra/dnsmasq.origin.conf @@ -0,0 +1,2 @@ +dhcp-host=c5,10.0.3.100 +dhcp-host=c6,10.0.3.101 \ No newline at end of file diff --git a/extra/dnsmasq.temp.conf b/extra/dnsmasq.temp.conf new file mode 100644 index 0000000..dd056e4 --- /dev/null +++ b/extra/dnsmasq.temp.conf @@ -0,0 +1 @@ +dhcp-host=c6,10.0.3.101 \ No newline at end of file diff --git a/extra/ec2-firstboot.sh b/extra/ec2-firstboot.sh new file mode 100755 index 0000000..2d19f6b --- /dev/null +++ b/extra/ec2-firstboot.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# +# Regenerate the ssh host key +# + +rm -f /etc/ssh/ssh_host_*_key* + +ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N '' | logger -s -t "ec2" +ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N '' | logger -s -t "ec2" + +# This allows user to get host keys securely through console log +echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" | logger -s -t "ec2" +ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key.pub | logger -s -t "ec2" +ssh-keygen -l -f /etc/ssh/ssh_host_dsa_key.pub | logger -s -t "ec2" +echo "-----END SSH HOST KEY FINGERPRINTS-----" | logger -s -t "ec2" + +depmod -a + +exit 0 diff --git a/extra/ec2-firstlogin.sh b/extra/ec2-firstlogin.sh new file mode 100755 index 0000000..8745516 --- /dev/null +++ b/extra/ec2-firstlogin.sh @@ -0,0 +1,49 @@ +#!/bin/bash + +trap cleanup 1 2 3 6 + +cleanup() { + exit 1 +} + +ACCOUNT="ubuntu" + +echo +echo "======================================================" +echo "CONFIGURATION OF YOUR UBUNTU EC2 IMAGE" +echo "======================================================" +echo "Your EC2 image is about to be finished to be set up." +echo +echo "------------------------------------------------------" + +PASSWORD=$(uuidgen -r | head -c8) + +echo "New password for ${ACCOUNT} account: ${PASSWORD}" +echo "ubuntu:${PASSWORD}" | chpasswd -m +passwd -u ${ACCOUNT} + +echo "Setting up ssh public keys for the ${ACCOUNT} account." +[ ! -e /home/${ACCOUNT}/.ssh ] && mkdir -p /home/${ACCOUNT}/.ssh +cp -a /root/.ssh/authorized_keys* /home/${ACCOUNT}/.ssh +chown -R ${ACCOUNT}:${ACCOUNT} /home/${ACCOUNT}/.ssh + +echo +echo "------------------------------------------------------" +echo "Please select software that you wish to install:" + +tasksel --section server + +echo +echo "------------------------------------------------------" +echo +echo "We are now going to log you out of the root account." +echo "To perform administrative tasks please use the ${ACCOUNT} account" +echo "in combination with sudo using the password: ${PASSWORD}" +echo +echo "======================================================" +echo + +touch /root/firstlogin_done + +kill -HUP $PPID + diff --git a/extra/old/__init__.py b/extra/old/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/extra/old/client.py b/extra/old/client.py new file mode 100644 index 0000000..d663755 --- /dev/null +++ b/extra/old/client.py @@ -0,0 +1,10 @@ +import logging +# import lxc +import node +import config + +# config logging +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + +# print SWS_CLIENT_AUTH['DC_ID'] +node.Task().init() diff --git a/extra/old/dnsmasq.py b/extra/old/dnsmasq.py new file mode 100644 index 0000000..ab924cc --- /dev/null +++ b/extra/old/dnsmasq.py @@ -0,0 +1,65 @@ +import shutil + +# dhcp-host=c5,10.0.3.100 +# dhcp-host=c6,10.0.3.101 + +find = { + "ip": "10.0.3.100", + "host": "c5" +} + + +to_write = [] + +# if find['ip'] in open("dnsmasq.conf").read(): +# print "true" + +# for i in open("dnsmasq.conf", "r+"): +# for i in open("dnsmasq.conf", "r"): +# i = i.rstrip() +# # string = i.split(",") +# # +# # host = string[0].split("=") +# # +# # if host[1] == find['host'] or string[1] == find['ip']: +# # print "fail" +# # # print host[1], string[1] +# if find['ip'] in i: +# print " x %s" % i +# else: +# print " v %s" % i +# to_write.append(i) + + +def dnsmasq_add(host, ip): + print "read config" + + origin = [] + clean = [] + new = [] + + for i in open("dnsmasq.conf", "r"): + origin.append(i.rstrip()) + + print "ip duplicates" + for i in origin: + if find['ip'] in i: + print "delete ip: %s" % find['ip'] + else: + clean.append(i) + + print "host duplicates" + for i in clean: + if find['host'] in i: + print "delete host: %s" % find['host'] + else: + new.append(i) + + + f = open("dnsmasq.temp.conf", 'w') + f.writelines(new) + f.close() + + shutil.copyfile("dnsmasq.temp.conf", "dnsmasq.conf") + + print new diff --git a/extra/old/dnsmasq/__init__.py b/extra/old/dnsmasq/__init__.py new file mode 100644 index 0000000..a1b9879 --- /dev/null +++ b/extra/old/dnsmasq/__init__.py @@ -0,0 +1,146 @@ +__author__ = 'vanzhiganov' + +""" +import dnsmasq +dnsmasq.Dnsmasq().exists_name('test') +dnsmasq.Dnsmasq().add('test', '10.10.10.10') +dnsmasq.Dnsmasq().exists_name('test') + +""" + + +import shutil +import subprocess +import os.path + + +class Error(Exception): + pass + + +class NameAlreadyExists(Exception): + pass + + +class NameNotExists(Exception): + pass + + +class IPAlreadyExists(Exception): + pass + + +class IPNotExists(Exception): + pass + + +class Dnsmasq: + def __init__(self, dnsmasq_conf='/etc/lxc/dnsmasq.conf'): + """ + + :param dnsmasq_conf: + :return: + """ + if not self.__exists_conf(dnsmasq_conf): + raise Error("Config not exists") + + self.dnsmasq_conf = dnsmasq_conf + self.list = self.__read_conf() + + def exists_name(self, name): + """ + + :param name: + :return: + """ + # origin = self.__read_conf() + for i in self.list: + if name in i: + return True + return False + + # @staticmethod + def exists_ip(self, ip): + """ + + :param ip: + :return: + """ + # origin = self.__read_conf() + for i in self.list: + if ip in i: + return True + return False + + def add(self, name, ip): + """ + + :param name: + :param ip: + :return: + """ + if not self.__exists_conf(self.dnsmasq_conf): + return False + + if self.exists_name(name): + raise NameAlreadyExists("Already exists: %s" % name) + if self.exists_ip(ip): + raise IPAlreadyExists("Already exists: %s" % ip) + self.list.append("dhcp-host=%s,%s" % (name, ip)) + result_write = self.__write_conf() + if not result_write: + return False + return True + + def delete(self, name): + if not self.__exists_conf(self.dnsmasq_conf): + return False + + clean = [] + for i in self.__read_conf(): + if name not in i: + clean.append(i) + result_write = self.__write_conf() + if not result_write: + return False + return True + + def __read_conf(self): + """ + + :return: + """ + origin = [] + for i in open(self.dnsmasq_conf, "r"): + origin.append(i.rstrip()) + return origin + + def __write_conf(self): + """ + + :return: + """ + if not self.__exists_conf(self.dnsmasq_conf): + return False + + f = open("/tmp/dnsmasq.conf", 'w') + # f.writelines(self.list) + for i in self.list: + f.write("%s\n" % i) + f.close() + shutil.copyfile("/tmp/dnsmasq.conf", "/etc/lxc/dnsmasq.conf") + return True + + def __exists_conf(self, conf): + if os.path.isfile(conf): + return True + return False + + +class Service: + def restart(self): + subprocess.call("service dnsmasq restart", shell=True) + return True + + def pid(self): + return None diff --git a/extra/old/logger.py b/extra/old/logger.py new file mode 100644 index 0000000..15df88a --- /dev/null +++ b/extra/old/logger.py @@ -0,0 +1,43 @@ +__author__ = 'vanzhiganov' + +""" +This script collect information about each hosted containers and host-system +and send all data to root server +""" + +# from optparse import OptionParser + +# parser = OptionParser() +# parser.add_option("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE") +# parser.add_option("observer", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout") +# +# (options, args) = parser.parse_args() + +import logging +import os +import sys + +import node +from SWSCloudNode.lxc import lxc + +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + +if os.geteuid() != 0: + print "This application must be running under user `root`" + sys.exit() + +# connect to database +# for use this need to add timestamp into container data +# r = redis.StrictRedis(host=config.redis_conf['host'], port=config.redis_conf['port'], db=config.redis_conf['db']) +# r.set('foo', 'bar') +# r.delete('foo') +# print r.get('foo') + +# localtime = time.localtime(time.time()) +# print "Local current time :", localtime[''] +# + +# get current status each containers +for container in lxc().list(): + data = lxc().info(container) + result = node.Report().container_info(data) diff --git a/extra/old/nginx/__init__.py b/extra/old/nginx/__init__.py new file mode 100644 index 0000000..3143426 --- /dev/null +++ b/extra/old/nginx/__init__.py @@ -0,0 +1,151 @@ +import os +from pyparsing import ( + Literal, White, Word, alphanums, CharsNotIn, Forward, Group, + Optional, OneOrMore, ZeroOrMore, pythonStyleComment) + + +class Service(): + def reload(self): + os.system('service nginx reload') + + +class Nginx(): + def vhost_add(self, vhost_id, vhost, ip): + nginxfile = open('%s/%s_%s.conf' % ("/etc/nginx/sites-enabled", ip, vhost_id), 'w+') + config = """ +server { + listen 80; + server_name %s; + + location / { + proxy_pass http://%s:80; + proxy_redirect off; + proxy_set_header Host $host; + proxy_set_header X-Url-Scheme $scheme; + proxy_set_header X-Host $http_host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Proxy-host $proxy_host; + } +} + """ % (vhost, ip) + + nginxfile.write(config) + return False + + def vhost_delete(self, ip, vhost_id): + os.remove("/etc/nginx/sites-enabled/%s_%s.conf" % (ip, vhost_id)) + return True + + def add_alias(self): + return False + + def delete_alias(self): + return False + + def delete_site(self, containser_id): + return False + + +class NginxParser(object): + """ + A class that parses nginx configuration with pyparsing + """ + + # constants + left_bracket = Literal("{").suppress() + right_bracket = Literal("}").suppress() + semicolon = Literal(";").suppress() + space = White().suppress() + key = Word(alphanums + "_/") + value = CharsNotIn("{};,") + location = CharsNotIn("{};, ") + + # rules + assignment = (key + Optional(space + value) + semicolon) + block = Forward() + + block << Group( + Group(key + Optional(space + location)) + + left_bracket + + Group(ZeroOrMore(Group(assignment) | block)) + + right_bracket) + + script = OneOrMore(Group(assignment) | block).ignore(pythonStyleComment) + + def __init__(self, source): + self.source = source + + def parse(self): + """ + Returns the parsed tree. + """ + return self.script.parseString(self.source) + + def as_list(self): + """ + Returns the list of tree. + """ + return self.parse().asList() + + +class NginxDumper(object): + """ + A class that dumps nginx configuration from the provided tree. + """ + def __init__(self, blocks, indentation=4): + self.blocks = blocks + self.indentation = indentation + + def __iter__(self, blocks=None, current_indent=0, spacer=' '): + """ + Iterates the dumped nginx content. + """ + blocks = blocks or self.blocks + for key, values in blocks: + if current_indent: + yield spacer + indentation = spacer * current_indent + if isinstance(key, list): + yield indentation + spacer.join(key) + ' {' + for parameter in values: + if isinstance(parameter[0], list): + dumped = self.__iter__([parameter], + current_indent + self.indentation) + for line in dumped: + yield line + else: + dumped = spacer.join(parameter) + ';' + yield spacer * (current_indent + self.indentation) + dumped + + yield indentation + '}' + else: + yield spacer * current_indent + key +spacer + values + ';' + + def as_string(self): + return '\n'.join(self) + + def to_file(self, out): + for line in self: + out.write(line) + out.close() + return out + + +# Shortcut functions to respect Python's serialization interface +# (like pyyaml, picker or json) + +def loads(source): + return NginxParser(source).as_list() + + +def load(_file): + return loads(_file.read()) + + +def dumps(blocks, indentation=4): + return NginxDumper(blocks, indentation).as_string() + + +def dump(blocks, _file, indentation=4): + return NginxDumper(blocks, indentation).to_file(_file) diff --git a/setup.py b/setup.py index 9879ec3..169569a 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup setup( name='SWSCloudNode', - version='3.0.1-beta2', + version='3.1.0', author='Vyacheslav Anzhiganov', author_email='vanzhiganov@ya.ru', packages=[