Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024.11 #4

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Vagrant.configure(2) do |config|
config.vm.define 'controller' , autostart: true, primary: true do |controller|
controller.vm.box = "almalinux/8"
controller.vm.network "private_network", ip: "192.168.56.3"
controller.vm.network :forwarded_port, host: 2223, guest: 22
controller.vm.hostname = "controller"
controller.vm.provider :virtualbox do |virtualbox|
virtualbox.customize ["modifyvm", :id,
Expand Down
8 changes: 8 additions & 0 deletions files/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
collections:
- name: ansible.posix
- name: ansible.utils
- name: community.crypto
- name: community.docker
- name: community.general
- name: community.postgresql
5 changes: 5 additions & 0 deletions roles/api/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ backup: false
# Easier debugging
debug: false
desired_state: present
# Create an SSH key to download private repos from GitHub
my_github_key: ~/.ssh/github_key
controller_repository_name: controller
controller_repository_git_url: 'https://github.com/playingfield/controller'
controller_repository_git_branch: main
40 changes: 37 additions & 3 deletions roles/api/tasks/credentials.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
status_code: 200
register: "semaphore_keystores"


- name: "Create SSH key for ansible"
- name: "Create SSH key for Controller"
changed_when: "semaphore_key_ansible_created.status == 204"
ansible.builtin.uri:
url: "{{ semaphore_api_url }}/project/{{ semaphore_project_id }}/keys"
Expand All @@ -44,4 +43,39 @@
status_code: 204
register: semaphore_key_ansible_created
when:
- "semaphore_keystores.json | selectattr('name', 'equalto', 'ansible-ssh-key') | length == 0"
- "semaphore_keystores.json | selectattr('name', 'equalto', 'Controller-ssh-key') | length == 0"

- name: "Semaphore | Read ssh key from system"
delegate_to: localhost
connection: local
become: false
no_log: "{{ not debug }}"
ansible.builtin.slurp:
path: "{{ my_github_key }}"
register: "ssh_key_github"

- name: "Create SSH key for GitHub"
changed_when: "semaphore_key_github_created.status == 204"
ansible.builtin.uri:
use_proxy: false
url: "{{ semaphore_api_url }}/project/{{ semaphore_project_id }}/keys"
method: POST
headers:
Cookie: "{{ cookie }}"
body: >-
{
"project_id": {{ semaphore_project_id | int }},
"name": "github-ssh-key",
"type": "ssh",
"ssh":
{
"private_key": {{ ssh_key_github.content | b64decode | to_json }},
"passphrase": "{{ ssh_passphrase | default('') }}"
}
}
body_format: json
status_code: 204
timeout: 5
register: semaphore_key_github_created
when:
- "semaphore_keystores.json | selectattr('name', 'equalto', 'github-ssh-key') | length == 0"
7 changes: 4 additions & 3 deletions roles/api/tasks/repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
changed_when: false
check_mode: false
ansible.builtin.uri:
url: "{{ semaphore_api_url }}/project/{{ semaphore_project_id }}/repositories"
url: "{{ semaphore_api_url }}/project/{{ semaphore_project_id }}/repositories?sort=name&order=asc"
method: GET
headers:
Cookie: "{{ cookie }}"
Expand All @@ -21,16 +21,17 @@
# project_id must be integer
body: >-
{
"project_id": {{ semaphore_project_id | int }},
"name": "{{ controller_repository_name }}",
"project_id": {{ semaphore_project_id | int }},
"git_url": "{{ controller_repository_git_url }}",
"git_branch": "{{ controller_repository_git_branch }}",
"ssh_key_id": 0
}
body_format: json
status_code: 204
register: semaphore_repository_created
when:
(semaphore_repositories.status == "404") or ("semaphore_repositories.json | selectattr('name', 'equalto', controller_repository_name) | length == 0")
(semaphore_repositories.json | length == 0) or ("semaphore_repositories.json | selectattr('name', 'equalto', controller_repository_name) | length == 0")

- name: "Determine Repositories (after creation)"
changed_when: false
Expand Down
18 changes: 1 addition & 17 deletions roles/semaphore/tasks/present.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
- "{{ semaphore_config_dir }}"
- /home/semaphore/tmp

- name: Copy requirements
- name: Copy requirements for ExecStartPre
ansible.builtin.copy:
src: "{{ item }}"
dest: /home/semaphore/
Expand All @@ -110,22 +110,6 @@
- requirements.txt
- requirements.yml

- name: Install requirements for semaphore user
become: true
become_user: semaphore
ansible.builtin.pip:
requirements: '/home/semaphore/requirements.txt'
executable: /usr/bin/pip3.12
extra_args: '--user'
state: present

- name: Install collections
become: true
become_user: semaphore
ansible.builtin.command:
cmd: ansible-galaxy collection install -r /home/semaphore/requirements.yml
changed_when: true

- name: Deploy Semaphore Config
become: true
ansible.builtin.template:
Expand Down
13 changes: 12 additions & 1 deletion roles/semaphore/templates/semaphore.service.j2
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
[Unit]
Description=Semaphore UI
Documentation=https://github.com/semaphoreui/semaphore
Requires=network.target
After=network.target syslog.target
Documentation=https://github.com/semaphoreui/semaphore
ConditionPathExists=/usr/bin/semaphore
ConditionPathExists=/etc/semaphore/config.json

[Service]
User=semaphore
Group=semaphore
Environment=LANG="en_US.UTF-8"
Environment=LC_ALL="en_US.UTF-8"
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:~/.local/bin"
ExecStart=/usr/bin/semaphore server --config /etc/semaphore/config.json
ExecStop=/bin/kill -s QUIT $MAINPID
ExecReload=/bin/kill -s HUP $MAINPID
Restart=always
RestartSec=25s
WorkingDirectory=/home/semaphore
StandardOutput=journal
StandardError=journal
SyslogIdentifier=semaphore

ExecStartPre=/bin/bash -c 'pip3.12 install --upgrade --user -r /home/semaphore/requirements.txt'
ExecStartPre=/bin/bash -c 'ansible-galaxy collection install --upgrade -r /home/semaphore/requirements.yml'

[Install]
WantedBy=multi-user.target
Loading