def get_next_provider(self, image_id): try: instances = Instance.objects.filter(policy=self).all() except ObjectDoesNotExist: instances = [] num_provider_instances = defaultdict(lambda:0) for instance in instances: num_provider_instances[instance.provider] += 1 for policyburstingprovider in self.policyburstingproviders_set.all(): # Check for capacity if policyburstingprovider.capacity < 0 or \ num_provider_instances[policyburstingprovider.provider] < \ policyburstingprovider.capacity: provider = policyburstingprovider.provider # Make sure there is a matching RealImage count = RealImage.objects.filter(provider=provider, aggregate_image__id=image_id).count() if count >= 1: return policyburstingprovider.provider else: #TODO: Return error code log.error('No real image image found for provider "%s" and ' \ 'image_id "%s"' % (provider, image_id)) log.error("No suitable provider found in cloudbursting policy %s." % self.id) return None #TODO: Return error code
def add_user(self, username, password): try: output = self.node.ssh_run_command(["NET", "USER", username, password, "/ADD"]) if output.find("The command completed successfully.") > -1: log.debug("User %s has been created" % username) elif output.find("The account already exists.") > -1: log.debug("User %s already exists, going to try to set the password" % username) output = self.change_user_password(username, password) if output.find("The command completed successfully.") > -1: log.debug("THE PASSWORD WAS RESET") else: error_string = ( "An unknown error occured while trying to set the password for user %s on machine %s. The error from the machine was %s" % (username, self.ip, output) ) log.error(error_string) return False, error_string else: error_string = ( "An unknown error occured while trying to create user %s on machine %s. The error from the machine was %s" % (username, self.ip, output) ) log.error(error_string) return False, error_string return True, "" except HostNotConnectableError: return False, ""
def create(request, projectname): """Create and deploy a new project. Displays the form to do so on GET, goes and does a create + deploy operation on POST. Also has the feature to pre-fill out the form from an incomming JSON token. """ if request.method == "POST" and \ request.META['CONTENT_TYPE'].find("application/x-www-form-urlencoded") != -1: # If the submitted type is not form encoded data, it's probably a json # spec of applications, which should instead go to populate and display # the forms. pform = forms.ProjectForm(request.POST) appsform = forms.AppFormSet(request.POST) dform = forms.DeploymentForm(request.POST, noactive=True) allforms = [pform, appsform, dform] # If forms aren't valid, fall through and display the (invalid) forms # with error text if all(f.is_valid() for f in allforms): log.info("Preparing to create+deploy %s", projectname) pdata = pform.cleaned_data # Create the deployment object to do some early validation checks deployment = models.DeployedProject() deployment.name = projectname deployment.owner = request.user deployment.full_clean() # Configure the new project. None of these actions actually execute # until we enter the try block below builder = opus.lib.builder.ProjectBuilder(projectname) for appdata in appsform.cleaned_data: if not appdata: # Left blank, nothing to add continue log.debug(" ... with app %r", appdata['apppath']) builder.add_app(appdata['appname'], appdata['apppath'], appdata['apptype']) if pdata['idprovider'] != 'local': log.debug(" ... and the idp app %r", pdata['idprovider']) appname, apptype, apppath = \ settings.OPUS_ALLOWED_AUTH_APPS[pdata['idprovider']] builder.add_app(appname, apppath, apptype) # Now actually execute the tasks. This is done in a try block which # catches all exceptions so that we can roll back failed partial # deployments in any error cases. log.debug("Executing create action on %r...", projectname) try: # Create the project directory projectdir = builder.create(settings.OPUS_BASE_DIR) log.info("%r created, starting deploy process", projectname) # Prepare deployment parameters info = models.DeploymentInfo() info.dbengine = dform.cleaned_data['dbengine'] # If requested, create a database for it if info.dbengine == "postgresql_psycopg2" and \ settings.OPUS_AUTO_POSTGRES_CONFIG: autodb = opus.project.deployment.database.\ setup_postgres(projectname) info.dbname, info.dbuser, info.dbpassword, \ info.dbhost, info.dbport = autodb elif info.dbengine == "sqlite3": # SQLite database locations get set automatically by the # deployment libraries. No other options are set. # SQLite is handled differently (as far as the location of # the code) since the file must be secured properly. So # the deployer handles that correctly along side its # routines to change permissions on the directory. pass else: info.dbname = dform.cleaned_data['dbname'] info.dbuser = dform.cleaned_data['dbuser'] info.dbpassword = dform.cleaned_data['dbpassword'] info.dbhost = dform.cleaned_data['dbhost'] info.dbport = dform.cleaned_data['dbport'] info.superusername = dform.cleaned_data['superusername'] info.superemail = dform.cleaned_data['superemail'] info.superpassword = dform.cleaned_data['superpassword'] # Deploy it now! But don't activate it. deployment.deploy(info, False) deployment.set_debug(dform.cleaned_data['debug']) deployment.save() except Exception, e: # The project didn't deploy for whatever reason. Delete the # project directory and re-raise the exception # Careful that this method isn't called on an existing project, # since it could be tricked into deleting an existing project. # edit_or_create() ought to check that for us, this function # shouldn't be called on an existing project. log.error("Project didn't fully create or deploy, rolling back deployment. %s", e) # Schedule a task to delete the project tasks.destroy_project_by_name.delay(deployment.name) raise log.info("Project %r successfully deployed", projectname) return redirect("opus.project.deployment.views.set_app_settings", projectname) else: log.debug(request.POST) log.debug("Create view called, but forms didn't validate")