on 13f36bd help sign in
263 lines 9.3 kB history raw
1{-# LANGUAGE DeriveFunctor #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE OverloadedStrings #-}
4{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE TemplateHaskell #-}
7module Network.Docker.Types where
9import Control.Applicative
10import Control.Lens.TH
11import Data.Aeson
12import Data.Aeson.TH
13import Data.Bool
14import qualified Data.ByteString.Lazy as BS
15import Data.Default
16import qualified Data.Map as Map
17import qualified Data.Text as T
18import GHC.Generics
19import Network.Docker.Options
20import Network.Wreq.Types (Postable)
21import OpenSSL.Session (SSLContext)
22import Prelude hiding (id)
24type URL = String
25type ApiVersion = String
26type Endpoint = String
28type Tag = String
29type IP = String
30type Port = Int
31type PortType = String
33data SSL = NoSSL | SSL SSLOptions deriving Show
35data DockerClientOpts = DockerClientOpts {
36 apiVersion :: ApiVersion
37 , baseUrl :: URL
38 , ssl :: SSL
39 } deriving (Show)
42data SSLOptions = SSLOptions {
43 optionsKey :: FilePath
44 , optionsCert :: FilePath
45 } deriving Show
48data ResourceId = ResourceId { _id :: String } deriving (Show, Eq)
51data DockerImage = DockerImage
52 { _imageId :: ResourceId
53 , _imageCreatedAt :: Int
54 , _parentId :: Maybe String
55 , _repoTags :: [Tag]
56 , _size :: Int
57 , _virtualSize :: Int
58 } deriving (Show, Eq)
61data DockerVersion = DockerVersion
62 { _Version :: String
63 , _GitCommit :: String
64 , _GoVersion :: String
65 , _Arch :: String
66 , _KernelVersion :: String
67 } deriving (Show, Eq)
70-- The JSON looks likes this:
71-- "Ports":[{"IP":"0.0.0.0","PrivatePort":55555,"PublicPort":55555,"Type":"tcp"}]
73data PortMap = PortMap
74 { _ip :: IP
75 , _privatePort :: Port
76 , _publicPort :: Port
77 , _type :: PortType
78 } deriving (Show, Eq)
80data DeleteOpts = DeleteOpts
81 { removeVolumes :: Bool
82 , force :: Bool
83 }
85defaultDeleteOpts = DeleteOpts False False
88data DockerContainer = DockerContainer
89 { _containerId :: ResourceId
90 , _containerImageId :: ResourceId
91 , _command :: String
92 , _containerCreatedAt :: Int
93 , _names :: [String]
94 , _status :: String
95 , _ports :: Maybe [PortMap]
96 } deriving (Show, Eq)
99data CreateContainerOpts = CreateContainerOpts
100 { _hostname :: String
101 , _user :: String
102 , _memory :: Int
103 , _memorySwap :: Int
104 , _attachStdin :: Bool
105 , _attachStdout :: Bool
106 , _attachStderr :: Bool
107 , _portSpecs :: Maybe Object
108 , _tty :: Bool
109 , _openStdin :: Bool
110 , _stdinOnce :: Bool
111 , _env :: Maybe Object
112 , _cmd :: [String]
113 , _image :: String
114 , _volumes :: Maybe Object
115 , _volumesFrom :: Maybe Object
116 , _workingDir :: String
117 , _disableNetwork :: Bool
118 , _exposedPorts :: Maybe Object
119 } deriving (Show)
121defaultCreateOpts = CreateContainerOpts {
122 _hostname = ""
123 , _user = ""
124 , _memory = 0
125 , _memorySwap = 0
126 , _attachStdin = False
127 , _attachStdout = False
128 , _attachStderr = False
129 , _portSpecs = Nothing
130 , _tty = False
131 , _openStdin = False
132 , _stdinOnce = False
133 , _env = Nothing
134 , _cmd = []
135 , _image = "debian"
136 , _volumes = Nothing
137 , _volumesFrom = Nothing
138 , _workingDir = ""
139 , _disableNetwork = False
140 , _exposedPorts = Nothing
141 }
143instance ToJSON CreateContainerOpts where
144 toJSON (CreateContainerOpts {..}) = object
145 [ "Hostname" .= _hostname
146 , "User" .= _user
147 , "Memory" .= _memory
148 , "MemorySwap" .= _memorySwap
149 , "AttachStdin" .= _attachStdin
150 , "AttachStdout" .= _attachStdout
151 , "AttachStderr" .= _attachStderr
152 , "PortSpecs" .= _portSpecs
153 , "Tty" .= _tty
154 , "OpenStdin" .= _openStdin
155 , "StdinOnce" .= _stdinOnce
156 , "Env" .= _env
157 , "Cmd" .= _cmd
158 , "Image" .= _image
159 , "Volumes" .= _volumes
160 , "VolumesFrom" .= _volumesFrom
161 , "WrokingDir" .= _workingDir
162 , "DisableNetwork" .= _disableNetwork
163 , "ExposedPorts" .= _exposedPorts
164 ]
166-- data CreateContainerResponse = CreateContainerResponse
167-- { _createdContainerId :: String
168-- , _warnings :: Maybe [T.Text]
169-- } deriving (Show)
171data StartContainerOpts = StartContainerOpts
172 { _Binds :: [T.Text]
173 , _Links :: [T.Text]
174 , _LxcConf :: [(T.Text, T.Text)]
175 , _PortBindings :: [((Int,T.Text),Int)]
176 , _PublishAllPorts :: Bool
177 , _Privileged :: Bool
178 , _Dns :: [T.Text]
179 , _VolumesFrom :: [T.Text]
180 , _RestartPolicy :: RestartPolicy
181 } deriving (Show)
183defaultStartOpts = StartContainerOpts
184 { _Binds = []
185 , _Links = []
186 , _LxcConf = []
187 , _PortBindings = []
188 , _PublishAllPorts = False
189 , _Privileged = False
190 , _Dns = []
191 , _VolumesFrom = []
192 , _RestartPolicy = RestartNever
193 }
195instance ToJSON StartContainerOpts where
196 toJSON (StartContainerOpts {..}) = object
197 [ "Binds" .= _Binds
198 , "Links" .= _Links
199 , "LxcConf" .= _LxcConf
200 , "PortBindings" .= object (map (\((h,p),c)->T.concat [T.pack (show h),"/",p] .= [object ["HostPort" .= show c]]) _PortBindings)
201 , "PublishAllPorts" .= _PublishAllPorts
202 , "Privileged" .= _Privileged
203 , "Dns" .= _Dns
204 , "VolumesFrom" .= _VolumesFrom
205 , "RestartPolicy" .= _RestartPolicy
206 ]
208data RestartPolicy = RestartNever
209 | RestartAlways
210 | RestartOnFailure Int
211 deriving (Show)
213instance ToJSON RestartPolicy where
214 toJSON RestartNever = object ["Name" .= (""::String), "MaximumRetryCount" .= (0::Int) ]
215 toJSON RestartAlways = object ["Name" .= ("always"::String), "MaximumRetryCount" .= (0::Int) ]
216 toJSON (RestartOnFailure n) = object ["Name" .= ("on-failure"::String), "MaximumRetryCount" .= n ]
218makeClassy ''ResourceId
220-- makeLenses ''CreateContainerResponse
221makeLenses ''DockerImage
222makeLenses ''DockerContainer
223makeLenses ''CreateContainerOpts
225instance HasResourceId DockerImage where
226 resourceId = imageId
228instance FromJSON DockerImage where
229 parseJSON (Object v) =
230 DockerImage <$> ResourceId <$> (v .: "Id")
231 <*> (v .: "Created")
232 <*> (v .:? "ParentId")
233 <*> (v .: "RepoTags")
234 <*> (v .: "Size")
235 <*> (v .: "VirtualSize")
237instance FromJSON PortMap where
238 parseJSON (Object v) =
239 PortMap <$> (v .: "IP")
240 <*> (v .: "PrivatePort")
241 <*> (v .: "PublicPort")
242 <*> (v .: "Type")
244instance HasResourceId DockerContainer where
245 resourceId = containerId
247instance FromJSON DockerContainer where
248 parseJSON (Object v) =
249 DockerContainer <$> (ResourceId <$> (v .: "Id"))
250 <*> (ResourceId <$> (v .: "Id"))
251 <*> (v .: "Command")
252 <*> (v .: "Created")
253 <*> (v .: "Names")
254 <*> (v .: "Status")
255 <*> (v .:? "Ports")
257-- instance FromJSON CreateContainerResponse where
258-- parseJSON (Object v) =
259-- CreateContainerResponse <$> (v .: "Id")
260-- <*> (v .:? "warnings")
262$(deriveJSON dopts ''DockerVersion)