summaryrefslogtreecommitdiff
path: root/dotfiles/.config/shepherd/init.scm
blob: 5ee7b5fb691d68db4847669f9b0a21fca07d78de (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
(use-modules (ice-9 match)
             (srfi srfi-1)
             (system repl server))

(define-syntax-rule (forever body ...)
  (while #t body ...))

(define-syntax-rule (with-fork body ...)
  (match (primitive-fork)
    (0 (begin body ...))
    (pid pid)))

(define provided-by (@@ (shepherd service) provided-by))
(define deregister-service (@@ (shepherd service) deregister-service))

(define deregister-service*
  (compose deregister-service symbol->string))

(define (register-services* . services)
  (for-each deregister-service* (append-map provided-by services))
  (apply register-services services))

(define-syntax-rule (define-service name service)
  (define name (begin (register-services* service) service)))

(define (touch-file file)
  (close-port (open-file file "a0b")))

(define (run-command command)
  (zero? (status:exit-val (apply system* command))))

(define (make-system-constructor command)
  (lambda _
    (run-command command)))

(define (make-system-destructor command)
  (lambda _
    (not (run-command command))))

(define (simple-service program)
  (make <service>
    #:provides (list (string->symbol program))
    #:requires '()
    #:start (make-forkexec-constructor (list program))
    #:stop (make-kill-destructor)))

(define %home (getenv "HOME"))

(define (rbenv-bin version)
  "Return the binary path for the VERSION of Ruby managed by rbenv."
  (string-append %home "/.rbenv/versions/" version "/bin"))

(define (ruby-environment version gemset)
  "Return the environment variables needed to run Ruby applications
that use a specific VERSION of Ruby, whose dependencies are stored in
GEMSET."
  (let ((gem-home (string-append %home "/.gems/" gemset)))
    (list (string-append "GEM_PATH=" gem-home)
          (string-append "GEM_HOME=" gem-home)
          (string-append "PATH=" gem-home "/bin:"
                         (rbenv-bin version) ":"
                         (getenv "PATH")))))

(define* (rails-service name port ruby-version #:optional (requires '()))
  "Create a service that runs the Rails application NAME located in
DIR.  The application runs with RUBY, the file name of the necessary
Ruby version, and listens on PORT."
  (define gem-home
    (string-append %home "/.gems/" name))

  (make <service>
    #:provides (list (string->symbol name))
    #:requires requires
    #:start (make-forkexec-constructor
             `("passenger" "start"
               "--address" "127.0.0.1"
               "--port" ,port
               "--ruby" ,(string-append (rbenv-bin ruby-version) "/ruby"))
             #:directory (string-append %home "/Code/" name)
             #:environment-variables (ruby-environment ruby-version name))
    #:stop (make-kill-destructor)))

;; (define-service vhl-tunnel
;;   (make <service>
;;     #:provides '(vhl-tunnel)
;;     #:requires '()
;;     #:start (make-forkexec-constructor
;;              '("ssh" "-N" "-L" "1234:7VWJD42.vhl.dom:22" "vhl"))
;;     #:stop (make-kill-destructor)))

;; (define-service vhl-proxy
;;   (make <service>
;;     #:provides '(vhl-proxy)
;;     #:requires '(vhl-tunnel)
;;     #:start (make-forkexec-constructor
;;              '("ssh" "-N" "-D8080" "-p1234" "dthompson@localhost"))
;;     #:stop (make-kill-destructor)))

(register-services
 ;; VHL applications
 (rails-service "api" "3002" "2.2.2")
 (rails-service "ua" "3000" "2.1.5" '(api))
 (rails-service "m3" "3001" "2.2.2" '(api ua punjab openfire sidekiq))

 ;; M3 DB replica
 (make <service>
   #:provides '(m3-replica)
   #:start (make-forkexec-constructor
            `("mysqld"
              "--defaults-file=/home/dthompson/Code/m3/replica/etc/my.cnf"
              "--user=dthompson"))
   #:stop (make-kill-destructor))

 ;; Punjab (BOSH)
 (let* ((punjab-home (string-append %home "/Code/vhl-bosh"))
        (punjab-config (string-append punjab-home "/conf/development.tac")))
   (make <service>
     #:provides '(punjab)
     #:requires '()
     #:start (make-forkexec-constructor
              (list "twistd" "--nodaemon" "-y" punjab-config)
              #:directory punjab-home)
     #:stop (make-kill-destructor)))

 ;; Openfire (XMPP)
 (let ((openfire-home (string-append %home "/Code/vhl-xmpp/target/openfire")))
   (make <service>
     #:provides '(openfire)
     #:requires '()
     #:start (make-forkexec-constructor
              (list "java" "-server"
                    (string-append "-DopenfireHome=" openfire-home)
                    (string-append "-Dopenfire.lib.dir=" openfire-home "/lib")
                    "-jar" (string-append openfire-home "/lib/startup.jar"))
              #:directory openfire-home)
     #:stop (make-kill-destructor)))

 ;; Redis
 (make <service>
   #:provides '(redis)
   #:requires '()
   #:start (make-forkexec-constructor '("redis-server"))
   #:stop (make-kill-destructor))

 ;; Sidekiq
 (make <service>
    #:provides '(sidekiq)
    #:requires '(redis)
    #:start (make-forkexec-constructor
             '("sidekiq")
             #:directory (string-append %home "/Code/m3")
             #:environment-variables
             (ruby-environment "1.9.3-p551" "m3"))
    #:stop (make-kill-destructor))

 ;; Emacs
 (make <service>
   #:provides '(emacs)
   #:requires '()
   #:start (make-system-constructor '("emacs" "--daemon"))
   #:stop (make-system-destructor '("emacsclient" "--eval" "(kill-emacs)")))

 ;; Xorg
 (make <service>
   #:provides '(xmodmap)
   #:requires '()
   #:start (make-forkexec-constructor
            (list "xmodmap" (string-append %home "/.xmodmap"))))

 ;; GPG/SSH Agent
 (make <service>
   #:provides '(gpg-agent)
   #:requires '()
   #:start (make-system-constructor
            (list "gpg-agent" "--daemon" "--enable-ssh-support"
                  "--default-cache-ttl=10800" ; cache for 3 hours
                  "--pinentry-program"
                  (string-append %home "/.guix-profile/bin/pinentry-gtk-2")))
   #:stop (make-system-destructor
           '("gpg-connect-agent" "killagent" "/bye")))

 ;; Mail
 (simple-service "davmail") ; for dealing with Exchange at work.
 (make <service>
   #:provides '(offlineimap)
   #:requires '()
   #:start (lambda args
             (with-fork
              ;; OfflineIMAP's "autorefresh" mode consistently
              ;; consumes all of the RAM on my machine, so let's just
              ;; run it in a loop instead.
              (forever
               (system* "offlineimap")
               (sleep 40))))
   #:stop (make-kill-destructor))

 ;; FUSE
 (make <service>
   #:provides '(sshfs)
   #:requires '()
   #:start (make-forkexec-constructor
            (list "sshfs" "aigis:Music" (string-append %home "/Music"))))

 ;; Music
 (make <service>
   #:provides '(mpd)
   #:requires '()
   #:start (lambda args
             (define (scope file)
               (string-append %home "/.config/mpd/" file))

             (unless (file-exists? (scope "playlists"))
               (mkdir (scope "playlists")))
             (touch-file (scope "database"))
             (fork+exec-command
              (list "mpd" "--no-daemon"(scope "mpd.conf"))))
   #:stop (make-kill-destructor))
 (simple-service "mpdscribble"))

;; Send shepherd into the background.
(action 'shepherd 'daemonize)

;; Start the REPL server.
;; (spawn-server (make-tcp-server-socket #:port 37148))

;; Services to start when shepherd starts:
(for-each start '(xmodmap emacs))