@@ -3,19 +3,25 @@
33{-# LANGUAGE OverloadedStrings #-}
44{-# LANGUAGE ScopedTypeVariables #-}
55{-# LANGUAGE ViewPatterns #-}
6+{-# LANGUAGE DeriveGeneric #-}
67module Obelisk.Command.Deploy where
78
89import Control.Lens
910import Control.Monad
1011import Control.Monad.Catch (Exception (displayException), MonadThrow, throwM, try)
1112import Control.Monad.IO.Class (liftIO)
13+import Data.Aeson (FromJSON, ToJSON, encode, eitherDecode)
1214import Data.Bits
15+import qualified Data.ByteString.Lazy as BSL
1316import Data.Default
1417import Data.List (isSuffixOf)
18+import Data.Map (Map)
1519import qualified Data.Map as Map
1620import Data.Maybe
21+import Data.Text (Text)
1722import qualified Data.Text as T
1823import qualified Data.Text.IO as T
24+import GHC.Generics
1925import System.Directory
2026import System.Environment (getEnvironment)
2127import System.FilePath
@@ -152,6 +158,15 @@ deployPush deployPath getNixBuilders = do
152158deployUpdate :: MonadObelisk m => FilePath -> m ()
153159deployUpdate deployPath = updateThunkToLatest $ deployPath </> "src"
154160
161+keytoolToAndroidConfig :: KeytoolConfig -> Map Text NValue
162+keytoolToAndroidConfig conf = Map.fromList
163+ [ ("storeFile", NValue_Path $ _keytoolConfig_keystore conf)
164+ , ("storePassword", NValue_Text $ T.pack $ _keytoolConfig_storepass conf)
165+ , ("keyAlias", NValue_Text $ T.pack $ _keytoolConfig_alias conf)
166+ , ("keyPassword", NValue_Text $ T.pack $ _keytoolConfig_storepass conf)
167+ ]
168+
169+
155170deployMobile :: MonadObelisk m => String -> [String] -> m ()
156171deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
157172 let srcDir = root </> "src"
@@ -159,22 +174,26 @@ deployMobile platform mobileArgs = withProjectRoot "." $ \root -> do
159174 unless exists $ failWith "ob test should be run inside of a deploy directory"
160175 when (platform == "android") $ do
161176 let keystorePath = root </> "android_keystore.jks"
177+ keytoolConfPath = root </> "config/backend/androidKeyStore"
162178 hasKeystore <- liftIO $ doesFileExist keystorePath
163179 when (not hasKeystore) $ do
164180 -- TODO log instructions for how to modify the keystore
165181 putLog Notice $ "Creating keystore: " <> T.pack keystorePath
166− createKeystore root $ KeytoolConfig
167− { _keytoolConfig_keystore = keystorePath
168− , _keytoolConfig_alias = "obelisk"
169− , _keytoolConfig_storepass = "obelisk"
170− , _keytoolConfig_dname = "CN=mqttserver.ibm.com, OU=ID, O=IBM, L=Hursley, S=Hants, C=GB" -- TODO Read these from config?
171− }
172− let releaseKey = renderAttrset $ Map.fromList
173− [ ("storeFile", NValue_Path keystorePath)
174− , ("storePassword", NValue_Text "obelisk")
175− , ("keyAlias", NValue_Text "obelisk")
176− , ("keyPassword", NValue_Text "obelisk")
177− ]
182+ let keyToolConf = KeytoolConfig
183+ { _keytoolConfig_keystore = keystorePath
184+ , _keytoolConfig_alias = "obelisk"
185+ , _keytoolConfig_storepass = "obelisk"
186+ , _keytoolConfig_dname = "CN=mqttserver.ibm.com, OU=ID, O=IBM, L=Hursley, S=Hants, C=GB" -- TODO Read these from config?
187+ }
188+ createKeystore root $ keyToolConf
189+ liftIO $ BSL.writeFile keytoolConfPath $ encode keyToolConf
190+ checkKeytoolConfExist <- liftIO $ doesFileExist keytoolConfPath
191+ unless checkKeytoolConfExist $ failWith "Missing android KeytoolConfig"
192+ keytoolConfContents <- liftIO $ BSL.readFile keytoolConfPath
193+ liftIO $ putStrLn $ show keytoolConfContents
194+ releaseKey <- case eitherDecode keytoolConfContents of
195+ Left err -> failWith $ T.pack err
196+ Right conf -> return $ renderAttrset $ keytoolToAndroidConfig conf
178197 result <- nixCmd $ NixCmd_Build $ def
179198 & nixBuildConfig_outLink .~ OutLink_None
180199 & nixCmdConfig_target .~ Target
@@ -189,7 +208,10 @@ data KeytoolConfig = KeytoolConfig
189208 , _keytoolConfig_alias :: String
190209 , _keytoolConfig_storepass :: String
191210 , _keytoolConfig_dname :: String
192− } deriving (Show)
211+ } deriving (Show, Generic)
212+
213+instance FromJSON KeytoolConfig
214+instance ToJSON KeytoolConfig
193215
194216createKeystore :: MonadObelisk m => FilePath -> KeytoolConfig -> m ()
195217createKeystore root config = do