Browse Source

* Clean debug tasks

* Correct idempotence of docker push taskk
* WIP: Add nginx-user test
tags/v1.2.0^2^2
Lyz 1 year ago
parent
commit
4cde67254b
Signed by: Lyz <lyz@riseup.net> GPG Key ID: 6C7D7C1612CDE02F
4 changed files with 32 additions and 34 deletions
  1. 7
    1
      README.md
  2. 4
    3
      molecule/nginx-user/playbook.yml
  3. 8
    4
      molecule/nginx-user/tests/test_default.py
  4. 13
    26
      tasks/main.yml

+ 7
- 1
README.md View File

@@ -10,6 +10,7 @@ Requirements
10 10
 
11 11
 * Pip installed on host
12 12
 * Docker installed on the host
13
+/home/jm/projects/ansible.roles/deploy/docker/deploy-docker
13 14
 * Docker installed on the device that is running ansible
14 15
 * A registry where you can log in
15 16
 
@@ -92,9 +93,14 @@ enough to test the other case, please make a new molecule scenery. Both in this
92 93
 role and in the ui-registry one.
93 94
 
94 95
 ```bash
95
-molecule test
96
+molecule test --all
96 97
 ```
97 98
 
99
+Right now we've got two sceneries:
100
+* *default*: Tests the creation of a service with root user
101
+* *nginx-user*: Tests the creation of a service with a user specified in the var
102
+  `docker_user`
103
+
98 104
 License
99 105
 -------
100 106
 

+ 4
- 3
molecule/nginx-user/playbook.yml View File

@@ -17,7 +17,7 @@
17 17
     - role: docker
18 18
 
19 19
 - name: '[Pretask] Configure docker-registry on /etc/hosts'
20
-  hosts: deploy-docker-registry-instance
20
+  hosts: all
21 21
   tasks:
22 22
     - name: Configure docker-registry on /etc/hosts
23 23
       become: True
@@ -27,7 +27,7 @@
27 27
         line: '127.0.0.1  docker-registry'
28 28
 
29 29
 - name: '[Pretask] Configure docker basic authentication'
30
-  hosts: deploy-docker-registry-instance
30
+  hosts: all
31 31
   tasks:
32 32
     - name: Create registry/auth directory
33 33
       become: True
@@ -43,7 +43,7 @@
43 43
 
44 44
 
45 45
 - name: '[Pretask] Install registry on the registry instance'
46
-  hosts: deploy-docker-registry-instance
46
+  hosts: all
47 47
   vars:
48 48
     docker_command: /usr/bin/docker run -i --name {{ registry_service_name }} -p 127.0.0.1:5000:5000 --rm -v {{ registry_data }}:/var/lib/registry -v {{ registry_auth }}:/var/lib/auth -v {{ registry_config }}/:/etc/docker/registry -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/var/lib/auth/htpasswd registry:2
49 49
   roles:
@@ -84,6 +84,7 @@
84 84
     docker_registry_read: "{{ docker_registry }}"
85 85
     registry_user: testuser
86 86
     registry_password: testpassword
87
+    docker_user: nginx
87 88
     docker_image_tag: latest
88 89
     docker_command: /usr/bin/docker run --rm -i --name "{{ service_name }}" -p 8081:80 "{{ docker_registry_read }}/{{ service_name }}"
89 90
   roles:

+ 8
- 4
molecule/nginx-user/tests/test_default.py View File

@@ -23,8 +23,12 @@ def test_required_pip_packages_exist(host, pip_package):
23 23
     assert pip_package in pip_packages
24 24
 
25 25
 
26
+def test_user_nginx_exist(host, pip_package):
27
+    assert host.user('nginx').name == 'nginx'
28
+
26 29
 # Test remotely build from image
27 30
 
31
+
28 32
 @pytest.mark.parametrize("directories", [
29 33
     ("/root/docker/"),
30 34
     ("/root/docker/image-nginx/data"),
@@ -34,8 +38,8 @@ def test_required_directories_exist(host, directories):
34 38
     with host.sudo():
35 39
         directory = host.file(directories)
36 40
         assert directory.exists
37
-        assert directory.user == 'root'
38
-        assert directory.group == 'root'
41
+        assert directory.user == 'nginx'
42
+        assert directory.group == 'nginx'
39 43
         assert oct(directory.mode) == '0700'
40 44
 
41 45
 
@@ -71,8 +75,8 @@ def test_required_directories_exist_for_git_build(host, directories):
71 75
     with host.sudo():
72 76
         directory = host.file(directories)
73 77
         assert directory.exists
74
-        assert directory.user == 'root'
75
-        assert directory.group == 'root'
78
+        assert directory.user == 'nginx'
79
+        assert directory.group == 'nginx'
76 80
         assert oct(directory.mode) == '0700'
77 81
 
78 82
 

+ 13
- 26
tasks/main.yml View File

@@ -1,10 +1,20 @@
1 1
 ---
2
+- name: Create docker service group
3
+  become: True
4
+  group:
5
+    name: "{{ docker_group }}"
6
+    state: present
7
+  when:
8
+    - docker_group != 'root'
9
+
2 10
 - name: Create docker service user
3 11
   become: True
4 12
   user:
5 13
     name: "{{ docker_user }}"
6
-    groups: "{{ docker_group }}"
7
-  when: docker_user != 'root'
14
+    group: "{{ docker_group }}"
15
+  when:
16
+    - docker_user != 'root'
17
+    - docker_group != 'root'
8 18
 
9 19
 - name: Create docker data directories
10 20
   become: True
@@ -112,30 +122,6 @@
112 122
   changed_when: false
113 123
   failed_when: false
114 124
 
115
-- name: Debug 0
116
-  debug:
117
-    var: image_exists
118
-- name: Debug 1
119
-  debug:
120
-    var: image_exists
121
-  when:
122
-  - image_exists.rc != '0'
123
-- name: Debug 2
124
-  debug:
125
-    var: image_exists
126
-  when:
127
-  - image_exists.rc != 0
128
-- name: Debug 3
129
-  debug:
130
-    var: image_exists
131
-  when:
132
-  - not image_exists.rc == 0
133
-- name: Debug 4
134
-  debug:
135
-    var: image_exists
136
-  when:
137
-  - not image_exists.rc == '0'
138
-
139 125
 - name: Clone git repository remotely
140 126
   git:
141 127
     repo: "{{ git_repository }}"
@@ -179,6 +165,7 @@
179 165
     push: yes
180 166
   when:
181 167
     - docker_image is not defined
168
+    - image_exists.rc != 0
182 169
     - remote_build == True
183 170
 
184 171
 - name: Download image remotely from public registry

Loading…
Cancel
Save