Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Added a subcommand to docker login to the default AWS ECR
Browse files Browse the repository at this point in the history
  • Loading branch information
timbod7 committed Feb 14, 2018
1 parent d4c62d2 commit d0d14b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions hx-deploy-tool.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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, (</>))
Expand All @@ -17,6 +17,7 @@ usage = do
putStrLn " hx-deploy-tool fetch-context [--retry]"
putStrLn " hx-deploy-tool unpack <releaseid> <todir>"
putStrLn " hx-deploy-tool select <releaseid>"
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)."
Expand Down Expand Up @@ -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)

0 comments on commit d0d14b1

Please sign in to comment.