From dc030d2cf7c1593db442cf6bfa227d0eeadf6a92 Mon Sep 17 00:00:00 2001 From: Bas Meijer Date: Sun, 14 Apr 2019 14:17:56 +0200 Subject: [PATCH] Refactored from base_cplusplus. --- .gitignore | 68 +++++++++++++++ .travis.yml | 18 ++++ .yamllint | 49 +++++++++++ LICENSE | 22 +++++ README.md | 45 ++++++++++ defaults/main.yml | 43 ++++++++++ meta/main.yml | 26 ++++++ molecule/default/Dockerfile.j2 | 14 +++ molecule/default/INSTALL.rst | 26 ++++++ molecule/default/molecule.yml | 31 +++++++ molecule/default/playbook.yml | 8 ++ molecule/default/tests/test_default.yml | 11 +++ molecule/default/verify.yml | 53 ++++++++++++ package.json | 10 +++ requirements.txt | 3 + tasks/main.yml | 109 ++++++++++++++++++++++++ templates/hello.cc | 8 ++ templates/test_compile_mysql.sh | 7 ++ templates/test_gcc.yml.j2 | 12 +++ test.yml | 14 +++ tox.ini | 21 +++++ 21 files changed, 598 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 .yamllint create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 molecule/default/Dockerfile.j2 create mode 100644 molecule/default/INSTALL.rst create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/playbook.yml create mode 100644 molecule/default/tests/test_default.yml create mode 100644 molecule/default/verify.yml create mode 100644 package.json create mode 100644 requirements.txt create mode 100644 tasks/main.yml create mode 100644 templates/hello.cc create mode 100755 templates/test_compile_mysql.sh create mode 100644 templates/test_gcc.yml.j2 create mode 100644 test.yml create mode 100644 tox.ini diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..866ff51 --- /dev/null +++ b/.gitignore @@ -0,0 +1,68 @@ +# Config file for instance +ansible.cfg +.molecule/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +.DS_Store +.vagrant + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +#Ipython Notebook +.ipynb_checkpoints diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9fe1701 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +--- +sudo: required +language: python +services: + - docker + +before_install: + - sudo apt-get -qq update + +install: + - pip install tox-travis + - pip install -r requirements.txt + +script: + - tox + +notifications: + webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..a6cff5d --- /dev/null +++ b/.yamllint @@ -0,0 +1,49 @@ +--- + +extends: default + +rules: + braces: + min-spaces-inside: 0 + max-spaces-inside: 0 + min-spaces-inside-empty: -1 + max-spaces-inside-empty: -1 + brackets: + min-spaces-inside: 0 + max-spaces-inside: 0 + min-spaces-inside-empty: -1 + max-spaces-inside-empty: -1 + colons: + max-spaces-before: 0 + max-spaces-after: 1 + commas: + max-spaces-before: 0 + min-spaces-after: 1 + max-spaces-after: 1 + comments: + level: warning + require-starting-space: false + min-spaces-from-content: 2 + comments-indentation: disable + document-end: disable + document-start: disable + empty-lines: + max: 2 + max-start: 0 + max-end: 0 + hyphens: + max-spaces-after: 1 + indentation: + spaces: consistent + indent-sequences: true + check-multi-line-strings: false + key-duplicates: enable + line-length: + max: 250 + allow-non-breakable-words: true + allow-non-breakable-inline-mappings: false + new-line-at-end-of-file: enable + new-lines: + type: unix + trailing-spaces: enable + truthy: disable diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bf57557 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2019 Bas Meijer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b798c8 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +[![Galaxy](https://img.shields.io/badge/galaxy-dockpack.base__gcc-blue.svg?style=flat)](https://galaxy.ansible.com/dockpack/base_gcc)[![Build Status](https://api.travis-ci.org/dockpack/base_gcc.svg)](https://travis-ci.org/dockpack/base_gcc) + +base_gcc is an ansible-role that installs Gnu compiler tools + +Requirements +------------ + +RHEL-like system, or Ubuntu + + +Role Variables +-------------- +- Corporate feature: toggles desired state +`compilers_present: present` + +- Software collections offer latest versions of programming languages +`collections_enabled: true` + +- approved/test release of software collections' devtoolset +`cplusplus_devtoolset: devtoolset-7` + +- Change these defaults for optional packages. +`cplusplusrpms` +`cplusplusapts + +Dependencies +------------ + +https://www.softwarecollections.org/en/docs/ + +Example Usage +---------------- + +Refer to a complete build server https://github.com/bbaassssiiee/buildserver + +License +------- + +MIT + +Author Information +------------------ + +Bas Meijer +@bbaassssiiee diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..3c7f171 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,43 @@ +--- + +# Corporate feature: toggles desired state +compilers_present: present + +# yum groupinstall @Development +development_groupinstall: true + +# Software collections offer latest versions of programming languages +collections_enabled: false + +# major release of software collections' devtoolset +GCCVER: 7 +cplusplus_devtoolset: "devtoolset-{{ GCCVER }}" + +cplusplus_dts_rpms: + - "{{ cplusplus_devtoolset }}-binutils" + - "{{ cplusplus_devtoolset }}-gcc" + - "{{ cplusplus_devtoolset }}-gcc-c++" + - "{{ cplusplus_devtoolset }}-make" + +# optional rpms +cplusplusrpms: + - autoconf + - automake + - git + - gcc + - file-devel + - make + - openssl-devel + - libstdc++-docs + - ncurses-devel + +# Ubuntu & Debian only basic compiler support +cplusplusapts: + - build-essential + - autoconf + - automake + - g++ + - gcc + - git + - libc6-dev + - make diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..9893e17 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,26 @@ +--- + +galaxy_info: + author: Bas Meijer + description: Ansible role to install GNU C/C++ Compilers + company: dockpack + + license: MIT + + min_ansible_version: 1.9.0 + + platforms: + - name: EL + versions: + - 6 + - 7 + galaxy_tags: + - system + - development + - buildserver + - gcc + - automake + - autoconf + - centos + +dependencies: [] diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 new file mode 100644 index 0000000..19692c2 --- /dev/null +++ b/molecule/default/Dockerfile.j2 @@ -0,0 +1,14 @@ +# Molecule managed + +{% if item.registry is defined %} +FROM {{ item.registry.url }}/{{ item.image }} +{% else %} +FROM {{ item.image }} +{% endif %} + +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get upgrade -y && apt-get install -y python sudo bash ca-certificates && apt-get clean; \ + elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install python sudo python-devel python2-dnf bash && dnf clean all; \ + elif [ $(command -v yum) ]; then yum makecache fast && yum update -y && yum install -y python sudo yum-plugin-ovl bash && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ + elif [ $(command -v zypper) ]; then zypper refresh && zypper update -y && zypper install -y python sudo bash python-xml && zypper clean -a; \ + elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ + elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates && xbps-remove -O; fi diff --git a/molecule/default/INSTALL.rst b/molecule/default/INSTALL.rst new file mode 100644 index 0000000..3904805 --- /dev/null +++ b/molecule/default/INSTALL.rst @@ -0,0 +1,26 @@ +******* +Docker driver installation guide +******* + +Requirements +============ + +* General molecule dependencies (see https://molecule.readthedocs.io/en/latest/installation.html) +* Docker Engine +* docker-py +* docker + +Install +======= + +Ansible < 2.6 + +.. code-block:: bash + + $ sudo pip install docker-py + +Ansible >= 2.6 + +.. code-block:: bash + + $ sudo pip install docker diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..8ba7c28 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,31 @@ +--- +dependency: + name: galaxy + requirements_file: requirements.yml +driver: + name: docker +verifier: + name: goss + lint: + name: yamllint +platforms: + - name: base-gcc-centos7 + image: centos:7 + privileged: true + - name: base-gcc-centos6 + image: centos:6 + privileged: true + - name: base-gcc-ubuntu + image: ubuntu:xenial + privileged: true +provisioner: + name: ansible + config_options: + defaults: + remote_tmp: /tmp + playbooks: + converge: playbook.yml + lint: + name: ansible-lint +scenario: + name: default diff --git a/molecule/default/playbook.yml b/molecule/default/playbook.yml new file mode 100644 index 0000000..45e0221 --- /dev/null +++ b/molecule/default/playbook.yml @@ -0,0 +1,8 @@ +--- +- hosts: all + vars: + development_groupinstall: false + + roles: + - role: base_gcc + collections_enabled: true diff --git a/molecule/default/tests/test_default.yml b/molecule/default/tests/test_default.yml new file mode 100644 index 0000000..907b5b5 --- /dev/null +++ b/molecule/default/tests/test_default.yml @@ -0,0 +1,11 @@ +--- +# Molecule managed +package: + automake: + installed: true + autoconf: + installed: true + gcc: + installed: true + make: + installed: true diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..77a24ea --- /dev/null +++ b/molecule/default/verify.yml @@ -0,0 +1,53 @@ +--- +# This is an example playbook to execute goss tests. +# Tests need distributed to the appropriate ansible host/groups +# prior to execution by `goss validate`. + +- name: Verify + hosts: all + become: true + vars: + goss_version: v0.3.2 + goss_arch: amd64 + goss_dst: /usr/local/bin/goss + goss_sha256sum: 2f6727375db2ea0f81bee36e2c5be78ab5ab8d5981f632f761b25e4003e190ec + goss_url: "https://github.com/aelsabbahy/goss/releases/download/{{ goss_version }}/goss-linux-{{ goss_arch }}" + goss_test_directory: /tmp + goss_format: documentation + tasks: + - name: Download and install Goss + get_url: + url: "{{ goss_url }}" + dest: "{{ goss_dst }}" + sha256sum: "{{ goss_sha256sum }}" + mode: 0755 + register: download_goss + until: download_goss is succeeded + retries: 3 + + - name: Copy Goss tests to remote + copy: + src: "{{ item }}" + dest: "{{ goss_test_directory }}/{{ item | basename }}" + with_fileglob: + - "{{ lookup('env', 'MOLECULE_VERIFIER_TEST_DIRECTORY') }}/test_*.yml" + + - name: Register test files + shell: "ls {{ goss_test_directory }}/test_*.yml" + register: test_files + + - name: Execute Goss tests + command: "{{ goss_dst }} -g {{ item }} validate --format {{ goss_format }}" + register: test_results + with_items: "{{ test_files.stdout_lines }}" + + - name: Display details about the Goss results + debug: + msg: "{{ item.stdout_lines }}" + with_items: "{{ test_results.results }}" + + - name: Fail when tests fail + fail: + msg: "Goss failed to validate" + when: item.rc != 0 + with_items: "{{ test_results.results }}" diff --git a/package.json b/package.json new file mode 100644 index 0000000..21055be --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "name": "dockpack.base_cplusplus", + "version": "0.3.5", + "scripts": { + "release": "release-it" + }, + "devDependencies": { + "release-it": "^7.6.1" + } +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..be03f32 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +tox +molecule +docker diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..0089d41 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,109 @@ +--- + +- name: ensure directory exists + file: + path: /root + state: directory + owner: root + group: root + mode: 0700 + tags: + - base_gcc + +- name: copy goss tests template + template: + src: test_gcc.yml.j2 + dest: /root/test_gcc.yml + tags: + - base_gcc + +- name: yum install development tools + when: ansible_os_family == 'RedHat' and development_groupinstall + yum: + name: "@Development" + state: "{{ compilers_present }}" + tags: + - base_gcc + +- name: install centos software collections + package: + name: centos-release-scl-rh + state: present + when: collections_enabled and ansible_distribution == 'CentOS' + tags: + - base_gcc + +- name: enable rhel software collections + command: "yum-config-manager --enable rhel-server-rhscl-{{ ansible_distribution_major_version }}-rpms" + when: collections_enabled and ansible_distribution == 'Red Hat Enterprise Linux' + tags: + - base_gcc + +- name: yum install devtoolset rpms + yum: + name: "{{ cplusplus_dts_rpms }}" + state: "{{ compilers_present }}" + when: collections_enabled and ansible_os_family == 'RedHat' + tags: + - base_gcc + +- name: remove broken sudo from devtoolset for security + file: + path: "/opt/rh/{{ cplusplus_devtoolset }}/root/usr/bin/sudo" + state: absent + when: collections_enabled and ansible_os_family == 'RedHat' + tags: + - base_gcc + +- name: enable devtoolset + file: + src: "/opt/rh/{{ cplusplus_devtoolset }}/enable" + path: /etc/profile.d/devtoolset.sh + state: link + when: collections_enabled and ansible_os_family == 'RedHat' + tags: + - base_gcc + +- name: disable devtoolset + file: + src: "/opt/rh/{{ cplusplus_devtoolset }}/enable" + path: /etc/profile.d/devtoolset.sh + state: absent + when: not collections_enabled and ansible_os_family == 'RedHat' + tags: + - base_gcc + +- name: yum install additional tools + when: ansible_os_family == 'RedHat' + yum: + name: "{{ cplusplusrpms }}" + state: "{{ compilers_present }}" + skip_broken: true + tags: + - base_gcc + +- name: source devtoolset enable file + when: collections_enabled and ansible_os_family == 'RedHat' + set_fact: + cmd_env: "source /opt/rh/{{ cplusplus_devtoolset }}/enable && " + +- name: apt install packages for C++ development + when: ansible_os_family == 'Debian' + apt: + pkg: "{{ cplusplusapts }}" + update_cache: yes + cache_valid_time: 86400 + state: "{{ compilers_present }}" + tags: + - base_gcc + +- name: what is my compiler? + shell: "{{ cmd_env | default('') }} gcc --version" + register: gcc_version + changed_when: false + tags: + - skip_ansible_lint + +- name: Display details about gcc version + debug: + msg: "{{ gcc_version.stdout_lines }}" diff --git a/templates/hello.cc b/templates/hello.cc new file mode 100644 index 0000000..0a61897 --- /dev/null +++ b/templates/hello.cc @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() +{ + cout << "Hello World\n"; + return 0; +} diff --git a/templates/test_compile_mysql.sh b/templates/test_compile_mysql.sh new file mode 100755 index 0000000..36dba6c --- /dev/null +++ b/templates/test_compile_mysql.sh @@ -0,0 +1,7 @@ +#!/bin/bash +# Can we compile C++? + +mkdir mysql && cd mysql +wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.15.tar.gz +tar xfz mysql-boost-8.0.15.tar.gz +cmake -DWITH_BOOST=/var/local/boost_1_68_0/ mysql-8.0.15/ diff --git a/templates/test_gcc.yml.j2 b/templates/test_gcc.yml.j2 new file mode 100644 index 0000000..cd258ab --- /dev/null +++ b/templates/test_gcc.yml.j2 @@ -0,0 +1,12 @@ +package: +{% if ansible_os_family == 'RedHat' %} +{% for item in cplusplusrpms %} + {{ item }}: + installed: true +{% endfor %} +{% else %} +{% for item in cplusplusapts %} + {{ item }}: + installed: true +{% endfor %} +{% endif %} diff --git a/test.yml b/test.yml new file mode 100644 index 0000000..0d74292 --- /dev/null +++ b/test.yml @@ -0,0 +1,14 @@ +--- +- hosts: all + connection: local + vars_files: + - defaults/main.yml + + tasks: + - name: set string + set_fact: + options: "{{ compile_boost_with }}" + + - name: display string + debug: + msg: "{{ compile_boost_with }}" diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..e562bed --- /dev/null +++ b/tox.ini @@ -0,0 +1,21 @@ +[tox] +minversion = 1.8 +envlist = py{36,37,27}-ansible{26,27,devel} +skipsdist = true +skip_missing_interpreters = True + +[testenv] +passenv = * +deps = + -rrequirements.txt + ansible26: ansible>=2.6,<2.7 + ansible27: ansible>=2.7,<2.8 + ansibledevel: git+https://github.com/ansible/ansible.git +commands = + pip list + molecule dependency + molecule syntax + molecule lint + molecule create + molecule converge + molecule verify