Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to load image from docker save command #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docker.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ test-suite docker-hs-tests
, aeson
, containers
, unordered-containers
, conduit
, http-client
, http-client-tls
, http-types
Expand Down
5 changes: 5 additions & 0 deletions src/Docker/Client/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Docker.Client.Api (
, deleteImage
, buildImageFromDockerfile
, pullImage
, loadImage
-- * Network
, createNetwork
, removeNetwork
Expand Down Expand Up @@ -217,6 +218,10 @@ buildImageFromDockerfile opts base = do
pullImage :: forall m b . (MonadIO m, MonadMask m) => T.Text -> Tag -> Sink BS.ByteString m b -> DockerT m (Either DockerError b)
pullImage name tag = requestHelper' POST (CreateImageEndpoint name tag Nothing)

-- | Loads a previously saved image
loadImage :: forall m b . (MonadIO m, MonadMask m) => Bool -> FilePath -> Sink BS.ByteString m b -> DockerT m (Either DockerError b)
loadImage quiet fp = requestHelper' POST (LoadImageEndpoint quiet fp)

-- | Creates network
createNetwork :: forall m. (MonadIO m, MonadMask m) => CreateNetworkOpts -> DockerT m (Either DockerError NetworkID)
createNetwork opts = requestHelper POST (CreateNetworkEndpoint opts) >>= parseResponse
Expand Down
5 changes: 5 additions & 0 deletions src/Docker/Client/Http.hs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ statusCodeToError (BuildImageEndpoint _ _) st =
Nothing
else
Just $ DockerInvalidStatusCode st
statusCodeToError (LoadImageEndpoint _ _) st =
if st == status200 then
Nothing
else
Just $ DockerInvalidStatusCode st
statusCodeToError (CreateImageEndpoint _ _ _) st =
if st == status200 then
Nothing
Expand Down
6 changes: 5 additions & 1 deletion src/Docker/Client/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import qualified Data.Conduit.Binary as CB
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import qualified Network.HTTP.Client as HTTP
import Network.HTTP.Conduit (requestBodySourceChunked)
import Network.HTTP.Conduit (requestBodySourceChunked, RequestBody(RequestBodyBS))
import Network.HTTP.Types (Query, encodePath,
encodePathSegments)
import Prelude hiding (all)
Expand Down Expand Up @@ -75,6 +75,8 @@ getEndpoint v (CreateImageEndpoint name tag _) = encodeURLWithQuery [v, "images"
where query = [("fromImage", Just n), ("tag", Just t)]
n = encodeQ $ T.unpack name
t = encodeQ $ T.unpack tag
getEndpoint v (LoadImageEndpoint quiet _) = encodeURLWithQuery [v, "images", "load"] query
where query = [("quiet", Just $ encodeQ $ show quiet)]
getEndpoint v (DeleteImageEndpoint _ cid) = encodeURL [v, "images", fromImageID cid]
getEndpoint v (CreateNetworkEndpoint _) = encodeURL [v, "networks", "create"]
getEndpoint v (RemoveNetworkEndpoint nid) = encodeURL [v, "networks", fromNetworkID nid]
Expand All @@ -97,12 +99,14 @@ getEndpointRequestBody (InspectContainerEndpoint _) = Nothing

getEndpointRequestBody (BuildImageEndpoint _ fp) = Just $ requestBodySourceChunked $ CB.sourceFile fp
getEndpointRequestBody (CreateImageEndpoint _ _ _) = Nothing
getEndpointRequestBody (LoadImageEndpoint _ fp) = Just $ requestBodySourceChunked $ CB.sourceFile fp
getEndpointRequestBody (DeleteImageEndpoint _ _) = Nothing

getEndpointRequestBody (CreateNetworkEndpoint opts) = Just $ HTTP.RequestBodyLBS (JSON.encode opts)
getEndpointRequestBody (RemoveNetworkEndpoint _) = Nothing

getEndpointContentType :: Endpoint -> BSC.ByteString
getEndpointContentType (BuildImageEndpoint _ _) = BSC.pack "application/tar"
getEndpointContentType (LoadImageEndpoint _ _) = BSC.pack "application/tar"
getEndpointContentType _ = BSC.pack "application/json; charset=utf-8"

2 changes: 2 additions & 0 deletions src/Docker/Client/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import Data.Scientific (floatingOrInteger)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Time.Clock (UTCTime)
import qualified Data.ByteString as BS
import qualified Data.Vector as V
import GHC.Generics (Generic)
import Prelude hiding (all, tail)
Expand All @@ -136,6 +137,7 @@ data Endpoint =
| InspectContainerEndpoint ContainerID
| BuildImageEndpoint BuildOpts FilePath
| CreateImageEndpoint T.Text Tag (Maybe T.Text) -- ^ Either pull an image from docker hub or imports an image from a tarball (or URL)
| LoadImageEndpoint Bool FilePath
| DeleteImageEndpoint ImageDeleteOpts ImageID
| CreateNetworkEndpoint CreateNetworkOpts
| RemoveNetworkEndpoint NetworkID
Expand Down