#!/usr/bin/python

# LXCF - LXC Facility
# copyright (C) 2014 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.

#
# A scripts for submit LXCF command in which queue daemons are running.
#

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

import sys
import os
import fcntl
import uuid
import time


MAXBUF = 4096-1

# LOGFILE
LOGFILE = '/var/log/lxcf/lxcf-messages'

# QUEUE FILE
MAXQNUM = 100
QDIR = '/var/lib/lxcf'
HQFILE = '/var/lib/lxcf/hqueue'
QQFILE = '/var/lib/lxcf/qqueue'
LQFILE = '/var/lib/lxcf/lqueue'

# word count
def wc(filename):
    try:
        f = open(filename, "r")
    except:
        return 0

    fcntl.flock(f.fileno(), fcntl.LOCK_EX)

    Lines = f.readlines()
    num = len(Lines)

    # unlock hosts file
    fcntl.flock(f.fileno(), fcntl.LOCK_UN)

    f.close()

    return num

# check root
if os.geteuid() != 0:
    print "error: Because you are not root, you cannot execute this command. "
    exit(-1)

argc = len(sys.argv)

if (argc < 3):
    print "usage : lxcf submit [-h] [-q] [-l] COMMAND"
    quit()

QUEUE = sys.argv[1]

if sys.argv[2] != "sysgen" and sys.argv[2] != "sysgen-n" and \
   sys.argv[2] != "clone" and sys.argv[2] != "clone-n" and \
   sys.argv[2] != "set" and sys.argv[2] != "set-n" and \
   sys.argv[2] != "erase" and sys.argv[2] != "erase-n" and \
   sys.argv[2] != "update" and sys.argv[2] != "deploy" and \
   sys.argv[2] != "snapshot" and sys.argv[2] != "restore":
    print "error : The execution command of submit is sysgen, sysgen-n, \
clone, clone-n, set, set-n, update, deploy, snapshot and restore. "
    quit()

QCMD = " ".join(sys.argv[2: argc])

if len(QCMD) > MAXBUF:
    print "error: The command line is too long. "
    quit()

if QCMD.find(";") >= 0 or QCMD.find("&") >= 0 or QCMD.find("`") >= 0 or \
   QCMD.find("$") >= 0 or QCMD.find("|") >= 0:
    print "error: The character not permitted was used (;&`$|). "
    quit()

qnum = wc(HQFILE) + wc(QQFILE) + wc(LQFILE)

if qnum > MAXQNUM:
    print "error: There are a lot of jobs on queue(>",MAXQNUM,")"
    quit()

t = time.asctime()

UUID = str(uuid.uuid1())

QLINE = UUID + " " + QCMD

if QUEUE == "H-QUEUE":
    QFILE = HQFILE
elif QUEUE == "L-QUEUE":
    QFILE = LQFILE
else:
    QFILE = QQFILE

f = open(QFILE, "a")
fcntl.flock(f.fileno(), fcntl.LOCK_EX)

f.write(QLINE+"\n")

# unlock hosts file
fcntl.flock(f.fileno(), fcntl.LOCK_UN)

f.close()

try:
    with open(LOGFILE, 'a') as fd:
        fd.write("### "+t+" SUBMITED :"+QUEUE+": "+QLINE+" ###\n")
except:
    pass

print "submited : "+UUID


quit()
