💾 Archived View for chirale.org › 2013-06-01_1161.gmi captured on 2024-05-12 at 15:23:35. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
Some months ago I talked about how to achieve a unified login from Django to Drupal using drush. The basic assumption was that both Drupal and Django are on the same server. What if the two components are on different servers?
unified login from Django to Drupal using drush
Paramiko is a SSH2 protocol library aimed to provide simple classes to make SSH connection. Let’s see how the code to call drush on command line changes.
Prerequisites:
DRUPAL_SERVER_SSH_HOST = '0' # Your host here DRUPAL_SERVER_SSH_USERNAME = 'YourRemoteServerUserHere' DRUPAL_SERVER_SSH_PASSWORD = 'YourRemoteServerPasswordHere'
```
And then:
```brush:
assert request.user.drupal_id > 0 # user id to log in drupal_id = str(request.user.drupal_id) output = "" try: # a list with command as first element and arguments following get_password_recovery_url = ["drush", "-r", settings.DRUPAL_SITE_PATH, "-l", settings.DRUPAL_SITE_NAME, "user-login", drupal_id] # via ssh http://stackoverflow.com/a/3586168/892951 ssh = paramiko.SSHClient() # add to known_host the remote server key if it's not already stored # @see http://jessenoller.com/blog/2009/02/05/ssh-programming-with-paramiko-completely-different ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(settings.DRUPAL_SERVER_SSH_HOST, username=settings.DRUPAL_SERVER_SSH_USERNAME, password=settings.DRUPAL_SERVER_SSH_PASSWORD) ssh_stdin, output, ssh_stderr = ssh.exec_command(" ".join(get_password_recovery_url)) output_lines = output.read().splitlines() # taking only the first line of the output: # e.g. 'http://example.com.it/user/reset/16/1369986816/67k7ReHi97FdtRfdrrXGqqesyz6FXyy7T8jqHiXxsrY/login' except: # @todo additional statements here pass finally: if ssh: ssh.close() if output_lines: drupal_login_url = output_lines[0].replace("http://example.com/", "http://%s/" % settings.DRUPAL_SITE_URL).strip() destination = "%s?destination=%s" % (drupal_login_url, settings.DRUPAL_LOGIN_DESTINATION) return redirect(destination) else: return HttpResponse(' <h1>Wrong request</h1> ')
```
This is the same code of the previous howto, with the difference that drush now is running on a different server of django. You can use the same method to do anything you have to with drush, any time you call this piece of code an SSH connection is opened.
See also:
* Paramiko (github \| cheeseshop)
*
https://web.archive.org/web/20130601000000*/http://www.lag.net/paramiko/
https://web.archive.org/web/20130601000000*/http://www.lag.net/paramiko/
https://web.archive.org/web/20130601000000*/https://github.com/paramiko/paramiko
https://web.archive.org/web/20130601000000*/https://pypi.python.org/pypi/paramiko/1.10.1
https://web.archive.org/web/20130601000000*/http://stackoverflow.com/a/8293225/892951