]>
Commit | Line | Data |
---|---|---|
1 | #!/bin/bash | |
2 | ||
3 | set -euxo pipefail | |
4 | ||
5 | RemoteRepo="gitolite@git.immae.eu:perso/Immae/Prive/Password_store/Sites" | |
6 | DeploymentUuid="cef694f3-081d-11e9-b31f-0242ec186adf" | |
7 | ||
8 | if ! which nix 2>/dev/null >/dev/null; then | |
9 | cat <<-EOF | |
10 | nix is needed, please install it: | |
11 | > curl https://nixos.org/nix/install | sh | |
12 | (or any other way handled by your distribution) | |
13 | EOF | |
14 | exit 1 | |
15 | fi | |
16 | ||
17 | if [ "${NIX_STORE:-/nix/store}" != "/nix/store" ]; then | |
18 | cat <<-EOF | |
19 | Nix store outside of /nix/store is not supported | |
20 | EOF | |
21 | exit 1 | |
22 | fi | |
23 | ||
24 | if [ -z "$NIXOPS_CONFIG_PASS_SUBTREE_REMOTE" \ | |
25 | -o -z "$NIXOPS_CONFIG_PASS_SUBTREE_PATH" ]; then | |
26 | cat <<-EOF | |
27 | Two environment variables are needed to setup the password store: | |
28 | NIXOPS_CONFIG_PASS_SUBTREE_PATH : path where the subtree will be imported | |
29 | NIXOPS_CONFIG_PASS_SUBTREE_REMOTE : remote name to give to the repository | |
30 | EOF | |
31 | exit 1 | |
32 | fi | |
33 | ||
34 | if ! pass $NIXOPS_CONFIG_PASS_SUBTREE_PATH > /dev/null 2>/dev/null; then | |
35 | cat <<-EOF | |
36 | /!\ This will modify your password store to add and import a subtree | |
37 | with the specific passwords files. Choose a path that doesn’t exist | |
38 | yet in your password store. | |
39 | > pass git remote add $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE $RemoteRepo | |
40 | > pass git subtree add --prefix=$NIXOPS_CONFIG_PASS_SUBTREE_PATH $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE master | |
41 | Later, you can use pull_environment and push_environment scripts to | |
42 | update the passwords when needed | |
43 | Continue? [y/N] | |
44 | EOF | |
45 | read y | |
46 | if [ "$y" = "y" -o "$y" = "Y" ]; then | |
47 | pass git remote add $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE $RemoteRepo | |
48 | pass git subtree add --prefix=$NIXOPS_CONFIG_PASS_SUBTREE_PATH $NIXOPS_CONFIG_PASS_SUBTREE_REMOTE master | |
49 | else | |
50 | echo "Aborting" | |
51 | exit 1 | |
52 | fi | |
53 | fi | |
54 | ||
55 | nix_group=$(stat -c %G /nix/store) | |
56 | if [ "$nix_group" = "nixbld" ]; then | |
57 | nix_user="nixbld1" | |
58 | else | |
59 | nix_user="$(stat -c %U /nix/store)" | |
60 | fi | |
61 | ||
62 | if [ ! -f /etc/ssh/ssh_rsa_key_nixops ]; then | |
63 | cat <<-EOF | |
64 | The key to access private git repositories (websites hosted by the | |
65 | server) needs to be accessible to nix builders. It will be put in | |
66 | /etc/ssh/ssh_rsa_key_nixops (sudo right is needed for that) | |
67 | > pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey | sudo tee /etc/ssh/ssh_rsa_key_nixops > /dev/null | |
68 | > pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey.pub | sudo tee /etc/ssh/ssh_rsa_key_nixops.pub > /dev/null | |
69 | > sudo chmod u=r,go-rwx /etc/ssh/ssh_rsa_key_nixops | |
70 | > sudo chown $nix_user:$nix_group /etc/ssh/ssh_rsa_key_nixops /etc/ssh/ssh_rsa_key_nixops.pub | |
71 | Continue? [y/N] | |
72 | EOF | |
73 | read y | |
74 | if [ "$y" = "y" -o "$y" = "Y" ]; then | |
75 | if ! id -u $nix_user 2>/dev/null >/dev/null; then | |
76 | echo "User $nix_user seems inexistant, did you install nix?" | |
77 | exit 1 | |
78 | fi | |
79 | mask=$(umask) | |
80 | umask 0777 | |
81 | # Don’t forward it directly to tee, it would break ncurse pinentry | |
82 | key=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey) | |
83 | echo "$key" | sudo tee /etc/ssh/ssh_rsa_key_nixops > /dev/null | |
84 | sudo chmod u=r,go=- /etc/ssh/ssh_rsa_key_nixops | |
85 | pubkey=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/SshKey.pub) | |
86 | echo "$pubkey" | sudo tee /etc/ssh/ssh_rsa_key_nixops.pub > /dev/null | |
87 | sudo chmod a=r /etc/ssh/ssh_rsa_key_nixops.pub | |
88 | sudo chown $nix_user:$nix_group /etc/ssh/ssh_rsa_key_nixops /etc/ssh/ssh_rsa_key_nixops.pub | |
89 | umask $mask | |
90 | else | |
91 | echo "Aborting" | |
92 | exit 1 | |
93 | fi | |
94 | fi | |
95 | ||
96 | if ! which nixops 2>/dev/null >/dev/null; then | |
97 | cat <<-EOF | |
98 | nixops is needed: | |
99 | > nix-env -i nixops | |
100 | If it fails, please check that $HOME/.nix-profile/bin is in your PATH. | |
101 | Continue? [y/N] | |
102 | EOF | |
103 | read y | |
104 | if [ "$y" = "y" -o "$y" = "Y" ]; then | |
105 | nix-env -i nixops | |
106 | if ! which nixops 2>/dev/null >/dev/null; then | |
107 | echo "Installation failed, please check that $HOME/.nix-profile/bin is in your path." | |
108 | exit 1 | |
109 | fi | |
110 | else | |
111 | echo "Aborting" | |
112 | exit 1 | |
113 | fi | |
114 | fi | |
115 | ||
116 | DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | |
117 | export NIXOPS_STATE="$(dirname $DIR)/state/eldiron.nixops" | |
118 | export NIXOPS_DEPLOYMENT="$DeploymentUuid" | |
119 | ||
120 | if ! nixops info 2>/dev/null >/dev/null; then | |
121 | cat <<-EOF | |
122 | Importing deployment file into nixops: | |
123 | Continue? [y/N] | |
124 | EOF | |
125 | read y | |
126 | if [ "$y" = "y" -o "$y" = "Y" ]; then | |
127 | deployment=$(pass show $NIXOPS_CONFIG_PASS_SUBTREE_PATH/Nixops/Deployment) | |
128 | echo "$deployment" | nixops import | |
129 | ||
130 | nixops modify "$(dirname $DIR)/eldiron.nix" | |
131 | else | |
132 | echo "Aborting" | |
133 | exit 1 | |
134 | fi | |
135 | fi | |
136 | ||
137 | cat <<-EOF | |
138 | All set up. | |
139 | Please make sure you’re using scripts/nixops_wrap when deploying | |
140 | EOF |