Repositories setup role

Role for setting up repositories for running host or
injecting to the image.

Change-Id: Ifd546febb34efa11330ea375697554533c7cf611
This commit is contained in:
Sagi Shnaidman 2016-12-07 16:03:45 +01:00
parent 656336b916
commit fda3b1f08f
7 changed files with 282 additions and 0 deletions

6
playbooks/repo-setup.yml Normal file
View File

@ -0,0 +1,6 @@
---
- name: Setup repositories
hosts: undercloud
gather_facts: yes
roles:
- repo-setup

159
roles/repo-setup/README.md Normal file
View File

@ -0,0 +1,159 @@
tripleo-repo-setup
=========
Set up yum repositories on host or in image
This Ansible role allows setting up repositories on a live host or within an image.
Requirements
------------
`libguestfs-tools-c` package is required in case of injecting repositories into an image.
Role Variables
--------------
* `repo_setup_script` - path to repositories setup script template
* `repo_setup_log` - path to repositories setup script log
* `repo_run_live`: false/true - where to run repo setup script on host (live host that playbook runs on it) (default: true)
* `repo_inject_image_path` - path to image, in case of injecting repositories into the image (default: not defined)
* `repo_cmd_before`: false/true - whether to run `yum update` after repositories are set up (default: true)
* `repo_cmd_after`: false/true - whether to include Ceph repositories (default: true)
* `libvirt_uri` - URI of libvirt in case of using virt-customize to inject repos into the image
* `repos` - dictionary or repositories to set, the keys are explained below:
* `repos.type` - file / generic / package / rpm_url
* `repos.releases` - for which releases to set up this repo, if not defined - for all releases.
It support shortcut for all stable releases - '{{ stable }}'
*File*
------
Just download the repo file from URL and change some of its parameters if required.
* `repos.filename` - filename for saving the downloaded file
* `repos.down_url` - URL to download repo file from
* `repos.priority` - change priority of this repo (default: not defined)
*Generic*
------
Construct repository file from various parameters and use parameters from downloaded file
if required (for example delorean hash).
* `repos.filename` - filename for saving the resulting repo (mandatory)
* `repos.reponame` - name of repository (mandatory)
* `repos.baseurl` - base URL of the repository (mandatory)
* `repos.hash_url` - URL of repo file in network, used for extracting trunk hash (optional)
* `repos.priority` - priority of resulting repo (optional)
* `repos.includepkgs` - includepkgs parameter of resulting repo (use this repo only for these packages) (optional)
* `repos.enabled` - 0/1 whether the repo is enabled or not (default: 1 - enabled)
* `repos.gpgcheck` - whether to check GPG keys for repo (default: 0 - don't check)
*Package*
------
Install repository from package
* `repos.pkg_name` - package name (should be available in the installed repositories)
* `repos.custom_cmd` - custom command to install this package (default: 'yum install -y')
* `repos.pkg_url` - direct URL of the package to install
Dependencies
------------
No dependencies
Example Playbook
----------------
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
---
- name: Run repo setup
hosts: undercloud
gather_facts: no
roles:
- repo-setup
Settings example for repositories:
repos:
# Just download file
- type: file
filename: delorean.repo
down_url: https://trunk.rdoproject.org/centos7-{{ release }}/current/delorean.repo
# In case of stable release
- type: generic
reponame: delorean
filename: delorean.repo
baseurl: https://trunk.rdoproject.org/centos7-{{ release }}/current/
hash_url: https://trunk.rdoproject.org/centos7-{{ release }}/current/delorean.repo
priority: 20
releases: "{{ stable }}"
# In case of master
- type: generic
reponame: delorean
filename: delorean.repo
baseurl: http://buildlogs.centos.org/centos/7/cloud/x86_64/rdo-trunk-master-tripleo/
hash_url: http://buildlogs.centos.org/centos/7/cloud/x86_64/rdo-trunk-master-tripleo/delorean.repo
priority: 20
releases:
- master
# In case of master
- type: generic
reponame: delorean-current
filename: delorean-current.repo
baseurl: http://trunk.rdoproject.org/centos7/current
hash_url: http://trunk.rdoproject.org/centos7/current/delorean.repo
priority: 10
includepkgs:
- diskimage-builder
- instack
- instack-undercloud
- os-apply-config
- os-cloud-config
- os-collect-config
- os-net-config
- os-refresh-config
- python-tripleoclient
- openstack-tripleo-common
- openstack-tripleo-heat-templates
- openstack-tripleo-image-elements
- openstack-tripleo
- openstack-tripleo-puppet-elements
- openstack-puppet-modules
- openstack-tripleo-ui
- puppet-*
releases:
- master
# In case of all releases
- type: file
filename: delorean-deps.repo
down_url: http://trunk.rdoproject.org/centos7-{{ release }}/delorean-deps.repo
priority: 30
- type: package
pkg_name: centos-release-ceph-hammer
custom_cmd: 'yum install -y --enablerepo=extras'
releases:
- liberty
- mitaka
- type: package
pkg_name: centos-release-ceph-jewel
custom_cmd: 'yum install -y --enablerepo=extras'
releases:
- newton
- master
- type: package
pkg_url: https://rdoproject.org/repos/openstack-{{ release }}/rdo-release-{{ release }}.rpm
releases:
- newton
License
-------
Apache 2.0
Author Information
------------------
RDO-CI Team

View File

@ -0,0 +1,29 @@
working_dir: /home/stack
repo_setup_script: repo_setup.sh.j2
repo_setup_log: "{{ working_dir }}/repo_setup.log"
repo_run_live: true
libvirt_uri: qemu:///session
# Releases shortcuts
stable:
- newton
- mitaka
- liberty
repo_cmd_before: |
yum install -y yum-plugin-priorities;
yum clean all;
rm -rf /etc/yum.repos.d/delorean*;
rm -rf /etc/yum.repos.d/*.rpmsave;
repo_cmd_after: |
yum repolist;
yum update -y
# Repositories definitions
repos:
- type: file
filename: delorean.repo
down_url: https://trunk.rdoproject.org/centos7-{{ release }}/current/delorean.repo
- type: file
filename: delorean-deps.repo
down_url: http://trunk.rdoproject.org/centos7-{{ release }}/delorean-deps.repo

View File

@ -0,0 +1,18 @@
- environment:
LIBGUESTFS_BACKEND: direct
LIBVIRT_DEFAULT_URI: "{{ libvirt_uri }}"
block:
- name: Check if the image exists
stat:
path: '{{ repo_inject_image_path }}'
register: image_stat
failed_when: not image_stat.stat.exists
- name: Install libguestfs-tools-c for virt-customize
yum: name=libguestfs-tools-c state=latest
- name: Inject the repositories into the image
command: >
virt-customize -a {{ repo_inject_image_path }}
--run '{{ working_dir }}/repo_setup.sh'
when: image_stat.stat.exists

View File

@ -0,0 +1,11 @@
- name: Create repo setup script
template:
src: "{{ repo_setup_script }}"
dest: "{{ working_dir }}/repo_setup.sh"
mode: 0755
- include: setup_repos.yml
when: repo_run_live|bool
- include: inject_repos_into_image.yml
when: repo_inject_image_path is defined

View File

@ -0,0 +1,5 @@
- name: Setup repos on live host
shell: >
{{ working_dir }}/repo_setup.sh >
{{ repo_setup_log }} 2>&1
become: true

View File

@ -0,0 +1,54 @@
#!/bin/bash
set -eux
{{ repo_cmd_before|default('') }}
# Make temp dir for files
rm -rf /tmp/repo_role_tmp && mkdir -p /tmp/repo_role_tmp
{% for repo in repos %}
{% if repo.releases is not defined or release in repo.releases %}
{% if repo.type == 'file' %}
curl -Lvo /tmp/repo_role_tmp/{{ repo.filename }} {{ repo.down_url }}
{% if repo.priority is defined and repo.priority %}
if ! grep -q "priority" /tmp/repo_role_tmp/{{ repo.filename }}; then
sed -i "s/\(baseurl=.*\)/\1\npriority={{ repo.priority }}/g" /tmp/repo_role_tmp/{{ repo.filename }}
else
sed -i "s/priority=.*/priority={{ repo.priority }}/g" /tmp/repo_role_tmp/{{ repo.filename }}
fi
{% endif %}
cp -f /tmp/repo_role_tmp/{{ repo.filename }} /etc/yum.repos.d/
{% endif %}
{% if repo.type == 'generic' %}
{% if repo.hash_url is defined %}
HASH=$(curl {{ repo.hash_url }} 2>/dev/null | grep baseurl | grep -Eo '[^/]*/[^/]*/[^/]*_.*' ||:)
{% endif %}
cat<<EOF >/tmp/repo_role_tmp/{{ repo.filename }}
[{{ repo.reponame }}]
name={{ repo.reponame }}
{% if repo.baseurl is defined %}baseurl={{ repo.baseurl }}
{% endif %}
{% if repo.mirrorlist is defined %}mirrorlist={{ repo.mirrorlist }}
{% endif %}
gpgcheck={{ repo.gpgcheck|default('0') }}
enabled={{ repo.enabled|default('1') }}
{% if repo.priority is defined %}priority={{ repo.priority }}
{% endif %}
{% if repo.includepkgs is defined %}includepkgs={{ repo.includepkgs|join(",") }}
{% endif %}
{% if repo.down_url is defined %}# trunk_hash=${HASH:-}
{% endif %}
EOF
cp -f /tmp/repo_role_tmp/{{ repo.filename }} /etc/yum.repos.d/
{% endif %}
{% if repo.type == 'package' %}
{{ repo.custom_cmd|default('yum install -y') }} {{ repo.pkg_name|default(repo.pkg_url) }}
{% endif %}
{% endif %}
{% endfor %}
{{ repo_cmd_after|default('') }}