From 15cceca6a25b9e5c228a6aa39fb087f8063273f6 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 15 Aug 2023 15:07:48 +0200 Subject: workspace -> my_workspace --- roles/my_workspace/README.md | 7 ++++ roles/my_workspace/defaults/main.yml | 4 +++ roles/my_workspace/tasks/main.yml | 35 ++++++++++++++++++ roles/my_workspace/tasks/project.yml | 70 ++++++++++++++++++++++++++++++++++++ roles/workspace/README.md | 8 ----- roles/workspace/defaults/main.yml | 4 --- roles/workspace/tasks/main.yml | 35 ------------------ roles/workspace/tasks/project.yml | 70 ------------------------------------ 8 files changed, 116 insertions(+), 117 deletions(-) create mode 100644 roles/my_workspace/README.md create mode 100644 roles/my_workspace/defaults/main.yml create mode 100644 roles/my_workspace/tasks/main.yml create mode 100644 roles/my_workspace/tasks/project.yml delete mode 100644 roles/workspace/README.md delete mode 100644 roles/workspace/defaults/main.yml delete mode 100644 roles/workspace/tasks/main.yml delete mode 100644 roles/workspace/tasks/project.yml diff --git a/roles/my_workspace/README.md b/roles/my_workspace/README.md new file mode 100644 index 0000000..3e87881 --- /dev/null +++ b/roles/my_workspace/README.md @@ -0,0 +1,7 @@ +This role clones a bunch of repositories to /srv/workspace; it then tries to +run either `make` or `docker-compose up -d` in each of these repositories, +which are called "projects". Not really usable by anybody else. + +This is how my web "projects" are set up; typically, a project is a repository +with a Makefile which calls docker-compose in a bunch of sub-directories. This +feels like a lame hack, but has worked pretty well so far. diff --git a/roles/my_workspace/defaults/main.yml b/roles/my_workspace/defaults/main.yml new file mode 100644 index 0000000..b517f2c --- /dev/null +++ b/roles/my_workspace/defaults/main.yml @@ -0,0 +1,4 @@ +git_name: Egor Tensin +git_email: Egor.Tensin@gmail.com + +workspace_dir: /srv/workspace diff --git a/roles/my_workspace/tasks/main.yml b/roles/my_workspace/tasks/main.yml new file mode 100644 index 0000000..179bdef --- /dev/null +++ b/roles/my_workspace/tasks/main.yml @@ -0,0 +1,35 @@ +- name: Create workspace directory + become: true + ansible.builtin.file: + path: '{{ workspace_dir }}' + state: directory + owner: '{{ ansible_user }}' + group: '{{ ansible_user }}' + mode: '750' + +- name: Install dependencies + become: true + ansible.builtin.apt: + name: + - git + # I use Makefiles a lot, makes sense to install this: + - make + - vim + +- name: Configure git + block: + - name: Set user.name + community.general.git_config: + scope: global + name: user.name + value: '{{ git_name }}' + + - name: Set user.email + community.general.git_config: + scope: global + name: user.email + value: '{{ git_email }}' + +- name: Update projects + ansible.builtin.include_tasks: project.yml + loop: '{{ workspace_projects }}' diff --git a/roles/my_workspace/tasks/project.yml b/roles/my_workspace/tasks/project.yml new file mode 100644 index 0000000..ae139f5 --- /dev/null +++ b/roles/my_workspace/tasks/project.yml @@ -0,0 +1,70 @@ +- name: Set project URL + ansible.builtin.set_fact: + project_url: '{{ item.url | default(item) }}' + +- name: Set project name + ansible.builtin.set_fact: + project_name: "{{ item.name | default(project_url | urlsplit('path') | basename | regex_replace('\\.git$', '')) }}" + +- name: Set project directory + ansible.builtin.set_fact: + project_dir: '{{ workspace_dir }}/{{ project_name }}' + +- name: 'Update repository: {{ project_name }}' + ansible.builtin.git: + accept_hostkey: true + dest: '{{ project_dir }}' + repo: '{{ project_url }}' + +- name: Check for Makefile + ansible.builtin.file: + path: '{{ project_dir }}/Makefile' + state: file + register: makefile_check + ignore_errors: true + +- name: This project uses make + become: true + when: makefile_check is succeeded + block: + - name: Run make + ansible.builtin.command: make + args: + chdir: '{{ project_dir }}' + register: make_output + when: makefile_check is succeeded + + - name: Show make output + ansible.builtin.debug: + var: make_output + +- name: Check for docker-compose.yml + ansible.builtin.file: + path: '{{ project_dir }}/docker-compose.yml' + state: file + register: docker_compose_check + ignore_errors: true + +- name: This project uses docker-compose + become: true + when: makefile_check is not succeeded and docker_compose_check is succeeded + block: + - name: docker-compose pull + ansible.builtin.command: docker-compose pull + args: + chdir: '{{ project_dir }}' + + - name: docker-compose build + ansible.builtin.command: docker-compose build --force-rm --pull -q + args: + chdir: '{{ project_dir }}' + + - name: docker-compose up + ansible.builtin.command: docker-compose up --remove-orphans -d + args: + chdir: '{{ project_dir }}' + register: docker_compose_up + + - name: Show docker-compose up output + ansible.builtin.debug: + var: docker_compose_up diff --git a/roles/workspace/README.md b/roles/workspace/README.md deleted file mode 100644 index 44af085..0000000 --- a/roles/workspace/README.md +++ /dev/null @@ -1,8 +0,0 @@ -This role is the most specialized and opinionated of all. It can be used to -clone a bunch of repositories to /srv/workspace; it then tries to run either -`make` or `docker-compose up -d` in each of these repositories, which are -called "projects". - -This is how my web "projects" are set up; typically, a project is a repository -with a Makefile which calls docker-compose in a bunch of sub-directories. This -feels like a lame hack, but has worked pretty well so far. diff --git a/roles/workspace/defaults/main.yml b/roles/workspace/defaults/main.yml deleted file mode 100644 index b517f2c..0000000 --- a/roles/workspace/defaults/main.yml +++ /dev/null @@ -1,4 +0,0 @@ -git_name: Egor Tensin -git_email: Egor.Tensin@gmail.com - -workspace_dir: /srv/workspace diff --git a/roles/workspace/tasks/main.yml b/roles/workspace/tasks/main.yml deleted file mode 100644 index 179bdef..0000000 --- a/roles/workspace/tasks/main.yml +++ /dev/null @@ -1,35 +0,0 @@ -- name: Create workspace directory - become: true - ansible.builtin.file: - path: '{{ workspace_dir }}' - state: directory - owner: '{{ ansible_user }}' - group: '{{ ansible_user }}' - mode: '750' - -- name: Install dependencies - become: true - ansible.builtin.apt: - name: - - git - # I use Makefiles a lot, makes sense to install this: - - make - - vim - -- name: Configure git - block: - - name: Set user.name - community.general.git_config: - scope: global - name: user.name - value: '{{ git_name }}' - - - name: Set user.email - community.general.git_config: - scope: global - name: user.email - value: '{{ git_email }}' - -- name: Update projects - ansible.builtin.include_tasks: project.yml - loop: '{{ workspace_projects }}' diff --git a/roles/workspace/tasks/project.yml b/roles/workspace/tasks/project.yml deleted file mode 100644 index ae139f5..0000000 --- a/roles/workspace/tasks/project.yml +++ /dev/null @@ -1,70 +0,0 @@ -- name: Set project URL - ansible.builtin.set_fact: - project_url: '{{ item.url | default(item) }}' - -- name: Set project name - ansible.builtin.set_fact: - project_name: "{{ item.name | default(project_url | urlsplit('path') | basename | regex_replace('\\.git$', '')) }}" - -- name: Set project directory - ansible.builtin.set_fact: - project_dir: '{{ workspace_dir }}/{{ project_name }}' - -- name: 'Update repository: {{ project_name }}' - ansible.builtin.git: - accept_hostkey: true - dest: '{{ project_dir }}' - repo: '{{ project_url }}' - -- name: Check for Makefile - ansible.builtin.file: - path: '{{ project_dir }}/Makefile' - state: file - register: makefile_check - ignore_errors: true - -- name: This project uses make - become: true - when: makefile_check is succeeded - block: - - name: Run make - ansible.builtin.command: make - args: - chdir: '{{ project_dir }}' - register: make_output - when: makefile_check is succeeded - - - name: Show make output - ansible.builtin.debug: - var: make_output - -- name: Check for docker-compose.yml - ansible.builtin.file: - path: '{{ project_dir }}/docker-compose.yml' - state: file - register: docker_compose_check - ignore_errors: true - -- name: This project uses docker-compose - become: true - when: makefile_check is not succeeded and docker_compose_check is succeeded - block: - - name: docker-compose pull - ansible.builtin.command: docker-compose pull - args: - chdir: '{{ project_dir }}' - - - name: docker-compose build - ansible.builtin.command: docker-compose build --force-rm --pull -q - args: - chdir: '{{ project_dir }}' - - - name: docker-compose up - ansible.builtin.command: docker-compose up --remove-orphans -d - args: - chdir: '{{ project_dir }}' - register: docker_compose_up - - - name: Show docker-compose up output - ansible.builtin.debug: - var: docker_compose_up -- cgit v1.2.3