Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Added tunnels support to the rails-proxy.
Browse files Browse the repository at this point in the history
Automaticaly create the ssh tunnel when start webserver at the remote host.
  • Loading branch information
dmexe committed Mar 2, 2009
1 parent cf1035d commit cb11e73
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
2 changes: 2 additions & 0 deletions bundles/rails-webserver-bundle.el
Expand Up @@ -60,6 +60,7 @@
(type rails/webserver-bundle/process-type)
(addr rails/webserver-bundle/process-addr))
(when (memq (process-status proc) '(exit signal))
(rails/proxy/down-tunnel-if-need port)
(setq rails/webserver-bundle/process-env nil)
(setq rails/webserver-bundle/process-port nil)
(setq rails/webserver-bundle/process-type nil)
Expand Down Expand Up @@ -89,6 +90,7 @@
(cdr cmd-alist))))
(when (processp proc)
(rails/runner/prepare-buffer proc)
(rails/proxy/up-tinnel-if-need root port)
(set-process-sentinel proc 'rails/webserver-bundle/sentinel-proc)
(setq rails/webserver-bundle/process-env env)
(setq rails/webserver-bundle/process-type type)
Expand Down
74 changes: 51 additions & 23 deletions rails-proxy.el
Expand Up @@ -26,47 +26,56 @@

(defvar rails/proxy/ssh "ssh")
(defvar rails/proxy/ssh-args "-t -t -q")
(defvar rails/proxy/tunnel-local-port "80")
(defvar rails/proxy/tunnel-args "-t -t -q -L %s:127.0.0.1:%s %s")
(defvar rails/proxy/tunnel-buffer "*rails remote tunnel :%s*")

(defvar rails/proxy/dir-list
'(("z:/apps/" "dima-exe@d2.undev.ru" "/home/dima-exe/apps/")))

(defvar rails/proxy/local-root nil)
(defvar rails/proxy/remote-root nil)

(defun rails/proxy/remote-p (dir)
(files-ext/file-in-directories-p
(mapcar 'car rails/proxy/dir-list) dir))

(defun rails/proxy/remote-list (dir)
(find (rails/proxy/remote-p dir)
rails/proxy/dir-list
:key 'car :test 'string=))

(defun rails/proxy/make-command (root command &optional args)
(let ((loc (files-ext/file-in-directories-p
(mapcar 'car rails/proxy/dir-list) root)))
(if loc
(let ((host (cdr (find loc rails/proxy/dir-list :key 'car :test 'string=)))
(dir (string-ext/cut root loc :begin)))
(list rails/proxy/ssh
(format (concat "%s %s \"(cd %s%s && %s " args ")\"")
rails/proxy/ssh-args
(first host)
(cadr host)
dir
command)
host))
(list command args))))

(defun rails/proxy/remote-p (cmd)
(string= rails/proxy/ssh (car cmd)))
(if (rails/proxy/remote-p root)
(let* ((plist (rails/proxy/remote-list root))
(dir (car plist))
(rdir (concat (caddr plist) (string-ext/cut root dir :begin)))
(host (cadr plist)))
(list rails/proxy/ssh
(format (concat "%s %s \"(cd %s && %s " args ")\"")
rails/proxy/ssh-args
host
rdir
command)
host))
(list command args)))

(defun rails/proxy/shell-command (root name buffer command &optional args)
(let* ((cmd (rails/proxy/make-command root command args))
(proc (start-process-shell-command name
buffer
(car cmd)
(cadr cmd))))
(if (rails/proxy/remote-p cmd)
(rails/proxy/setup-remote root proc cmd)
(if (rails/proxy/remote-p root)
(rails/proxy/setup-remote root proc)
proc)))

(defun rails/proxy/setup-remote (root proc cmd)
(defun rails/proxy/setup-remote (root proc)
(with-current-buffer (process-buffer proc)
(set (make-local-variable 'rails/proxy/local-root) root)
(set (make-local-variable 'rails/proxy/remote-root) (car (nth 2 cmd))))
proc)
(let ((plist (rails/proxy/remote-list root)))
(set (make-local-variable 'rails/proxy/local-root) (car plist))
(set (make-local-variable 'rails/proxy/remote-root) (caddr plist)))
proc))

(defun rails/proxy/shell-command-to-string (root command)
(let ((cmd (rails/proxy/make-command root command)))
Expand All @@ -75,4 +84,23 @@
(format "%s %s" (car cmd) (cadr cmd))
(car cmd)))))

(defun rails/proxy/up-tinnel-if-need (root remote-port)
(when-bind (plist (rails/proxy/remote-list root))
(let ((args (format rails/proxy/tunnel-args
rails/proxy/tunnel-local-port
remote-port
(cadr plist)))
(name (format rails/proxy/tunnel-buffer remote-port)))
(unless (get-buffer rails/proxy/tunnel-buffer)
(start-process-shell-command name
name
rails/proxy/ssh
args)))))

(defun rails/proxy/down-tunnel-if-need (remote-port)
(let ((name (format rails/proxy/tunnel-buffer remote-port)))
(when-bind (proc (get-buffer-process name))
(kill-process proc))))


(provide 'rails-proxy)

0 comments on commit cb11e73

Please sign in to comment.