#!/usr/bin/python
# copyright (C) 2013 FUJITSU LIMITED All Rights Reserved

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
# 02110-1301, USA.

# -*- coding: utf-8 -*-

import sys, os, shutil
import json
from optparse import OptionParser,OptionGroup

parser = OptionParser()

(options, args) = parser.parse_args(sys.argv)
argc = len(args)

if ((argc != 2) and (argc != 3)) :
  print "usage : lxcf load LXCNAME [ JSON_FILE ]"
  quit()

LXCNAME = args[1]


RESOURCEFILE ="/etc/lxcf/rsc/"+LXCNAME+"/resource.val"

if (argc == 2) :
  RESOURCEFILE2 = ""
else :
  RESOURCEFILE2 = args[2]

# The unit of "K,M,G" is converted. 
def checkKMG(val) :
  last = val[-1:] 
  if (val == '-') :
    return val
  elif (last == "k") or (last == "K") :
    return str(int(1024.*float(val[:-1])))
  elif (last == "m") or (last == "M") :
    return str(int(1024.*1024.*float(val[:-1])))
  elif (last == "g") or (last == "G") :
    return str(int(1024.*1024.*1024.*float(val[:-1])))
  elif (last == "t") or (last == "T") :
    return str(int(1024.*1024.*1024.*1024.*float(val[:-1])))
  else :
    return str(val)

# The unit of blkio is converted. 
def blkio_val(val) :
  s = val.split()
  if (len(s) != 2) :
    printf ("illegal blkio value")
    exit(-1)
  s2 = checkKMG(s[1])
  return s[0]+" "+str(s2)

# load resource json file
try :
  f = open(RESOURCEFILE)
except :
  print("can't open "+LXCNAME)
  quit()

try : 
  data = json.load(f)
except :
  f.close()
  print("illegal json file"+RESOURCEFILE)
  quit()

f.close()

# load parameter resource json file
data2 = []
if argc == 3 :
  try :
    f2 = open(RESOURCEFILE2)
  except :
    print("can't open "+RESOURCEFILE2)
    quit()

  try : 
    data2 = json.load(f2)
  except :
    print("illegal json file"+RESOURCEFILE2)
    f2.close()
    exit(1)

  f2.close()


# get resource date
try : 
  name = data['name']
except :
  pass

try : 
  cpun = data['cpun']
  cpun = data2['cpun']
except :
  pass

try : 
  cpurate = data['cpurate']
  cpurate = data2['cpurate']
except :
  pass

try : 
  numa = data['numa']
  numa = data2['numa']
except :
  pass

try : 
  memlimit = checkKMG(data['memlimit'])
  memlimit = checkKMG(data2['memlimit'])
except :
  pass

try : 
  memswlimit = checkKMG(data['memswlimit'])
  memswlimit = checkKMG(data2['memswlimit'])
except :
  pass

try : 
  blkio_r = data['blkio_r']
  blkio_r = data2['blkio_r']
except :
  pass

try : 
  blkio_w = data['blkio_w']
  blkio_w = data2['blkio_w']
except :
  pass

try : 
  blkiops_r = data['blkiops_r']
  blkiops_r = data2['blkiops_r']
except :
  pass

try : 
  blkiops_w = data['blkiops_w']
  blkiops_w = data2['blkiops_w']
except :
  pass

try : 
  net_cls = data['net_cls']
  net_cls = data2['net_cls']
except :
  pass

os.system("/usr/lib/lxcf/lxcf-resource1  cpun "+LXCNAME+" "+cpun)
os.system("/usr/lib/lxcf/lxcf-resource1  cpurate "+LXCNAME+" "+cpurate)
os.system("/usr/lib/lxcf/lxcf-resource1  numa "+LXCNAME+" "+numa)
os.system("/usr/lib/lxcf/lxcf-resource1  memlimit "+LXCNAME+" "+memlimit)
os.system("/usr/lib/lxcf/lxcf-resource1  memswlimit "+LXCNAME+" "+memswlimit)
os.system("/usr/lib/lxcf/lxcf-resource1  net_cls "+LXCNAME+" "+net_cls)

for val in blkio_r :
  os.system("/usr/lib/lxcf/lxcf-resource1  blkio_r "+LXCNAME+" "+blkio_val(val))

for val in blkio_w :
  os.system("/usr/lib/lxcf/lxcf-resource1  blkio_w "+LXCNAME+" "+blkio_val(val))

for val in blkiops_r :
  os.system("/usr/lib/lxcf/lxcf-resource1  blkiops_r "+LXCNAME+" "+blkio_val(val))

for val in blkiops_w :
  os.system("/usr/lib/lxcf/lxcf-resource1  blkiops_w "+LXCNAME+" "+blkio_val(val))

exit(0)
