Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Added deploy container-storage command.
Browse files Browse the repository at this point in the history
1) Added the logic to the deploy container storage. This addresses
   issue #4
2) Rearranged deploy folder to be more flexible
3) Added ability for ansible to save stats to a yaml file
  • Loading branch information
rmkraus committed May 21, 2020
1 parent f965a43 commit 1b9c02b
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 29 deletions.
3 changes: 3 additions & 0 deletions app/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ gather_subset = virtual
forks=10
roles_path=/app/roles
log_path=/data/ansible.log
show_custom_stats=yes

# configure output
callback_plugins=/app/plugins/callback
stdout_callback=my_dense
callback_whitelist=timer

filter_plugins=/app/plugins/filter
library=/app/plugins/modules
lookup_plugins=/app/plugins/lookup

[inventory]
enable_plugins=script
Expand Down
10 changes: 5 additions & 5 deletions app/cli/deploy
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

export COOKBOOK=/app/cookbook.deploy
CMDS=$(cd $COOKBOOK; ls *.yml | sed 's/.yml//g' | awk '{ print " " $1; }')
export COOKBOOK=/app/deploy.d
CMDS=$(cd $COOKBOOK; ls * | awk '{ print " " $1; }')
HELP="""
USAGE: deploy RECIPE [ANSIBLE_ARGS]
USAGE: deploy COMMAND [COMMAND_ARGS]
Available Recipes:
Available Commands:
help
$CMDS
"""
Expand All @@ -32,4 +32,4 @@ export KUBECONFIG=/data/openshift-installer/auth/kubeconfig
## EXECUTE
RECIPE=$1
shift
ansible-playbook ${COOKBOOK}/${RECIPE}.yml $@
/bin/bash $COOKBOOK/$RECIPE/main.sh $@
104 changes: 104 additions & 0 deletions app/deploy.d/container-storage/configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import os
import sys
import pickle
import yaml
from collections import defaultdict
import ansible.parsing.yaml.objects
import ansible.utils.unsafe_proxy
from PyInquirer import Token, prompt, Separator


def checkbox(prompt_label, options, cache=None):
# ask for input
cached_data = []
if cache:
try:
cached_data = pickle.load(open(cache, 'rb'))
except (IOError, EOFError):
pass

if type(options) == list:
choices = [{'name': item,
'checked': item in cached_data} for item in options]
else:
choices = []
for item in options.keys():
item = str(item)
choices += [Separator(item)]
choices += [{'name': f'{item}: {option}',
'checked': f'{item}: {option}' in cached_data}
for option in options[item]]
questions = [
{
'type': 'checkbox',
'message': str(prompt_label),
'name': 'results',
'choices': choices
}
]
result = prompt(questions)['results']
result.sort()

# update cache
if cache and result != cached_data:
pickle.dump(result, open(cache, 'wb'))

# nice format results
if type(options) != list:
nice_result = defaultdict(list)
for key, val in [item.split(': ') for item in result]:
nice_result[key].append(val)
return nice_result

# return results to ansible
return result


def load_stats(stats_file):
return yaml.load(open(stats_file, 'r'), Loader=yaml.FullLoader)


def save_stats(stats_file, stats_data):
yaml.dump(stats_data, open(stats_file, 'w'))


def main():
# load data
stats = load_stats(os.environ['STATS_FILE'])

# ask for storage nodes
stg_nodes = checkbox('Select nodes to use for storage',
stats['cluster_nodes'],
'/data/container-storage-nodes.cache')
stg_nodes = [str(item) for item in stg_nodes]

# ask for storage devices
drives = dict(zip(stg_nodes,
[stats['cluster_drives'][item]
for item in stg_nodes]))
for key in drives.keys():
drives[key].sort()
stg_drives = checkbox('Select the block devices to use for storage',
drives,
'/data/container-storage-drives.cache')
stg_drives_out = []
for key, val in stg_drives.items():
stg_drives_out.append({'host': key, 'drives': val})

# count drives per node
drives_per_node = min([len(item['drives']) for item in stg_drives_out])

# save data
save_stats(os.environ['STATS_FILE'], {
'stg_nodes': stg_nodes,
'stg_drives': stg_drives_out,
'cluster_nodes': stats['cluster_nodes'],
'cluster_drives': stats['cluster_drives'],
'drives_per_node': drives_per_node
})

return 0


if __name__ == '__main__':
sys.exit(main())
122 changes: 122 additions & 0 deletions app/deploy.d/container-storage/container-storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
- name: install openshift container storage
hosts: localhost
become: no
gather_facts: no

tasks:
- name: ensure project exists
k8s:
state: present
definition:
apiVersion: project.openshift.io/v1
kind: Project
metadata:
name: openshift-storage
annotations:
openshift.io/cluster-monitoring: "true"
spec:

- name: ensure operatorgroup exists
k8s:
state: present
definition:
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: openshift-storage
namespace: openshift-storage
spec:
targetNamespaces:
- openshift-storage

- name: ensure subscription exists
k8s:
state: present
definition:
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: ocs-operator
namespace: openshift-storage
spec:
channel: "{{ lookup('ini', 'container_storage section=operators file=/app/versions.ini') }}"
name: ocs-operator
source: redhat-operators
sourceNamespace: openshift-marketplace
installPlanApproval: "Automatic"

- name: save clusterserviceversion object
set_fact:
k8s_obj:
apiVersion: operators.coreos.com/v1alpha1
kind: ClusterServiceVersion
name: ocs-operator*
namepace: openshift-storage

- name: wait for operator to become ready
assert:
that: "lookup('k8s', resource_definition=k8s_obj)[0].status.phase | default('error') == 'Succeeded'"
retries: 60
delay: 15

- name: ensure storagecluster exists
k8s:
state: present
definition:
apiVersion: ocs.openshift.io/v1
kind: StorageCluster
metadata:
name: ocs-storagecluster
namespace: openshift-storage
spec:
manageNodes: false
monDataDirHostPath: /var/lib/rook
storageDeviceSets:
- count: "{{ drives_per_node }}"
dataPVCTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: localblock
volumeMode: Block
name: ocs-deviceset
placement: {}
portable: false
replica: 3
resources:
mds:
limits:
cpu: 0.5
requests:
cpu: 0.25
noobaa-core:
limits:
cpu: 0.5
memory: 4Gi
requests:
cpu: 0.25
memory: 2Gi
noobaa-db:
limits:
cpu: 0.5
memory: 4Gi
requests:
cpu: 0.25
memory: 2Gi
mon:
limits:
cpu: 0.5
memory: 2Gi
requests:
cpu: 0.25
memory: 1Gi
rook-ceph-rgw:
limits:
cpu: 250m
memory: 2Gi
requests:
cpu: 100m
memory: 1Gi
26 changes: 26 additions & 0 deletions app/deploy.d/container-storage/gather-facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: query cluster facts
hosts: localhost
become: no
gather_facts: no

pre_tasks:
- name: lookup cluster nodes
set_fact:
cluster_nodes: "{{ lookup('k8s', api_version='v1', kind='Node') | json_query('[*].metadata.name')}}"

- name: query cluster node drives
shell: oc debug node/{{ item }} -- chroot /host lsblk -o NAME 2>/dev/null | egrep -v '((^ *`|^\|)|^NAME)'
loop: "{{ cluster_nodes }}"
register: cluster_drives
ignore_errors: yes

- name: save discovered hosts
set_stats:
data:
cluster_nodes: "{{ cluster_nodes }}"

- name: save discovered drives
set_stats:
data:
cluster_drives: "{{ {item.item: item.stdout_lines} }}"
loop: "{{ cluster_drives.results }}"
Loading

0 comments on commit 1b9c02b

Please sign in to comment.