From d0d14b1251ceb6b5fb98b96031655dab3294c040 Mon Sep 17 00:00:00 2001 From: Tim Docker Date: Wed, 14 Feb 2018 15:44:48 +1100 Subject: [PATCH] Added a subcommand to docker login to the default AWS ECR --- hx-deploy-tool.cabal | 1 + src/Commands.hs | 28 ++++++++++++++++++++++++++++ src/Main.hs | 6 +++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/hx-deploy-tool.cabal b/hx-deploy-tool.cabal index ba84d75..83a2f22 100644 --- a/hx-deploy-tool.cabal +++ b/hx-deploy-tool.cabal @@ -28,6 +28,7 @@ executable hx-deploy-tool , amazonka >= 1.4 && < 1.5 , amazonka-core >= 1.4 && < 1.5 , amazonka-s3 >= 1.4 && < 1.5 + , amazonka-ecr >= 1.4 && < 1.5 , base64-bytestring >= 1.0 && < 1.1 , bytestring >= 0.10 && < 0.11 , conduit >= 1.2.9 && < 1.3 diff --git a/src/Commands.hs b/src/Commands.hs index e2cd051..687d153 100644 --- a/src/Commands.hs +++ b/src/Commands.hs @@ -3,12 +3,14 @@ module Commands where import qualified Data.Aeson as JS import qualified Data.ByteString.Lazy as LBS +import qualified Data.ByteString.Base64 as B64 import qualified Data.HashMap.Strict as HM import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Text.IO as T import qualified Text.Mustache as TM import qualified Text.Mustache.Types as TM +import qualified Network.AWS.ECR as ECR import ADL.Config(ToolConfig(..), DeployContextFile(..)) import ADL.Release(ReleaseConfig(..)) @@ -22,6 +24,7 @@ import Control.Monad.Trans.AWS import Control.Monad.Trans.Resource import Control.Monad.Catch import Control.Lens +import Data.Maybe(fromMaybe) import Data.Monoid import Data.Conduit(runConduit, (.|), ($$), ($$+-)) import Data.Conduit.Binary(sinkFile) @@ -111,6 +114,31 @@ select tcfg release = do rcfg <- adlFromJsonFile' "release.json" callCommand (T.unpack (rc_startCommand rcfg)) +-- Output the command line to docker login to access the default +-- repository +awsDockerLoginCmd :: ToolConfig -> IO () +awsDockerLoginCmd tcfg = do + env <- mkAwsEnv + runResourceT . runAWST env $ do + resp <- send ECR.getAuthorizationToken + case view ECR.gatrsAuthorizationData resp of + [authData] -> do + let rawtoken = fromMaybe ("error no token in authdata") (view ECR.adAuthorizationToken authData) + let rawendpoint = fromMaybe ("error no endpointing in authdata") (view ECR.adProxyEndpoint authData) + liftIO $ T.putStrLn (loginCmd rawtoken rawendpoint) + _ -> error ("Expected authdata for a single registry") + where + loginCmd :: T.Text -> T.Text -> T.Text + loginCmd rawtoken rawendpoint = "docker login -u AWS -p " <> password <> " " <> endpoint + where + password = case (T.stripPrefix "AWS:". T.decodeUtf8 . B64.decodeLenient . T.encodeUtf8) rawtoken of + Nothing -> error ("Unable to decode docker password") + (Just password) -> password + + endpoint = case T.stripPrefix "https://" rawendpoint of + Nothing -> endpoint + (Just endpoint) -> endpoint + downloadFileFromS3 :: Env -> BucketName -> ObjectKey -> FilePath -> Maybe Int -> IO () downloadFileFromS3 env bucketName objectKey toFilePath retryAfter = do handling _ServiceError onServiceError $ do diff --git a/src/Main.hs b/src/Main.hs index 5c699d5..0b17fa8 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -5,7 +5,7 @@ import qualified Data.Text as T import ADL.Config(ToolConfig(..)) import ADL.Core(adlFromJsonFile') -import Commands(fetchContext, select, unpack) +import Commands(fetchContext, select, unpack, awsDockerLoginCmd) import System.Environment(getArgs, lookupEnv, getExecutablePath) import System.Exit(exitWith,ExitCode(..)) import System.FilePath(takeDirectory, ()) @@ -17,6 +17,7 @@ usage = do putStrLn " hx-deploy-tool fetch-context [--retry]" putStrLn " hx-deploy-tool unpack " putStrLn " hx-deploy-tool select " + putStrLn " hx-deploy-tool aws-docker-login-cmd" putStrLn "" putStrLn "The config file is read from the file specified with HX_DEPLOY_CONFIG." putStrLn "It defaults to ../etc/hx-deploy-tool.json (relative to the executable)." @@ -47,6 +48,9 @@ main = do ["select", release] -> do config <- adlFromJsonFile' configPath select config (T.pack release) + ["aws-docker-login-cmd"] -> do + config <- adlFromJsonFile' configPath + awsDockerLoginCmd config _ -> do usage exitWith (ExitFailure 1)