def grab_package(pname): cprint('Grabbing : ' , end='') cprint('%s' % pname, 'green') # Now really publish it proxy = CONFIG['shinken.io']['proxy'] api_key = CONFIG['shinken.io']['api_key'] # Ok we will push the file with a 5m timeout c = pycurl.Curl() c.setopt(c.POST, 0) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 300) if proxy: c.setopt(c.PROXY, proxy) c.setopt(c.URL, str('shinken.io/grab/%s' % pname)) response = StringIO() c.setopt(pycurl.WRITEFUNCTION, response.write) #c.setopt(c.VERBOSE, 1) try: c.perform() except pycurl.error, exp: logger.error("There was a critical error : %s", exp) sys.exit(2) return ''
def grab_package(pname): cprint('Grabbing : ' , end='') cprint('%s' % pname, 'green') # Now really publish it proxy = CONFIG['shinken.io']['proxy'] api_key = CONFIG['shinken.io']['api_key'] # Ok we will push the file with a 5m timeout c = pycurl.Curl() c.setopt(c.POST, 0) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 300) if proxy: c.setopt(c.PROXY, proxy) c.setopt(c.URL, str('shinken.io/grab/%s' % pname)) response = StringIO() c.setopt(pycurl.WRITEFUNCTION, response.write) #c.setopt(c.VERBOSE, 1) try: c.perform() except pycurl.error, exp: logger.error("There was a critical error : %s" % exp) return ''
def grab_package(pname): cprint('Grabbing : ' , end='') cprint('%s' % pname, 'green') # Now really publish it proxy = CONFIG['shinken.io']['proxy'] api_key = CONFIG['shinken.io']['api_key'] # Ok we will push the file with a 10s timeout c = pycurl.Curl() c.setopt(c.POST, 0) c.setopt(c.CONNECTTIMEOUT, 10) c.setopt(c.TIMEOUT, 10) if proxy: c.setopt(c.PROXY, proxy) c.setopt(c.URL, str('shinken.io/grab/%s' % pname)) response = StringIO() c.setopt(pycurl.WRITEFUNCTION, response.write) #c.setopt(c.VERBOSE, 1) c.perform() r = c.getinfo(pycurl.HTTP_CODE) c.close() if r != 200: logger.error("There was a critical error : %s" % response.getvalue()) sys.exit(2) else: ret = response.getvalue() logger.debug("CURL result len : %d " % len(ret)) return ret
def print_search_matches(matches): if len(matches) == 0: print "Sorry, no match found in shinken.io" return for p in matches: name = p['name'] user_id = p['user_id'] keywords = p['keywords'] description = p['description'] cprint('%s ' % name, 'green', end='') cprint('(%s) [%s] : %s' % (user_id, ','.join(keywords), description))
def print_search_matches(matches): if len(matches) == 0: print "Sorry, no match found in shinken.io" return for p in matches: name = p['name'] user_id = p['user_id'] keywords = p['keywords'] description = p['description'] cprint('%s ' % name , 'green', end='') cprint('(%s) [%s] : %s' % (user_id, ','.join(keywords), description))
def do_install(pname, local, download_only): raw = '' if local: pname, raw = grab_local(pname) if not local: raw = grab_package(pname) if download_only: tmpf = os.path.join(tempfile.gettempdir(), pname + '.tar.gz') try: f = open(tmpf, 'wb') f.write(raw) f.close() cprint('Download OK: %s' % tmpf, 'green') except Exception, exp: logger.error("Package save fail: %s" % exp) return
def do_install(pname, local, download_only): raw = '' if local: pname, raw = grab_local(pname) if not local: raw = grab_package(pname) if download_only: tmpf = os.path.join(tempfile.gettempdir(), pname+'.tar.gz') try: f = open(tmpf, 'wb') f.write(raw) f.close() cprint('Download OK: %s' % tmpf, 'green') except Exception, exp: logger.error("Package save fail: %s" % exp) return
def print_search_matches(matches): if len(matches) == 0: logger.warning("No match founded in shinken.io") return # We will sort and uniq results (maybe we got a all search # so we will have both pack&modules, but some are both ps = {} names = [p["name"] for p in matches] names = list(set(names)) names.sort() for p in matches: name = p["name"] ps[name] = p for name in names: p = ps[name] user_id = p["user_id"] keywords = p["keywords"] description = p["description"] cprint("%s " % name, "green", end="") cprint("(%s) [%s] : %s" % (user_id, ",".join(keywords), description))
def do_install(pname='', local=False, download_only=False): raw = '' if local: pname, raw = grab_local(pname) if not local: if pname == '': logger.error('Please select a package to instal') return raw = grab_package(pname) if download_only: tmpf = os.path.join(tempfile.gettempdir(), pname+'.tar.gz') try: f = open(tmpf, 'wb') f.write(raw) f.close() cprint('Download OK: %s' % tmpf, 'green') except Exception, exp: logger.error("Package save fail: %s", exp) sys.exit(2) return
def print_search_matches(matches): if len(matches) == 0: logger.warning("No match founded in shinken.io") return # We will sort and uniq results (maybe we got a all search # so we will have both pack&modules, but some are both ps = {} names = [p['name'] for p in matches] names = list(set(names)) names.sort() for p in matches: name = p['name'] ps[name] = p for name in names: p = ps[name] user_id = p['user_id'] keywords = p['keywords'] description = p['description'] cprint('%s ' % name , 'green', end='') cprint('(%s) [%s] : %s' % (user_id, ','.join(keywords), description))
def grab_package(pname): cprint('Grabbing : ' , end='') cprint('%s' % pname, 'green') c = prepare_curl_connection('/grab/%s' % pname) response = StringIO() c.setopt(pycurl.WRITEFUNCTION, response.write) #c.setopt(c.VERBOSE, 1) try: c.perform() except pycurl.error as exp: logger.error("There was a critical error : %s", exp) sys.exit(2) return '' r = c.getinfo(pycurl.HTTP_CODE) c.close() if r != 200: logger.error("There was a critical error : %s", response.getvalue()) sys.exit(2) else: ret = response.getvalue() logger.debug("CURL result len : %d ", len(ret)) return ret
def main(custom_args=None): parser = optparse.OptionParser( '', version="%prog " + VERSION, add_help_option=False) parser.add_option('--proxy', dest="proxy", help="""Proxy URI. Like http://user:password@proxy-server:3128""") parser.add_option('-A', '--api-key', dest="api_key", help=("Your API key for uploading the package to the " "Shinken.io website. If you don't have one, " "please go to your account page")) parser.add_option('-l', '--list', action='store_true', dest="do_list", help=("List available commands")) parser.add_option('--init', action='store_true', dest="do_init", help=("Initialize/refill your shinken.ini file " "(default to %s)" % DEFAULT_CFG)) parser.add_option('-D', action='store_true', dest="do_debug", help=("Enable the debug mode")) parser.add_option('-c', '--config', dest="iniconfig", default=DEFAULT_CFG, help=("Path to your shinken.ini file. Default: %s" % DEFAULT_CFG)) parser.add_option('-v', action='store_true', dest="do_verbose", help=("Be more verbose")) parser.add_option('-h', '--help', action='store_true', dest="do_help", help=("Print help")) # First parsing, for purely internal parameters, but disable # errors, because we only want to see the -D -v things old_error = parser.error parser.error = lambda x: 1 opts, args = parser.parse_args(custom_args) # reenable the errors for later use parser.error = old_error do_help = opts.do_help if do_help and len(args) == 0: parser.print_help() sys.exit(0) if opts.do_verbose: logger.setLevel('INFO') if opts.do_debug: logger.setLevel('DEBUG') cfg = None if not os.path.exists(opts.iniconfig): logger.debug('Missing configuration file!') else: cfg = ConfigParser.ConfigParser() cfg.read(opts.iniconfig) for section in cfg.sections(): if section not in CONFIG: CONFIG[section] = {} for (key, value) in cfg.items(section): CONFIG[section][key] = value # Now replace if given in command line if opts.api_key: CONFIG['shinken.io']['api_key'] = opts.api_key logger.debug("Using given api-key in command line") if opts.proxy: CONFIG['shinken.io']['proxy'] = opts.proxy logger.debug("Using given proxy in command line") CLI = CLICommander(CONFIG, cfg, opts) # We should look on the sys.argv if we find a valid keywords to # call in one loop or not. def hack_sys_argv(): command_values = [] internal_values = [] # print "RARGS", parser.rargs founded = False for arg in sys.argv: if arg in CLI.keywords: founded = True # Did we found it? if founded: command_values.append(arg) else: # ok still not, it's for the shinekn command so internal_values.append(arg) sys.argv = internal_values return command_values # We will remove specific commands from the sys.argv list and keep # them for parsing them after command_args = hack_sys_argv() # Global command parsing, with the error enabled this time opts, args = parser.parse_args(custom_args) if opts.do_help: if len(command_args) == 0: logger.error("Cannot find any help for you") sys.exit(1) a = command_args.pop(0) if a not in CLI.keywords: logger.error("Cannot find any help for %s" % a) sys.exit(1) cprint('%s' % a, 'green') for arg in CLI.keywords[a]['args']: n = arg.get('name', '') desc = arg.get('description', '') cprint('\t%s' % n, 'green', end='') cprint(': %s' % desc) sys.exit(0) # If the user explicitely set the proxy, take it! if opts.proxy: CONFIG['shinken.io']['proxy'] = opts.proxy # Maybe he/she just want to list our commands? if opts.do_list: if not CLI.init_done: sys.exit(0) print "Available commands:" all_from = {} for (k, m) in CLI.keywords.iteritems(): came_from = m['came_from'] if came_from not in all_from: all_from[came_from] = [(k, m)] else: all_from[came_from].append((k, m)) _mods = all_from.keys() _mods.sort() for mod_name in _mods: d = all_from[mod_name] print '%s:' % mod_name d.sort(key=lambda e: e[0]) for (k, m) in d: cprint('\t%s ' % k, 'green', end='') cprint(': %s' % m['description']) sys.exit(0) if opts.do_init: write_ini_file(cfg, CLI, opts) sys.exit(0) # if just call shinken, we must open a prompt, but will be for another version if len(command_args) == 0: CLI.loop() sys.exit(0) # If it's just a one call shot, do it! CLI.one_loop(command_args)
def install_package(pname, raw): logger.debug("Installing the package %s (size:%d)" % (pname, len(raw))) tmpdir = os.path.join(tempfile.gettempdir(), pname) logger.debug("Unpacking the package into %s" % tmpdir) if os.path.exists(tmpdir): logger.debug("Removing previous tmp dir %s" % tmpdir) shutil.rmtree(tmpdir) logger.debug("Creating temporary dir %s" % tmpdir) os.mkdir(tmpdir) package_content = [] # open a file with the content f = StringIO(raw) tar_file = tarfile.open(fileobj=f, mode="r") logger.debug("Tar file contents:") for i in tar_file.getmembers(): path = i.name if path == ".": continue if path.startswith("/") or ".." in path: logger.error("SECURITY: the path %s seems dangerous!" % path) return # Adding all files into the package_content list package_content.append({"name": i.name, "mode": i.mode, "type": i.type, "size": i.size}) logger.debug("\t%s" % path) # Extract all in the tmpdir tar_file.extractall(tmpdir) tar_file.close() # Now we look at the package.json that will give us our name and co package_json_p = os.path.join(tmpdir, "package.json") if not os.path.exists(package_json_p): logger.error("Error : bad archive : Missing file %s" % package_json_p) return None package_json = read_package_json(open(package_json_p)) logger.debug("Package.json content %s " % package_json) modules_dir = CONFIG["paths"]["modules"] share_dir = CONFIG["paths"]["share"] packs_dir = CONFIG["paths"]["packs"] etc_dir = CONFIG["paths"]["etc"] doc_dir = CONFIG["paths"]["doc"] inventory_dir = CONFIG["paths"]["inventory"] test_dir = CONFIG["paths"].get("test", "/__DONOTEXISTS__") for d in (modules_dir, share_dir, packs_dir, doc_dir, inventory_dir): if not os.path.exists(d): logger.error("The installation directory %s is missing!" % d) return p_share = os.path.join(tmpdir, "share") logger.debug("TMPDIR:%s modules_dir:%s pname:%s" % (tmpdir, modules_dir, pname)) # Now install the package from $TMP$/module/* to $MODULES$/pname/* p_module = os.path.join(tmpdir, "module") if os.path.exists(p_module): logger.info("Installing the module package data") mod_dest = os.path.join(modules_dir, pname) if os.path.exists(mod_dest): logger.info("Removing previous module install at %s" % mod_dest) shutil.rmtree(mod_dest) # shutil will do the create dir shutil.copytree(p_module, mod_dest) logger.info("Copy done in the module directory %s" % mod_dest) p_doc = os.path.join(tmpdir, "doc") logger.debug("TMPDIR:%s doc_dir:%s pname:%s" % (tmpdir, doc_dir, pname)) # Now install the package from $TMP$/doc/* to $MODULES$/doc/source/89_packages/pname/* if os.path.exists(p_doc): logger.info("Installing the doc package data") doc_dest = os.path.join(doc_dir, "source", "89_packages", pname) if os.path.exists(doc_dest): logger.info("Removing previous doc install at %s" % doc_dest) shutil.rmtree(doc_dest) # shutil will do the create dir shutil.copytree(p_doc, doc_dest) logger.info("Copy done in the doc directory %s" % doc_dest) # Now install the pack from $TMP$/pack/* to $PACKS$/pname/* p_pack = os.path.join(tmpdir, "pack") if os.path.exists(p_pack): logger.info("Installing the pack package data") pack_dest = os.path.join(packs_dir, pname) if os.path.exists(pack_dest): logger.info("Removing previous pack install at %s" % pack_dest) shutil.rmtree(pack_dest) # shutil will do the create dir shutil.copytree(p_pack, pack_dest) logger.info("Copy done in the pack directory %s" % pack_dest) # Now install the etc from $TMP$/etc/* to $ETC$/etc/* p_etc = os.path.join(tmpdir, "etc") if os.path.exists(p_etc): logger.info("Merging the etc package data into your etc directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... _copytree(p_etc, etc_dir) logger.info("Copy done in the etc directory %s" % etc_dir) # Now install the tests from $TMP$/tests/* to $TESTS$/tests/* # if the last one is specified on the configuration file (optionnal) p_tests = os.path.join(tmpdir, "test") if os.path.exists(p_tests) and os.path.exists(test_dir): logger.info("Merging the test package data into your test directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... logger.debug("COPYING %s into %s" % (p_tests, test_dir)) _copytree(p_tests, test_dir) logger.info("Copy done in the test directory %s" % test_dir) # then samve the package.json into the inventory dir p_inv = os.path.join(inventory_dir, pname) if not os.path.exists(p_inv): os.mkdir(p_inv) shutil.copy2(package_json_p, os.path.join(p_inv, "package.json")) # and the package content cont = open(os.path.join(p_inv, "content.json"), "w") cont.write(json.dumps(package_content)) cont.close() # THE END, output all is OK :D cprint("OK ", "green", end="") cprint("%s" % pname)
def install_package(pname, raw, update_only=False): if update_only: logger.debug('UPDATE ONLY ENABLED') logger.debug("Installing the package %s (size:%d)", pname, len(raw)) if len(raw) == 0: logger.error('The package %s cannot be found', pname) sys.exit(2) return tmpdir = os.path.join(tempfile.gettempdir(), pname) logger.debug("Unpacking the package into %s", tmpdir) if os.path.exists(tmpdir): logger.debug("Removing previous tmp dir %s", tmpdir) shutil.rmtree(tmpdir) logger.debug("Creating temporary dir %s", tmpdir) os.mkdir(tmpdir) package_content = [] # open a file with the content f = StringIO(raw) tar_file = tarfile.open(fileobj=f, mode="r") logger.debug("Tar file contents:") for i in tar_file.getmembers(): path = i.name if path == '.': continue if path.startswith('/') or '..' in path: logger.error("SECURITY: the path %s seems dangerous!", path) sys.exit(2) return # Adding all files into the package_content list package_content.append( {'name':i.name, 'mode':i.mode, 'type':i.type, 'size':i.size} ) logger.debug("\t%s", path) # Extract all in the tmpdir tar_file.extractall(tmpdir) tar_file.close() # Now we look at the package.json that will give us our name and co package_json_p = os.path.join(tmpdir, 'package.json') if not os.path.exists(package_json_p): logger.error("Error : bad archive : Missing file %s", package_json_p) sys.exit(2) return None package_json = read_package_json(open(package_json_p)) logger.debug("Package.json content %s ", package_json) modules_dir = CONFIG['paths']['modules'] share_dir = CONFIG['paths']['share'] packs_dir = CONFIG['paths']['packs'] etc_dir = CONFIG['paths']['etc'] doc_dir = CONFIG['paths']['doc'] inventory_dir = CONFIG['paths']['inventory'] libexec_dir = CONFIG['paths'].get('libexec', os.path.join(CONFIG['paths']['lib'], 'libexec')) test_dir = CONFIG['paths'].get('test', '/__DONOTEXISTS__') for d in (modules_dir, share_dir, packs_dir, doc_dir, inventory_dir): if not os.path.exists(d): logger.error("The installation directory %s is missing!", d) sys.exit(2) return # Now install the package from $TMP$/share/* to $SHARE$/* p_share = os.path.join(tmpdir, 'share') logger.debug("TMPDIR:%s aahre_dir:%s pname:%s", tmpdir, share_dir, pname) if os.path.exists(p_share): logger.info("Installing the share package data") # shutil will do the create dir _copytree(p_share, share_dir) logger.info("Copy done in the share directory %s", share_dir) logger.debug("TMPDIR:%s modules_dir:%s pname:%s", tmpdir, modules_dir, pname) # Now install the package from $TMP$/module/* to $MODULES$/pname/* p_module = os.path.join(tmpdir, 'module') if os.path.exists(p_module): logger.info("Installing the module package data") mod_dest = os.path.join(modules_dir, pname) if os.path.exists(mod_dest): logger.info("Removing previous module install at %s", mod_dest) shutil.rmtree(mod_dest) # shutil will do the create dir shutil.copytree(p_module, mod_dest) logger.info("Copy done in the module directory %s", mod_dest) p_doc = os.path.join(tmpdir, 'doc') logger.debug("TMPDIR:%s doc_dir:%s pname:%s", tmpdir, doc_dir, pname) # Now install the package from $TMP$/doc/* to $MODULES$/doc/source/89_packages/pname/* if os.path.exists(p_doc): logger.info("Installing the doc package data") doc_dest = os.path.join(doc_dir, 'source', '89_packages', pname) if os.path.exists(doc_dest): logger.info("Removing previous doc install at %s", doc_dest) shutil.rmtree(doc_dest) # shutil will do the create dir shutil.copytree(p_doc, doc_dest) logger.info("Copy done in the doc directory %s", doc_dest) if not update_only: # Now install the pack from $TMP$/pack/* to $PACKS$/pname/* p_pack = os.path.join(tmpdir, 'pack') if os.path.exists(p_pack): logger.info("Installing the pack package data") pack_dest = os.path.join(packs_dir, pname) if os.path.exists(pack_dest): logger.info("Removing previous pack install at %s", pack_dest) shutil.rmtree(pack_dest) # shutil will do the create dir shutil.copytree(p_pack, pack_dest) logger.info("Copy done in the pack directory %s", pack_dest) # Now install the etc from $TMP$/etc/* to $ETC$/etc/* p_etc = os.path.join(tmpdir, 'etc') if os.path.exists(p_etc): logger.info("Merging the etc package data into your etc directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... _copytree(p_etc, etc_dir) logger.info("Copy done in the etc directory %s", etc_dir) # Now install the tests from $TMP$/tests/* to $TESTS$/tests/* # if the last one is specified on the configuration file (optionnal) p_tests = os.path.join(tmpdir, 'test') if os.path.exists(p_tests) and os.path.exists(test_dir): logger.info("Merging the test package data into your test directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... logger.debug("COPYING %s into %s", p_tests, test_dir) _copytree(p_tests, test_dir) logger.info("Copy done in the test directory %s", test_dir) # Now install the libexec things from $TMP$/libexec/* to $LIBEXEC$/* # but also chmod a+x the plugins copied p_libexec = os.path.join(tmpdir, 'libexec') if os.path.exists(p_libexec) and os.path.exists(libexec_dir): logger.info("Merging the libexec package data into your libexec directory") logger.debug("COPYING %s into %s", p_libexec, libexec_dir) # Before be sure all files in there are +x _chmodplusx(p_libexec) _copytree(p_libexec, libexec_dir) logger.info("Copy done in the libexec directory %s", libexec_dir) # then samve the package.json into the inventory dir p_inv = os.path.join(inventory_dir, pname) if not os.path.exists(p_inv): os.mkdir(p_inv) shutil.copy2(package_json_p, os.path.join(p_inv, 'package.json')) # and the package content cont = open(os.path.join(p_inv, 'content.json'), 'w') cont.write(json.dumps(package_content)) cont.close() # We now clean (rm) the tmpdir we don't need any more try: shutil.rmtree(tmpdir, ignore_errors=True) # cannot remove? not a crime except OSError: pass # THE END, output all is OK :D cprint('OK ', 'green', end='') cprint('%s' % pname)
def install_package(pname, raw): logger.debug("Installing the package %s (size:%d)" % (pname, len(raw))) tmpdir = os.path.join(tempfile.gettempdir(), pname) logger.debug("Unpacking the package into %s" % tmpdir) if os.path.exists(tmpdir): logger.debug("Removing previous tmp dir %s" % tmpdir) shutil.rmtree(tmpdir) logger.debug("Creating temporary dir %s" % tmpdir) os.mkdir(tmpdir) # open a file with the content f = StringIO(raw) tar_file = tarfile.open(fileobj=f, mode="r") logger.debug("Tar file contents:") for i in tar_file.getmembers(): path = i.name if path == '.': continue if not path.startswith('./') or '..' in path: logger.error("SECURITY: the path %s seems dangerous!" % path) return logger.debug("\t%s" % path) # Extract all in the tmpdir tar_file.extractall(tmpdir) tar_file.close() package_json_p = os.path.join(tmpdir, 'package.json') if not os.path.exists(package_json_p): logger.error("Error : bad archive : Missing file %s" % package_json_p) return None package_json = read_package_json(open(package_json_p)) logger.debug("Package.json content %s " % package_json) modules_dir = CONFIG['paths']['modules'] share_dir = CONFIG['paths']['share'] packs_dir = CONFIG['paths']['packs'] etc_dir = CONFIG['paths']['etc'] doc_dir = CONFIG['paths']['doc'] test_dir = CONFIG['paths'].get('test', '/__DONOTEXISTS__') for d in (modules_dir, share_dir, packs_dir, doc_dir): if not os.path.exists(d): logger.error("The installation directory %s is missing!" % d) return p_share = os.path.join(tmpdir, 'share') logger.debug("TMPDIR:%s modules_dir:%s pname:%s" %(tmpdir, modules_dir, pname)) # Now install the package from $TMP$/module/* to $MODULES$/pname/* p_module = os.path.join(tmpdir, 'module') if os.path.exists(p_module): logger.info("Installing the module package data") mod_dest = os.path.join(modules_dir, pname) if os.path.exists(mod_dest): logger.info("Removing previous module install at %s" % mod_dest) shutil.rmtree(mod_dest) # shutil will do the create dir shutil.copytree(p_module, mod_dest) logger.info("Copy done in the module directory %s" % mod_dest) p_doc = os.path.join(tmpdir, 'doc') logger.debug("TMPDIR:%s doc_dir:%s pname:%s" %(tmpdir, doc_dir, pname)) # Now install the package from $TMP$/doc/* to $MODULES$/doc/source/89_packages/pname/* if os.path.exists(p_doc): logger.info("Installing the doc package data") doc_dest = os.path.join(doc_dir, 'source', '89_packages', pname) if os.path.exists(doc_dest): logger.info("Removing previous doc install at %s" % doc_dest) shutil.rmtree(doc_dest) # shutil will do the create dir shutil.copytree(p_doc, doc_dest) logger.info("Copy done in the doc directory %s" % doc_dest) # Now install the pack from $TMP$/pack/* to $PACKS$/pname/* p_pack = os.path.join(tmpdir, 'pack') if os.path.exists(p_pack): logger.info("Installing the pack package data") pack_dest = os.path.join(packs_dir, pname) if os.path.exists(pack_dest): logger.info("Removing previous pack install at %s" % pack_dest) shutil.rmtree(pack_dest) # shutil will do the create dir shutil.copytree(p_pack, pack_dest) logger.info("Copy done in the pack directory %s" % pack_dest) # Now install the etc from $TMP$/etc/* to $ETC$/etc/* p_etc = os.path.join(tmpdir, 'etc') if os.path.exists(p_etc): logger.info("Merging the etc package data into your etc directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... _copytree(p_etc, etc_dir) logger.info("Copy done in the etc directory %s" % etc_dir) # Now install the tests from $TMP$/tests/* to $TESTS$/tests/* # if the last one is specified on the configuration file (optionnal) p_tests = os.path.join(tmpdir, 'test') if os.path.exists(p_tests) and os.path.exists(test_dir): logger.info("Merging the test package data into your test directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... logger.debug("COPYING %s into %s" % (p_tests, test_dir)) _copytree(p_tests, test_dir) logger.info("Copy done in the test directory %s" % test_dir) cprint('OK ', 'green', end='') cprint('%s' % pname)
def install_package(pname, raw): logger.debug("Installing the package %s (size:%d)" % (pname, len(raw))) if len(raw) == 0: logger.error('The package %s cannot be found' % pname) return tmpdir = os.path.join(tempfile.gettempdir(), pname) logger.debug("Unpacking the package into %s" % tmpdir) if os.path.exists(tmpdir): logger.debug("Removing previous tmp dir %s" % tmpdir) shutil.rmtree(tmpdir) logger.debug("Creating temporary dir %s" % tmpdir) os.mkdir(tmpdir) package_content = [] # open a file with the content f = StringIO(raw) tar_file = tarfile.open(fileobj=f, mode="r") logger.debug("Tar file contents:") for i in tar_file.getmembers(): path = i.name if path == '.': continue if path.startswith('/') or '..' in path: logger.error("SECURITY: the path %s seems dangerous!" % path) return # Adding all files into the package_content list package_content.append( {'name':i.name, 'mode':i.mode, 'type':i.type, 'size':i.size} ) logger.debug("\t%s" % path) # Extract all in the tmpdir tar_file.extractall(tmpdir) tar_file.close() # Now we look at the package.json that will give us our name and co package_json_p = os.path.join(tmpdir, 'package.json') if not os.path.exists(package_json_p): logger.error("Error : bad archive : Missing file %s" % package_json_p) return None package_json = read_package_json(open(package_json_p)) logger.debug("Package.json content %s " % package_json) modules_dir = CONFIG['paths']['modules'] share_dir = CONFIG['paths']['share'] packs_dir = CONFIG['paths']['packs'] etc_dir = CONFIG['paths']['etc'] doc_dir = CONFIG['paths']['doc'] inventory_dir = CONFIG['paths']['inventory'] libexec_dir = CONFIG['paths'].get('libexec', os.path.join(CONFIG['paths']['lib'], 'libexec')) test_dir = CONFIG['paths'].get('test', '/__DONOTEXISTS__') for d in (modules_dir, share_dir, packs_dir, doc_dir, inventory_dir): if not os.path.exists(d): logger.error("The installation directory %s is missing!" % d) return # Now install the package from $TMP$/share/* to $SHARE$/* p_share = os.path.join(tmpdir, 'share') logger.debug("TMPDIR:%s aahre_dir:%s pname:%s" %(tmpdir, share_dir, pname)) if os.path.exists(p_share): logger.info("Installing the share package data") # shutil will do the create dir _copytree(p_share, share_dir) logger.info("Copy done in the share directory %s" % share_dir) logger.debug("TMPDIR:%s modules_dir:%s pname:%s" %(tmpdir, modules_dir, pname)) # Now install the package from $TMP$/module/* to $MODULES$/pname/* p_module = os.path.join(tmpdir, 'module') if os.path.exists(p_module): logger.info("Installing the module package data") mod_dest = os.path.join(modules_dir, pname) if os.path.exists(mod_dest): logger.info("Removing previous module install at %s" % mod_dest) shutil.rmtree(mod_dest) # shutil will do the create dir shutil.copytree(p_module, mod_dest) logger.info("Copy done in the module directory %s" % mod_dest) p_doc = os.path.join(tmpdir, 'doc') logger.debug("TMPDIR:%s doc_dir:%s pname:%s" %(tmpdir, doc_dir, pname)) # Now install the package from $TMP$/doc/* to $MODULES$/doc/source/89_packages/pname/* if os.path.exists(p_doc): logger.info("Installing the doc package data") doc_dest = os.path.join(doc_dir, 'source', '89_packages', pname) if os.path.exists(doc_dest): logger.info("Removing previous doc install at %s" % doc_dest) shutil.rmtree(doc_dest) # shutil will do the create dir shutil.copytree(p_doc, doc_dest) logger.info("Copy done in the doc directory %s" % doc_dest) # Now install the pack from $TMP$/pack/* to $PACKS$/pname/* p_pack = os.path.join(tmpdir, 'pack') if os.path.exists(p_pack): logger.info("Installing the pack package data") pack_dest = os.path.join(packs_dir, pname) if os.path.exists(pack_dest): logger.info("Removing previous pack install at %s" % pack_dest) shutil.rmtree(pack_dest) # shutil will do the create dir shutil.copytree(p_pack, pack_dest) logger.info("Copy done in the pack directory %s" % pack_dest) # Now install the etc from $TMP$/etc/* to $ETC$/etc/* p_etc = os.path.join(tmpdir, 'etc') if os.path.exists(p_etc): logger.info("Merging the etc package data into your etc directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... _copytree(p_etc, etc_dir) logger.info("Copy done in the etc directory %s" % etc_dir) # Now install the tests from $TMP$/tests/* to $TESTS$/tests/* # if the last one is specified on the configuration file (optionnal) p_tests = os.path.join(tmpdir, 'test') if os.path.exists(p_tests) and os.path.exists(test_dir): logger.info("Merging the test package data into your test directory") # We don't use shutils because it NEED etc_dir to be non existant... # Come one guys..... cp is not as terrible as this... logger.debug("COPYING %s into %s" % (p_tests, test_dir)) _copytree(p_tests, test_dir) logger.info("Copy done in the test directory %s" % test_dir) # Now install the libexec things from $TMP$/libexec/* to $LIBEXEC$/* # but also chmod a+x the plugins copied p_libexec = os.path.join(tmpdir, 'libexec') if os.path.exists(p_libexec) and os.path.exists(libexec_dir): logger.info("Merging the libexec package data into your libexec directory") logger.debug("COPYING %s into %s" % (p_libexec, libexec_dir)) # Before be sure all files in there are +x _chmodplusx(p_libexec) _copytree(p_libexec, libexec_dir) logger.info("Copy done in the libexec directory %s" % libexec_dir) # then samve the package.json into the inventory dir p_inv = os.path.join(inventory_dir, pname) if not os.path.exists(p_inv): os.mkdir(p_inv) shutil.copy2(package_json_p, os.path.join(p_inv, 'package.json')) # and the package content cont = open(os.path.join(p_inv, 'content.json'), 'w') cont.write(json.dumps(package_content)) cont.close() # We now clean (rm) the tmpdir we don't need any more try: shutil.rmtree(tmpdir, ignore_errors=True) # cannot remove? not a crime except OSError: pass # THE END, output all is OK :D cprint('OK ', 'green', end='') cprint('%s' % pname)
def main(custom_args=None): parser = optparse.OptionParser('', version="%prog " + VERSION, add_help_option=False) parser.add_option( '--proxy', dest="proxy", help="""Proxy URI. Like http://user:password@proxy-server:3128""") parser.add_option('-A', '--api-key', dest="api_key", help=("Your API key for uploading the package to the " "Shinken.io website. If you don't have one, " "please go to your account page")) parser.add_option('-l', '--list', action='store_true', dest="do_list", help=("List available commands")) parser.add_option('--init', action='store_true', dest="do_init", help=("Initialize/refill your shinken.ini file " "(default to %s)" % DEFAULT_CFG)) parser.add_option('-D', action='store_true', dest="do_debug", help=("Enable the debug mode")) parser.add_option('-c', '--config', dest="iniconfig", default=DEFAULT_CFG, help=("Path to your shinken.ini file. Default: %s" % DEFAULT_CFG)) parser.add_option('-v', action='store_true', dest="do_verbose", help=("Be more verbose")) parser.add_option('-h', '--help', action='store_true', dest="do_help", help=("Print help")) # First parsing, for purely internal parameters, but disable # errors, because we only want to see the -D -v things old_error = parser.error parser.error = lambda x: 1 opts, args = parser.parse_args(custom_args) # reenable the errors for later use parser.error = old_error do_help = opts.do_help if do_help and len(args) == 0: parser.print_help() sys.exit(0) if opts.do_verbose: logger.setLevel('INFO') if opts.do_debug: logger.setLevel('DEBUG') cfg = None if not os.path.exists(opts.iniconfig): logger.debug('Missing configuration file!') else: cfg = ConfigParser.ConfigParser() cfg.read(opts.iniconfig) for section in cfg.sections(): if section not in CONFIG: CONFIG[section] = {} for (key, value) in cfg.items(section): CONFIG[section][key] = value # Now replace if given in command line if opts.api_key: CONFIG['shinken.io']['api_key'] = opts.api_key logger.debug("Using given api-key in command line") if opts.proxy: CONFIG['shinken.io']['proxy'] = opts.proxy logger.debug("Using given proxy in command line") CLI = CLICommander(CONFIG, cfg, opts) # We should look on the sys.argv if we find a valid keywords to # call in one loop or not. def hack_sys_argv(): command_values = [] internal_values = [] # print "RARGS", parser.rargs founded = False for arg in sys.argv: if arg in CLI.keywords: founded = True # Did we found it? if founded: command_values.append(arg) else: # ok still not, it's for the shinekn command so internal_values.append(arg) sys.argv = internal_values return command_values # We will remove specific commands from the sys.argv list and keep # them for parsing them after command_args = hack_sys_argv() # Global command parsing, with the error enabled this time opts, args = parser.parse_args(custom_args) if opts.do_help: if len(command_args) == 0: logger.error("Cannot find any help for you") sys.exit(1) a = command_args.pop(0) if a not in CLI.keywords: logger.error("Cannot find any help for %s" % a) sys.exit(1) cprint('%s' % a, 'green') for arg in CLI.keywords[a]['args']: n = arg.get('name', '') desc = arg.get('description', '') cprint('\t%s' % n, 'green', end='') cprint(': %s' % desc) sys.exit(0) # If the user explicitely set the proxy, take it! if opts.proxy: CONFIG['shinken.io']['proxy'] = opts.proxy # Maybe he/she just want to list our commands? if opts.do_list: if not CLI.init_done: sys.exit(0) print "Available commands:" all_from = {} for (k, m) in CLI.keywords.iteritems(): came_from = m['came_from'] if came_from not in all_from: all_from[came_from] = [(k, m)] else: all_from[came_from].append((k, m)) _mods = all_from.keys() _mods.sort() for mod_name in _mods: d = all_from[mod_name] print '%s:' % mod_name d.sort(key=lambda e: e[0]) for (k, m) in d: cprint('\t%s ' % k, 'green', end='') cprint(': %s' % m['description']) sys.exit(0) if opts.do_init: write_ini_file(cfg, CLI, opts) sys.exit(0) # if just call shinken, we must open a prompt, but will be for another version if len(command_args) == 0: CLI.loop() sys.exit(0) # If it's just a one call shot, do it! CLI.one_loop(command_args)