forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.hs
More file actions
222 lines (204 loc) · 8.45 KB
/
Image.hs
File metadata and controls
222 lines (204 loc) · 8.45 KB
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
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
-- | This module builds Docker (OpenContainer) images.
module Stack.Image
(stageContainerImageArtifacts, createContainerImageFromStage,
imgCmdName, imgDockerCmdName, imgOptsFromMonoid)
where
import Stack.Prelude
import qualified Data.ByteString as B
import Data.Char (toLower)
import qualified Data.Map.Strict as Map
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text as T
import Path
import Path.Extra
import Path.IO
import Stack.Constants.Config
import Stack.PrettyPrint
import Stack.Types.Config
import Stack.Types.Image
import RIO.Process
-- | Stages the executables & additional content in a staging
-- directory under '.stack-work'
stageContainerImageArtifacts
:: HasEnvConfig env
=> Maybe (Path Abs Dir) -> [Text] -> RIO env ()
stageContainerImageArtifacts mProjectRoot imageNames = do
config <- view configL
forM_
(zip
[0 ..]
(filterImages
(map T.unpack imageNames)
(imgDockers $ configImage config)))
(\(idx,opts) ->
do imageDir <-
imageStagingDir (fromMaybeProjectRoot mProjectRoot) idx
liftIO (ignoringAbsence (removeDirRecur imageDir))
ensureDir imageDir
stageExesInDir opts imageDir
syncAddContentToDir opts imageDir)
-- | Builds a Docker (OpenContainer) image extending the `base` image
-- specified in the project's stack.yaml. Then new image will be
-- extended with an ENTRYPOINT specified for each `entrypoint` listed
-- in the config file.
createContainerImageFromStage
:: HasConfig env
=> Maybe (Path Abs Dir) -> [Text] -> RIO env ()
createContainerImageFromStage mProjectRoot imageNames = do
config <- view configL
forM_
(zip
[0 ..]
(filterImages
(map T.unpack imageNames)
(imgDockers $ configImage config)))
(\(idx,opts) ->
do imageDir <-
imageStagingDir (fromMaybeProjectRoot mProjectRoot) idx
createDockerImage opts imageDir
extendDockerImageWithEntrypoint opts imageDir)
filterImages :: [String] -> [ImageDockerOpts] -> [ImageDockerOpts]
filterImages [] = id -- all: no filter
filterImages names = filter (imageNameFound . imgDockerImageName)
where
imageNameFound (Just name) = name `elem` names
imageNameFound _ = False
-- | Stage all the Package executables in the usr/local/bin
-- subdirectory of a temp directory.
stageExesInDir
:: HasEnvConfig env
=> ImageDockerOpts -> Path Abs Dir -> RIO env ()
stageExesInDir opts dir = do
srcBinPath <- fmap (</> $(mkRelDir "bin")) installationRootLocal
let destBinPath = dir </> $(mkRelDir "usr/local/bin")
ensureDir destBinPath
case imgDockerExecutables opts of
Nothing -> do
logInfo ""
logInfo "Note: 'executables' not specified for a image container, so every executable in the project's local bin dir will be used."
mcontents <- liftIO $ forgivingAbsence $ listDir srcBinPath
case mcontents of
Just (files, dirs)
| not (null files) || not (null dirs) -> liftIO $ copyDirRecur srcBinPath destBinPath
_ -> prettyWarn "The project's local bin dir contains no files, so no executables will be added to the docker image."
logInfo ""
Just exes ->
forM_
exes
(\exe ->
copyFile
(srcBinPath </> exe)
(destBinPath </> exe))
-- | Add any additional files into the temp directory, respecting the
-- (Source, Destination) mapping.
syncAddContentToDir
:: HasEnvConfig env
=> ImageDockerOpts -> Path Abs Dir -> RIO env ()
syncAddContentToDir opts dir = do
root <- view projectRootL
let imgAdd = imgDockerAdd opts
forM_
(Map.toList imgAdd)
(\(source,destPath) ->
do sourcePath <- resolveDir root source
let destFullPath = dir </> dropRoot destPath
ensureDir destFullPath
liftIO $ copyDirRecur sourcePath destFullPath)
-- | Derive an image name from the project directory.
imageName
:: Path Abs Dir -> String
imageName = map toLower . toFilePathNoTrailingSep . dirname
-- | Create a general purpose docker image from the temporary
-- directory of executables & static content.
createDockerImage
:: HasConfig env
=> ImageDockerOpts -> Path Abs Dir -> RIO env ()
createDockerImage dockerConfig dir =
case imgDockerBase dockerConfig of
Nothing -> throwM StackImageDockerBaseUnspecifiedException
Just base -> do
liftIO
(B.writeFile
(toFilePath (dir </> $(mkRelFile "Dockerfile")))
(encodeUtf8 (T.pack (unlines ["FROM " ++ base, "ADD ./ /"]))))
let args =
[ "build"
, "-t"
, fromMaybe
(imageName (parent . parent . parent $ dir))
(imgDockerImageName dockerConfig)
, toFilePathNoTrailingSep dir]
withProc "docker" args runProcess_
-- | Extend the general purpose docker image with entrypoints (if specified).
extendDockerImageWithEntrypoint
:: HasConfig env
=> ImageDockerOpts -> Path Abs Dir -> RIO env ()
extendDockerImageWithEntrypoint dockerConfig dir = do
let dockerImageName =
fromMaybe
(imageName (parent . parent . parent $ dir))
(imgDockerImageName dockerConfig)
let imgEntrypoints = imgDockerEntrypoints dockerConfig
case imgEntrypoints of
Nothing -> return ()
Just eps ->
forM_
eps
(\ep ->
do liftIO
(B.writeFile
(toFilePath
(dir </> $(mkRelFile "Dockerfile")))
(encodeUtf8 (T.pack (unlines
[ "FROM " ++ dockerImageName
, "ENTRYPOINT [\"/usr/local/bin/" ++
ep ++ "\"]"
, "CMD []"]))))
withProc
"docker"
[ "build"
, "-t"
, dockerImageName ++ "-" ++ ep
, toFilePathNoTrailingSep dir]
runProcess_)
-- | Fail with friendly error if project root not set.
fromMaybeProjectRoot :: Maybe (Path Abs Dir) -> Path Abs Dir
fromMaybeProjectRoot =
fromMaybe (impureThrow StackImageCannotDetermineProjectRootException)
-- | The command name for dealing with images.
imgCmdName
:: String
imgCmdName = "image"
-- | The command name for building a docker container.
imgDockerCmdName
:: String
imgDockerCmdName = "container"
-- | Convert image opts monoid to image options.
imgOptsFromMonoid
:: ImageOptsMonoid -> ImageOpts
imgOptsFromMonoid ImageOptsMonoid{..} =
ImageOpts
{ imgDockers = imgMonoidDockers
}
-- | Stack image exceptions.
data StackImageException
= StackImageDockerBaseUnspecifiedException -- ^ Unspecified parent docker
-- container makes building
-- impossible
| StackImageCannotDetermineProjectRootException -- ^ Can't determine the
-- project root (where to
-- put image sandbox).
deriving (Typeable)
instance Exception StackImageException
instance Show StackImageException where
show StackImageDockerBaseUnspecifiedException =
"You must specify a base docker image on which to place your haskell executables."
show StackImageCannotDetermineProjectRootException =
"Stack was unable to determine the project root in order to build a container."