Skip to content

Commit

Permalink
Add IMAGE_CLEANUP setting
Browse files Browse the repository at this point in the history
Our integration tests run all possible restylers in a space-restricted
environment, which has started to become a problem[^1]. This option will
tell Restyler to remove each image after using it, thus saving space.

[^1]: https://github.com/restyled-io/restylers/actions/runs/6522631491/job/17712406781
  • Loading branch information
pbrisbin committed Oct 16, 2023
1 parent a251e29 commit 241fb0d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions restyle-path/main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ main = do
, oRestrictions = eoRestrictions
, oStatsdHost = Nothing
, oStatsdPort = Nothing
, oImageCleanup = False
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/Restyler/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ data EnvOptions = EnvOptions
, eoRestrictions :: Restrictions
, eoStatsdHost :: Maybe String
, eoStatsdPort :: Maybe Int
, eoImageCleanup :: Bool
}

data CLIOptions = CLIOptions
Expand All @@ -47,6 +48,7 @@ data Options = Options
, oRestrictions :: Restrictions
, oStatsdHost :: Maybe String
, oStatsdPort :: Maybe Int
, oImageCleanup :: Bool
}

class HasOptions env where
Expand Down Expand Up @@ -84,6 +86,7 @@ parseOptions = do
, oRestrictions = eoRestrictions
, oStatsdHost = eoStatsdHost
, oStatsdPort = eoStatsdPort
, oImageCleanup = eoImageCleanup
}

-- brittany-disable-next-binding
Expand All @@ -102,6 +105,7 @@ envParser =
<*> envRestrictions
<*> optional (Env.var Env.str "STATSD_HOST" mempty)
<*> optional (Env.var Env.auto "STATSD_PORT" mempty)
<*> Env.switch "IMAGE_CLEANUP" mempty

-- brittany-disable-next-binding

Expand Down
25 changes: 23 additions & 2 deletions src/Restyler/Restyler/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import Restyler.Restyler
import Restyler.RestylerResult
import qualified Restyler.Wiki as Wiki
import System.FilePath ((</>))
import UnliftIO.Exception (tryAny)

data RestylerExitFailure = RestylerExitFailure Restyler Int
deriving stock (Show, Eq)
Expand Down Expand Up @@ -275,7 +276,7 @@ getDockerRunStyles Restyler {..} paths = case rRunStyle of
RestylerRunStylePathOverwriteSep -> map (DockerRunPathOverwrite True) paths

dockerRunRestyler
:: ( MonadIO m
:: ( MonadUnliftIO m
, MonadLogger m
, MonadSystem m
, MonadProcess m
Expand All @@ -287,6 +288,7 @@ dockerRunRestyler
-> m ()
dockerRunRestyler r@Restyler {..} WithProgress {..} = do
cwd <- getHostDirectory
imageCleanup <- oImageCleanup <$> view optionsL
restrictions <- oRestrictions <$> view optionsL

let
Expand All @@ -299,14 +301,19 @@ dockerRunRestyler r@Restyler {..} WithProgress {..} = do
progress :: Text
progress = pack (show pIndex) <> " of " <> pack (show pTotal)

-- Our integration tests run every restyler we support in a space-restricted
-- environment. This switch triggers removal of each image after running it,
-- to avoid out-of-space errors.
withImageCleanup f = if imageCleanup then f `finally` cleanupImage else f

logInfo
$ "Restyling"
:# [ "restyler" .= rName
, "run" .= progress
, "style" .= rRunStyle
]

ec <- case pItem of
ec <- withImageCleanup $ case pItem of
DockerRunPathToStdout path -> do
(ec, out) <- readProcessExitCode "docker" (args <> [prefix path])
ec <$ writeFile path (fixNewline $ pack out)
Expand All @@ -325,6 +332,20 @@ dockerRunRestyler r@Restyler {..} WithProgress {..} = do
| "./" `isPrefixOf` p = p
| otherwise = "./" <> p

cleanupImage = do
eec <- tryAny $ callProcessExitCode "docker" ["image", "rm", "--force", rImage]
case eec of
Left ex ->
logWarn
$ "Exception removing Restyler image"
:# ["exception" .= displayException ex]
Right ExitSuccess ->
logInfo "Removed Restyler image"
Right (ExitFailure i) ->
logWarn
$ "Error removing Restyler image"
:# ["status" .= i]

fixNewline :: Text -> Text
fixNewline = (<> "\n") . T.dropWhileEnd (== '\n')

Expand Down
1 change: 1 addition & 0 deletions test/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ testOptions =
, oRestrictions = fullRestrictions
, oStatsdHost = Nothing
, oStatsdPort = Nothing
, oImageCleanup = False
}

testAppExample :: TestAppT a -> TestAppT a
Expand Down

0 comments on commit 241fb0d

Please sign in to comment.