From 3fd2e1ba9d560fe9351cef917c5f719d715cbe56 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sun, 26 Jan 2020 00:09:27 +0100 Subject: [PATCH] initial commit --- .drone.jsonnet | 155 +++++++++++++++++++++++++ .drone.yml | 152 ++++++++++++++++++++++++ .gitignore | 11 ++ .later.yml | 18 +++ LICENSE | 8 ++ README.md | 16 +++ defaults/main.yml | 22 ++++ meta/main.yml | 23 ++++ molecule/centos7/create.yml | 106 +++++++++++++++++ molecule/centos7/destroy.yml | 71 +++++++++++ molecule/centos7/molecule.yml | 22 ++++ molecule/centos7/playbook.yml | 16 +++ molecule/centos7/prepare.yml | 9 ++ molecule/centos7/tests/test_default.py | 21 ++++ molecule/default | 1 + molecule/pytest.ini | 3 + tasks/main.yml | 2 + tasks/setup.yml | 38 ++++++ templates/smbcredentials.j2 | 2 + 19 files changed, 696 insertions(+) create mode 100644 .drone.jsonnet create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .later.yml 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/centos7/create.yml create mode 100644 molecule/centos7/destroy.yml create mode 100644 molecule/centos7/molecule.yml create mode 100644 molecule/centos7/playbook.yml create mode 100644 molecule/centos7/prepare.yml create mode 100644 molecule/centos7/tests/test_default.py create mode 120000 molecule/default create mode 100644 molecule/pytest.ini create mode 100644 tasks/main.yml create mode 100644 tasks/setup.yml create mode 100644 templates/smbcredentials.j2 diff --git a/.drone.jsonnet b/.drone.jsonnet new file mode 100644 index 0000000..5c510bd --- /dev/null +++ b/.drone.jsonnet @@ -0,0 +1,155 @@ +local PipelineLinting = { + kind: 'pipeline', + name: 'linting', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'ansible-later', + image: 'xoxys/ansible-later:latest', + commands: [ + 'ansible-later', + ], + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, +}; + +local PipelineDeployment(scenario='centos7') = { + kind: 'pipeline', + name: 'testing-' + scenario, + platform: { + os: 'linux', + arch: 'amd64', + }, + concurrency: { + limit: 1, + }, + workspace: { + base: '/drone/src', + path: 'xoxys.smb', + }, + steps: [ + { + name: 'ansible-molecule', + image: 'xoxys/molecule:latest', + environment: { + DO_API_KEY: { from_secret: 'do_api_key' }, + USER: 'root', + MOLECULE_CUSTOM_MODULES_REPO: 'https://gitea.rknet.org/ansible/custom_modules', + MOLECULE_CUSTOM_FILTERS_REPO: 'https://gitea.rknet.org/ansible/custom_filters', + }, + commands: [ + 'molecule test -s' + scenario, + ], + }, + ], + depends_on: [ + 'linting', + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +local PipelineDocumentation = { + kind: 'pipeline', + name: 'documentation', + platform: { + os: 'linux', + arch: 'amd64', + }, + steps: [ + { + name: 'generate', + image: 'xoxys/ansible-doctor:latest', + environment: { + ANSIBLE_DOCTOR_LOG_LEVEL: 'INFO', + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true, + ANSIBLE_DOCTOR_EXCLUDE_FILES: 'molecule/', + ANSIBLE_DOCTOR_TEMPLATE: 'hugo-book', + ANSIBLE_DOCTOR_ROLE_NAME: '${DRONE_REPO_NAME#*.}', + ANSIBLE_DOCTOR_OUTPUT_DIR: '_docs/', + }, + }, + { + name: 'publish', + image: 'plugins/gh-pages:latest', + settings: { + remote_url: 'https://gitea.rknet.org/ansible/xoxys.smb', + netrc_machine: 'gitea.rknet.org', + username: { from_secret: 'gitea_username' }, + password: { from_secret: 'gitea_token' }, + pages_directory: '_docs/', + target_branch: 'docs', + }, + when: { + ref: ['refs/heads/master'], + }, + }, + { + name: 'trigger', + image: 'plugins/downstream', + settings: { + server: 'https://drone.rknet.org', + token: { from_secret: 'drone_token' }, + fork: true, + repositories: [ + 'ansible/ansible-galaxy', + ], + }, + when: { + ref: ['refs/heads/master'], + }, + }, + ], + trigger: { + ref: ['refs/heads/master', 'refs/tags/**', 'refs/pull/**'], + }, + depends_on: [ + 'testing-centos7', + ], +}; + +local PipelineNotification = { + kind: 'pipeline', + name: 'notification', + platform: { + os: 'linux', + arch: 'amd64', + }, + clone: { + disable: true, + }, + steps: [ + { + name: 'matrix', + image: 'plugins/matrix', + settings: { + homeserver: { from_secret: 'matrix_homeserver' }, + roomid: { from_secret: 'matrix_roomid' }, + template: 'Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}', + username: { from_secret: 'matrix_username' }, + password: { from_secret: 'matrix_password' }, + }, + }, + ], + depends_on: [ + 'documentation', + ], + trigger: { + status: ['success', 'failure'], + ref: ['refs/heads/master', 'refs/tags/**'], + }, +}; + +[ + PipelineLinting, + PipelineDeployment(scenario='centos7'), + PipelineDocumentation, + PipelineNotification, +] diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..1b153c4 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,152 @@ +--- +kind: pipeline +name: linting + +platform: + os: linux + arch: amd64 + +steps: +- name: ansible-later + image: xoxys/ansible-later:latest + commands: + - ansible-later + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +--- +kind: pipeline +name: testing-centos7 + +platform: + os: linux + arch: amd64 + +concurrency: + limit: 1 + +workspace: + base: /drone/src + path: xoxys.smb + +steps: +- name: ansible-molecule + image: xoxys/molecule:latest + commands: + - molecule test -scentos7 + environment: + DO_API_KEY: + from_secret: do_api_key + MOLECULE_CUSTOM_FILTERS_REPO: https://gitea.rknet.org/ansible/custom_filters + MOLECULE_CUSTOM_MODULES_REPO: https://gitea.rknet.org/ansible/custom_modules + USER: root + +trigger: + ref: + - refs/heads/master + - refs/tags/** + +depends_on: +- linting + +--- +kind: pipeline +name: documentation + +platform: + os: linux + arch: amd64 + +steps: +- name: generate + image: xoxys/ansible-doctor:latest + environment: + ANSIBLE_DOCTOR_EXCLUDE_FILES: molecule/ + ANSIBLE_DOCTOR_FORCE_OVERWRITE: true + ANSIBLE_DOCTOR_LOG_LEVEL: INFO + ANSIBLE_DOCTOR_OUTPUT_DIR: _docs/ + ANSIBLE_DOCTOR_ROLE_NAME: ${DRONE_REPO_NAME#*.} + ANSIBLE_DOCTOR_TEMPLATE: hugo-book + +- name: publish + image: plugins/gh-pages:latest + settings: + netrc_machine: gitea.rknet.org + pages_directory: _docs/ + password: + from_secret: gitea_token + remote_url: https://gitea.rknet.org/ansible/xoxys.smb + target_branch: docs + username: + from_secret: gitea_username + when: + ref: + - refs/heads/master + +- name: trigger + image: plugins/downstream + settings: + fork: true + repositories: + - ansible/ansible-galaxy + server: https://drone.rknet.org + token: + from_secret: drone_token + when: + ref: + - refs/heads/master + +trigger: + ref: + - refs/heads/master + - refs/tags/** + - refs/pull/** + +depends_on: +- testing-centos7 + +--- +kind: pipeline +name: notification + +platform: + os: linux + arch: amd64 + +clone: + disable: true + +steps: +- name: matrix + image: plugins/matrix + settings: + homeserver: + from_secret: matrix_homeserver + password: + from_secret: matrix_password + roomid: + from_secret: matrix_roomid + template: "Status: **{{ build.status }}**
Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}
Message: {{ build.message }}" + username: + from_secret: matrix_username + +trigger: + ref: + - refs/heads/master + - refs/tags/** + status: + - success + - failure + +depends_on: +- documentation + +--- +kind: signature +hmac: 1d91a342164771aaff18780330ce15c9f4368a25bea13ad0a290f5751515ff96 + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5becda8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# ---> Ansible +*.retry +filter/plugins/ +library + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + diff --git a/.later.yml b/.later.yml new file mode 100644 index 0000000..45911c9 --- /dev/null +++ b/.later.yml @@ -0,0 +1,18 @@ +--- +ansible: + custom_modules: + - iptables_raw + - openssl_pkcs12 + - proxmox_kvm2 + - ucr + - yum_versionlock + +rules: + exclude_files: + - molecule/ + - "LICENSE*" + - "**/*.md" + - "**/*.ini" + + exclude_filter: + - LINT0009 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..472ac23 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +MIT License +Copyright (c) + +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..7b923fe --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# xoxys.smb + +[![Build Status](https://drone.rknet.org/api/badges/ansible/xoxys.smb/status.svg)](https://drone.rknet.org/ansible/xoxys.smb) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) + +Mount a list of SMB shares. + +You can find the full documentation at [galaxy.geekdocs.de](https://galaxy.geekdocs.de/system/smb/). + +### License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +### Maintainers and Contributors + +[Robert Kaussow](https://gitea.rknet.org/xoxys) diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..5b1b3b4 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,22 @@ +--- +smb_packages: + - cifs-utils + +# @var smb_shares:description: > +# Add mount defined samba shares. You can specify samba credentials with +# username/password parameter. Credentials will be used in a credentials file +# and saved to `/root/.smbcredentials/` and not in the fstab directly. + +# @var smb_shares:example: > +# smb_shares: +# - name: multimedia +# source: //share.example.com/media +# username: myuser +# password: secure +# mountpoint: /media/data +# mountopts: +# - acl +# - uid=1000 +# - gid=1000 +# state: mounted +smb_shares: [] diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..4189d86 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,23 @@ +# Standards: 0.1 +--- +galaxy_info: +# @meta author:value: [xoxys](https://gitea.rknet.org/xoxys) + author: xoxys +# @meta description: > +# [![Build Status](https://drone.rknet.org/api/badges/ansible/xoxys.smb/status.svg)](https://drone.rknet.org/ansible/xoxys.smb) +# [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) +# [![Source Code](https://img.shields.io/badge/Git-Source%20code-blue?logo=git)](https://gitea.rknet.org/ansible/xoxys.smb) +# +# Mount a list of smb shares. +# @end + description: Mount a list of SMB shares + license: MIT + min_ansible_version: 2.4 + platforms: + - name: EL + versions: + - 7 + galaxy_tags: + - storage + - smb +dependencies: diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml new file mode 100644 index 0000000..a9dae89 --- /dev/null +++ b/molecule/centos7/create.yml @@ -0,0 +1,106 @@ +--- +- name: Create + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + vars: + ssh_user: root + ssh_port: 22 + + keypair_name: molecule_key + keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" + tasks: + - name: Create local keypair + user: + name: "{{ lookup('env', 'USER') }}" + generate_ssh_key: true + ssh_key_file: "{{ keypair_path }}" + register: local_keypair + + - name: Create remote keypair + digital_ocean_sshkey: + name: "{{ keypair_name }}" + ssh_pub_key: "{{ local_keypair.ssh_public_key }}" + state: present + register: remote_keypair + + - name: Create molecule instance(s) + digital_ocean_droplet: + name: "{{ item.name }}" + unique_name: true + region: "{{ item.region_id }}" + image: "{{ item.image_id }}" + size: "{{ item.size_id }}" + ssh_keys: "{{ remote_keypair.data.ssh_key.id }}" + wait: true + wait_timeout: 300 + state: present + register: server + loop: "{{ molecule_yml.platforms }}" + async: 7200 + poll: 0 + + - name: Wait for instance(s) creation to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - name: Create new block storage + digital_ocean_block_storage: + state: present + command: create + region: "{{ item.data.droplet.region.slug }}" + block_size: 2 + volume_name: "{{ item.data.droplet.name }}-blk" + loop: "{{ digitalocean_jobs.results }}" + + - name: Attach block storage to instance + digital_ocean_block_storage: + state: present + command: attach + volume_name: "{{ item.data.droplet.name }}-blk" + region: "{{ item.data.droplet.region.slug }}" + droplet_id: "{{ item.data.droplet.id }}" + loop: "{{ digitalocean_jobs.results }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config dict + set_fact: + instance_conf_dict: { + 'instance': "{{ item.data.droplet.name }}", + 'address': "{{ item.data.ip_address }}", + 'user': "{{ ssh_user }}", + 'port': "{{ ssh_port }}", + 'identity_file': "{{ keypair_path }}", + 'droplet_id': "{{ item.data.droplet.id }}", + 'droplet_region': "{{ item.data.droplet.region.slug }}", + 'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}", + } + loop: "{{ digitalocean_jobs.results }}" + register: instance_config_dict + when: server.changed | bool + + - name: Convert instance config dict to a list + set_fact: + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" + when: server.changed | bool + + - name: Dump instance config + copy: + content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool + + - name: Wait for SSH + wait_for: + port: "{{ ssh_port }}" + host: "{{ item.address }}" + search_regex: SSH + delay: 10 + timeout: 320 + loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml new file mode 100644 index 0000000..e6c36c1 --- /dev/null +++ b/molecule/centos7/destroy.yml @@ -0,0 +1,71 @@ +--- +- name: Destroy + hosts: localhost + connection: local + gather_facts: false + no_log: "{{ molecule_no_log }}" + tasks: + - block: + - name: Populate instance config + set_fact: + instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" + skip_instances: false + rescue: + - name: Populate instance config when file missing + set_fact: + instance_conf: {} + skip_instances: true + + - name: Detach block storage from instance + digital_ocean_block_storage: + state: absent + command: attach + volume_name: "{{ item.instance }}-blk" + region: "{{ item.droplet_region }}" + droplet_id: "{{ item.droplet_id }}" + loop: "{{ instance_conf | flatten(levels=1) }}" + + - name: Destroy molecule instance(s) + digital_ocean_droplet: + name: "{{ item.instance }}" + id: "{{ item.droplet_id }}" + state: absent + register: server + loop: "{{ instance_conf | flatten(levels=1) }}" + when: not skip_instances + async: 7200 + poll: 0 + + - name: Wait for instance(s) deletion to complete + async_status: + jid: "{{ item.ansible_job_id }}" + register: digitalocean_jobs + until: digitalocean_jobs.finished + retries: 300 + loop: "{{ server.results }}" + + - name: Delete block storage + digital_ocean_block_storage: + state: absent + command: create + region: "{{ item.droplet_region }}" + volume_name: "{{ item.instance }}-blk" + loop: "{{ instance_conf | flatten(levels=1) }}" + + - name: Delete remote keypair + digital_ocean_sshkey: + fingerprint: "{{ item.ssh_key_id }}" + state: absent + loop: "{{ instance_conf | flatten(levels=1) }}" + + # Mandatory configuration for Molecule to function. + + - name: Populate instance config + set_fact: + instance_conf: {} + + - name: Dump instance config + copy: + content: "{{ instance_conf | molecule_to_yaml | molecule_header }}" + dest: "{{ molecule_instance_config }}" + when: server.changed | bool diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml new file mode 100644 index 0000000..adf0b63 --- /dev/null +++ b/molecule/centos7/molecule.yml @@ -0,0 +1,22 @@ +--- +dependency: + name: galaxy +driver: + name: digitalocean +platforms: + - name: centos7-smb + region_id: fra1 + image_id: centos-7-x64 + size_id: s-1vcpu-1gb +lint: + name: yamllint + enabled: False +provisioner: + name: ansible + lint: + name: ansible-lint + enabled: False +verifier: + name: testinfra + lint: + name: flake8 diff --git a/molecule/centos7/playbook.yml b/molecule/centos7/playbook.yml new file mode 100644 index 0000000..d3cd455 --- /dev/null +++ b/molecule/centos7/playbook.yml @@ -0,0 +1,16 @@ +--- +- name: Converge + hosts: all + vars: + smb_shares: + - name: multimedia + source: //share.example.com/media + username: myuser + password: secure + mountpoint: /media/data + mountopts: + - acl + state: present + + roles: + - role: xoxys.smb diff --git a/molecule/centos7/prepare.yml b/molecule/centos7/prepare.yml new file mode 100644 index 0000000..4b18d48 --- /dev/null +++ b/molecule/centos7/prepare.yml @@ -0,0 +1,9 @@ +--- +- name: Prepare + hosts: all + gather_facts: false + tasks: + - name: Install python for Ansible + raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal) + become: true + changed_when: false diff --git a/molecule/centos7/tests/test_default.py b/molecule/centos7/tests/test_default.py new file mode 100644 index 0000000..cc2d0a9 --- /dev/null +++ b/molecule/centos7/tests/test_default.py @@ -0,0 +1,21 @@ +import os + +import testinfra.utils.ansible_runner + +import warnings +warnings.filterwarnings("ignore", category=DeprecationWarning) + +testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( + os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all') + + +def test_smb_in_fstab(host): + fstab = host.file("/etc/fstab") + assert fstab.contains("//share.example.com/media") + + +def test_smb_credentials(host): + credentials = host.file("/root/.smbcredentials/multimedia") + assert credentials.exists + assert credentials.contains("username=myuser") + assert credentials.contains("password=secure") diff --git a/molecule/default b/molecule/default new file mode 120000 index 0000000..2fdf3e8 --- /dev/null +++ b/molecule/default @@ -0,0 +1 @@ +centos7 \ No newline at end of file diff --git a/molecule/pytest.ini b/molecule/pytest.ini new file mode 100644 index 0000000..c24fe5b --- /dev/null +++ b/molecule/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..1f69f7a --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,2 @@ +--- +- include_tasks: setup.yml diff --git a/tasks/setup.yml b/tasks/setup.yml new file mode 100644 index 0000000..0a5858c --- /dev/null +++ b/tasks/setup.yml @@ -0,0 +1,38 @@ +--- +- block: + - name: Install requirements + package: + name: "{{ item }}" + state: present + loop: "{{ smb_packages }}" + + - name: Ensure credentials folder exist + file: + path: /root/.smbcredentials + owner: root + group: root + mode: 0600 + state: directory + + - name: Create secrets file + template: + src: "smbcredentials.j2" + dest: "/root/.smbcredentials/{{ item.name }}" + mode: 0600 + loop: "{{ smb_shares }}" + loop_control: + label: "/root/.smbcredentials/{{ item.name }}" + when: item.password is defined and item.password + + - name: Mount volume + mount: + path: "{{ item.mountpoint }}" + src: "{{ item.source }}" + fstype: "cifs" + opts: "{{ ((item.mountopts | default([])) if not (item.password is defined and item.password) else ((item.mountopts | default([])) + ['credentials=/root/.smbcredentials/' + item.name])) | join(',') }}" + state: "{{ item.state | default('mounted') }}" + loop: "{{ smb_shares }}" + loop_control: + label: "mount: {{ item.source }} {{ item.mountpoint }} cifs {% if item.mountopts is defined %}{{ item.mountopts | join(',') }}{% endif %}" + become: True + become_user: root diff --git a/templates/smbcredentials.j2 b/templates/smbcredentials.j2 new file mode 100644 index 0000000..903e368 --- /dev/null +++ b/templates/smbcredentials.j2 @@ -0,0 +1,2 @@ +username={{ item.username }} +password={{ item.password }}