/usr/bin/shinken is in shinken-common 2.0.3-4.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | #! /usr/bin/python
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
# Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken 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.
#
# Shinken 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 Shinken. If not, see <http://www.gnu.org/licenses/>.
import optparse
import sys
import os
import re
import tempfile
import json
import shutil
import zipfile
import tarfile
import pycurl
import urllib
import ConfigParser
import imp
import shlex
import readline
from StringIO import StringIO
try:
import shinken
from shinken.bin import VERSION
except ImportError:
# If importing shinken fails, try to load from current directory
# or parent directory to support running without installation.
# Submodules will then be loaded from there, too.
import imp
imp.load_module('shinken', *imp.find_module('shinken', [os.path.realpath("."), os.path.realpath(".."), os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "..")]))
from shinken.bin import VERSION
from shinken.log import logger, cprint
from shinken.objects.config import Config
logger.set_level(logger.WARNING)
logger.set_display_time(False)
logger.set_display_level(False)
logger.log_colors[logger.DEBUG] = 'cyan'
logger.log_colors[logger.INFO] = 'magenta'
CONFIG = {}
class Dummy():
def __init__(self): pass
def add(self, obj): pass
logger.load_obj(Dummy())
if os.name != 'nt':
DEFAULT_CFG = os.path.expanduser('~/.shinken.ini')
else:
DEFAULT_CFG = 'c:\\shinken\\etc\\shinken.ini'
# This will allow to add comments to the generated configuration file, once
# by section.
class ConfigParserWithComments(ConfigParser.RawConfigParser):
def add_comment(self, section, comment):
if not comment.startswith('#'):
comment = '#'+comment
self.set(section, ';%s' % (comment,), None)
def write(self, fp):
"""Write an .ini-format representation of the configuration state."""
if self._defaults:
fp.write("[%s]\n" % ConfigParser.DEFAULTSECT)
for (key, value) in self._defaults.items():
self._write_item(fp, key, value)
fp.write("\n")
for section in self._sections:
fp.write("[%s]\n" % section)
for (key, value) in self._sections[section].items():
self._write_item(fp, key, value)
fp.write("\n")
def _write_item(self, fp, key, value):
if key.startswith(';') and value is None:
fp.write("%s\n" % (key[1:],))
else:
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
# Commander is the main class for managing the CLI session and behavior
class CLICommander(object):
ini_defaults = {'paths': {
'comment' : ''' # Set the paths according to your setup. Defaults follow
# the Linux Standard Base''',
'values' : [('etc', '/etc/shinken'), ('lib','/var/lib/shinken'),
('share', '%(lib)s/share'), ('cli','%(lib)s/cli'),
('packs', '%(etc)s/packs'), ('modules', '/usr/share/pyshared/shinken/modules'),
('doc', '%(lib)s/doc'), ('inventory', '%(lib)s/inventory'),
('libexec', '%(lib)s/libexec'),
]}
,
'shinken.io':{
'comment': '''# Options for connection to the shinken.io website.
# proxy: curl style, should look as http://user:password@server:3128
# api_key: useful for publishing packages or earn XP after each install. Create an account at http://shinken.io and go to http://shinken.io/~
''',
'values' : [('proxy',''), ('api_key','')]}
}
def __init__(self, config, opts):
self.keywords = {}
self.config = config
self.init_done = False
# We will now try to load the keywords from the modules
self.load_cli_mods(opts)
self.completion_matches = []
def load_cli_mods(self, opts):
# Main list of keywords for the first parameter
self.keywords = {}
if not 'paths' in self.config or not 'cli' in self.config.get('paths',[]):
# We are dign the init, so bail out
if opts.do_init:
return
logger.error('Cannot load cli commands, missing paths or cli entry in the config')
return
self.init_done = True
cli_mods_dir = os.path.abspath(self.config['paths']['cli'])
logger.debug ("WILL LOAD THE CLI DIR %s" % cli_mods_dir)
cli_mods_dirs = [os.path.join(cli_mods_dir, d) for d in os.listdir(cli_mods_dir) if os.path.isdir(os.path.join(cli_mods_dir, d))]
for d in cli_mods_dirs:
f = os.path.join(d, 'cli.py')
if os.path.exists(f):
dname = os.path.split(d)[1]
# Let's load it, but first att it to sys.path
sys.path.append(d)
# Load this PATH/cli.py file
m = imp.load_source(dname, f)
# Link the CONFIG objet into it
m.CONFIG = self.config
exports = getattr(m, 'exports', {})
for (f, v) in exports.iteritems():
m_keywords = v.get('keywords', [])
for k in m_keywords:
e = {'f':f, 'args' : v.get('args', []),
'description':v.get('description', ''),
'came_from':dname}
# Finally save it
self.keywords[k] = e
logger.debug('We load the keywords %s' % self.keywords)
def loop(self):
if not self.init_done:
logger.error('CLI loading not done: missing configuration data. Please run --init')
return
readline.parse_and_bind('tab: complete')
# Use the CLI as completer
readline.set_completer(self.complete)
# EMACS rules :) / VI sucks
readline.parse_and_bind('set editing-mode emacs')
# Try to read and save the history when exiting
histfile = os.path.join(os.path.expanduser("~"), ".shinken.history")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
while True:
try:
line = raw_input('> ')
except EOFError:
print ''
break
line = line.strip()
if line in ['quit', 'bye', 'sayonara']:
break
if not line:
continue
if line.startswith('!'):
self.execute_shell(line[1:])
continue
# More cleassic command
args = shlex.split(line.encode('utf8', 'ignore'))
logger.debug("WANT TO CALL WITH ARGS %s" % args)
self.one_loop(args)
def execute_shell(self, line):
output = os.popen(line).read()
print output
# Execute a function based on the command line
def one_loop(self, command_args):
if not self.init_done:
logger.error('CLI loading not done: missing configuration data. Please run --init')
return
logger.debug("ARGS: %s" % command_args)
keyword = command_args.pop(0)
mod = self.keywords.get(keyword, None)
if mod is None:
logger.error("UNKNOWN command %s" % keyword)
return
# Now prepare a new parser, for the command call this time
command_parser = optparse.OptionParser(
'',
version="%prog " + VERSION)
command_parser.prog = keyword
f_args = []
for a in mod.get('args', []):
n = a.get('name', None)
if n is None:
continue
default = a.get('default', Dummy)
description = a.get('description', '')
_type = a.get('type', 'standard')
if n.startswith('-'):
# Get a clean version of the parameter, without - or --
dest = n[1:]
if dest.startswith('-'):
dest = dest[1:]
# And if the parameter is like download-only, map it to
# download_only
dest = dest.replace('-', '_')
if _type == 'bool':
command_parser.add_option(n, action='store_true', dest=dest, help=(description))
else:
command_parser.add_option(n, dest=dest, help=(description))
cmd_opts, cmd_args = command_parser.parse_args(command_args)
f = mod.get('f', None)
logger.debug("CALLING" + str(f) + "WITH" + str(cmd_args) + "and" + str(cmd_opts) + str(type(cmd_opts)) + str(dir(cmd_opts)))
f(*cmd_args,**cmd_opts.__dict__)
# Complete is a bit strange in readline. It will call it as it do not answser None, by increasing the
# state int for each call. So don't loop forever!
def complete(self, text, state):
#print "STATE?", text, state
# New completion call
if state == 0:
self.completion_matches = []
#print "TRY TO COMPLETE", text
text = text.strip()
args = shlex.split(text.encode('utf8', 'ignore'))
#print "ARGS", args
if len(args) == 0:
args = ['']
keyword = args[0]
# Trying to expand the command name
if len(args) == 1 and state == 0:
for k in self.keywords:
if k.startswith(text):
self.completion_matches.append(k)
response = None
try:
response = self.completion_matches[state]
except IndexError:
response = None
#print "CALL", text, "state", state, response
return response
if __name__ == '__main__':
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()
# 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.set_level(logger.INFO)
if opts.do_debug:
logger.set_level(logger.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 not section in CONFIG:
CONFIG[section] = {}
for (key, value) in cfg.items(section):
CONFIG[section][key] = value
CLI = CLICommander(CONFIG, 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()
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))
for (mod_name, d) in all_from.iteritems():
print '%s:' % mod_name
for (k,m) in d:
cprint('\t%s ' % k , 'green', end='')
cprint(': %s' % m['description'])
sys.exit(0)
if opts.do_init:
new_cfg = ConfigParserWithComments()
modify = False
if not cfg:
cfg = ConfigParser.ConfigParser()
# Import data from the loaded configuration
for section in cfg.sections():
if not new_cfg.has_section(section):
new_cfg.add_section(section)
for (k, v) in cfg.items(section):
new_cfg.set(section, k, v)
for (s,d) in CLI.ini_defaults.iteritems():
comment = d.get('comment', '')
values = d.get('values', [])
if not cfg.has_section(s) and not new_cfg.has_section(s):
print "Creating ini section", s
new_cfg.add_section(s)
modify = True
if comment:
new_cfg.add_comment(s, comment)
for (k,v) in values:
if not cfg.has_option(s, k):
new_cfg.set(s,k,v)
modify = True
#print new_cfg.items(s)
if modify:
print "Saving the new configuration file", opts.iniconfig
with open(opts.iniconfig, 'wb') as configfile:
new_cfg.write(configfile)
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)
|