@@ -9,17 +9,35 @@ deployments.
9
9
Using remote state is therefore strongly recommended for environments which
10
10
should only be instantiated once, e.g. ` production ` and ` staging ` .
11
11
12
- This page provides some guidance for configuring remote states using
13
- commonly- available backends .
12
+ This page provides guidance for configuring remote states using backends
13
+ commonly available on OpenStack deployments .
14
14
15
15
> [ !IMPORTANT]
16
16
> In the below replace ` $ENV ` with the relevant environment name.
17
17
18
18
## GitLab
19
19
20
- With the environment activated:
20
+ GitLab can be used with the [ http backend] ( https://opentofu.org/docs/language/settings/backends/http/ )
21
+ to store separate states for each environment within the GitLab project.
22
+ Access is protected by GitLab access tokens, which in the approach below are
23
+ persisted to local files. Therefore each repository checkout will need to
24
+ authenticate separately, using either a separate token or a shared token from
25
+ some external secret store.
26
+
27
+ The below is based on the [ official docs] ( https://docs.gitlab.com/user/infrastructure/iac/terraform_state/ )
28
+ but includes some missing details and is modified for common appliance workflows.
29
+
30
+ ### Initial setup
21
31
22
- 1 . Create a backend file and commit it:
32
+ 1 . Create the backend file:
33
+
34
+ ``` shell
35
+ cp environments/site/tofu/example-backends/gitlab.tf environments/$ENV /tofu
36
+ ```
37
+
38
+ 2. Modify ` environments/$ENV /tofu/gitlab.tf` file to set the default for the
39
+ project ID. This can be found by clicking the 3-dot menu at the top right of
40
+ the GitLab project page.
23
41
24
42
` ` ` terraform
25
43
# environments/$ENV/tofu/backend.tf:
@@ -28,63 +46,60 @@ With the environment activated:
28
46
}
29
47
` ` `
30
48
31
- 2. Create a personal access token with API access (note Project tokens do not
32
- appear to work):
33
-
34
- TODO: appears maybe the do with `project_$ID_bot` as the username
35
-
36
- - In GitLab, click on your user button at top left and select 'Preferences'.
37
- - Select 'Access tokens', 'Add new token'.
38
- - Optionally set an expiry date, select 'API' scope and click 'Create token'.
39
- - Copy the generated secret and set an environment variable in your terminal
40
- ```shell
41
- export TF_PASSWORD=$SECRET
42
- ```
49
+ 3. Commit it.
43
50
44
- TODO: how does this get persisted??
51
+ 4. Follow the per-checkout steps below.
45
52
46
- 3. Create this script and commit it:
53
+ # ## Per-checkout configuration
47
54
48
- ```shell
49
- # environments/$ENV/tofu/init-gitlab-backend.sh
50
- PROJECT_ID="<gitlab-project-id>"
51
- TF_USERNAME="<gitlab-username>"
52
- TF_STATE_NAME="$(basename $APPLIANCES_ENVIRONMENT_ROOT)"
53
- TF_ADDRESS="https://gitlab.com/api/v4/projects/${PROJECT_ID}/terraform/state/${TF_STATE_NAME}"
54
-
55
- tofu init \
56
- -backend-config=address=${TF_ADDRESS} \
57
- -backend-config=lock_address=${TF_ADDRESS}/lock \
58
- -backend-config=unlock_address=${TF_ADDRESS}/lock \
59
- -backend-config=username=${TF_USERNAME} \
60
- -backend-config=password=${TF_PASSWORD} \
61
- -backend-config=lock_method=POST \
62
- -backend-config=unlock_method=DELETE \
63
- -backend-config=retry_wait_min=5
64
- ```
55
+ 1. Create an access token in the GitLab UI, using either:
65
56
66
- The project id can be found by clicking the 3-dot menu at the top right of
67
- the GitLab project page.
57
+ a. If project access tokens are available, create one via
58
+ Project > Settings > Access tokens.
59
+ The token must have ` Maintainer` role and ` api` scope.
60
+
61
+ b. Otherwise create a personal access token via
62
+ User profile > Preferences > Access tokens.
63
+ The token must have ` api` scope.
64
+
65
+ Copy the generated secret and set an environment variable:
68
66
69
- 4. Run the script:
67
+ ` ` ` shell
68
+ export TF_VAR_gitlab_access_token=$secret
69
+ ` ` `
70
70
71
+ 2. If using a personal access token, set the GitLab username as an environment variable:
72
+
73
+ ` ` ` shell
74
+ export TF_VAR_gitlab_username=$your_username
75
+ ` ` `
76
+
77
+ 4. With the environment activated, initialise OpenTofu.
78
+
79
+ If no local state exists run:
80
+
71
81
` ` ` shell
72
82
cd environments/$ENV /tofu/
73
- source init-gitlab-backend.sh
83
+ tofu init
74
84
` ` `
75
-
85
+
86
+ otherwise append ` -migrate-state` to the ` init` command to attempt to copy
87
+ local state to the new backend.
88
+
76
89
OpenTofu is now configured to use GitLab to store state for this environment.
77
90
78
91
Repeat for each environment needing remote state.
79
92
80
- If the project token expires repeat the above but with the option `-reconfigure`
81
- added to the script.
82
-
83
93
> [! CAUTION]
84
94
> The GitLab credentials are [persisted](https://opentofu.org/docs/language/settings/backends/configuration/# credentials-and-sensitive-data)
85
95
> into a file ` environments/$ENV /tofu/.terraform/terraform.tfstate` and any
86
96
> plan files. These should therefore not be committed.
87
97
98
+ # ## Token expiry
99
+
100
+ If the project token expires repeat the per-checkout configuration, but using
101
+ ` opentofu init -reconfigure` instead.
102
+
88
103
# # S3
89
104
90
105
For clouds with S3-compatible object storage (e.g. Ceph with [radosgw](https://docs.ceph.com/en/latest/radosgw/))
0 commit comments