def package_version_overview(request, namespace, version_id): version = get_object_or_404(PackageVersion, package__namespace=namespace, id=version_id) fork = request.GET.get('fork', None) git_ref = request.GET.get('git_ref', None) # Support the Install with mrbelvedere button by inspecting github referer url to determine fork and git_ref referer = request.META.get('HTTP_REFERRER', '') if referer.startswith('https://github.com/'): url_parts = referer.replace('https://github.com/', '').split('/') owner = url_parts[0] repo = url_parts[1] repo_base_url = 'https://github.com/{}/{}'.format(owner, repo) if fork is None: if version.repo_url != repo_base_url: fork = owner if git_ref is None: try: if url_parts[2] == 'tree': git_ref = '/'.join(url_parts[3:]) if git_ref.endswith('/README.md'): git_ref = git_ref.replace('/README.md', '') except IndexError: pass oauth = request.session.get('oauth') request.session['mpinstaller_current_version'] = version.id install_map = [] package_list = [] if oauth and oauth.get('access_token', None): org_packages = request.session.get('org_packages', None) metadata = request.session.get('metadata', None) if org_packages is None or metadata is None: if 'post_login_redirected' not in request.session: request.session[ 'mpinstaller_redirect'] = request.build_absolute_uri( request.path) request.session['post_login_redirected'] = False return HttpResponseRedirect( request.build_absolute_uri( '/mpinstaller/oauth/post_login')) else: del request.session['post_login_redirected'] redirect = quote(request.build_absolute_uri(request.path)) return HttpResponseRedirect( request.build_absolute_uri( '/mpinstaller/oauth/logout?redirect=%s' % redirect)) elif 'post_login_redirected' in request.session: del request.session['post_login_redirected'] # Ensure installation is available in connected org, logout and redirect if not reason = check_installation_available(request, version) if reason: return HttpResponseRedirect( '/mpinstaller/%s/version/%s/installation-unavailable/%s' % (version.package.namespace, version.id, reason)) # Get the install map and package list install_map = version_install_map(version, org_packages, metadata, git_ref, fork) package_list = install_map_to_package_list(install_map) logged_in = False redirect = quote(request.build_absolute_uri(request.path)) if oauth and oauth.get('access_token', None): login_url = None logout_url = request.build_absolute_uri( '/mpinstaller/oauth/logout?redirect=%s' % redirect) else: login_url = request.build_absolute_uri( '/mpinstaller/oauth/login?redirect=%s' % redirect) logout_url = None install_url = request.build_absolute_uri( '/mpinstaller/%s/version/%s/install' % (namespace, version_id)) if git_ref: install_url += '?git_ref=%s' % urllib.quote(git_ref) if fork: install_url += '&fork=%s' % urllib.quote(fork) elif fork: install_url += '?fork=%s' % urllib.quote(fork) repo_url = version.repo_url if repo_url and fork: repo_url_parts = repo_url.split('/') repo_url_parts[3] = fork repo_url = '/'.join(repo_url_parts) data = { 'version': version, 'oauth': request.session.get('oauth', None), 'git_ref': git_ref, 'fork': fork, 'repo_url': repo_url, 'login_url': login_url, 'logout_url': logout_url, 'install_url': install_url, 'base_url': request.build_absolute_uri('/mpinstaller/'), 'package_list': package_list, 'install_map': install_map, 'content_intro': version.get_content_intro(), } return render_to_response('mpinstaller/package_version_overview.html', data)
def start_package_installation(request, namespace, version_id): """ Kicks off a package installation and redirects to the installation's page """ version = get_object_or_404(PackageVersion, package__namespace=namespace, id=version_id) oauth = request.session.get('oauth', None) git_ref = request.GET.get('git_ref', None) fork = request.GET.get('fork', None) # Redirect back to the package overview page if not connected to an org if not oauth or not oauth.get('access_token'): redirect = version.get_installer_url(request) return HttpResponseRedirect(redirect) # Ensure installation is available in connected org, logout and redirect if not reason = check_installation_available(request, version) if reason: return HttpResponseRedirect( '/mpinstaller/%s/version/%s/installation-unavailable/%s' % (version.package.namespace, version.id, reason)) # This view should only be used for executing a map already reviewed by the user. # If there is no installed list or metadata list in session, that didn't happen for some reason installed = request.session.get('org_packages', None) if installed is None: return HttpResponseRedirect(version.get_installer_url(request)) metadata = request.session.get('metadata', None) if metadata is None: return HttpResponseRedirect(version.get_installer_url(request)) install_map = version_install_map(version, installed, metadata, git_ref, fork) installation_obj = PackageInstallation( package=version.package, version=version, git_ref=git_ref, fork=fork, org_id=oauth['org_id'], org_type=oauth['org_type'], instance_url=oauth['instance_url'], status='Pending', username=oauth['username'], install_map=install_map_to_json(install_map), ) installation_obj.save() # Temporarily save the needed session variables so background processes can do the work session_obj = PackageInstallationSession( installation=installation_obj, oauth=json.dumps(oauth), org_packages=json.dumps(installed), metadata=json.dumps(request.session.get('metadata', {})), ) session_obj.save() order = 0 for step in install_map: step_obj = PackageInstallationStep( installation=installation_obj, package=step['version'].package, version=step['version'], previous_version=step['installed'], action=step['action'], status='Pending', order=order, ) order += 1 if step_obj.action == 'skip': step_obj.status = 'Succeeded' step_obj.save() # Clear out the org_packages and metadata cached in session if 'org_packages' in request.session: del request.session['org_packages'] if 'metadata' in request.session: del request.session['metadata'] return HttpResponseRedirect('/mpinstaller/installation/%s' % installation_obj.id)
def deploy_commit_to_org(self, commit=None, sync=None): if not self.sf_oauth: if sync: sync.log += 'FAILED: No Saleforce Org credentials available to sync with DE org\n' sync.status = 'failed' sync.save() return # If there is currently an installation running, return None if self.last_deployment_installation and self.last_deployment_installation.status in ('Pending','In Progress'): if sync: sync.log += 'FAILED: Another installation is already in progress for this contribution. Please try again later\n' sync.status = 'failed' sync.save() return version = self.package_version oauth = self.decode_sf_oauth() if commit: git_ref = commit else: git_ref = self.fork_branch fork = self.contributor.user.username # Get installed packages in org api = ApiRetrieveInstalledPackages(oauth) if sync: sync.log += '--- Getting list of installed packages in org\n' sync.save() installed = api() if sync: sync.log += 'DONE' sync.save() if not installed: installed = {} # Store the access token from the api call if it changed if api.oauth != oauth: self.sf_oauth = json.dumps(api.oauth) # With GitHub installations, we don't run conditions since we dynamically construct the install map metadata = {} # Build the install map install_map = version_install_map(version, installed, metadata, git_ref, fork) if sync: sync.log += '--- Configuring installation steps\n' sync.save() # Build the Installation object installation_obj = PackageInstallation( package = version.package, version = version, git_ref = git_ref, fork = fork, org_id = oauth['org_id'], org_type = oauth['org_type'], instance_url = oauth['instance_url'], status = 'Pending', username = oauth['username'], install_map = install_map_to_json(install_map), ) installation_obj.save() # Create a PackageInstallationSession so we can reuse the same install method from mpinstaller session_obj = PackageInstallationSession( installation = installation_obj, oauth = json.dumps(oauth), org_packages = json.dumps(installed), metadata = json.dumps(metadata) ) session_obj.save() order = 0 # Create the PackageInstallationStep objects for step in install_map: step_obj = PackageInstallationStep( installation = installation_obj, package = step['version'].package, version = step['version'], previous_version = step['installed'], action = step['action'], status = 'Pending', order = order, ) order += 1 if step_obj.action == 'skip': step_obj.status = 'Succeeded' else: if sync: sync.log += '--- * %s: %s\n' % (step_obj.version, step_obj.action) sync.save() step_obj.save() if sync: sync.log += 'DONE\n\n' sync.log += '--- Starting installation #%s\n' % installation_obj.id sync.log += '--- * NOTE: This can take anywhere from 5-15 minutes to complete\n' sync.log += '--- * <a href="/mpinstaller/installation/%s" target="_blank">View installation status in separate tab</a>\n' % installation_obj.id sync.save() # Run the installer synchronously since we should already be inside a background process install_package_version(installation_obj.id) if sync: sync.log += 'DONE\n\n' sync.save() # Record the deployment on the Contribution if successful self.last_deployment_installation = installation_obj self.last_deployment_date = datetime.now() if installation_obj.status != 'Failed': if commit: self.last_deployed_commit = commit else: branch = self.get_fork_branch() self.last_deployed_commit = branch['object']['sha'] return installation_obj
def package_version_overview(request, namespace, version_id): version = get_object_or_404(PackageVersion, package__namespace=namespace, id=version_id) fork = request.GET.get('fork', None) git_ref = request.GET.get('git_ref', None) oauth = request.session.get('oauth') request.session['mpinstaller_current_version'] = version.id install_map = [] package_list = [] if oauth and oauth.get('access_token', None): org_packages = request.session.get('org_packages', None) metadata = request.session.get('metadata', None) if org_packages is None or metadata is None: if 'post_login_redirected' not in request.session: request.session[ 'mpinstaller_redirect'] = request.build_absolute_uri( request.path) request.session['post_login_redirected'] = False return HttpResponseRedirect( request.build_absolute_uri( '/mpinstaller/oauth/post_login')) else: del request.session['post_login_redirected'] redirect = quote(request.build_absolute_uri(request.path)) return HttpResponseRedirect( request.build_absolute_uri( '/mpinstaller/oauth/logout?redirect=%s' % redirect)) elif 'post_login_redirected' in request.session: del request.session['post_login_redirected'] # Ensure installation is available in connected org, logout and redirect if not reason = check_installation_available(request, version) if reason: return HttpResponseRedirect( '/mpinstaller/%s/version/%s/installation-unavailable/%s' % (version.package.namespace, version.id, reason)) # Get the install map and package list install_map = version_install_map(version, org_packages, metadata, git_ref, fork) package_list = install_map_to_package_list(install_map) logged_in = False redirect = quote(request.build_absolute_uri(request.path)) if oauth and oauth.get('access_token', None): login_url = None logout_url = request.build_absolute_uri( '/mpinstaller/oauth/logout?redirect=%s' % redirect) else: login_url = request.build_absolute_uri( '/mpinstaller/oauth/login?redirect=%s' % redirect) logout_url = None install_url = request.build_absolute_uri( '/mpinstaller/%s/version/%s/install' % (namespace, version_id)) if git_ref: install_url += '?git_ref=%s' % urllib.quote(git_ref) if fork: install_url += '&fork=%s' % urllib.quote(fork) elif fork: install_url += '?fork=%s' % urllib.quote(fork) repo_url = version.repo_url if repo_url and fork: repo_url_parts = repo_url.split('/') repo_url_parts[3] = fork repo_url = '/'.join(repo_url_parts) data = { 'version': version, 'oauth': request.session.get('oauth', None), 'git_ref': git_ref, 'fork': fork, 'repo_url': repo_url, 'login_url': login_url, 'logout_url': logout_url, 'install_url': install_url, 'base_url': request.build_absolute_uri('/mpinstaller/'), 'package_list': package_list, 'install_map': install_map, 'content_intro': version.get_content_intro(), } return render_to_response('mpinstaller/package_version_overview.html', data)
def package_version_overview(request, namespace, version_id): version = get_object_or_404(PackageVersion, package__namespace = namespace, id=version_id) fork = request.GET.get('fork',None) git_ref = request.GET.get('git_ref',None) oauth = request.session.get('oauth') request.session['mpinstaller_current_version'] = version.id install_map = [] package_list = [] if oauth and oauth.get('access_token', None): org_packages = request.session.get('org_packages', None) metadata = request.session.get('metadata', None) if org_packages is None or metadata is None: if 'post_login_redirected' not in request.session: request.session['mpinstaller_redirect'] = request.build_absolute_uri(request.path) request.session['post_login_redirected'] = False return HttpResponseRedirect(request.build_absolute_uri('/mpinstaller/oauth/post_login')) else: del request.session['post_login_redirected'] redirect = quote(request.build_absolute_uri(request.path)) return HttpResponseRedirect(request.build_absolute_uri('/mpinstaller/oauth/logout?redirect=%s' % redirect)) elif 'post_login_redirected' in request.session: del request.session['post_login_redirected'] # Ensure installation is available in connected org, logout and redirect if not reason = check_installation_available(request, version) if reason: return HttpResponseRedirect('/mpinstaller/%s/version/%s/installation-unavailable/%s' % (version.package.namespace, version.id, reason)) # Get the install map and package list install_map = version_install_map(version, org_packages, metadata, git_ref, fork) package_list = install_map_to_package_list(install_map) logged_in = False redirect = quote(request.build_absolute_uri(request.path)) if oauth and oauth.get('access_token', None): login_url = None logout_url = request.build_absolute_uri('/mpinstaller/oauth/logout?redirect=%s' % redirect) else: login_url = request.build_absolute_uri('/mpinstaller/oauth/login?redirect=%s' % redirect) logout_url = None install_url = request.build_absolute_uri('/mpinstaller/%s/version/%s/install' % (namespace, version_id)) if git_ref: install_url += '?git_ref=%s' % urllib.quote(git_ref) if fork: install_url += '&fork=%s' % urllib.quote(fork) elif fork: install_url += '?fork=%s' % urllib.quote(fork) repo_url = version.repo_url if repo_url and fork: repo_url_parts = repo_url.split('/') repo_url_parts[3] = fork repo_url = '/'.join(repo_url_parts) data = { 'version': version, 'oauth': request.session.get('oauth',None), 'git_ref': git_ref, 'fork': fork, 'repo_url': repo_url, 'login_url': login_url, 'logout_url': logout_url, 'install_url': install_url, 'base_url': request.build_absolute_uri('/mpinstaller/'), 'package_list': package_list, 'install_map': install_map, 'content_intro': version.get_content_intro(), } return render_to_response('mpinstaller/package_version_overview.html', data)
def start_package_installation(request, namespace, version_id): """ Kicks off a package installation and redirects to the installation's page """ version = get_object_or_404(PackageVersion, package__namespace=namespace, id=version_id) oauth = request.session.get('oauth', None) git_ref = request.GET.get('git_ref',None) fork = request.GET.get('fork',None) # Redirect back to the package overview page if not connected to an org if not oauth or not oauth.get('access_token'): redirect = version.get_installer_url(request) return HttpResponseRedirect(redirect) # Ensure installation is available in connected org, logout and redirect if not reason = check_installation_available(request, version) if reason: return HttpResponseRedirect('/mpinstaller/%s/version/%s/installation-unavailable/%s' % (version.package.namespace, version.id, reason)) # This view should only be used for executing a map already reviewed by the user. # If there is no installed list or metadata list in session, that didn't happen for some reason installed = request.session.get('org_packages', None) if installed is None: return HttpResponseRedirect(version.get_installer_url(request)) metadata = request.session.get('metadata', None) if metadata is None: return HttpResponseRedirect(version.get_installer_url(request)) install_map = version_install_map(version, installed, metadata, git_ref, fork) installation_obj = PackageInstallation( package = version.package, version = version, git_ref = git_ref, fork = fork, org_id = oauth['org_id'], org_type = oauth['org_type'], instance_url = oauth['instance_url'], status = 'Pending', username = oauth['username'], install_map = install_map_to_json(install_map), ) installation_obj.save() # Temporarily save the needed session variables so background processes can do the work session_obj = PackageInstallationSession( installation = installation_obj, oauth = json.dumps(oauth), org_packages = json.dumps(installed), metadata = json.dumps(request.session.get('metadata', {})), ) session_obj.save() order = 0 for step in install_map: step_obj = PackageInstallationStep( installation = installation_obj, package = step['version'].package, version = step['version'], previous_version = step['installed'], action = step['action'], status = 'Pending', order = order, ) order += 1 if step_obj.action == 'skip': step_obj.status = 'Succeeded' step_obj.save() # Clear out the org_packages and metadata cached in session if 'org_packages' in request.session: del request.session['org_packages'] if 'metadata' in request.session: del request.session['metadata'] return HttpResponseRedirect('/mpinstaller/installation/%s' % installation_obj.id)