예제 #1
0
def configure_openstack():
    # quote strip
    def dequote(string):
        if string.startswith('"') and string.endswith('"'):
            string = string[1:-1]
        return string

    # check configuration
    settings = Status().check_settings()

    # get the form
    form = OpenStackForm(request.form)

    # try to select one and only record
    openstack = db.session.query(OpenStack).first()

    # create new entry if configuration doesn't exist
    if not openstack:
        openstack = OpenStack()

    if request.method == 'POST':
        # clear settings cache
        Status().flush()

        # handle a file upload
        try:
            file = request.files['file']
        except:
            file = False

        if file and allowed_file(file.filename):
            keyvals = {}
            for line in file:
                keyval = dict(re.findall(r'(\S+)=(".*?"|\S+)', line))
                if len(keyval) > 0:
                    keyvals.update(keyval)

            # set values from extracted lines above - needs SQL injection protection?
            openstack.authurl = "%s" % dequote(keyvals['OS_AUTH_URL'])
            openstack.tenantname = "%s" % dequote(keyvals['OS_TENANT_NAME'])
            openstack.tenantid = "%s" % dequote(keyvals['OS_TENANT_ID'])
            try:
                openstack.region = "%s" % dequote(keyvals['OS_REGION_NAME'])

                if openstack.region == None:
                    openstack.region = ""
            except:
                # don't need it
                openstack.region = ""
                pass
            openstack.osusername = "******" % dequote(keyvals['OS_USERNAME'])
            openstack.ospassword = "******"

            # update entry
            openstack.update(openstack)

        elif file:
            # file type not allowed
            flash("File type not allowed or empty.  Try again.", "file-error")

        else:
            # user is manually updating form
            current_password = openstack.ospassword

            if form.validate_on_submit():
                # populate with form and update
                form.populate_obj(openstack)

                # check if password was blank and use old one
                if openstack.ospassword == "":
                    openstack.ospassword = current_password

                openstack.update(openstack)

                flash("OpenStack settings updated!", "success")

            else:
                flash(
                    "There were form errors. Please check your entries and try again.",
                    "error")

            # having to do this to get the form to update on password update
            return redirect("/configure/openstack")

    # get existing form data
    openstack = db.session.query(OpenStack).first()

    # if notice, show message
    return render_template('configure/openstack.html',
                           settings=settings,
                           form=form,
                           openstack=openstack)
예제 #2
0
def configure_openstack():
# quote strip
	def dequote(string):
		if string.startswith('"') and string.endswith('"'):
			string = string[1:-1]
		return string

	# check configuration
	settings = Status().check_settings()

	# get the form
	form = OpenStackForm(request.form)

	# try to select one and only record
	openstack = db.session.query(OpenStack).first()


	# create new entry if configuration doesn't exist
	if not openstack:
		openstack = OpenStack()

	if request.method == 'POST':
		# clear settings cache
		Status().flush()

		# handle a file upload		
		try:
			file = request.files['file']
		except:
			file = False

		if file and allowed_file(file.filename):
			keyvals = {}
			for line in file:
				keyval = dict(re.findall(r'(\S+)=(".*?"|\S+)', line))
				if len(keyval) > 0:
					keyvals.update(keyval)
			
			# set values from extracted lines above - needs SQL injection protection?
			openstack.authurl = "%s" % dequote(keyvals['OS_AUTH_URL'])
			openstack.tenantname = "%s" % dequote(keyvals['OS_TENANT_NAME'])
			openstack.tenantid = "%s" % dequote(keyvals['OS_TENANT_ID'])
			try:
				openstack.region = "%s" % dequote(keyvals['OS_REGION_NAME'])

				if openstack.region == None:
					openstack.region = ""
			except:
				# don't need it
				openstack.region = ""
				pass
			openstack.osusername = "******" % dequote(keyvals['OS_USERNAME'])
			openstack.ospassword = "******"

			# update entry
			openstack.update(openstack)

		elif file:
			# file type not allowed
			flash("File type not allowed or empty.  Try again.", "file-error")
		
		else:
			# user is manually updating form
			current_password = openstack.ospassword

			if form.validate_on_submit():
				# populate with form and update
				form.populate_obj(openstack)

				# check if password was blank and use old one
				if openstack.ospassword == "":
					openstack.ospassword = current_password

				openstack.update(openstack)

				flash("OpenStack settings updated!", "success")

			else:
				flash("There were form errors. Please check your entries and try again.", "error")

			# having to do this to get the form to update on password update
			return redirect("/configure/openstack")

	# get existing form data
	openstack = db.session.query(OpenStack).first()

	# if notice, show message
	return render_template(
		'configure/openstack.html', 
		settings=settings, 
		form=form, 
		openstack=openstack
	)