Example #1
0
def volume_size(name_id):

    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(parser.config_params('ssh')['server'],
                    port=22,
                    username=parser.config_params('ssh')['user'],
                    password=parser.config_params('ssh')['password'])
    except paramiko.AuthenticationException:
        print 'issues with the connection'

    #get total.
    #my_command_total = "docker inspect -f '{{ range .Mounts }}{{ .Source }}{{ end }}' %s | xargs sudo du -shm| sed 's/\s.*$//'"%name_id
    my_command_total = "docker inspect -f '{{ range .Mounts }}{{ .Source }}{{ end }}' %s | xargs sudo df -h | sed -n 2p | awk '{print $2}'" % name_id
    # get free
    my_command_free = "docker inspect -f '{{ range .Mounts }}{{ .Source }}{{ end }}' %s | xargs sudo df -h | sed -n 2p | awk '{print $4}'" % name_id
    #get used
    my_command_used = "docker inspect -f '{{ range .Mounts }}{{ .Source }}{{ end }}' %s | xargs sudo df -h | sed -n 2p | awk '{print $3}'" % name_id
    #
    stdin, stdout, stderr = ssh.exec_command(my_command_total)
    storage_total = stdout.read().rstrip()
    stdin, stdout, stderr = ssh.exec_command(my_command_free)
    storage_free = stdout.read().rstrip()
    stdin, stdout, stderr = ssh.exec_command(my_command_used)
    storage_used = stdout.read().rstrip()
    json_create = json.dumps({"stats": dict(total='%s', free='%s', used='%s')
                              }) % (storage_total, storage_free, storage_used)
    print json_create
    return json_create
    def __init__(self):
        # host value

        self.host = parser.config_params('kubeauth')['host']
        # check if certificate exists
        try:
            # certificate - authority - data
            os.path.isfile(parser.config_params('kubeauth')['ssl_ca_cert_file'])
            self.ssl_ca_cert_file = parser.config_params('kubeauth')['ssl_ca_cert_file']
        except IOError as e:
            logger_settings.logger.error(' certificate-authority-data file missing:{0}'.format(e))

        try:
            # client - certificate - data
            os.path.isfile(parser.config_params('kubeauth')['ssl_cert_file'])
            self.ssl_cert_file = parser.config_params('kubeauth')['ssl_cert_file']
        except IOError as e:
            logger_settings.logger.error('client-certificate-data file missing:{0}'.format(e))

        try:
            # client - key - data
            os.path.isfile(parser.config_params('kubeauth')['ssl_key_file'])
            self.ssl_key_file = parser.config_params('kubeauth')['ssl_key_file']
        except IOError as e:
            logger_settings.logger.error('client-key-data file missing:{0}'.format(e))


        # proxy settings
        self.proxy = parser.config_params('kubeauth')['proxy']
        # context
        self.context = parser.config_params('kubeauth')['context']
 def __init__(self, min_interval, max_interval):
     """
     :param min_interval:
     :param max_interval:
     :return:
     """
     self.min_interval = min_interval
     self.max_interval = max_interval
     self.salt = 2
     self.my_excluded = parser.config_params('rand_exclusion')['exclude_ports'].split()
     self.range = 6
Example #4
0
def make_the_connection():
    """
    this function is making the connection and map app name with schema
    :return:
    """
    try:
        app.config['SQLALCHEMY_DATABASE_URI'] = parser.config_params('dbname')['location_name_db']
    except ImportError:
        print 'no db connection availabe'
        app.logger.info('no db connection available'
                        )
Example #5
0
 def test_random_port_generator_not_restricred(self):
     """
     Test if random generated port is not in restricted list
     :return:
     """
     print 'test 2 - test if random generated port is not in the excluded list'
     my_excluded_list = parser.config_params(
         'rand_exclusion')['exclude_ports'].split()
     my_port = random_generator_function.generator_instance.random_port()
     for i in my_excluded_list:
         self.assertNotEqual(my_port, i,
                             'This port should not be generated')
Example #6
0
def connect_docker_server():
    """
    this function will connect to docker server endpoint
    :rtype : object
    :return:
    """
    server_address = parser.config_params('server')['server_address']
    try:
        cli = docker.APIClient(base_url=server_address)
        cli.info()
        return cli
    except:
        print 'no connection to the server :('
Example #7
0
def get_md5():
    """
    :rtype : object
    """
    openvpn_config = parser.config_params('openvpnconfig')['file']
    md5 = hashlib.md5()
    try:
        f = open(openvpn_config)
        for line in f:
            md5.update(line)
        f.close()
        return md5.hexdigest()
    except IOError:
        logger_settings.logger.info('There is no config file...will download one for you')
        return md5.hexdigest()
Example #8
0
def get_md5():
    """
    :rtype : object
    """
    openvpn_config = parser.config_params('openvpnconfig')['file']
    md5 = hashlib.md5()
    try:
        f = open(openvpn_config)
        for line in f:
            md5.update(line)
        f.close()
        return md5.hexdigest()
    except IOError:
        logger_settings.logger.info(
            'There is no config file...will download one for you')
        return md5.hexdigest()
Example #9
0
 def __init__(self, parser):
     """
     :type self: object
     :type parser: object
     :rtype : object
     """
     self.parser = parser
     self.http_md5 = parser.config_params('http')['http_md5']
     self.openvpn_config_file = parser.config_params('http')['config_file']
     self.user = parser.config_params('authentication')['auth_user']
     self.password = parser.config_params('authentication')['auth_passwd']
     self.openvpn_config_save_file = parser.config_params('openvpnconfig')['file']
     self.file_name_saved_local = 'client_danbordeanu_saved'
     self.ssl_crt = parser.config_params('certs')['ssl_crt']
     self.ssl_key = parser.config_params('certs')['ssl_key']
Example #10
0
 def __init__(self, parser):
     """
     :type self: object
     :type parser: object
     :rtype : object
     """
     self.parser = parser
     self.http_md5 = parser.config_params('http')['http_md5']
     self.openvpn_config_file = parser.config_params('http')['config_file']
     self.user = parser.config_params('authentication')['auth_user']
     self.password = parser.config_params('authentication')['auth_passwd']
     self.openvpn_config_save_file = parser.config_params(
         'openvpnconfig')['file']
     self.file_name_saved_local = 'client_danbordeanu_saved'
     self.ssl_crt = parser.config_params('certs')['ssl_crt']
     self.ssl_key = parser.config_params('certs')['ssl_key']
Example #11
0
# proto = 'tcp'
#
# config_ports = parser.config_params('images')['test_multiple_port'].split()
#
# append_to = str(random.randrange(1025, 65000, 2))
#
# my_new_list = [x + '/' + proto + ':'+ str(random.randrange(1025, 65000, 2)) for x in config_ports]
#
# print my_new_list
#
#
# my_dict_port_list = dict(map(str, x.split(':')) for x in my_new_list)
#
# print my_dict_port_list

config_ports = parser.config_params('images')['test_internal_port'].split()

print config_ports

my_new_list = [x + ':'+ str(random.randrange(1025, 65000, 2)) for x in config_ports]

print my_new_list

my_dict_port_list = dict(map(str, x.split(':')) for x in my_new_list)

print my_dict_port_list

container_id = cli.create_container(
    image='eg_ngphp', hostname='wtfplm', ports=config_ports,
    host_config=cli.create_host_config(port_bindings=my_dict_port_list),
    name='wtf')
Example #12
0
 def decorated_function(*args, **kwargs):
     if request.headers.get('secretkey') and request.headers.get('secretkey') \
             == parser.config_params('proxy')['secretkey']:
         return view_function(*args, **kwargs)
     else:
         abort(401)
Example #13
0
def makevm(name_id):
    """
    curl -i -H "secretkey:1234" -H "Content-Type: application/json" -X POST -d '{"username":"******","password":
    "******", "options": {"diskspace":"500m","service":"ssh"}}' http://localhost:5000/api/seedboxes/new/sshdan
    this will create a container
    we parse the json data and take username/password and we insert this into db
    :param name_id:
    :return:
    """
    content = request.json
    my_request = json.dumps(content)

    app.logger.info('Username {0}, passwd:{1}, diskspace:{2}, service:{3}'.format(content['username'],
                                                                                  content['password'],
                                                                                  content['options']['diskspace'],
                                                                                  content['options']['service']))


    #check if the hostname is valid, should not contain strange stuff
    if not validatehostname.isvalidhostname(name_id):
        raise InvalidUsage('invalid hostname', status_code=404)

    #check if we have plex related stuff
    if my_request.find('plex') != -1:
        plex_secret_token = content['plex']['plex_secret_token']
        plex_server_name = content['plex']['plex_server_name']
    else:
        plex_secret_token = ''
        plex_server_name = ''

    # TODO convert this to dictionary or invent something smarter than if else

    #create ssh container
    if content['options']['service'] == 'ssh':
        image_name = parser.config_params('images')['ssh_image_name']
        exec_this = ''
        cap_value = 'NET_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['ssh_internal_port'].split()


    #create web container
    if content['options']['service'] == 'web':
        image_name = parser.config_params('images')['web_image_name']
        exec_this = 'python app.py'
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['web_internal_port'].split()

    #create rtorrent container
    if content['options']['service'] == 'rtorrent':
        image_name = parser.config_params('images')['rtorrent_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['rtorrent_internal_port'].split()

    #create rutorrent container
    if content['options']['service'] == 'rutorrent':
        image_name = parser.config_params('images')['rutorrent_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['rutorrent_internal_port'].split()

    #create transmission container
    if content['options']['service'] == 'transmission':
        image_name = parser.config_params('images')['transmission_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['transmission_internal_port'].split()

    #create deluge container
    if content['options']['service'] == 'deluge':
        image_name = parser.config_params('images')['deluge_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['deluge_internal_port'].split()

    #create openvpn over udp container
    if content['options']['service'] == 'openvpnudp':
        image_name = parser.config_params('images')['openvpnudp_image_name']
        exec_this = ''
        cap_value = 'NET_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['openvpnudp_internal_port'].split()

    #create owncloud container
    if content['options']['service'] == 'owncloud':
        image_name = parser.config_params('images')['owncloud_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['owncloud_internal_port'].split()

    #create nginxphp container
    if content['options']['service'] == 'nginxphp':
        image_name = parser.config_params('images')['ngphp_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['ngphp_internal_port'].split()

    #create mariadb container
    if content['options']['service'] == 'mariadb':
        image_name = parser.config_params('images')['mariadb_image_name']
        exec_this = ''
        cap_value = 'SYS_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['mariadb_internal_port'].split()

    #create plex container
    if content['options']['service'] == 'plex':
        image_name = parser.config_params('images')['plex_image_name']
        exec_this = ''
        cap_value = 'NET_ADMIN'
        privileged = True
        internal_port = parser.config_params('images')['plex_internal_port'].split()

    #create sftp container
    if content['options']['service'] == 'sftp':
        image_name = parser.config_params('images')['sftp_image_name']
        exec_this = ''
        cap_value = 'NET_ADMIN'
        privileged = False
        internal_port = parser.config_params('images')['sftp_internal_port'].split()

    new_container = docker_create(name_id, content['username'], content['password'], content['options']['service'],
                      content['options']['diskspace'], image_name, internal_port, exec_this, cap_value, privileged,
                                  plex_secret_token, plex_server_name)
    return jsonify(new_container)
Example #14
0
def docker_create(name_id, username, password, service, diskspace, image_name, internal_port, exec_this, cap_add_value,
                  privileged, plex_secret_token, plex_server_name):
    """
    this function will create the container
    :rtype : object
    :return:
    """
    #check if there is a container with the same name, anyway useless because docker is doing same thing
    #we can remove this in the future
    if db.session.query(exists().where(my_sql_stuff.ContainerNames.name_of_container == name_id)).scalar():
        raise InvalidUsage('there is a vm already with this name', status_code=404)


    #to the magic, generate unique port, make the shared volume
    my_new_volume = give_me_mount_point(username, diskspace)

    #making the list of ports from config
    #appending random values
    #making a dict of port from config and random ports

    my_new_list = [x + ':' + str(give_me_something_unique(name_id, name_id, username, password, service)) for x in internal_port]
    my_dict_port_list = dict(map(str, x.split(':')) for x in my_new_list)

    try:
        app.logger.info('Generating and inserting in db a new allocated port {0}'.format(my_new_list))
        where_to_mount = my_new_volume + parser.config_params('mount')['where_to_mount_dir']
        app.logger.info('We will mount in this location {0}'.format(where_to_mount))

        #here we make the list of ports from confing into a string
        #and remove / tcp udp
        #make removed string into a list and use it in ports
        internal_port_udp_tcp_removed = ''.join(c for c in ' '.join(internal_port) if c not in '/;udp;tcp').split()

        #creating the container
        response = make_connection.connect_docker_server().create_container(image=image_name, hostname=name_id,
                                                                            ports=internal_port_udp_tcp_removed,
                                                                            environment={'ACCESS_TOKEN': plex_secret_token,
                                                                                         'SERVER_NAME': plex_server_name,
                                                                                         'MANUAL_PORT': my_dict_port_list.values()[0]},
                                                                            host_config=make_connection.connect_docker_server().create_host_config(
                                                                                cap_add=[cap_add_value],
                                                                                binds=[where_to_mount],
                                                                                port_bindings=my_dict_port_list,
                                                                                privileged=privileged, cpuset_cpus='0', cpu_period=100000,
                                                                                mem_limit=parser.config_params('container_settings')['memory']),
                                                                            command=exec_this, name=name_id)
        #starting the container
        make_connection.connect_docker_server().start(container=response.get('Id'))

        result_new_container = make_connection.connect.inspect_container(response.get('Id'))
        new_hostname = result_new_container['Config']['Hostname']
        new_name = result_new_container['Name']
        new_exposed_ports = result_new_container['Config']['ExposedPorts']
        app.logger.info('New container with hostname {0} and name {1} exposed ports {2} created'.format(new_hostname,
                                                                                                        new_name,
                                                                                                        new_exposed_ports))
        app.logger.info('New container created with id {0}'.format(name_id))
        return result_new_container
    except:
            #this will delete the table raw where added port and name in ContainerNames
            my_sql_stuff.ContainerNames.query.filter_by(name_of_container=name_id).delete()
            db.session.commit()
            app.logger.error('An error occurred creating the container')
            raise InvalidUsage('can\'t make this container', status_code=404)