on 35ad365 help sign in
docker-hs/src/Docker/Types.hs
277 lines 10 kB history raw
1module Docker.Types where
3import Control.Monad (mzero)
4import Control.Monad.Except (ExceptT, runExceptT)
5import Control.Monad.Reader (ReaderT, runReaderT)
6import Data.Aeson (FromJSON, ToJSON, decode, encode,
7 genericParseJSON, genericToJSON, object,
8 parseJSON, toJSON, (.:), (.=))
9import qualified Data.Aeson as JSON
10import Data.Aeson.Types (defaultOptions, fieldLabelModifier)
11import qualified Data.ByteString.Lazy as BL
12import Data.Char (toUpper)
13import qualified Data.Map as M
14import Data.Text (Text)
15import qualified Data.Text as T
16import GHC.Generics (Generic)
17import qualified Network.HTTP.Client as HTTP
19type Endpoint = Text
20type URL = Text
21type ApiVersion = Text
22type ContainerID = Text
23type ImageID = Text
24type Timeout = Integer
26type Request = HTTP.Request
27type Response = HTTP.Response BL.ByteString
29data Signal = SIGINT | SIGKILL deriving (Eq, Show)
31data DockerClientOpts m = DockerClientOpts {
32 http :: Request -> m Response
33 , apiVer :: ApiVersion
34 , baseUrl :: URL
35 }
37type DockerT m a = ReaderT (DockerClientOpts m) (ExceptT String m) a
39runDockerT :: (Monad m) => DockerClientOpts m -> DockerT m a -> m (Either String a)
40runDockerT opts a = runExceptT $ runReaderT a opts
41-- runDockerT opts a = (runExceptT .) . flip runReaderT opts a
43data ListOpts = ListOpts { all :: Bool } deriving (Eq, Show)
45defaultClientOpts :: DockerClientOpts IO
46defaultClientOpts = DockerClientOpts {
47 http = undefined
48 , apiVer = "v1.22"
49 , baseUrl = "http://127.0.0.1:2375/"
50 }
52data DockerVersion = DockerVersion {
53 version :: Text
54 , apiVersion :: ApiVersion
55 , gitCommit :: Text
56 , goVersion :: Text
57 , os :: Text
58 , arch :: Text
59 , kernelVersion :: Text
60 , buildTime :: Text
61 } deriving (Show, Eq, Generic)
64instance ToJSON DockerVersion where
65 toJSON = genericToJSON defaultOptions {
66 fieldLabelModifier = (\(x:xs) -> toUpper x : xs)}
68instance FromJSON DockerVersion where
69 parseJSON = genericParseJSON defaultOptions {
70 fieldLabelModifier = (\(x:xs) -> toUpper x : xs)}
72data Container = Container
74type Digest = Text
75newtype Labels = Labels (M.Map Name Value) deriving (Eq, Show)
77instance FromJSON Labels where
78 parseJSON val = Labels <$> parseJSON val
80instance ToJSON Labels where
81 toJSON (Labels kvs) = object [k .= v | (k,v) <- (M.toList kvs)]
83type Tag = Text
85data Image = DockerImage {
86 imageId :: ImageID
87 , imageCreated :: Integer
88 , imageParentId :: Maybe ImageID
89 , imageRepoTags :: [Tag]
90 , imageRepoDigests :: [Digest]
91 , imageSize :: Integer
92 , imageVirtualSize :: Integer
93 , imageLabels :: Labels
94 } deriving (Show, Eq, Generic)
96dropImagePrefix = drop 5
98instance FromJSON Image where
99 parseJSON = genericParseJSON defaultOptions {
100 fieldLabelModifier = dropImagePrefix}
102data CreateOpts = CreateOpts
103data StartOpts = StartOpts
105data DeleteOpts = DeleteOpts {
106 deleteVolumes :: Bool
107 , force :: Bool
108 } deriving (Eq, Show)
110type Timestamp = Integer
111data TailLogOpt = Tail Integer | All deriving (Eq, Show)
113data LogOpts = LogOpts {
114 follow :: Bool
115 , stdout :: Bool
116 , stderr :: Bool
117 , since :: Timestamp
118 , timestamps :: Bool
119 , tail :: TailLogOpt
120 } deriving (Eq, Show)
122data VolumePermission = Rw | Ro deriving (Eq, Show, Generic)
124instance ToJSON VolumePermission
125instance FromJSON VolumePermission
127data Volume = Volume { hostSrc :: Text
128 , containerDest :: Text
129 , volumePermission :: Maybe VolumePermission
130 } deriving (Eq, Show)
132type ContainerName = Text
134data VolumeFrom = VolumeFrom ContainerName VolumePermission deriving (Eq, Show)
136instance ToJSON Volume where
137 toJSON (Volume src dest mode) = toJSON $ case mode of
138 Nothing -> T.concat[src, ":", dest]
139 Just m -> T.concat[src, ":", dest, ":", (T.pack $ show m)]
141data Link = Link Text (Maybe Text) deriving (Eq, Show)
143instance ToJSON Link where
144 toJSON (Link n1 n2) = toJSON $ case n2 of
145 Nothing -> T.concat[n1, ":", n1]
146 Just n -> T.concat[n1, ":", n]
148-- { "Type": "<driver_name>", "Config": {"key1": "val1"} }
149data LogDriverType = JsonFile | Syslog | Journald | Gelf | AwsLogs | Splunk | LoggingDisabled deriving (Eq, Show)
151data LogDriverOptions = Map Text Text deriving (Eq, Show)
153data LogDriverConfig = LogDriverConfig LogDriverType (Maybe LogDriverOptions) deriving (Eq, Show)
155-- TODO: Add container:<name|id> mode
156data NetworkMode = Bridge | Host | NetworkDisabled deriving (Eq, Show)
158data PortType = TCP | UDP deriving (Eq, Show)
160newtype NetworkInterface = NetworkInterface Text deriving (Eq, Show)
162-- { <port>/<protocol>: [{ "HostPort": "<port>" }] }
163data PortBinding = PortBinding {
164 containerPort :: Integer
165 , portType :: PortType
166 , hostPort :: [HostPort]
167 } deriving (Eq, Show)
169data HostPort = HostPort NetworkInterface Port deriving (Eq, Show)
171-- { "Name": "on-failure" , "MaximumRetryCount": 2}
172type RetryCount = Integer
173data RestartPolicy = RestartAlways | RestartUnlessStopped | RestartOnFailure RetryCount | RestartOff deriving (Eq, Show)
175data Isolation = Default | Process | Hyperv deriving (Eq, Show)
177newtype UTSMode = UTSMode Text deriving (Eq, Show)
179-- TODO: Add Tmpfs : List of tmpfs (mounts) used for the container
180-- TODO: Add UTSMode : UTS namespace to use for the container
181data HostConfig = HostConfig
182 { binds :: [Volume]
183 , containerIDFile :: FilePath
184 , logConfig :: LogDriverConfig
185 , networkMode :: NetworkMode
186 , portBindings :: [PortBinding]
187 , restartPolicy :: RestartPolicy
188 , volumeDriver :: Text
189 , volumesFrom :: [VolumeFrom]
190 , capAdd :: [Text]
191 , capDrop :: [Text]
192 , dns :: [Text]
193 , dnsOptions :: [Text]
194 , dnsSearch :: [Text]
195 , extraHosts :: [Text]
196 , groupAdd :: [Integer]
197 , ipcMode :: Text
198 , links :: [Link]
199 , oomScoreAdj :: Integer
200 , pidMode :: Text
201 , privileged :: Bool
202 , publishAllPorts :: Bool
203 , readOnlyRootfs :: Bool
204 , securityOpt :: [Text]
205 , storageOpt :: [Text]
206 , utsMode :: UTSMode
207 , shmSize :: Integer
208 , consoleSize :: Integer
209 , isolation :: Isolation
210 , resources :: ContainerResources
211 } deriving (Eq, Show, Generic)
213-- { "Name": <name>, "Soft": <soft limit>, "Hard": <hard limit> }
214-- { "Name": "nofile", "Soft": 1024, "Hard": 2048 }
215data Ulimit = Ulimit {
216 ulimitName :: Text
217 , ulimitSoft :: Integer
218 , ulimitHard :: Integer
219 } deriving (Eq, Show)
221data ContainerResources = ContainerResources {
222 cpuShares :: Integer
223 , cgroupParent :: Text
224 , blkioWeight :: Text
225 , blkioWeightDevice :: Text
226 , blkioDeviceReadBps :: Text
227 , blkioDeviceWriteBps :: Text
228 , blkioDeviceReadIOps :: Text
229 , blkioDeviceWriteIOps :: Text
230 , cpuPeriod :: Integer
231 , cpuQuota :: Integer
232 , cpusetCpus :: Integer
233 , cpusetMems :: Text
234 , devices :: [Volume]
235 , diskQuota :: Integer
236 , kernelMemory :: Integer
237 , memory :: Integer
238 , memoryReservation :: Integer
239 , memorySwap :: Integer
240 , memorySwappiness :: Integer
241 , oomKillDisable :: Bool
242 , pidsLimit :: Integer
243 , ulimits :: [Ulimit]
244 } deriving (Eq, Show)
246type Port = Integer
248type Name = Text
249type Value = Text
251data EnvVar = EnvVar Name Value deriving (Eq, Show)
253data ContainerConfig = ContainerConfig {
254 hostname :: Text
255 , domainname :: Text
256 , user :: Text
257 , attachStdin :: Bool
258 , attachStdout :: Bool
259 , attachStderr :: Bool
260 , exposedPorts :: [Port]
261 , publishService :: Text
262 , tty :: Bool
263 , openStdin :: Bool
264 , stdinOnce :: Bool
265 , env :: [EnvVar]
266 , cmd :: Text
267 , argsEscaped :: Bool
268 , image :: Text
269 , volumes :: [Volume]
270 , workingDir :: FilePath
271 , entrypoint :: Text
272 , networkDisabled :: Bool
273 , macAddress :: Text
274 , onBuild :: Text
275 , labels :: Labels
276 , stopSignal :: Signal
277 } deriving (Eq, Show)