From ef0986b5d45d7aba81ecd187c49688708d785a87 Mon Sep 17 00:00:00 2001
From: Kenny Woodson <kwoodson@redhat.com>
Date: Mon, 3 Aug 2015 13:04:36 -0400
Subject: Added a pv creation script

---
 playbooks/adhoc/create_pv/create_pv.yaml | 134 +++++++++++++++++++++++++++++++
 playbooks/adhoc/create_pv/pv-template.j2 |  16 ++++
 2 files changed, 150 insertions(+)
 create mode 100644 playbooks/adhoc/create_pv/create_pv.yaml
 create mode 100644 playbooks/adhoc/create_pv/pv-template.j2

(limited to 'playbooks')

diff --git a/playbooks/adhoc/create_pv/create_pv.yaml b/playbooks/adhoc/create_pv/create_pv.yaml
new file mode 100644
index 000000000..c74734fb7
--- /dev/null
+++ b/playbooks/adhoc/create_pv/create_pv.yaml
@@ -0,0 +1,134 @@
+---
+- name: Create a volume and attach it to master
+  hosts: localhost
+  gather_facts: no
+  vars:
+    cli_volume_type: gp2
+    cli_volume_iops: ''
+    oo_name: "{{ groups['tag_host-type_' ~ cli_hosttype] |
+                 intersect(groups['tag_environment_' ~ cli_environment]) |
+                 first }}"
+  pre_tasks:
+  - fail:
+      msg: "This playbook requires {{item}} to be set."
+    when: "{{ item }} is not defined or {{ item }} == ''"
+    with_items:
+    - cli_volume_size
+    - cli_device_name
+    - cli_hosttype
+    - cli_environment
+
+  - name: set oo_name fact
+    set_fact:
+      oo_name: "{{ oo_name }}"
+
+
+  - name: Select a single master to run this on
+    add_host:
+      hostname: "{{ oo_name }}"
+      ansible_ssh_host: "{{ hostvars[oo_name].ec2_public_dns_name }}"
+      groups: oo_master
+
+  - name: Create a volume and attach it
+    ec2_vol:
+      state: present
+      instance: "{{ hostvars[oo_name]['ec2_id'] }}"
+      region: "{{ hostvars[oo_name]['ec2_region'] }}"
+      volume_size: "{{ cli_volume_size }}"
+      volume_type: "{{ cli_volume_type }}"
+      device_name: "{{ cli_device_name }}"
+      iops: "{{ cli_volume_iops }}"
+    register: vol
+
+  - debug: var=vol
+
+- name: Configure the drive
+  gather_facts: no
+  hosts: oo_master
+  user: root
+  connection: ssh
+  vars:
+    pv_tmpdir: /tmp/persistentvolumes
+
+  post_tasks:
+  - name: Setting facts for template
+    set_fact:
+      pv_name: "pv-{{cli_volume_size}}-{{ hostvars[hostvars.localhost.oo_name]['ec2_tag_Name'] }}-{{hostvars.localhost.vol.volume_id }}"
+      vol_az: "{{ hostvars[hostvars.localhost.oo_name]['ec2_placement'] }}"
+      vol_id: "{{ hostvars.localhost.vol.volume_id }}"
+      vol_size: "{{ cli_volume_size }}"
+      pv_mntdir: "{{ pv_tmpdir }}/mnt-{{ 1000 | random }}"
+
+  - set_fact:
+      pv_template: "{{ pv_tmpdir }}/{{ pv_name }}.yaml"
+
+  - name: "Mkdir {{ pv_tmpdir }}"
+    file:
+      state: directory
+      path: "{{ pv_tmpdir }}"
+      mode: '0750'
+
+  - name: "Mkdir {{ pv_mntdir }}"
+    file:
+      state: directory
+      path: "{{ pv_mntdir }}"
+      mode: '0750'
+
+  - name: Create pv file from template
+    template:
+      src: ./pv-template.j2
+      dest: "{{ pv_template }}"
+      owner: root
+      mode: '0640'
+
+  - name: mkfs
+    filesystem:
+      dev: "{{ cli_device_name }}"
+      fstype: ext4
+    
+  - name: Mount the dev
+    mount:
+      name: "{{ pv_mntdir }}"
+      src: "{{ cli_device_name }}"
+      fstype: ext4
+      state: mounted
+
+  - name: chgrp g+rwXs
+    file: 
+      path: "{{ pv_mntdir }}"
+      mode: 'g+rwXs'
+      recurse: yes
+      seuser: system_u
+      serole: object_r
+      setype: svirt_sandbox_file_t
+      selevel: s0
+
+  - name: umount
+    mount:
+      name: "{{ pv_mntdir }}"
+      src: "{{ cli_device_name }}"
+      state: unmounted
+      fstype: ext4
+
+  - name: detach drive
+    delegate_to: localhost
+    ec2_vol:
+      region: "{{ hostvars[hostvars.localhost.oo_name].ec2_region }}"
+      id: "{{ hostvars.localhost.vol.volume_id }}"
+      instance: None
+
+  - name: "Remove {{ pv_mntdir }}"
+    file:
+      state: absent
+      path: "{{ pv_mntdir }}"
+
+  # We have to use the shell module because we can't set env vars with the command module.
+  - name: "Place PV into oc"
+    shell: "KUBECONFIG=/etc/openshift/master/admin.kubeconfig oc create -f {{ pv_template | quote }}"
+    register: oc_output
+
+  - debug: var=oc_output
+
+  - fail: 
+      msg: "Failed to add {{ pv_template }} to master."
+    when: oc_output.rc != 0
diff --git a/playbooks/adhoc/create_pv/pv-template.j2 b/playbooks/adhoc/create_pv/pv-template.j2
new file mode 100644
index 000000000..5654ef6c4
--- /dev/null
+++ b/playbooks/adhoc/create_pv/pv-template.j2
@@ -0,0 +1,16 @@
+---
+apiVersion: v1
+kind: PersistentVolume
+metadata:
+  name: {{ pv_name }}
+  labels:
+    type: ebs
+spec:
+  capacity:
+    storage: {{ vol_size }}Gi
+  accessModes:
+    - ReadWriteOnce
+  persistentVolumeReclaimPolicy: Recycle
+  awsElasticBlockStore:
+    volumeID: aws://{{ vol_az }}/{{ vol_id }}
+    fsType: ext4
-- 
cgit v1.2.3