#!/usr/bin/env oo-ruby

# Purpose: Chcon /var/lib/openshift to proper mcs contexts
#
# Type should generally be openshift_var_lib_t
# MCS should be s0:c$UID-499  so UID500 has a mcs of s0:c1

require 'rubygems'
require 'openshift-origin-node'


debug=1

OpenShift::Runtime::ApplicationContainer.all.each do |container|
  mcs_label = OpenShift::Runtime::Utils::SELinux.get_mcs_label(container.uid)

  # Use case not a good match for the SELinux module's file methods
  puts "restorecon -R #{container.container_dir}" if debug
  puts "chcon -l #{mcs_label} -R #{container.container_dir}/*" if debug
  %x[restorecon -R #{container.container_dir} ]
  %x[chcon -l #{mcs_label} -R #{container.container_dir}/* ]
end
