@@ -17,7 +17,6 @@ import Data.Bits
1717import qualified Data.ByteString.Lazy as BSL
1818import Data.Default
1919import qualified Data.Map as Map
20−import Data.Maybe (listToMaybe)
2120import qualified Data.Set as Set
2221import Data.String.Here.Interpolated (i)
2322import qualified Data.Text as T
@@ -49,8 +48,6 @@ data DeployInitOpts = DeployInitOpts
4948 , _deployInitOpts_enableHttps :: Bool
5049 } deriving Show
5150
52−data DoActivateDeployment = DoActivateDeployment deriving (Eq, Ord, Show)
53−
5451deployInit :: MonadObelisk m => DeployInitOpts -> FilePath -> m ()
5552deployInit deployOpts root = do
5653 let deployDir = _deployInitOpts_outputDir deployOpts
@@ -88,13 +85,12 @@ deployInit' thunkPtr (DeployInitOpts deployDir sshKeyPath hostnames route adminE
8885 --accidentally create a production deployment that uses development
8986 --credentials to connect to some resources. This could result in, e.g.,
9087 --production data backed up to a dev environment.
91− withSpinner "Creating project configuration directories" $ do
92− callProcessAndLogOutput (Notice, Error) $
93− proc "mkdir" [ "-p"
94− , deployDir </> "config" </> "backend"
95− , deployDir </> "config" </> "common"
96− , deployDir </> "config" </> "frontend"
97− ]
88+ withSpinner "Creating project configuration directories" $ liftIO $ do
89+ mapM_ (createDirectoryIfMissing True)
90+ [ deployDir </> "config" </> "backend"
91+ , deployDir </> "config" </> "common"
92+ , deployDir </> "config" </> "frontend"
93+ ]
9894
9995 let srcDir = deployDir </> "src"
10096 withSpinner ("Creating source thunk (" <> T.pack (makeRelative deployDir srcDir) <> ")") $ do
@@ -120,28 +116,22 @@ setupObeliskImpl deployDir = liftIO $ do
120116 createDirectoryIfMissing True implDir
121117 writeFile (implDir </> "default.nix") $ "(import " <> toNixPath (goBackUp </> "src") <> " {}).obelisk"
122118
123−deployPush :: MonadObelisk m => FilePath -> Maybe DoActivateDeployment -> m [String] -> m ()
124−deployPush deployPath doActivateDeployment getNixBuilders = do
119+deployPush :: MonadObelisk m => FilePath -> m [String] -> m ()
120+deployPush deployPath getNixBuilders = do
125121 hosts <- Set.fromList . filter (/= mempty) . lines <$> readDeployConfig deployPath "backend_hosts"
126122 adminEmail <- readDeployConfig deployPath "admin_email"
127123 enableHttps <- read <$> readDeployConfig deployPath "enable_https"
128124 route <- readDeployConfig deployPath $ "config" </> "common" </> "route"
129125 routeHost <- getHostFromRoute enableHttps route
130− let
131− srcPath = deployPath </> "src"
132− handleUnpackedWith f = checkGitCleanStatus srcPath True >>= \case
133− True -> f
134− False -> failWith $ "ob deploy push: ensure " <> T.pack srcPath <> " has no pending changes and latest is pushed upstream."
135−
136− version' <- readThunk srcPath >>= \case
137− Right (ThunkData_Packed _ ptr) -> return $ Just $ show $ _thunkRev_commit $ _thunkPtr_rev ptr
138− Right ThunkData_Checkout -> handleUnpackedWith $ Just . show . _thunkRev_commit . _thunkPtr_rev <$> packThunk (ThunkPackConfig False (ThunkConfig Nothing)) srcPath
139− Left err -> do
140− putLog Warning [i|No valid thunk found at ${srcPath} due to ${err}: Attempting to find commit hash anyway.|]
141− handleUnpackedWith $ fmap show . listToMaybe . T.lines <$> readGitProcess srcPath ["rev-parse", "HEAD"]
142− version <- case version' of
143− Nothing -> failWith [i|Could not determine commit hash of ${srcPath}|]
144− Just v -> v <$ putLog Notice [i|Deploying commit ${v}|]
126+ let srcPath = deployPath </> "src"
127+ thunkPtr <- readThunk srcPath >>= \case
128+ Right (ThunkData_Packed _ ptr) -> return ptr
129+ Right ThunkData_Checkout -> do
130+ checkGitCleanStatus srcPath True >>= \case
131+ True -> packThunk (ThunkPackConfig False (ThunkConfig Nothing)) srcPath
132+ False -> failWith $ T.pack $ "ob deploy push: ensure " <> srcPath <> " has no pending changes and latest is pushed upstream."
133+ Left err -> failWith $ "ob deploy push: couldn't read src thunk: " <> T.pack (show err)
134+ let version = show . _thunkRev_commit $ _thunkPtr_rev thunkPtr
145135 builders <- getNixBuilders
146136 let moduleFile = deployPath </> "module.nix"
147137 moduleFileExists <- liftIO $ doesFileExist moduleFile
@@ -169,35 +159,33 @@ deployPush deployPath doActivateDeployment getNixBuilders = do
169159 callProcess'
170160 (Map.fromList [("NIX_SSHOPTS", unwords sshOpts)])
171161 "nix-copy-closure" ["-v", "--to", "--use-substitutes", "root@" <> host, "--gzip", outputPath]
172− case doActivateDeployment of
173− Nothing -> putLog Warning "Skipping activation of deployment; server is unchanged"
174− Just _ -> do
175− withSpinner "Uploading config" $ ifor_ buildOutputByHost $ \host _ -> do
176− callProcessAndLogOutput (Notice, Warning) $
177− proc "rsync"
178− [ "-e ssh " <> unwords sshOpts
179− , "-qarvz"
180− , deployPath </> "config"
181− , "root@" <> host <> ":/var/lib/backend"
182− ]
183− --TODO: Create GC root so we're sure our closure won't go away during this time period
184− withSpinner "Switching to new configuration" $ ifor_ buildOutputByHost $ \host outputPath -> do
185− callProcessAndLogOutput (Notice, Warning) $
186− proc "ssh" $ sshOpts <>
187− [ "root@" <> host
188− , unwords
189− [ "nix-env -p /nix/var/nix/profiles/system --set " <> outputPath
190− , "&&"
191− , "/nix/var/nix/profiles/system/bin/switch-to-configuration switch"
192− ]
162+ withSpinner "Uploading config" $ ifor_ buildOutputByHost $ \host _ -> do
163+ callProcessAndLogOutput (Notice, Warning) $
164+ proc rsyncPath
165+ [ "-e " <> sshPath <> " " <> unwords sshOpts
166+ , "-qarvz"
167+ , deployPath </> "config"
168+ , "root@" <> host <> ":/var/lib/backend"
169+ ]
170+ --TODO: Create GC root so we're sure our closure won't go away during this time period
171+ withSpinner "Switching to new configuration" $ ifor_ buildOutputByHost $ \host outputPath -> do
172+ callProcessAndLogOutput (Notice, Warning) $
173+ proc sshPath $ sshOpts <>
174+ [ "root@" <> host
175+ , unwords
176+ -- Note that we don't want to $(staticWhich "nix-env") here, because this is executing on a remote machine
177+ [ "nix-env -p /nix/var/nix/profiles/system --set " <> outputPath
178+ , "&&"
179+ , "/nix/var/nix/profiles/system/bin/switch-to-configuration switch"
193180 ]
181+ ]
194182 isClean <- checkGitCleanStatus deployPath True
195183 when (not isClean) $ do
196184 withSpinner "Committing changes to Git" $ do
197− callProcessAndLogOutput (Debug, Error) $ proc "git"
198− ["-C", deployPath, "add", "."]
199− callProcessAndLogOutput (Debug, Error) $ proc "git"
200− ["-C", deployPath, "commit", "-m", "New deployment"]
185+ callProcessAndLogOutput (Debug, Error) $
186+ gitProc deployPath ["add", "."]
187+ callProcessAndLogOutput (Debug, Error) $
188+ gitProc deployPath ["commit", "-m", "New deployment"]
201189 putLog Notice $ "Deployed => " <> T.pack route
202190 where
203191 callProcess' envMap cmd args = do
@@ -327,7 +315,7 @@ readDeployConfig deployDir fname = liftIO $ do
327315
328316verifyHostKey :: MonadObelisk m => FilePath -> FilePath -> String -> m ()
329317verifyHostKey knownHostsPath keyPath hostName =
330− callProcessAndLogOutput (Notice, Warning) $ proc "ssh" $
318+ callProcessAndLogOutput (Notice, Warning) $ proc sshPath $
331319 sshArgs knownHostsPath keyPath True <>
332320 [ "root@" <> hostName
333321 , "-o", "NumberOfPasswordPrompts=0"