Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use a different mirror than shinken.io for installing packages. #1735

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion bin/shinken
Original file line number Diff line number Diff line change
@@ -139,7 +139,8 @@ class CLICommander(object):
'''after each install. Create an account at '''
'''http://shinken.io and go to '''
'''http://shinken.io/~''',
'values': [('proxy', ''), ('api_key', '')]
'values': [('proxy', ''), ('api_key', ''), ('url', 'http://shinken.io/'), ('hard_ssl_name_check', '1'),
('ca_cert', ''), ('client_cert', ''), ('client_key', '')]
}
}

@@ -394,6 +395,16 @@ def main(custom_args=None):
add_help_option=False)
parser.add_option('--proxy', dest="proxy",
help="""Proxy URI. Like http://user:password@proxy-server:3128""")
parser.add_option('--url', dest='url',
help="""Base mirror URI. Like http://shinken.io/""")
parser.add_option('--hard-ssl-name-check', dest='hard_ssl_name_check',
help="""Set SSL certificate check. Like 1 (default, verify certificate) or 0""")
parser.add_option('--ca-cert', dest='ca_cert',
help="""Set SSL CA certificate. Like /etc/ssl/certs/ca.pem""")
parser.add_option('--client-cert', dest='client_cert',
help="""Set SSL client certificate. Like /etc/ssl/certs/cert.pem""")
parser.add_option('--client-key', dest='client_key',
help="""Set SSL client private key. Like /etc/ssl/private/key.pem""")
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, "
@@ -452,6 +463,26 @@ def main(custom_args=None):
CONFIG['shinken.io']['proxy'] = opts.proxy
logger.debug("Using given proxy in command line")

if opts.url:
CONFIG['shinken.io']['url'] = opts.url
logger.debug("Using given url in command line")

if opts.hard_ssl_name_check:
CONFIG['shinken.io']['hard_ssl_name_check'] = opts.hard_ssl_name_check
logger.debug("Using given hard_ssl_name_check in command line")

if opts.ca_cert:
CONFIG['shinken.io']['ca_cert'] = opts.ca_cert
logger.debug("Using given ca_cert in command line")

if opts.client_cert:
CONFIG['shinken.io']['client_cert'] = opts.client_cert
logger.debug("Using given client_cert in command line")

if opts.client_key:
CONFIG['shinken.io']['client_key'] = opts.client_key
logger.debug("Using given client_key in command line")

CLI = CLICommander(CONFIG, cfg, opts)

# We should look on the sys.argv if we find a valid keywords to
@@ -500,6 +531,16 @@ def main(custom_args=None):
# If the user explicitely set the proxy, take it!
if opts.proxy:
CONFIG['shinken.io']['proxy'] = opts.proxy
if opts.url:
CONFIG['shinken.io']['url'] = opts.url
if opts.hard_ssl_name_check:
CONFIG['shinken.io']['hard_ssl_name_check'] = opts.hard_ssl_name_check
if opts.ca_cert:
CONFIG['shinken.io']['ca_cert'] = opts.ca_cert
if opts.client_cert:
CONFIG['shinken.io']['client_cert'] = opts.client_cert
if opts.client_key:
CONFIG['shinken.io']['client_key'] = opts.client_key

# Maybe he/she just want to list our commands?
if opts.do_list:
93 changes: 49 additions & 44 deletions cli/shinkenio/cli.py
Original file line number Diff line number Diff line change
@@ -56,6 +56,43 @@ def read_package_json(fd):
return package_json


def prepare_curl_connection(url_path, post=0, verbose=0):
"""Create a cURL connection with all standard options

:param url_path: "/push" or "/grab" or "/search"
:return:
:rtype:
"""
proxy = CONFIG['shinken.io']['proxy']
url = CONFIG['shinken.io']['url']
if url.endswith('/'):
url = url[:-1]
hard_ssl_name_check = CONFIG['shinken.io']['hard_ssl_name_check']
ca_cert = CONFIG['shinken.io']['ca_cert']
client_cert = CONFIG['shinken.io']['client_cert']
client_key = CONFIG['shinken.io']['client_key']
# Ok we will push the file with a 10s timeout
c = pycurl.Curl()
c.setopt(c.POST, post)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.TIMEOUT, 300)
if proxy:
c.setopt(c.PROXY, proxy)
if hard_ssl_name_check != '0':
c.setopt(pycurl.SSL_VERIFYPEER, 1)
c.setopt(pycurl.SSL_VERIFYHOST, 2)
else:
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
if ca_cert:
c.setopt(pycurl.CAINFO, ca_cert)
if client_cert:
c.setopt(pycurl.SSLCERT, client_cert)
if client_key:
c.setopt(pycurl.SSLKEY, client_key)
c.setopt(c.URL, "%s%s" % (url, url_path))
c.setopt(c.VERBOSE, verbose)
return c


def create_archive(to_pack):
@@ -100,28 +137,19 @@ def tar_exclude_filter(f):

def publish_archive(archive):
# 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, 1)
c.setopt(c.CONNECTTIMEOUT, 30)
c.setopt(c.TIMEOUT, 300)
if proxy:
c.setopt(c.PROXY, proxy)
c.setopt(c.URL, "http://shinken.io/push")
c = prepare_curl_connection('/push', post=1, verbose=1)
c.setopt(c.HTTPPOST, [("api_key", api_key),
("data",
(c.FORM_FILE, str(archive),
c.FORM_CONTENTTYPE, "application/x-gzip"))
])
response = StringIO()
c.setopt(pycurl.WRITEFUNCTION, response.write)
c.setopt(c.VERBOSE, 1)

try:
c.perform()
except pycurl.error, exp:
except pycurl.error as exp:
logger.error("There was a critical error : %s", exp)
sys.exit(2)
return
@@ -152,22 +180,12 @@ def do_publish(to_pack='.'):
################" *********************** SEARCH *************** ##################
def search(look_at):
# Now really publish it
proxy = CONFIG['shinken.io']['proxy']
api_key = CONFIG['shinken.io']['api_key']
args = {'keywords': ','.join(look_at)}
c = prepare_curl_connection(str('/searchcli?' + urllib.urlencode(args)))

# Ok we will push the file with a 10s 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)

args = {'keywords':','.join(look_at)}
c.setopt(c.URL, str('shinken.io/searchcli?'+urllib.urlencode(args)))
response = StringIO()
c.setopt(pycurl.WRITEFUNCTION, response.write)
#c.setopt(c.VERBOSE, 1)
try:
c.perform()
except pycurl.error, exp:
@@ -200,11 +218,11 @@ def print_search_matches(matches):
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']
@@ -243,7 +261,7 @@ def inventor(look_at):
inventory = CONFIG['paths']['inventory']
logger.debug("dumping inventory %s", inventory)
# get all sub-direcotries

for d in os.listdir(inventory):
if os.path.exists(os.path.join(inventory, d, 'package.json')):
if not look_at or d in look_at:
@@ -309,26 +327,13 @@ def _chmodplusx(d):
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))
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, exp:
except pycurl.error as exp:
logger.error("There was a critical error : %s", exp)
sys.exit(2)
return ''
@@ -489,7 +494,7 @@ def install_package(pname, raw, update_only=False):
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')
@@ -544,7 +549,7 @@ def install_package(pname, raw, update_only=False):
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)
5 changes: 5 additions & 0 deletions manpages/sources/shinken.rst
Original file line number Diff line number Diff line change
@@ -31,6 +31,11 @@ OPTIONS

--version show program's version number and exit
--proxy=PROXY Proxy URI. Like http://user:password@proxy-server:3128
--url Base mirror URI. Like http://shinken.io/
--hard-ssl-name-check Set SSL certificate check. Like 1 (default, verify certificate) or 0
--ca-cert Set SSL CA certificate. Like /etc/ssl/certs/ca.pem
--client-cert Set SSL client certificate. Like /etc/ssl/certs/cert.pem
--client-key Set SSL client private key. Like /etc/ssl/private/key.pem
-A API_KEY, --api-key=API_KEY
Your API key for uploading the package to the
Shinken.io website. If you don't have one, please go