#!/usr/bin/python
# @package      hubzero-mw2-exec-service
# @file         firewall_readd
# @copyright    Copyright (c) 2016-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2016-2020 The Regents of the University of California.
#
# 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.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

"""
  After the execution host's firewall rules have been reloaded, add again the
  dynamic firewall rules for containers, based on the user's groups
"""
IONHELPER = True
DEBUG = True

import os
import sys
import stat
import subprocess
from hubzero.mw.log import log, setup_log
from hubzero.mw.constants import CONTAINER_K, SERVICE_LOG
# handle name change SERVICE_CONFIG_FILE vs EXEC_CONFIG_FILE
# because there's a service on execution hosts that isn't the same as the service on file servers
try:
  from hubzero.mw.constants import EXEC_CONFIG_FILE
except NameError:
  from hubzero.mw.constants import SERVICE_CONFIG_FILE
  EXEC_CONFIG_FILE = SERVICE_CONFIG_FILE
from hubzero.mw.user_account import User_account
from hubzero.mw.container import Container, make_Container
import pwd

# variables set in EXEC_CONFIG_FILE
CONTAINER_CONF = {}

def in_group(veid, group):
  """ deprecated, return whether group is defined in /etc/group.  Crude and often incorrect way
  of determining if user is a member of a group.  Always incorrect since we started defining
  groups in /etc/group regardless of user membership."""
  # -c: count
  cmd = ["grep", "-c", '^' + group, "/var/lib/vz/root/%s/etc/group" % veid]
  try:
    return int(subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]) >0
  except ValueError:
    return True

def main():
  """For each container, figure out firewall rules that need to be added."""
  # get list of active containers
  if "DOCKERTOOL_NET" in CONTAINER_CONF:
    ctnames = subprocess.Popen(['docker', 'ps', '-f', "name=tool", '--format', "{{.Names}}"], stdout=subprocess.PIPE).communicate()[0]
    ctname_a = ctnames.split('\n')
    # grab only the number in '4.tool'
    ctids = map(lambda x: x.split('.')[0], ctname_a)
  else:
    vzout = subprocess.Popen(["vzlist", "-1"], stdout=subprocess.PIPE).communicate()[0]
    ctids = vzout.split()
  if DEBUG:
    print ctids
  for ctid in ctids:
    if ctid == "":
      continue
    if DEBUG:
      print "container id '%s'" % ctid
    if int(ctid) < 1000:
      vps = make_Container(int(ctid), 0, CONTAINER_CONF)
      try:
        vps.firewall_by_group(vps.groups(), 'add')
      except StandardError:
        pass # probably a standby container without a valid user

# Load the configuration and override the default variables.
# First check that it is safe to do so
try:
  mode = os.lstat(EXEC_CONFIG_FILE)[stat.ST_MODE]
except OSError:
  print "The configuration file is not readable."
  sys.exit(1)

if mode & stat.S_IWOTH:
  print "configuration file is writable by others; exiting.\n"
  sys.exit(1)

execfile(EXEC_CONFIG_FILE)
setup_log(SERVICE_LOG, None) # no log id because the log file is unique to this script
log("re-adding container rules to firewall")
main()
