summaryrefslogtreecommitdiff
path: root/Omni/Deploy/Caddy.hs
blob: de73a357ac14043f5cef3a1327eab96015676b9b (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | Caddy admin API integration for the mini-PaaS deployment system.
--
-- : out deploy-caddy
-- : dep aeson
-- : dep http-conduit
-- : dep http-types
module Omni.Deploy.Caddy
  ( buildRoute,
    getCurrentRoutes,
    upsertRoute,
    deleteRoute,
    syncRoutes,
    getRouteById,
    caddyAdmin,
    main,
    test,
  )
where

import Alpha
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Network.HTTP.Simple as HTTP
import qualified Network.HTTP.Types.Status as Status
import Omni.Deploy.Manifest (Artifact (..), Exec (..), Hardening (..), Http (..), Service (..), Systemd (..))
import qualified Omni.Test as Test

caddyAdmin :: Text
caddyAdmin = "http://localhost:2019"

data Route = Route
  { routeId :: Text,
    routeMatch :: [RouteMatch],
    routeHandle :: [RouteHandler],
    routeTerminal :: Bool
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON Route where
  toJSON Route {..} =
    Aeson.object
      [ "@id" .= routeId,
        "match" .= routeMatch,
        "handle" .= routeHandle,
        "terminal" .= routeTerminal
      ]

data RouteMatch = RouteMatch
  { matchHost :: [Text]
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON RouteMatch where
  toJSON RouteMatch {..} =
    Aeson.object ["host" .= matchHost]

data RouteHandler = RouteHandler
  { handlerType :: Text,
    handlerUpstreams :: [Upstream]
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON RouteHandler where
  toJSON RouteHandler {..} =
    Aeson.object
      [ "handler" .= handlerType,
        "upstreams" .= handlerUpstreams
      ]

data Upstream = Upstream
  { upstreamDial :: Text
  }
  deriving (Show, Eq, Generic)

instance Aeson.ToJSON Upstream where
  toJSON Upstream {..} =
    Aeson.object ["dial" .= upstreamDial]

buildRoute :: Service -> Maybe Route
buildRoute Service {..} = case serviceHttp of
  Nothing -> Nothing
  Just Http {..} ->
    Just
      <| Route
        { routeId = "biz-" <> serviceName,
          routeMatch = [RouteMatch [httpDomain]],
          routeHandle =
            [ RouteHandler
                "reverse_proxy"
                [Upstream <| "localhost:" <> tshow httpInternalPort]
            ],
          routeTerminal = True
        }

getCurrentRoutes :: Text -> IO [Aeson.Value]
getCurrentRoutes adminUrl = do
  let url = Text.unpack adminUrl <> "/config/apps/http/servers/srv0/routes"
  request <- HTTP.parseRequest url
  result <- try @SomeException <| HTTP.httpLBS request
  case result of
    Left _ -> pure []
    Right response ->
      if Status.statusIsSuccessful (HTTP.getResponseStatus response)
        then case Aeson.decode (HTTP.getResponseBody response) of
          Just routes -> pure routes
          Nothing -> pure []
        else pure []

upsertRoute :: Text -> Service -> IO Bool
upsertRoute adminUrl svc = case buildRoute svc of
  Nothing -> pure False
  Just route -> do
    let routeId' = "biz-" <> serviceName svc
        patchUrl = Text.unpack adminUrl <> "/id/" <> Text.unpack routeId'
        postUrl = Text.unpack adminUrl <> "/config/apps/http/servers/srv0/routes"
        body = Aeson.encode route

    patchRequest <-
      HTTP.parseRequest patchUrl
        /> HTTP.setRequestMethod "PATCH"
        /> HTTP.setRequestBodyLBS body
        /> HTTP.setRequestHeader "Content-Type" ["application/json"]
    patchResult <- try @SomeException <| HTTP.httpLBS patchRequest

    case patchResult of
      Right resp
        | Status.statusIsSuccessful (HTTP.getResponseStatus resp) ->
            pure True
      _ -> do
        postRequest <-
          HTTP.parseRequest postUrl
            /> HTTP.setRequestMethod "POST"
            /> HTTP.setRequestBodyLBS body
            /> HTTP.setRequestHeader "Content-Type" ["application/json"]
        postResult <- try @SomeException <| HTTP.httpLBS postRequest
        case postResult of
          Right resp -> pure <| Status.statusIsSuccessful (HTTP.getResponseStatus resp)
          Left _ -> pure False

deleteRoute :: Text -> Text -> IO Bool
deleteRoute adminUrl serviceName' = do
  let routeId' = "biz-" <> serviceName'
      url = Text.unpack adminUrl <> "/id/" <> Text.unpack routeId'
  request <-
    HTTP.parseRequest url
      /> HTTP.setRequestMethod "DELETE"
  result <- try @SomeException <| HTTP.httpLBS request
  case result of
    Right resp -> pure <| Status.statusIsSuccessful (HTTP.getResponseStatus resp)
    Left _ -> pure False

syncRoutes :: Text -> [Service] -> IO (Map Text Bool)
syncRoutes adminUrl services = do
  results <-
    forM services <| \svc ->
      case serviceHttp svc of
        Nothing -> pure Nothing
        Just _ -> do
          success <- upsertRoute adminUrl svc
          pure <| Just (serviceName svc, success)
  pure <| Map.fromList <| catMaybes results

getRouteById :: Text -> Text -> IO (Maybe Aeson.Value)
getRouteById adminUrl routeId' = do
  let url = Text.unpack adminUrl <> "/id/" <> Text.unpack routeId'
  request <- HTTP.parseRequest url
  result <- try @SomeException <| HTTP.httpLBS request
  case result of
    Right resp
      | Status.statusIsSuccessful (HTTP.getResponseStatus resp) ->
          pure <| Aeson.decode (HTTP.getResponseBody resp)
    _ -> pure Nothing

test :: Test.Tree
test =
  Test.group
    "Omni.Deploy.Caddy"
    [ test_buildRouteWithHttp,
      test_buildRouteWithoutHttp,
      test_buildRouteWithPath
    ]

mkTestService :: Text -> Text -> Maybe Http -> Service
mkTestService name path http =
  Service
    { serviceName = name,
      serviceArtifact = Artifact "nix-closure" path,
      serviceHosts = ["biz"],
      serviceExec = Exec Nothing "root" "root",
      serviceEnv = mempty,
      serviceEnvFile = Nothing,
      serviceHttp = http,
      serviceSystemd = Systemd ["network-online.target"] [] "on-failure" 5,
      serviceHardening = Hardening False True "strict" True,
      serviceRevision = Nothing
    }

test_buildRouteWithHttp :: Test.Tree
test_buildRouteWithHttp =
  Test.unit "builds route for service with HTTP" <| do
    let svc = mkTestService "test-svc" "/nix/store/abc" (Just <| Http "example.com" "/" 8000)
    case buildRoute svc of
      Nothing -> Test.assertFailure "expected route"
      Just route -> do
        routeId route Test.@=? "biz-test-svc"
        case (head <| routeMatch route, head <| routeHandle route) of
          (Just m, Just h) -> do
            matchHost m Test.@=? ["example.com"]
            case head <| handlerUpstreams h of
              Just u -> upstreamDial u Test.@=? "localhost:8000"
              Nothing -> Test.assertFailure "no upstreams"
          _ -> Test.assertFailure "no match/handle"

test_buildRouteWithoutHttp :: Test.Tree
test_buildRouteWithoutHttp =
  Test.unit "returns Nothing for service without HTTP" <| do
    let svc = mkTestService "worker" "/nix/store/xyz" Nothing
    case buildRoute svc of
      Nothing -> pure ()
      Just _ -> Test.assertFailure "expected Nothing"

test_buildRouteWithPath :: Test.Tree
test_buildRouteWithPath =
  Test.unit "builds route with custom path" <| do
    let svc = mkTestService "api" "/nix/store/abc" (Just <| Http "api.example.com" "/v1" 8080)
    case buildRoute svc of
      Nothing -> Test.assertFailure "expected route"
      Just route -> case head <| routeMatch route of
        Nothing -> Test.assertFailure "no match"
        Just m -> matchHost m Test.@=? ["api.example.com"]

main :: IO ()
main = Test.run test