-
Notifications
You must be signed in to change notification settings - Fork 10
/
butler-servers.el
89 lines (70 loc) · 3.18 KB
/
butler-servers.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;; -*- lexical-binding: t -*-
;;; butler-servers.el --- Code to deal with Jenkins Servers
;; Copyright © 2012-2013 Ashton Kemerling
;;
;; Author: Ashton Kemerling <[email protected]>
;; URL: http://www.github.com/AshtonKem/Butler.git
;; Keywords: Jenkins, Hudson, CI
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Deals with storing & parsing Butler servers
;;; Code:
(defvar butler-hash (make-hash-table :test #'equal))
(defvar butler-server-list nil)
(defun parse-authinfo-file (filename servername)
(if (file-exists-p filename)
(with-temp-buffer
(insert-file-contents filename)
(search-forward (concat "machine " servername))
(let* ((line-start (line-beginning-position))
(line-end (line-end-position))
(line (buffer-substring line-start line-end))
(splitted (split-string line " "))
(filtered (delq "" splitted))
(username (car (cdr (member "login" filtered))))
(password (car (cdr (member "password" filtered)))))
(if (and username password)
(generate-basic-auth username password))))))
(defun generate-basic-auth (username password)
(replace-regexp-in-string "\n" ""
(concat "Basic "
(base64-encode-string
(concat username ":" password)))))
(defun prepare-servers ()
(when (= 0 (hash-table-count butler-hash))
(dolist (server butler-server-list)
(let* ((name (car (cdr server)))
(args (cdr (cdr server)))
(url (cdr (assoc 'server-address args)))
(username (cdr (assoc 'server-user args)))
(password (cdr (assoc 'server-password args)))
(auth-file (cdr (assoc 'auth-file args)))
(auth-string (if auth-file
(parse-authinfo-file auth-file name)
(generate-basic-auth username password)))
(server-hash (make-hash-table :test #'equal)))
(puthash 'name name server-hash)
(puthash 'username username server-hash)
(puthash 'auth auth-string server-hash)
(puthash 'url url server-hash)
(puthash 'jobs (make-hash-table :test #'equal) server-hash)
(puthash name
server-hash
butler-hash)))))
(defun get-server (name)
(prepare-servers)
(gethash name butler-hash))
(defun get-job (server name)
(gethash name (gethash 'jobs server)))
(provide 'butler-servers)
;; butler-servers.el ends here