#!/usr/bin/python -W ignore::DeprecationWarning
#
# archipel-importvirtualmachine
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import sqlite3
import sys
from optparse import OptionParser
import datetime
import random
import string
import commands

def insert(dbfile, uuid, xmppserver, name, password):
    db = sqlite3.connect(dbfile)
    jid = "%s@%s" % (uuid, xmppserver)

    c = db.cursor()
    c.execute("INSERT INTO virtualmachines (jid, password, name, creation_date, comment) VALUES (?, ?, ?, ?, ?)", (jid, password, name, datetime.datetime.now(), "user-imported virtual machine"))
    db.commit()
    c.close()
    db.close()
    print "\033[32mSUCCESS: Virtual machine %s has been inserted with JID %s\033[0m" % (name, jid)


if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-f", "--file",
                        dest="dbfile",
                        help="the sqlite3 file of hypervisor",
                        metavar="FILE",
                        default="/var/lib/archipel/hypervisor.sqlite3")
    parser.add_option("-x", "--xmppserver",
                        dest="xmppserver",
                        help="the current Archipel's XMPP server",
                        metavar="SERVER")
    parser.add_option("-u", "--uuid",
                        dest="uuid",
                        help="the UUID of the exting libvirt machine",
                        metavar="UUID")
    parser.add_option("-p", "--password",
                        dest="password",
                        help="OPTIONAL, the password the VM should use to connect to XMPP server. Generated if ommitted",
                        metavar="PASSWORD",
                        default=None)
    parser.add_option("-n", "--name",
                        dest="name",
                        help="the name you want to use for this virtual machine",
                        metavar="NAME")

    options, args = parser.parse_args()

    for p in ("/var/lock/subsys/archipel", "/var/lock/archipel", "/tmp/.lock-archipel"):
        if os.path.exists(p):
            print "\033[31mERROR: Archipel is running. please stop it before running this script\n\033[0m"
            sys.exit(1)

    if not options.dbfile or not options.uuid or not options.xmppserver or not options.name :
        parser.error("argument format is wrong. please use the --help option to get help")
        sys.exit(1)

    if not os.path.exists(options.dbfile):
        parser.error("database %s doesn't exist" % options.dbfile)
        sys.exit(1)

    insert(options.dbfile, options.uuid, options.xmppserver, options.name, options.password)
