summaryrefslogtreecommitdiff
path: root/src/Propellor/DotDir.hs
blob: 6c2feb831fec90808e4a973f252a70c7848091c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
module Propellor.DotDir
	( distrepo
	, dotPropellor
	, interactiveInit
	, checkRepoUpToDate
	) where

import Propellor.Message
import Propellor.Bootstrap
import Propellor.Git
import Propellor.Gpg
import Propellor.Types.Result
import Utility.UserInfo
import Utility.Monad
import Utility.Process
import Utility.SafeCommand
import Utility.Exception
import Utility.Directory
import Utility.Path
-- This module is autogenerated by the build system.
import qualified Paths_propellor as Package

import Data.Char
import Data.List
import Data.Version
import Control.Monad
import Control.Monad.IfElse
import System.FilePath
import System.Posix.Directory
import System.IO
import System.Console.Concurrent
import Control.Applicative
import Prelude

distdir :: FilePath
distdir = "/usr/src/propellor"

-- A distribution may include a bundle of propellor's git repository here.
-- If not, it will be pulled from the network when needed.
distrepo :: FilePath
distrepo = distdir </> "propellor.git"

-- File containing the head rev of the distrepo.
disthead :: FilePath
disthead = distdir </> "head"

upstreambranch :: String
upstreambranch = "upstream/master"

-- Using the joeyh.name mirror of the main propellor repo because
-- it is accessible over https for better security.
netrepo :: String
netrepo = "https://git.joeyh.name/git/propellor.git"

dotPropellor :: IO FilePath
dotPropellor = do
	home <- myHomeDir
	return (home </> ".propellor")

-- Detect if propellor was built using stack. This is somewhat of a hack.
buildSystem :: IO String
buildSystem = do
	d <- Package.getLibDir
	return $ if "stack-work" `isInfixOf` d then "stack" else "cabal"

interactiveInit :: IO ()
interactiveInit = ifM (doesDirectoryExist =<< dotPropellor)
	( error "~/.propellor/ already exists, not doing anything"
	, do
		welcomeBanner
		setup
	)

-- | Determine whether we need to create a cabal sandbox in ~/.propellor/,
-- which we do if the user has configured cabal to require a sandbox, and the
-- build system is cabal.
cabalSandboxRequired :: IO Bool
cabalSandboxRequired = ifM cabal
	( do
		home <- myHomeDir
		ls <- lines <$> catchDefaultIO []
			(readFile (home </> ".cabal" </> "config"))
		-- For simplicity, we assume a sane ~/.cabal/config here:
		return $ any ("True" `isInfixOf`) $
			filter ("require-sandbox:" `isPrefixOf`) ls
	, return False
	)
  where
	cabal = buildSystem >>= \bSystem -> return (bSystem == "cabal")

say :: String -> IO ()
say = outputConcurrent

sayLn :: String -> IO ()
sayLn s = say (s ++ "\n")

welcomeBanner :: IO ()
welcomeBanner = say $ unlines $ map prettify
	[ ""
	, ""
	, "                                 _         ______`|                     ,-.__"
	, " .---------------------------  /   ~___-=O`/|O`/__|                    (____.'"
	, "  - Welcome to              -- ~          / | /    )        _.-'-._"
	, "  -            Propellor!   --  `/-==__ _/__|/__=-|        (       ~_"
	, " `---------------------------   *             ~ | |         '--------'"
	, "                                            (o)  `"
	, ""
	, ""
	]
  where
	prettify = map (replace '~' '\\')
	replace x y c
		| c == x = y
		| otherwise = c

prompt :: String -> [(String, IO ())] -> IO ()
prompt p cs = do
	say (p ++ " [" ++ intercalate "|" (map fst cs) ++ "] ")
	flushConcurrentOutput
	hFlush stdout
	r <- map toLower <$> getLine
	if null r
		then snd (head cs) -- default to first choice on return
		else case filter (\(s, _) -> map toLower s == r) cs of
			[(_, a)] -> a
			_ -> do
				sayLn "Not a valid choice, try again.. (Or ctrl-c to quit)"
				prompt p cs

section :: IO ()
section = do
	sayLn ""
	sayLn "------------------------------------------------------------------------------"
	sayLn ""

setup :: IO ()
setup = do
	sayLn "Propellor's configuration file is ~/.propellor/config.hs"
	sayLn ""
	sayLn "Let's get you started with a simple config that you can adapt"
	sayLn "to your needs. You can start with:"
	sayLn "   A: A clone of propellor's git repository    (most flexible)"
	sayLn "   B: The bare minimum files to use propellor  (most simple)"
	prompt "Which would you prefer?"
		[ ("A", void $ actionMessage "Cloning propellor's git repository" fullClone)
		, ("B", void $ actionMessage "Creating minimal config" minimalConfig)
		]
	changeWorkingDirectory =<< dotPropellor

	section
	sayLn "Let's try building the propellor configuration, to make sure it will work..."
	sayLn ""
	b <- buildSystem
	void $ boolSystem "git"
		[ Param "config"
		, Param "propellor.buildsystem"
		, Param b
		]
	ifM cabalSandboxRequired
		( void $ boolSystem "cabal"
			[ Param "sandbox"
			, Param "init"
			]
		, return ()
		)
	buildPropellor Nothing
	sayLn ""
	sayLn "Great! Propellor is bootstrapped."

	section
	sayLn "Propellor can use gpg to encrypt private data about the systems it manages,"
	sayLn "and to sign git commits."
	gpg <- getGpgBin
	ifM (inPath gpg)
		( setupGpgKey
		, do
			sayLn "You don't seem to have gpg installed, so skipping setting it up."
			explainManualSetupGpgKey
		)

	section
	sayLn "Everything is set up ..."
	sayLn "Your next step is to edit ~/.propellor/config.hs"
	sayLn "and run propellor again to try it out."
	sayLn ""
	sayLn "For docs, see https://propellor.branchable.com/"
	sayLn "Enjoy propellor!"

explainManualSetupGpgKey :: IO ()
explainManualSetupGpgKey = do
	sayLn "Propellor can still be used without gpg, but it won't be able to"
	sayLn "manage private data. You can set this up later:"
	sayLn " 1. gpg --gen-key"
	sayLn " 2. propellor --add-key (pass it the key ID generated in step 1)"

setupGpgKey :: IO ()
setupGpgKey = do
	ks <- listSecretKeys
	sayLn ""
	case ks of
		[] -> makeGpgKey
		[(k, d)] -> do
			sayLn $ "You have one gpg key: " ++ desckey k d
			prompt "Should propellor use that key?"
				[ ("Y", propellorAddKey k)
				, ("N", sayLn $ "Skipping gpg setup. If you change your mind, run: propellor --add-key " ++ k)
				]
		_ -> do
			let nks = zip ks (map show ([1..] :: [Integer]))
			sayLn "I see you have several gpg keys:"
			forM_ nks $ \((k, d), n) ->
				sayLn $ "   " ++ n ++ "   " ++ desckey k d
			prompt "Which of your gpg keys should propellor use?"
				(map (\((k, _), n) -> (n, propellorAddKey k)) nks)
  where
	desckey k d = d ++ "  (keyid " ++ k ++ ")"

makeGpgKey :: IO ()
makeGpgKey = do
	sayLn "You seem to not have any gpg secret keys."
	prompt "Would you like to create one now?"
		[("Y", rungpg), ("N", nope)]
  where
	nope = do
		sayLn "No problem."
		explainManualSetupGpgKey
	rungpg = do
		sayLn "Running gpg --gen-key ..."
		gpg <- getGpgBin
		void $ boolSystem gpg [Param "--gen-key"]
		ks <- listSecretKeys
		case ks of
			[] -> do
				sayLn "Hmm, gpg seemed to not set up a secret key."
				prompt "Want to try running gpg again?"
					[("Y", rungpg), ("N", nope)]
			((k, _):_) -> propellorAddKey k

propellorAddKey :: String -> IO ()
propellorAddKey keyid = do
	sayLn ""
	sayLn $ "Telling propellor to use your gpg key by running: propellor --add-key " ++ keyid
	d <- dotPropellor
	unlessM (boolSystem (d </> "propellor") [Param "--add-key", Param keyid]) $ do
		sayLn "Oops, that didn't work! You can retry the same command later."
		sayLn "Continuing onward ..."

minimalConfig :: IO Result
minimalConfig = do
	d <- dotPropellor
	createDirectoryIfMissing True d
	changeWorkingDirectory d
	void $ boolSystem "git" [Param "init"]
	addfile "config.cabal" cabalcontent
	addfile "config.hs" configcontent
	addfile "stack.yaml" stackcontent
	return MadeChange
  where
	addfile f content = do
		writeFile f (unlines content)
		void $ boolSystem "git" [Param "add" , File f]
	cabalcontent =
		[ "-- This is a cabal file to use to build your propellor configuration."
		, ""
		, "Name: config"
		, "Cabal-Version: >= 1.6"
		, "Build-Type: Simple"
		, "Version: 0"
		, ""
		, "Executable propellor-config"
		, "  Main-Is: config.hs"
		, "  GHC-Options: -threaded -Wall -fno-warn-tabs -O0"
		, "  Extensions: TypeOperators"
		, "  Build-Depends: propellor >= 3.0, base >= 4.9"
		]
	configcontent =
		[ "-- This is the main configuration file for Propellor, and is used to build"
		, "-- the propellor program.    https://propellor.branchable.com/"
		, ""
		, "import Propellor"
		, "import qualified Propellor.Property.File as File"
		, "import qualified Propellor.Property.Apt as Apt"
		, "import qualified Propellor.Property.Cron as Cron"
		, "import qualified Propellor.Property.User as User"
		, ""
		, "main :: IO ()"
		, "main = defaultMain hosts"
		, ""
		, "-- The hosts propellor knows about."
		, "hosts :: [Host]"
		, "hosts ="
		, "        [ mybox"
		, "        ]"
		, ""
		, "-- An example host."
		, "mybox :: Host"
		, "mybox = host \"mybox.example.com\" $ props"
		, "        & osDebian Unstable X86_64"
		, "        & Apt.stdSourcesList"
		, "        & Apt.unattendedUpgrades"
		, "        & Apt.installed [\"etckeeper\"]"
		, "        & Apt.installed [\"ssh\"]"
		, "        & User.hasSomePassword (User \"root\")"
		, "        & File.dirExists \"/var/www\""
		, "        & Cron.runPropellor (Cron.Times \"30 * * * *\")"
		, ""
		]
	stackcontent =
		-- This should be the same resolver version in propellor's
		-- own stack.yaml
		[ "resolver: " ++ stackResolver
		, "packages:"
		, "- '.'"
		, "extra-deps:"
		, "- propellor-" ++ showVersion Package.version
		]

stackResolver :: String
stackResolver = "lts-9.21"

fullClone :: IO Result
fullClone = do
	d <- dotPropellor
	let enterdotpropellor = changeWorkingDirectory d >> return True
	ok <- ifM (doesFileExist distrepo <||> doesDirectoryExist distrepo)
		( allM id
			[ boolSystem "git" [Param "clone", File distrepo, File d]
			, fetchUpstreamBranch distrepo
			, enterdotpropellor
			, boolSystem "git" [Param "remote", Param "rm", Param "origin"]
			]
		, allM id
			[ boolSystem "git" [Param "clone", Param netrepo, File d]
			, enterdotpropellor
			-- Rename origin to upstream and avoid
			-- git push to that read-only repo.
			, boolSystem "git" [Param "remote", Param "rename", Param "origin", Param "upstream"]
			, boolSystem "git" [Param "config", Param "--unset", Param "branch.master.remote", Param "upstream"]
			]
		)
	return (toResult ok)

fetchUpstreamBranch :: FilePath -> IO Bool
fetchUpstreamBranch repo = do
	changeWorkingDirectory =<< dotPropellor
	boolSystem "git"
		[ Param "fetch"
		, File repo
		, Param ("+refs/heads/master:refs/remotes/" ++ upstreambranch)
		, Param "--quiet"
		]

checkRepoUpToDate :: IO ()
checkRepoUpToDate = whenM (gitbundleavail <&&> dotpropellorpopulated) $ do
	headrev <- takeWhile (/= '\n') <$> readFile disthead
	changeWorkingDirectory =<< dotPropellor
	headknown <- catchMaybeIO $
		withQuietOutput createProcessSuccess $
			proc "git" ["log", headrev]
	if (headknown == Nothing)
		then updateUpstreamMaster headrev
		else do
			theirhead <- getCurrentGitSha1 =<< getCurrentBranchRef
			when (theirhead /= headrev) $ do
				merged <- not . null <$>
					readProcess "git" ["log", headrev ++ "..HEAD", "--ancestry-path"]
				unless merged $
					warnoutofdate True
  where
	gitbundleavail = doesFileExist disthead
	dotpropellorpopulated = do
		d <- dotPropellor
		doesFileExist (d </> "propellor.cabal")

-- Updates upstream/master in dotPropellor so merging from it will update
-- to the latest distrepo.
--
-- We cannot just fetch the distrepo because the distrepo contains only 
-- 1 commit. So, trying to merge with it will result in lots of merge
-- conflicts, since git cannot find a common parent commit.
--
-- Instead, the new upstream/master branch is updated by taking the
-- current upstream/master branch (which must be an old version of propellor,
-- as distributed), and diffing from it to the current origin/master,
-- and committing the result. This is done in a temporary clone of the
-- repository, giving it a new master branch. That new branch is fetched
-- into the user's repository, as if fetching from a upstream remote,
-- yielding a new upstream/master branch.
--
-- If there's no upstream/master, or the repo is not using the distrepo,
-- do nothing.
updateUpstreamMaster :: String -> IO ()
updateUpstreamMaster newref = do
	changeWorkingDirectory =<< dotPropellor
	go =<< getoldref
  where
	go Nothing = return ()
	go (Just oldref) = do
		let tmprepo = ".git/propellordisttmp"
		let cleantmprepo = void $ catchMaybeIO $ removeDirectoryRecursive tmprepo
		cleantmprepo
		git ["clone", "--quiet", ".", tmprepo]

		changeWorkingDirectory tmprepo
		git ["fetch", distrepo, "--quiet"]
		git ["reset", "--hard", oldref, "--quiet"]
		v <- gitVersion
		let mergeparams =
			[ "merge", newref
			, "-s", "recursive"
			, "-Xtheirs"
			, "--quiet"
			, "-m", "merging upstream version"
			] ++ if v >= [2,9]
				then [ "--allow-unrelated-histories" ]
				else []
		git mergeparams

		void $ fetchUpstreamBranch tmprepo
		cleantmprepo
		warnoutofdate True

	git = run "git"
	run cmd ps = unlessM (boolSystem cmd (map Param ps)) $
		error $ "Failed to run " ++ cmd ++ " " ++ show ps

	-- Get ref that the upstreambranch points to, only when
	-- the distrepo is being used.
	getoldref = do
		mref <- catchMaybeIO $ takeWhile (/= '\n')
			<$> readProcess "git" ["show-ref", upstreambranch, "--hash"]
		case mref of
			Just _ -> do
				-- Normally there will be no upstream
				-- remote when the distrepo is used.
				-- Older versions of propellor set up
				-- an upstream remote pointing at the 
				-- distrepo.
				ifM (hasRemote "upstream")
					( do
						v <- remoteUrl "upstream"
						return $ case v of
							Just rurl | rurl == distrepo -> mref
							_ -> Nothing
					, return mref
					)
			Nothing -> return mref

warnoutofdate :: Bool -> IO ()
warnoutofdate havebranch = warningMessage $ unlines
	[ "** Your ~/.propellor/ is out of date.."
	, indent "A newer upstream version is available in " ++ distrepo
	, indent $ if havebranch
		then "To merge it, run: git merge " ++ upstreambranch
		else "To merge it, find the most recent commit in your repository's history that corresponds to an upstream release of propellor, and set refs/remotes/" ++ upstreambranch ++ " to it. Then run propellor again."
	]
  where
	indent s = "   " ++ s