Пример #1
0
def get(namespace_arg, application_arg):
    session = Session()
    namespace = session.query(Namespace).filter(
        Namespace.name == namespace_arg).one()
    application = namespace.projects.filter(
        Project.name == application_arg).one()
    key = application.key
    return (key.public_key)
Пример #2
0
def delete(namespace_arg, name_arg, ):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()

        key = namespace.keys.filter(and_(Key.name==name_arg, Key.active==True)).first()

	if not key:
                raise Exception('Key not found.')

	key.active=False
	session.commit()

	update(namespace)
Пример #3
0
def run(namespace, project):
    datadir = os.path.join(os.path.dirname(os.path.realpath(upscale.__file__)), "data")
    print datadir

    session = Session()
    namespace = session.query(Namespace).filter(Namespace.name == namespace).first()
    if not namespace:
        raise Exception("Unknown namespace")

    project = namespace.projects.filter(Project.name == project).first()
    if not project:
        raise Exception("Unknown project")

    from datetime import date

    bi = yaml.load(file(os.path.join(datadir, "runtime/{0}.yaml").format(project.template), "r"))

    container = "{0}_{1}_{2}".format(namespace.name, project.name, datetime.now().strftime("%y%m%d%H%M%S"))
    rootfs = "/var/lib/lxc/{0}/rootfs".format(container)
    mirror = "http://archive.ubuntu.com/ubuntu"
    security_mirror = "http://security.ubuntu.com/ubuntu"
    release = "raring"
    base = project.template + "_" + str(bi["version"])

    print ("Using runtime {0}".format(base))

    print (lxc.create(container))

    datadir = os.path.join(rootfs, "data")
    if not os.path.exists(datadir):
        os.mkdir(datadir)

    cmd("mount --bind /data/{0}/{1} {2}/data".format(namespace.name, project.name, rootfs))

    print lxc.start(container)
    print lxc.wait(container)

    import time

    hostname = None

    while not hostname:
        try:
            import socket

            (hostname, aliaslist, ipaddrlist) = socket.gethostbyname_ex(container)
        except socket.gaierror, exc:
            print "Container not reachable yet... Waiting 1 second..."
            time.sleep(1)
Пример #4
0
def get_applications():
    session = Session()

    hosts = []

    for host in get_hosts():
        hosts.append({
            'id': host.id + '_' + host.instance_type,
            'ipaddr': host.private_ip_address
        })

    applications = {}
    namespaces = session.query(Namespace).all()
    for namespace in namespaces:
        for project in namespace.projects:
            application = {}
            application['id'] = ''.join(
                filter(lambda c: c.islower() or c.isdigit(
                ) or c == '-', namespace.name)) + '_' + ''.join(
                    filter(lambda c: c.islower() or c.isdigit() or c == '-',
                           project.name))
            application['domains'] = [
                {
                    'name':
                    config['domain'].format(project.name, namespace.name),
                    'id':
                    ''.join(
                        filter(
                            lambda c: c.islower() or c.isdigit() or c == '-',
                            config['domain'].format(project.name,
                                                    namespace.name)))
                },
            ]
            for domain in project.domains:
                application['domains'].append({
                    'name':
                    domain.name,
                    'id':
                    ''.join(
                        filter(
                            lambda c: c.islower() or c.isdigit() or c == '-',
                            domain.name))
                })
            application['containers'] = []
            application['hosts'] = hosts
            applications[application['id']] = application
    return (applications)
Пример #5
0
def run (namespace, project):
	datadir = os.path.join(os.path.dirname(os.path.realpath(upscale.__file__)), 'data')
	print datadir

	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace).first()
	if (not namespace):
		raise Exception("Unknown namespace")

	project = namespace.projects.filter(Project.name==project).first()
	if (not project):
		raise Exception("Unknown project")

	from datetime import date
	bi = yaml.load(file(os.path.join(datadir, 'runtime/{0}.yaml').format(project.template), 'r'))	
	
	container="{0}_{1}_{2}".format(namespace.name, project.name, datetime.now().strftime("%y%m%d%H%M%S"))
	rootfs="/var/lib/lxc/{0}/rootfs".format(container)
	mirror="http://archive.ubuntu.com/ubuntu"
	security_mirror="http://security.ubuntu.com/ubuntu"
	release="raring"
	base=project.template + '_' + str(bi['version'])

	print ("Using runtime {0}".format(base))

	print (lxc.create(container,))
	
	datadir  = os.path.join (rootfs, "data")
	if not os.path.exists(datadir):
		os.mkdir(datadir)

	cmd("mount --bind /data/{0}/{1} {2}/data".format(namespace.name, project.name, rootfs))

	print lxc.start(container)
	print lxc.wait(container)

	import time
	hostname = None

	while (not hostname):
		try:
			import socket
			(hostname, aliaslist, ipaddrlist) = socket.gethostbyname_ex (container)
		except socket.gaierror, exc:
			print 'Container not reachable yet... Waiting 1 second...'
			time.sleep(1)
Пример #6
0
def delete(
    namespace_arg,
    name_arg,
):
    session = Session()
    namespace = session.query(Namespace).filter(
        Namespace.name == namespace_arg).one()

    key = namespace.keys.filter(and_(Key.name == name_arg,
                                     Key.active == True)).first()

    if not key:
        raise Exception('Key not found.')

    key.active = False
    session.commit()

    update(namespace)
Пример #7
0
def add(namespace_arg, application_arg, domain_arg):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()

	if not namespace:
		raise Exception ('Namespace not found')

	# should check domain text field if user owns this domain

	application = namespace.projects.filter(Project.name==application_arg).first()
	if not application:
		raise Exception ('Application not found')

        if (application.domains.filter(and_(Domain.name ==domain_arg, Domain.active==True)).first()):
                raise Exception('Domain already exists')

	domain = Domain()
	domain.name = domain_arg 
	domain.active = True
	application.domains.append(domain)
	session.commit()
Пример #8
0
def get_applications():
	session = Session()

	hosts = []
	
	for host in get_hosts():
		hosts.append({'id': host.id + '_' + host.instance_type, 'ipaddr': host.private_ip_address})

	
	applications={}
	namespaces = session.query(Namespace).all()
	for namespace in namespaces:
		for project in namespace.projects:
			application={}
			application['id'] = ''.join(filter(lambda c: c.islower() or c.isdigit() or c=='-', namespace.name)) + '_' + ''.join(filter(lambda c: c.islower() or c.isdigit() or c=='-', project.name))
			application['domains']=[{'name':config['domain'].format(project.name, namespace.name), 'id': ''.join(filter(lambda c: c.islower() or c.isdigit() or c=='-', config['domain'].format(project.name, namespace.name)))},]
			for domain in project.domains:
				application['domains'].append ({'name':domain.name, 'id': ''.join(filter(lambda c: c.islower() or c.isdigit() or c=='-', domain.name))})
			application['containers']=[]
			application['hosts']=hosts
			applications[application['id']]=application
	return (applications)
Пример #9
0
def add(namespace_arg, name_arg, public_arg):
    session = Session()
    namespace = session.query(Namespace).filter(
        Namespace.name == namespace_arg).one()

    from sqlalchemy.sql import exists
    from sqlalchemy.sql import and_, or_, not_

    if (namespace.keys.filter(and_(Key.name == name_arg,
                                   Key.active == True)).first()):
        raise Exception('Key already exists')

    if (namespace.keys.filter(
            and_(Key.public == public_arg, Key.active == True)).first()):
        raise Exception('Key already exists')

    # validate public key
    values = public_arg.split()

    if (len(values) == 2):
        data = base64.decodestring(values[1])
        if (data[4:11] != values[0]):
            raise Exception("Invalid ssh key")
    elif (len(values) == 3):
        data = base64.decodestring(values[1])
        if (data[4:11] != values[0]):
            raise Exception("Invalid ssh key")
    else:
        raise Exception("Invalid ssh key")

    key = Key()
    key.name = name_arg
    key.public = public_arg
    key.active = True
    namespace.keys.append(key)
    session.commit()

    # update git repository for namespace
    update(namespace)
Пример #10
0
def add(namespace_arg, name_arg, public_arg):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()

	from sqlalchemy.sql import exists
	from sqlalchemy.sql import and_, or_, not_

        if (namespace.keys.filter(and_(Key.name==name_arg, Key.active==True)).first()):
                raise Exception('Key already exists')

        if (namespace.keys.filter(and_(Key.public==public_arg, Key.active==True)).first()):
                raise Exception('Key already exists')

	# validate public key
	values = public_arg.split()

	if (len(values)==2):
		data = base64.decodestring(values[1])
		if (data[4:11] != values[0]):
			raise Exception("Invalid ssh key")
	elif (len(values)==3):
		data = base64.decodestring(values[1])
		if (data[4:11] != values[0]):
			raise Exception("Invalid ssh key")
	else:
		raise Exception("Invalid ssh key")

	key = Key()
	key.name = name_arg 
	key.public = public_arg 
	key.active = True
	namespace.keys.append(key)
	session.commit()
	
	# update git repository for namespace
	update(namespace)
Пример #11
0
def create(namespace_arg):
    session = Session()

    if (session.query(
            exists().where(Namespace.name == namespace_arg)).scalar()):
        print "Namespace already exists"
        sys.exit(1)

    try:
        namespace = Namespace()
        namespace.name = namespace_arg
        session.add(namespace)

        # add key to authorized_keys

        import subprocess
        # use skeleton dir?
        os.mkdir(os.path.join(config['data'], namespace.name))

        #home = os.path.join(config['data'], namespace.name, 'home')

        #s = subprocess.Popen(['useradd', '-m', '-d', home, '--shell', '/usr/bin/git-shell', namespace.name, ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, )
        #logging.debug(s.communicate())

        #import subprocess
        #s = subprocess.Popen(['su', '-s', '/bin/sh', namespace.name], stdin=subprocess.PIPE, stdout=subprocess.PIPE, )

        #from jinja2 import Template
        #logging.debug(s.communicate(Template(
        #	"""
        #	mkdir {{home}}/git
        #	mkdir {{home}}/.ssh
        #	touch {{home}}/.ssh/authorized_keys
        #	""").render(home = home )))

        session.commit()

        print "Namespace has been created."
    except:
        session.rollback()
        logging.exception("Exception while creating namespace.")
Пример #12
0
def create(namespace_arg):
	session=Session()

	if (session.query(exists().where(Namespace.name==namespace_arg)).scalar()):
		print "Namespace already exists"
		sys.exit(1)

	try:
		namespace = Namespace()
		namespace.name = namespace_arg 
		session.add(namespace)

		# add key to authorized_keys

		import subprocess
		# use skeleton dir?
		os.mkdir(os.path.join(config['data'], namespace.name))

		#home = os.path.join(config['data'], namespace.name, 'home')

		#s = subprocess.Popen(['useradd', '-m', '-d', home, '--shell', '/usr/bin/git-shell', namespace.name, ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, )
		#logging.debug(s.communicate())
		
		#import subprocess
		#s = subprocess.Popen(['su', '-s', '/bin/sh', namespace.name], stdin=subprocess.PIPE, stdout=subprocess.PIPE, )

		#from jinja2 import Template
		#logging.debug(s.communicate(Template(
		#	"""
		#	mkdir {{home}}/git
		#	mkdir {{home}}/.ssh
		#	touch {{home}}/.ssh/authorized_keys
		#	""").render(home = home )))
		
		session.commit()

		print "Namespace has been created."	
	except:
		session.rollback()
		logging.exception("Exception while creating namespace.")
Пример #13
0
def delete(namespace_arg, application_arg, domain_arg):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()

	if not namespace:
		raise Exception ('Namespace not found')

	application = namespace.projects.filter(Project.name==application_arg).first()
	if not application:
		raise Exception ('Application not found')

        domain = application.domains.filter(and_(Domain.name ==domain_arg, Domain.active==True)).first()

	if not domain:
                raise Exception('Domain doesn\'t exists')

	session = Session()
	domain.active=False
	session.commit()
Пример #14
0
def create(namespace_arg, application_arg, runtime_arg, repository_arg):
    if not os.path.exists(os.path.join(datadir, "runtime/{0}.yaml").format(runtime_arg)):
        raise Exception("Runtime {0} does not exist.".format(runtime_arg))

    try:
        session = Session()
        namespace = session.query(Namespace).filter(Namespace.name == namespace_arg).one()

        project = Project()
        project.name = application_arg
        project.template = runtime_arg
        namespace.projects.append(project)

        bi = yaml.load(file(os.path.join(datadir, "runtime/{0}.yaml").format(project.template), "r"))

        root = os.path.join(config["data"], namespace.name)

        if not os.path.exists(os.path.join(root, project.name)):
            os.mkdir(os.path.join(root, project.name))

            # home = os.path.join(config['data'], namespace.name, 'home')

        from Crypto.PublicKey import RSA
        import base64

        key = RSA.generate(2048, os.urandom)

        project.key = Key()
        # project.key.private_key = key.exportKey('OpenSSH')
        project.key.private_key = key.exportKey()
        project.key.public_key = key.exportKey("OpenSSH")
        project.key.active = True

        project.repository = Repository()
        project.repository.url = repository_arg

        # add registered (namespace) ssh keys!
        # also for generic user

        # using ssh because of ownership
        """
		dirpath = tempfile.mkdtemp()

		cloned_repo = git.Repo.clone_from(config['git']['ssh'].format(namespace.name, project.name), dirpath)
		
		print 'Repo cloned from {0}.'.format(config['git']['ssh'].format(namespace.name, project.name))
		index = cloned_repo.index

		if 'project' in bi:
			for template in bi['project'].keys():
				print 'Adding to repo ' + template
				with open(os.path.join (dirpath, template), "w") as fh:
					fh.write (
							Template(bi['project'][template]).render( namespace=namespace, application=project)
						 )
				index.add([template])

		new_commit = index.commit("Upscale initial commit.")
		origin = cloned_repo.remotes.origin
		origin.push(cloned_repo.head)

		# clean up temp folder
		shutil.rmtree(dirpath)
		"""

        session.commit()

        print "Application has been created. You can clone the git repository with \n" "git clone {0} .".format(
            config["git"]["public"].format(namespace.name, project.name)
        )

    except:
        session.rollback()
        logging.exception("Exception while creating application")
Пример #15
0
def list(namespace_arg):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()
	return (namespace.keys.filter(Key.active==True))
Пример #16
0
def list(namespace_arg):
    session = Session()
    namespace = session.query(Namespace).filter(
        Namespace.name == namespace_arg).one()
    return (namespace.keys.filter(Key.active == True))
Пример #17
0
def create(namespace_arg, application_arg, runtime_arg, repository_arg):
    if not os.path.exists(
            os.path.join(datadir, 'runtime/{0}.yaml').format(runtime_arg)):
        raise Exception('Runtime {0} does not exist.'.format(runtime_arg))

    try:
        session = Session()
        namespace = session.query(Namespace).filter(
            Namespace.name == namespace_arg).one()

        project = Project()
        project.name = application_arg
        project.template = runtime_arg
        namespace.projects.append(project)

        bi = yaml.load(
            file(
                os.path.join(datadir,
                             'runtime/{0}.yaml').format(project.template),
                'r'))

        root = os.path.join(
            config['data'],
            namespace.name,
        )

        if not os.path.exists(os.path.join(root, project.name)):
            os.mkdir(os.path.join(root, project.name))

        # home = os.path.join(config['data'], namespace.name, 'home')

        from Crypto.PublicKey import RSA
        import base64
        key = RSA.generate(2048, os.urandom)

        project.key = Key()
        #project.key.private_key = key.exportKey('OpenSSH')
        project.key.private_key = key.exportKey()
        project.key.public_key = key.exportKey('OpenSSH')
        project.key.active = True

        project.repository = Repository()
        project.repository.url = repository_arg

        # add registered (namespace) ssh keys!
        # also for generic user

        # using ssh because of ownership
        """
		dirpath = tempfile.mkdtemp()

		cloned_repo = git.Repo.clone_from(config['git']['ssh'].format(namespace.name, project.name), dirpath)
		
		print 'Repo cloned from {0}.'.format(config['git']['ssh'].format(namespace.name, project.name))
		index = cloned_repo.index

		if 'project' in bi:
			for template in bi['project'].keys():
				print 'Adding to repo ' + template
				with open(os.path.join (dirpath, template), "w") as fh:
					fh.write (
							Template(bi['project'][template]).render( namespace=namespace, application=project)
						 )
				index.add([template])

		new_commit = index.commit("Upscale initial commit.")
		origin = cloned_repo.remotes.origin
		origin.push(cloned_repo.head)

		# clean up temp folder
		shutil.rmtree(dirpath)
		"""

        session.commit()

        print "Application has been created. You can clone the git repository with \n" \
         "git clone {0} .".format(config['git']['public'].format(namespace.name, project.name))

    except:
        session.rollback()
        logging.exception("Exception while creating application")
Пример #18
0
def get(namespace_arg, application_arg):
	session = Session()
	namespace = session.query(Namespace).filter(Namespace.name==namespace_arg).one()
	application = namespace.projects.filter(Project.name == application_arg).one()
	key = application.key
	return (key.public_key)