Skip to content

Commit

Permalink
Move wskadmin to tools/admin.
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbah authored and dubee committed Sep 22, 2016
1 parent ee32831 commit 82fd93f
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 26 deletions.
8 changes: 2 additions & 6 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</natures>
<filteredResources>
<filter>
<id>1472286169084</id>
<id>1474064946127</id>
<name></name>
<type>30</type>
<matcher>
Expand All @@ -34,10 +34,6 @@
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-projectRelativePath-matches-false-false-core/controller</arguments>
</matcher>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-projectRelativePath-matches-false-false-tools/cli</arguments>
</matcher>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-projectRelativePath-matches-false-false-common/scala</arguments>
Expand All @@ -58,7 +54,7 @@
</matcher>
</filter>
<filter>
<id>1472286169086</id>
<id>1474064946128</id>
<name></name>
<type>10</type>
<matcher>
Expand Down
2 changes: 1 addition & 1 deletion bin/wskadmin
File renamed without changes.
74 changes: 74 additions & 0 deletions tools/admin/wskprop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python

#
# Copyright 2015-2016 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##
# Helper methods for whisk properties
##

import os
import pkg_resources

def propfile(base):
if base != '':
filename = '%s/whisk.properties' % base
if os.path.isfile(filename) and os.path.exists(filename):
return filename
else:
parent = os.path.dirname(base)
return propfile(parent) if parent != base else ''
else:
return ''

def importPropsIfAvailable(filename):
thefile = open(filename, 'r') if os.path.isfile(filename) and os.path.exists(filename) else []
return importProps(thefile)

def importProps(stream):
props = {}
for line in stream:
parts = line.split('=')
if len(parts) >= 1:
key = parts[0].strip()
if len(parts) >= 2:
val = parts[1].strip()
if key != '' and val != '':
props[key.upper().replace('.','_')] = val
elif key != '':
props[key.upper().replace('.','_')] = ''
return props

#
# Returns a triple of (length(requiredProperties), requiredProperties, deferredInfo)
# prints a message if a required property is not found
#
def checkRequiredProperties(requiredPropertiesByName, properties):
requiredPropertiesByValue = [ getPropertyValue(key, properties) for key in requiredPropertiesByName ]
requiredProperties = dict(zip(requiredPropertiesByName, requiredPropertiesByValue))
invalidProperties = [ key for key in requiredPropertiesByName if requiredProperties[key] == None ]
deferredInfo = ''
for key, value in requiredProperties.items():
if value == None or value == '':
print 'property "%s" not found in environment or property file' % key
else:
deferredInfo += 'using %(key)s = %(value)s\n' % {'key': key, 'value': value}
return (len(invalidProperties) == 0, requiredProperties, deferredInfo)

def getPropertyValue(key, properties):
evalue = os.environ.get(key)
value = evalue if evalue != None and evalue != '' else properties[key] if key in properties else None
return value
97 changes: 97 additions & 0 deletions tools/admin/wskutil.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#
# Copyright 2015-2016 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
import json
import httplib
import ssl
import base64
from urlparse import urlparse

# global configurations, can control whether to allow untrusted certificates on HTTPS connections
httpRequestProps = { 'secure': True }

def request(method, urlString, body = '', headers = {}, auth = None, verbose = False, https_proxy = os.getenv('https_proxy', None)):
url = urlparse(urlString)
if url.scheme == 'http':
conn = httplib.HTTPConnection(url.netloc)
else:
if httpRequestProps['secure'] or not hasattr(ssl, '_create_unverified_context'):
conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy)
else:
conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy, context=ssl._create_unverified_context())
if https_proxy:
conn.set_tunnel(url.netloc)

if auth != None:
auth = base64.encodestring(auth).replace('\n', '')
headers['Authorization'] = 'Basic %s' % auth

if verbose:
print '========'
print 'REQUEST:'
print '%s %s' % (method, urlString)
print 'Headers sent:'
print getPrettyJson(headers)
if body != '':
print 'Body sent:'
print body

try:
conn.request(method, urlString, body, headers)
res = conn.getresponse()
body = ''
try:
body = res.read()
except httplib.IncompleteRead as e:
body = e.partial

# patch the read to return just the body since the normal read
# can only be done once
res.read = lambda: body

if verbose:
print '--------'
print 'RESPONSE:'
print 'Got response with code %s' % res.status
print 'Body received:'
print res.read()
print '========'
return res
except Exception, e:
res = dict2obj({ 'status' : 500, 'error': str(e) })
return res

def getPrettyJson(obj):
return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))

# class to convert dictionary to objects
class dict2obj(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError('object has no attribute "%s"' % name)

def __setattr__(self, name, value):
self[name] = value

def __delattr__(self, name):
if name in self:
del self[name]
else:
raise AttributeError('object has no attribute "%s"' % name)
4 changes: 0 additions & 4 deletions tools/build/scanCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,8 @@
####
def exceptional_paths():
return [
"bin/wsk",
"bin/wskadmin",
"bin/wskdev",
"bin/wsk/mac",
"bin/wsk/linux",
"bin/wsk/windows",
"tests/build/reports"
]

Expand Down
2 changes: 1 addition & 1 deletion tools/deploy/deployInvoker
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import json
# ************************************************************************************
scriptDir = sys.path[0]
rootDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(rootDir, "core", "pythonAction", "cli")
cliDir = os.path.join(rootDir, "tools", "admin")
sys.path.insert(1, cliDir)

healthDir = os.path.join(os.path.join(rootDir, "tools"), "health")
Expand Down
7 changes: 3 additions & 4 deletions tools/health/isAlive
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ import monitorUtil
# wskprop is in the sibling directory
scriptDir = sys.path[0]
rootDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(rootDir, "core", "pythonAction", "cli")
cliDir = os.path.join(rootDir, "tools", "admin")
sys.path.insert(1, cliDir)
from wskprop import *


import wskprop

def getProp(key) :
try:
Expand Down Expand Up @@ -130,7 +129,7 @@ if (args.dir is None):
print 'Must specify directory with -d'
exit(-1)

whiskprops = importPropsIfAvailable(propfile(args.dir))
whiskprops = wskprop.importPropsIfAvailable(wskprop.propfile(args.dir))

if (args.wait is None):
delay = 0
Expand Down
10 changes: 5 additions & 5 deletions tools/health/killComponent
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import subprocess
import argparse

scriptDir = sys.path[0]
openDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(openDir, "core", "pythonAction", "cli")
rootDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(rootDir, "tools", "admin")
sys.path.insert(1, cliDir)

healthDir = os.path.join(os.path.join(openDir, "tools"), "health")
healthDir = os.path.join(os.path.join(rootDir, "tools"), "health")
sys.path.insert(1, healthDir)

from wskprop import *
import wskprop
import monitorUtil

whiskprops = importPropsIfAvailable(propfile(scriptDir))
whiskprops = wskprop.importPropsIfAvailable(wskprop.propfile(scriptDir))

parser = argparse.ArgumentParser(description='Kill some whisk component by stopping its container.')
parser.add_argument('component', help='name of component to kill: elk, kafka, ...')
Expand Down
7 changes: 3 additions & 4 deletions tools/health/kvstore
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ import json
import base64

# ************************************************************************************
# * Path stuff. If this script is moved, it should be enough to adjust blueDir only. *
# * Path stuff. If this script is moved, it should be enough to adjust rootDir only. *
# ************************************************************************************
scriptDir = sys.path[0]
blueDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(blueDir, "core", "pythonAction", "cli")

rootDir = os.path.dirname(os.path.dirname(scriptDir))
cliDir = os.path.join(rootDir, "tools", "admin")
sys.path.insert(1, cliDir)

# After path adjusted, do the remaining imports.
Expand Down
2 changes: 1 addition & 1 deletion tools/vagrant/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ $script_end = <<SCRIPT
# Setup OpenWhisk CLI
su vagrant -c 'mkdir ${HOME}/bin'
su vagrant -c 'ln -s ${OPENWHISK_HOME}/core/pythonAction/cli/wskadmin ${HOME}/bin/wskadmin'
su vagrant -c 'ln -s ${OPENWHISK_HOME}/tools/admin/wskadmin ${HOME}/bin/wskadmin'
su vagrant -c 'ln -s ${OPENWHISK_HOME}/tools/build/redo ${HOME}/bin/wskdev'
echo ${OPENWHISK_HOME}/bin/linux/amd64/wsk -i '"$@"' > ${HOME}/bin/wsk
chmod +x ${HOME}/bin/wsk
Expand Down

0 comments on commit 82fd93f

Please sign in to comment.