Beispiel #1
0
def main(dryr=0, dockit_log=dockit_log_file):

    parser = OptionParser()
    parser = add_options(parser)


    options, arguments = parser.parse_args()
    globalopts = dict(options.__dict__)

    pull_option_args = ['image', 'dockerrepo']
    build_option_args = ['dockerfile', 'imgtag']
    start_option_args = ['image', 'imgtag', 'count']
    # gluster_optins_args = ['gluvolume', 'gluinst']

    anyopt = [options.pullimg, options.buildimg, options.startc,
              options.dry]
    anyopt_dict = {'pullimg': pull_option_args, 'buildimg':
                   build_option_args, 'startc': start_option_args}

    check = [o for o in anyopt if o]
    if not check:
        logging.error("You missed one of the must required option..  "
                      "reread and execute.... exiting .")
        print_menu()
        sys.exit(1)
    if options.gluinst or options.gluvolume:
        if not options.glumode:
            logger.error("You can not use gluster actions without -g option")
            sys.exit(1)
    if options.glumode and not options.gluvolume and not options.gluinst:
        logger.warn("-g dont have any effect without --gv or --gi options")

    final_true_list = [
        [key, value] for key, value in globalopts.items()
        if value]
    logger.debug("Input \t :%s", final_true_list)
    final_list = []
    for it in final_true_list:
        for k, v in anyopt_dict.items():
            if k == it[0]:
                final_list.append(v)
    # print final_list
    my_good = list(set([item for sublist in final_list for item in sublist]))

    if options.startc and options.buildimg:
        my_good.remove('image')
        logger.debug("Required Parameters for your request:%s", my_good)

    if options.pullimg and options.startc:
        if options.imgtag is None:
            options.imgtag = 'latest'
            logger.debug("image tag : %s , docker repo:%s",
                         options.imgtag, options.dockerrepo)

    if options.pullimg and options.buildimg:
        logger.error("Only one at a time, pull or build")
        sys.exit(1)

    for good in my_good:
        if not options.__dict__[good]:
            logger.error("\n \t Unfortunately  You Missed:%s", good)
            parser.print_help()
            sys.exit(1)

    logger.info(
	"Dockit starting.. Process logs are available at:%s", dockit_log_file)

    if options.count:
        options.count = int(options.count)

    if options.startc:
        options = process_startc_option(options)

    if options.dry:
        logger.info("Dry run : Dockit will not attempt to install any package")
        dryr = 1
    else:
        logger.debug("Install packages if required, this is not a dry run...")

    try:
        sysobj = Packageinst(dryr)
        if sysobj:
            sysobj.getsysinfo()
            ret = sysobj.checkprereq()

            if ret:
                logger.info("Success:Pre-requisites are installed")
            else:
                logger.debug("Either install it or let me install ")
                sys.exit(1)
            logger.debug("Going to check/start docker daemon")
            procd = Procstart(process="docker")
            checkret = procd.checkproc()
            if not checkret:
                ret = procd.execproc()
                if ret:
                    logger.info("Successfully started docker deamon... ")
                else:
                    logger.error('Exiting')
                    sys.exit(1)
            procd.infoproc()
            logger.debug("Connecting to the docker deamon")

            talktoDocker(
                options.pullimg, options.image, options.imgtag,
                options.count, options.dockerfile, options.dockerrepo,
                options.buildimg, options.startc, options.glumode,
                options.gluinst, options.gluvolume)

    except Exception as e:
        logger.debug(e)
        sys.exit(1)
Beispiel #2
0
def main(dryr=0, dockit_log=dockit_log_file):

    parser = OptionParser()
    parser.add_option("-d", "--dry_run",
                      action="store_true", dest="dry", default=False,
                      help="Do dry run - dont try to install any packages")

    parser.add_option("-p", "--pullimage",
                      action="store_true", dest="pullimg", default=False,
                      help="Whether to pull from the docker repo ? Need to specify dockerrepo and image name ")


    parser.add_option("-s", "--startc",
                      action="store_true", dest="startc", default=False,
                      help="Whether to start from an image ? Need to specify image and tag ")

    parser.add_option("-b", "--buildimage",
                      action="store_true", dest="buildimg", default=False,
                      help="Whether to build image from the dockerfile? Need to specify dockerfile path, and imagetag")

    parser.add_option("-g", "--gluster_mode",
                      action="store_true", dest="glumode", default=False,
                      help="Configure gluster volume in containers")

    parser.add_option("-i", "--image",
                      dest="image", help="Image name  - Containers will be based on this image", metavar="IMAGE")

    parser.add_option("-t", "--imgtag",
                      dest="imgtag", help="Image tag name  - Containers will be assigned this tag", metavar="IMAGETAG")

    parser.add_option("-n", "--count",
                      dest="count", help="Number of containers to start  - ", metavar="COUNT")

    parser.add_option("-c", "--configfile",
                      dest="configfile", help="COnfig file path to read gluster configuration  - ", metavar="CONFIGFILE")

    parser.add_option("-f", "--dockerfile",
                      dest="dockerfile", help="Docker file path to build the container  - ", metavar="DOCKERFILE")

    parser.add_option("-r", "--dockerrepo",
                      dest="dockerrepo", help="Docker repository name with a trailing blackslash  - ", metavar="DOCKERREPO")

    parser.add_option("--gv", "--glustervolume",
                      action="store_true", dest = "gluvolume", default=False,help="Gluster Volume Creation  inside containers  - Valid with -g option ")
                     # dest="gluvolume", help="Gluster Volume Creation  inside containers  - Valid with -g option ", metavar="GLUSTERVOLUME")

    parser.add_option("--gi", "--glusterinstall",
                      dest="gluinst", help="Install gluster inside containers  - Valid with -g option ", metavar="GLUSTERVERSION")

    logger.info("Dockit starting.. Process logs are available at:%s", dockit_log_file)

    options, arguments = parser.parse_args()
    globalopts=dict(options.__dict__)

    pull_option_args = ['image','dockerrepo']
    #pull_option_args = ['image']
    build_option_args = ['dockerfile','imgtag']
    start_option_args = ['image', 'imgtag', 'count']
    gluster_optins_args = ['gluvolume','gluinst']

    anyopt = [ options.pullimg , options.buildimg , options.startc , options.dry]
    anyopt_dict = { 'pullimg':pull_option_args , 'buildimg':build_option_args , 'startc':start_option_args }

    check = [o for o in anyopt if o]
    if not check:
        logging.error( "You missed one of the must required option..  reread and execute.... exiting .")
        print_menu()
        sys.exit(1)
    if options.gluinst or options.gluvolume:
        if not options.glumode:
            logger.error("You can not use gluster actions without -g option")
            sys.exit(1)
    if options.glumode and not options.gluvolume and not options.gluinst:
        logger.warn("-g dont have any effect without --gv or --gi options")

    final_true_list = [[key,value] for key,value in globalopts.items() if value != False if value != None]
    logger.debug("Input \t :%s" ,final_true_list)
    final_list=[]
    for it in final_true_list:
        for k,v in anyopt_dict.items():
            if k == it[0]:
                final_list.append(v)
    #print final_list
    my_good = list(set([item for sublist in final_list for item in sublist]))

    if options.startc and options.buildimg:
        my_good.remove('image')
        logger.debug("Required Parameters for your request:%s", my_good)

    if options.pullimg and options.startc:
        if options.imgtag== None:
            options.imgtag='latest'
            logger.debug("image tag : %s , docker repo:%s", options.imgtag, options.dockerrepo)

    if options.pullimg and options.buildimg:
        logger.error( "Only one at a time, pull or build")
        sys.exit(1)

    for good in my_good:
        if not options.__dict__[good]:
                logger.error("\n \t Unfortunately  You Missed:%s", good)
                parser.print_help()
                sys.exit(1)


    if options.count:
        options.count=int(options.count)

    if options.startc:

        prefer =  raw_input ("Do you want to continue (y/n)")
        if prefer=='y':
            logger.info( "Proceeding ")
            if options.glumode:


                if options.gluinst:
                    logger.info( "Need to install gluster inside containers")
                    gluster_config['GLUSTER_VERSION'] = options.gluinst

                if options.gluvolume:
                    logger.info( "\n Need to configure gluster volume..\n")

                    g_voltype=''
                    if not options.configfile:
                        g_voltype = raw_input("Gluster Volume Type (ex: 2x2x1 where (distribute,replica, stripe count in order)\t :")
                        g_volname = raw_input("Gluster Volume Name (ex: glustervol)\t :")
                        g_export  = raw_input("Gluster Export Dir Name (ex: /rhs_bricks)\t :")
                        g_brick_file = raw_input("Gluster brick file (ex: /home/configfile)\t :")
                    else:
                        logger.info( "Reading gluster configuration from config file")
                        print read_config_file(options.configfile)

                    try:
                        if g_voltype:
                            volumeconfig = re.search(r'([0-9]+)x([0-9]+)x([0-9]+)', g_voltype)
                        else:
                            gluster_config['VOL_TYPE'] = gluster_config.get('VOL_TYPE', '1x2x1')
                            gluster_config['VOLNAME']=gluster_config.get('VOLNAME', 'defaultVol')
                            gluster_config['SERVER_EXPORT_DIR']=gluster_config.get('SERVER_EXPORT_DIR','/defaultExport')
                            volumeconfig = re.search(r'([0-9]+)x([0-9]+)x([0-9]+)',gluster_config['VOL_TYPE'])
                        distributecount = volumeconfig.group(1)
                        replicacount = volumeconfig.group(2)
                        stripevcount = volumeconfig.group(3)
                    except Exception as e:
                        logger.debug( "Error in parsing volume type string..exiting")
                        logger.debug(e)
                        sys.exit(1)

                    if distributecount == '0':
                        distributecount =  1
                    if replicacount == '0':
                        replicacount =  1
                    if stripevcount == '0':
                        stripevcount =   1

                    options.count = int(distributecount) * int(replicacount) * int(stripevcount)
                    logger.info( "No of gluster containers to spawn:%s" , options.count)
                    prefer = raw_input ("Do you want to continue (y/n):")
                    if prefer == 'y':
                        if not options.configfile:
                            gluster_config['VOLNAME']=g_volname
                            gluster_config['VOL_TYPE']=g_voltype
                            gluster_config['SERVER_EXPORT_DIR']=g_export
                            gluster_config['BRICK_FILE']=g_brick_file
                            #gluster_config['BRICKS'] = read_config_file_b(g_brick_file)
                            read_config_file_b(g_brick_file)
                        else:
                            logger.info( "Configuration read from configuration file")

                        logger.info("%s", gluster_config)
                    else:
                        logger.error( "Exiting.. Invoke dockit command with proper option of gluster mode")
                        sys.exit(1)
            else:
                logger.info( "Run containers natively, no mode configured")
            prefer=''
        else:
            logger.debug( "Exiting ")
            sys.exit(1)

    if options.dry:
        logger.info("Dry run : Dockit will not attempt to install any package")
        dryr= 1
    else:
        logger.debug("Install packages if required, this is not a dry run...")

    try:
        sysobj = Packageinst(dryr)
        if sysobj:
            sysobj.getsysinfo()
            ret = sysobj.checkprereq()

            if ret:
                logger.info("Success:Pre-requisites are installed")
            else:
                logger.debug("Either install it or let me install ")
                sys.exit(1)
            logger.debug("Going to check/start docker daemon")
            procd = Procstart(process="docker")
            checkret = procd.checkproc()
            if not checkret:
                ret = procd.execproc()
                if ret:
                    logger.info("Successfully started docker deamon... ")
                else:
                    logger.error('Exiting')
                    sys.exit(1)
            procd.infoproc()
            logger.debug("Connecting to the docker deamon")

            talktoDocker(options.pullimg, options.image, options.imgtag, options.count, options.dockerfile,
                         options.dockerrepo , options.buildimg, options.startc, options.glumode, options.gluinst, options.gluvolume)

    except Exception as e:
        logger.debug(e)
        sys.exit(1)
Beispiel #3
0
class Packageinst:
    """
    Class to retrieve system specific information, then call
    """

    def __init__(self, dryrun):
        self.skipflag = dryrun

    def getsysinfo(self):
        """
        This function will fetch system/platform information
        """
        try:
            dist, ver, name = platform.dist()
        except Exception as e:
            logger.debug(e)
            sys.exit(1)
        else:
            sysdict['dist'] = dist
            sysdict['ver'] = int(ver.split('.')[0])
            sysdict['name'] = name
            logger.info("Distribution:%s", sysdict['dist'])
            return True

    def checkprereq(self):
        """
        This function will check for pre-req packages
        which need to be installed and if its not available
        it will be installed
        """

        rhelflag = 0
        if sysdict['dist'] == "fedora":
            req_pcks = fedora_req_pcks
            rhelflag = 1
        elif sysdict['dist'] == "redhat":
            if sysdict['ver'] < 7:
                req_pcks = rhel_req_pcks
            else:
                req_pcks = rhel7_req_pcks
            rhelflag = 1
        elif sysdict['dist'] == "centos":
            if sysdict['ver'] < 7:
                req_pcks = centos_req_pcks
            else:
                req_pcks = centos7_req_pcks
            rhelflag = 1
        elif sysdict['dist'] == "Ubuntu":
            req_pcks = ubuntu_req_pcks
        else:
            logger.error("Unknown Distribution.")
            sys.exit(1)

        logger.info(
            "Distribution:%s Required %s packages \n\t \t \t "
            "Making yum transactions", sysdict['dist'], req_pcks)

        if rhelflag == 1:
            try:
                import yum
            except Exception as e:
                print "Error when importing yum module"
                sys.exit(1)
            yb = yum.YumBase()
            yb.conf.cache = os.geteuid() != 1
            for pck in req_pcks:
                if yb.rpmdb.searchNevra(name=pck):
                    logger.info("%s -> Installed" % (pck))
                    avail_pcks.append(pck)
                else:
                    logger.info("%s -> not installed" % (pck))
                    mis_pcks.append(pck)
                    if not self.skipflag:
                        try:
                            if pck == "python-docker-py":
                                logger.debug("Trying with pip")
                                cmd = "sudo pip install {0} -U "
                                cmd += ">/dev/null".format("docker-py")
                                os.system(cmd)
                                mis_pcks.remove(pck)
                            else:
                                logger.info(
                                    "Unknown package for me to install "
                                    "via pip.. Proceeding")
                        except Exception as e:
                            logger.error(e)
                            logger.error(
                                "Error occurred when trying to install %s "
                                "using pip -> Try to install "
                                "manually" % (pck))
                            sys.exit(1)
                        try:
                            yb.install(name=pck)
                            time.sleep(5)
                        except yum.Errors.InstallError, err:
                            logger.error(
                                "exiting : Error when installing "
                                "package %s", pck)
                            logger.error("%s", (str(err)))
                            sys.exit(1)
                        except Exception as e:
                            logger.critical(e)
                            logger.error(
                                "Error occurred when trying to install %s "
                                "-> Try to install manually" % (pck))
                            sys.exit(1)
            if len(mis_pcks) > 0:
                if self.skipflag:
                    logger.info(
                        "Please install the %s packages "
                        "and try again.", mis_pcks)

                    sys.exit(1)
                else:
                    try:
                        yb.resolveDeps()
                        yb.buildTransaction()
                        yb.processTransaction()
                        return True
                    except Exception as e:
                        logger.warn(
                            "Yum transaction failure:%s .. \n"
                            "Giving one more try", e)
                        for pkgs in mis_pcks:
                            os_cmd = "yum install -y %s >/dev/null" % (
                                pkgs)
                            if os.system(os_cmd):
                                print ("Failed again to install "
                                       "%s package" % (pkgs))
                                sys.exit(1)
Beispiel #4
0
                                logger.critical(e)
                                logger.error("Error occurred when trying to install %s -> Try to install manually" % (pck))
                                sys.exit(1)
                if len(mis_pcks) > 0:
                    if self.skipflag:
                        logger.info("Please install the %s packages and try again.", mis_pcks)

                        sys.exit(1)
                    else:
                        try:
                            yb.resolveDeps()
                            yb.buildTransaction()
                            yb.processTransaction()
                            return True
                        except Exception as e:
                            logger.warn(
                                "Yum transaction failure:%s .. \n Giving one more try", e)
                            for pkgs in mis_pcks:
                                os_cmd = "yum install -y %s >/dev/null" %(pkgs)
                                if os.system(os_cmd):
                                    print "Failed again to install %s package" % (pkgs)
                                    sys.exit(1)
            else:
                for pck in req_pcks:
                    u_pkg_check = "dpkg-query -l %s >/dev/null" %(pck)
                    if (os.system(u_pkg_check)) == 0:
                        logger.info("%s -> Installed" % (pck))
                        avail_pcks.append(pck)
                    else:
                        logger.info("%s -> not installed" % (pck))
                        mis_pcks.append(pck)
                        if not self.skipflag: