Skip to content

Commit 238f332

Browse files
danpawlikpraveenkumar
authored andcommitted
Add deploy-crc-cloud Ansible role
The Ansible tool might handle in better way how to deploy the CRC cloud. NOTE: The Ansible role can be optimized and it would be done in next pull requests. This commit just adds same functionality as it is done in clustersetup.sh script. Signed-off-by: Daniel Pawlik <[email protected]>
1 parent 436eacc commit 238f332

27 files changed

+703
-0
lines changed

ansible/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Deploy CRC cloud directly on CRC host
2+
3+
## Introduction
4+
5+
The CRC cloud tool deploys the CRC host using its own binary, but it
6+
requires access to the cloud provider credentials. That functionality
7+
is useless when the CRC cloud needs to be used in external CI, where
8+
the crc-cloud tool can not be used - especially when the CI is responsible
9+
to check how many VMs are spawned or checking job results.
10+
11+
## CRC QCOW2 image
12+
13+
There is a simple way to bootstrap the CRC without using the crc-cloud
14+
tool and upload it to your cloud provider:
15+
16+
- download libvirt bundle (you can see url when crc is in debug mode)
17+
- extract libvirt bundle using tar command with zst:
18+
19+
```shell
20+
tar xaf crc_libvirt_$VERSION_amd64.crcbundle
21+
```
22+
23+
- upload crc.qcow2 image to your cloud provider
24+
- take the id_ecdsa_crc file located in the root directory of
25+
the extracted crcbundle archive after unpacking the crcbundle file
26+
27+
## Bootstrap crc-cloud directly on host
28+
29+
Now after spawning VM using the `crc.qcow2` image:
30+
31+
- prepare `inventory.yaml` file:
32+
33+
```shell
34+
CRC_VM_IP=<ip address>
35+
36+
cat << EOF > inventory.yaml
37+
---
38+
all:
39+
hosts:
40+
crc:
41+
ansible_port: 22
42+
ansible_host: $CRC_VM_IP
43+
ansible_user: core
44+
ansible_ssh_private_key_file: upacked_crcbundle_dir/id_ecdsa
45+
vars:
46+
alternative_domain: true
47+
pass_developer: 12345678
48+
pass_kubeadmin: 12345678
49+
pass_redhat: 12345678
50+
openshift_pull_secret: |
51+
< PULL SECRET >
52+
EOF
53+
```
54+
55+
- clone crc-cloud project
56+
57+
```shell
58+
git clone https://github.com/crc-org/crc-cloud
59+
```
60+
61+
- run playbook to bootstrap the container that later would start deploy-crc-cloud role
62+
63+
```shell
64+
ansible-playbook -i inventory.yaml crc-cloud/ansible/playbooks/bootstrap.yaml
65+
```
66+
67+
Then just wait until the bootstrap container finish :)
68+
You can follow Ansible execution steps by checking the container logs
69+
on the remote CRC host:
70+
71+
```shell
72+
sudo podman logs -f crc-cloud-bootstrap
73+
```

ansible/playbooks/bootstrap.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Prepare crc-cloud bootstrap container
3+
gather_facts: true
4+
hosts: crc
5+
roles:
6+
- crc-bootstrap

ansible/playbooks/roles

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../roles

ansible/playbooks/start.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Start crc-cloud
3+
gather_facts: true
4+
hosts: crc
5+
roles:
6+
- deploy-crc-cloud
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
bootstrap_dir: /var/tmp/crc-bootstrap
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM quay.io/centos/centos:stream9
2+
3+
RUN dnf install -y ansible-core sudo && \
4+
ansible-galaxy collection install community.general \
5+
community.crypto ansible.posix \
6+
kubernetes.core
7+
8+
9+
RUN groupadd --gid 1000 user && \
10+
useradd --uid 1000 --gid 1000 -d /home/user user
11+
COPY entrypoint.sh /home/user/entrypoint.sh
12+
RUN chown -R user:user /home/user && chmod +x /home/user/entrypoint.sh
13+
14+
USER user
15+
16+
WORKDIR /home/user
17+
ENTRYPOINT ["/home/user/entrypoint.sh"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
echo "Check if inventory.yaml exists..."
4+
if [ -f "inventory.yaml" ]; then
5+
echo "Starting Ansible..."
6+
export ANSIBLE_HOST_KEY_CHECKING=False
7+
export ANSIBLE_LOG_PATH=/home/user/ansible-logs/ansible.log
8+
ansible-playbook -i inventory.yaml crc-cloud/ansible/playbooks/start.yaml
9+
else
10+
echo "Could not find inventory file. Exit"
11+
exit 1
12+
fi
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
- name: Create bootstrap dir
3+
ansible.builtin.file:
4+
path: "{{ bootstrap_dir }}"
5+
state: directory
6+
owner: 1000
7+
group: 1000
8+
mode: '0755'
9+
10+
- name: Clone crc-cloud repository
11+
ansible.builtin.git:
12+
repo: https://github.com/crc-org/crc-cloud
13+
dest: "{{ bootstrap_dir }}/crc-cloud"
14+
version: main
15+
16+
- name: Check if bootstrap ssh key exists
17+
ansible.builtin.stat:
18+
path: "{{ bootstrap_dir }}/crc-cloud-bootstrap.pub"
19+
register: _bootstrap_ssh_key
20+
21+
- name: Generate ssh keypair
22+
when: not _bootstrap_ssh_key.stat.exists
23+
community.crypto.openssh_keypair:
24+
path: "{{ bootstrap_dir }}/crc-cloud-bootstrap"
25+
type: ed25519
26+
state: present
27+
owner: 1000
28+
group: 1000
29+
mode: '0600'
30+
31+
- name: Set SELinux context for SSH key
32+
ansible.builtin.command: chcon -t ssh_home_t "{{ bootstrap_dir }}/crc-cloud-bootstrap"
33+
when: not _bootstrap_ssh_key.stat.exists
34+
35+
- name: Get public key content
36+
ansible.builtin.command: cat "{{ bootstrap_dir }}/crc-cloud-bootstrap.pub"
37+
register: _pub_key
38+
39+
- name: Add generated key to authorized keys
40+
ansible.posix.authorized_key:
41+
user: "{{ ansible_user }}"
42+
key: "{{ _pub_key.stdout }}"
43+
path: .ssh/authorized_keys.d/ignition
44+
state: present
45+
46+
- name: Create entrypoint file
47+
ansible.builtin.copy:
48+
src: entrypoint.sh
49+
dest: "{{ bootstrap_dir }}/entrypoint.sh"
50+
owner: 1000
51+
group: 1000
52+
mode: '0755'
53+
54+
- name: Prepare Dockerfile
55+
ansible.builtin.copy:
56+
src: Dockerfile
57+
dest: "{{ bootstrap_dir }}/Dockerfile"
58+
59+
- name: Create inventory file
60+
ansible.builtin.template:
61+
src: inventory.yaml.j2
62+
dest: "{{ bootstrap_dir }}/inventory.yaml"
63+
owner: 1000
64+
group: 1000
65+
mode: '0644'
66+
67+
- name: Create Ansible log directory
68+
ansible.builtin.file:
69+
path: "{{ bootstrap_dir }}/{{ item }}"
70+
state: directory
71+
owner: 1000
72+
group: 1000
73+
mode: '0755'
74+
loop:
75+
- ansible-logs
76+
- .kube
77+
78+
- name: Set SELinux context for directories
79+
ansible.builtin.command: >
80+
chcon -R -t container_file_t "{{ bootstrap_dir }}/{{ item }}"
81+
loop:
82+
- ansible-logs
83+
- .kube
84+
85+
- name: Build bootstrap container
86+
become: true
87+
ansible.builtin.shell: |
88+
podman build -t crc-cloud-bootstrap -f {{ bootstrap_dir }}/Dockerfile
89+
90+
- name: Create container to bootstrap
91+
become: true
92+
ansible.builtin.shell: >
93+
podman create --name crc-cloud-bootstrap
94+
--network host
95+
-v "{{ bootstrap_dir }}/crc-cloud-bootstrap:/home/user/.ssh/id_ed25519:Z"
96+
-v "{{ bootstrap_dir }}/inventory.yaml:/home/user/inventory.yaml:Z"
97+
-v "{{ bootstrap_dir }}/crc-cloud:/home/user/crc-cloud:Z"
98+
-v "{{ bootstrap_dir }}/ansible-logs:/home/user/ansible-logs:Z"
99+
-v "{{ bootstrap_dir }}/.kube:/home/user/.kube:Z"
100+
crc-cloud-bootstrap /home/user/entrypoint.sh
101+
102+
- name: Start the container
103+
become: true
104+
ansible.builtin.shell: |
105+
podman start crc-cloud-bootstrap
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
all:
3+
hosts:
4+
crc:
5+
ansible_port: 22
6+
ansible_host: {{ ansible_default_ipv4.address }}
7+
ansible_user: core
8+
ansible_ssh_private_key_file: /home/user/.ssh/id_ed25519
9+
vars:
10+
openshift_pull_secret: |
11+
{{ openshift_pull_secret }}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
dnsmasq_conf_path: /etc/dnsmasq.d/crc-dnsmasq.conf
3+
openshift_pull_secret: ""
4+
eip: crc.dev
5+
alternative_domain: nip.io
6+
7+
# wait for resource
8+
max_retry: 20
9+
wait_interval: 5
10+
11+
# wait cluster become healthy
12+
max_retries: 20
13+
retry_delay: 5
14+
15+
pass_developer: _PASS_DEVELOPER_
16+
pass_kubeadmin: _PASS_KUBEADMIN_
17+
pass_redhat: _PASS_REDHAT_
18+
19+
users:
20+
- name: developer
21+
password: "{{ pass_developer }}"
22+
- name: kubeadmin
23+
password: "{{ pass_kubeadmin }}"
24+
- name: redhat
25+
password: "{{ pass_redhat }}"
26+
27+
# replace default ca
28+
ca_user: "system:admin"
29+
ca_group: "system:masters"
30+
ca_user_subj: "/O=${GROUP}/CN=${USER}"
31+
ca_name: "custom"
32+
ca_subj: "/OU=openshift/CN=admin-kubeconfig-signer-custom"
33+
ca_validity: 3650
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
# https://github.com/crc-org/crc-cloud/blob/main/pkg/bundle/setup/clustersetup.sh#L282
3+
- name: Get route to console custom
4+
ansible.builtin.shell: |
5+
oc get route console-custom -n openshift-console
6+
register: _route_console_custom
7+
until: _route_console_custom.rc != 1
8+
retries: 60
9+
delay: 10
10+
changed_when: false
11+
12+
- name: Get console route
13+
ansible.builtin.shell: >
14+
oc get route console-custom
15+
-n openshift-console
16+
-o json | jq -r '.spec.host'
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
# https://github.com/crc-org/crc-cloud/blob/main/pkg/bundle/setup/clustersetup.sh#L185
3+
- name: Create alternative cert
4+
ansible.builtin.shell: >
5+
openssl req
6+
-newkey rsa:2048
7+
-new -nodes
8+
-x509
9+
-days 3650
10+
-keyout nip.key
11+
-out nip.crt
12+
-subj "/CN={{ eip }}.{{ alternative_domain }}"
13+
-addext "subjectAltName=DNS:apps.{{ eip }}.{{ alternative_domain }},DNS:*.apps.{{ eip }}.{{ alternative_domain }},DNS:api.{{ eip }}.{{ alternative_domain }}"
14+
15+
- name: "Create secret for {{ alternative_domain }}"
16+
ansible.builtin.command: >
17+
oc create secret tls nip-secret
18+
--cert=nip.crt
19+
--key=nip.key
20+
-n openshift-config
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
# From https://github.com/crc-org/crc-cloud/blob/main/pkg/bundle/setup/clustersetup.sh#L101
3+
- name: Create crc-dnsmasq.conf
4+
become: true
5+
ansible.builtin.copy:
6+
content: |
7+
listen-address={{ ansible_default_ipv4.address }}
8+
expand-hosts
9+
log-queries
10+
local=/crc.testing/
11+
domain=crc.testing
12+
address=/apps-crc.testing/{{ ansible_default_ipv4.address }}
13+
address=/api.crc.testing/{{ ansible_default_ipv4.address }}
14+
address=/api-int.crc.testing/{{ ansible_default_ipv4.address }}
15+
address=/{{ ansible_fqdn }}.crc.testing/192.168.126.11
16+
dest: "{{ dnsmasq_conf_path }}"
17+
register: _dnsmasq_conf
18+
19+
- name: Set this host as first nameserver in /etc/resolv.conf
20+
become: true
21+
ansible.builtin.lineinfile:
22+
path: /etc/resolv.conf
23+
regexp: '^# Generated by NetworkManager'
24+
line: "nameserver {{ item }}"
25+
create: true
26+
loop: "{{ [ansible_default_ipv4.address] + ansible_facts['dns']['nameservers'] | flatten }}"
27+
register: _etc_resolv
28+
29+
- name: Disable overwriting /etc/resolv.conf by the NetworkManager
30+
become: true
31+
ansible.builtin.copy:
32+
content: |
33+
[main]
34+
dns=none
35+
dest: /etc/NetworkManager/conf.d/00-custom-crc.conf
36+
register: _disable_dns_overwrite
37+
38+
- name: Restart NetworkManager when its needed
39+
when: _disable_dns_overwrite.changed
40+
become: true
41+
ansible.builtin.systemd:
42+
name: NetworkManager
43+
state: restarted
44+
45+
- name: Restart dnsmasq
46+
when: _etc_resolv.changed
47+
become: true
48+
ansible.builtin.systemd:
49+
name: dnsmasq
50+
state: restarted
51+
enabled: true
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
- name: Create temporary directory
3+
ansible.builtin.tempfile:
4+
state: directory
5+
register: _temp_dir
6+
7+
- name: Create Dockerfile
8+
ansible.builtin.copy:
9+
content: |
10+
FROM quay.io/centos/centos:stream9-minimal
11+
RUN microdnf --setopt=tsflags=nodocs --setopt=install_weak_deps=0 install -y httpd-tools
12+
ENTRYPOINT ["htpasswd", "-Bbn"]
13+
dest: "{{ _temp_dir.path }}/Dockerfile"
14+
15+
- name: Build container image for htpasswd
16+
ansible.builtin.command: |
17+
podman build -t localhost/htpasswd:latest -f {{ _temp_dir.path }}/Dockerfile
18+
19+
- name: "Get htpasswd for {{ user.name }}"
20+
ansible.builtin.shell: |
21+
podman run --rm -ti localhost/htpasswd:latest {{ user.name }} {{ user.password }} >> htpasswd.txt
22+
23+
- name: Remove temporary directory
24+
ansible.builtin.file:
25+
path: "{{ _temp_dir.path }}"
26+
state: absent

0 commit comments

Comments
 (0)