Reformat the helm plugin python code 25/55925/1
authorHong Guan <hg4105@att.com>
Fri, 6 Jul 2018 00:11:51 +0000 (20:11 -0400)
committerHong Guan <hg4105@att.com>
Fri, 6 Jul 2018 00:12:41 +0000 (20:12 -0400)
Change-Id: I9b826964db98dcae59ff6165be9ed0897dcd5f47
Issue-ID: CCSDK-351
Signed-off-by: Hong Guan <hg4105@att.com>
helm/plugin/tasks.py
helm/plugin/workflows.py
helm/setup.py

index 5fda295..9d03fba 100644 (file)
@@ -13,7 +13,7 @@
 # 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.
-#============LICENSE_END============================================
+# ============LICENSE_END============================================
 
 from cloudify.decorators import operation
 import shutil
@@ -37,7 +37,6 @@ from cloudify import exceptions
 from cloudify.exceptions import NonRecoverableError
 
 
-
 def execute_command(_command):
     ctx.logger.debug('_command {0}.'.format(_command))
 
@@ -71,71 +70,98 @@ def configure_admin_conf():
     gid = grp.getgrnam('docker').gr_gid
     admin_file_dest = os.path.join(os.path.expanduser('~'), 'admin.conf')
 
-    execute_command('sudo cp {0} {1}'.format('/etc/kubernetes/admin.conf', admin_file_dest))
+    execute_command(
+        'sudo cp {0} {1}'.format('/etc/kubernetes/admin.conf', admin_file_dest))
     execute_command('sudo chown {0}:{1} {2}'.format(uid, gid, admin_file_dest))
 
     with open(os.path.join(os.path.expanduser('~'), '.bashrc'), 'a') as outfile:
         outfile.write('export KUBECONFIG=$HOME/admin.conf')
     os.environ['KUBECONFIG'] = admin_file_dest
 
+
 def get_current_helm_value(chart_name):
-    tiller_host= str(ctx.node.properties['tiller-server-ip'])+':'+str(ctx.node.properties['tiller-server-port'])
-    config_dir_root= str(ctx.node.properties['config-dir'])
-    config_dir=config_dir_root+str(ctx.deployment.id)+'/'
+    tiller_host = str(ctx.node.properties['tiller-server-ip']) + ':' + str(
+        ctx.node.properties['tiller-server-port'])
+    config_dir_root = str(ctx.node.properties['config-dir'])
+    config_dir = config_dir_root + str(ctx.deployment.id) + '/'
     if str_to_bool(ctx.node.properties['tls-enable']):
-        getValueCommand=subprocess.Popen(["helm", "get","values","-a",chart_name,'--host',tiller_host,'--tls','--tls-ca-cert',config_dir+'ca.cert.pem','--tls-cert',config_dir+'helm.cert.pem','--tls-key',config_dir+'helm.key.pem'], stdout=subprocess.PIPE)
+        getValueCommand = subprocess.Popen(
+            ["helm", "get", "values", "-a", chart_name, '--host', tiller_host,
+             '--tls', '--tls-ca-cert', config_dir + 'ca.cert.pem', '--tls-cert',
+             config_dir + 'helm.cert.pem', '--tls-key',
+             config_dir + 'helm.key.pem'], stdout=subprocess.PIPE)
     else:
-        getValueCommand=subprocess.Popen(["helm", "get","values","-a",chart_name,'--host',tiller_host], stdout=subprocess.PIPE)
-    value=getValueCommand.communicate()[0]
-    valueMap= {}
+        getValueCommand = subprocess.Popen(
+            ["helm", "get", "values", "-a", chart_name, '--host', tiller_host],
+            stdout=subprocess.PIPE)
+    value = getValueCommand.communicate()[0]
+    valueMap = {}
     valueMap = yaml.safe_load(value)
     ctx.instance.runtime_properties['current-helm-value'] = valueMap
 
+
 def get_helm_history(chart_name):
-    tiller_host= str(ctx.node.properties['tiller-server-ip'])+':'+str(ctx.node.properties['tiller-server-port'])
-    config_dir_root= str(ctx.node.properties['config-dir'])
-    config_dir=config_dir_root+str(ctx.deployment.id)+'/'
+    tiller_host = str(ctx.node.properties['tiller-server-ip']) + ':' + str(
+        ctx.node.properties['tiller-server-port'])
+    config_dir_root = str(ctx.node.properties['config-dir'])
+    config_dir = config_dir_root + str(ctx.deployment.id) + '/'
     if str_to_bool(ctx.node.properties['tls-enable']):
-        getHistoryCommand=subprocess.Popen(["helm", "history",chart_name,'--host',tiller_host,'--tls','--tls-ca-cert',config_dir+'ca.cert.pem','--tls-cert',config_dir+'helm.cert.pem','--tls-key',config_dir+'helm.key.pem'], stdout=subprocess.PIPE)
+        getHistoryCommand = subprocess.Popen(
+            ["helm", "history", chart_name, '--host', tiller_host, '--tls',
+             '--tls-ca-cert', config_dir + 'ca.cert.pem', '--tls-cert',
+             config_dir + 'helm.cert.pem', '--tls-key',
+             config_dir + 'helm.key.pem'], stdout=subprocess.PIPE)
     else:
-        getHistoryCommand=subprocess.Popen(["helm", "history",chart_name,'--host',tiller_host], stdout=subprocess.PIPE)
-    history=getHistoryCommand.communicate()[0]
-    history_start_output = [line.strip() for line in history.split('\n') if line.strip()]
-    for index  in range(len(history_start_output)):
-        history_start_output[index]=history_start_output[index].replace('\t',' ')
+        getHistoryCommand = subprocess.Popen(
+            ["helm", "history", chart_name, '--host', tiller_host],
+            stdout=subprocess.PIPE)
+    history = getHistoryCommand.communicate()[0]
+    history_start_output = [line.strip() for line in history.split('\n') if
+                            line.strip()]
+    for index in range(len(history_start_output)):
+        history_start_output[index] = history_start_output[index].replace('\t',
+                                                                          ' ')
     ctx.instance.runtime_properties['helm-history'] = history_start_output
 
+
 def mergedict(dict1, dict2):
     for key in dict2.keys():
         if key not in dict1.keys():
             dict1[key] = dict2[key]
         else:
-            if type(dict1[key]) == dict and type(dict2[key]) == dict :
+            if type(dict1[key]) == dict and type(dict2[key]) == dict:
                 mergedict(dict1[key], dict2[key])
             else:
                 dict1[key] = dict2[key]
 
+
 def tls():
     if str_to_bool(ctx.node.properties['tls-enable']):
-        config_dir_root= str(ctx.node.properties['config-dir'])
-        config_dir=config_dir_root+str(ctx.deployment.id)+'/'
-        tls_command= ' --tls --tls-ca-cert '+config_dir+'ca.cert.pem --tls-cert '+config_dir+'helm.cert.pem --tls-key '+config_dir+'helm.key.pem '
+        config_dir_root = str(ctx.node.properties['config-dir'])
+        config_dir = config_dir_root + str(ctx.deployment.id) + '/'
+        tls_command = ' --tls --tls-ca-cert ' + config_dir + 'ca.cert.pem ' \
+                                                             '--tls-cert ' + \
+                      config_dir + 'helm.cert.pem --tls-key ' + config_dir + \
+                      'helm.key.pem '
         ctx.logger.debug(tls_command)
         return tls_command
-    else :
+    else:
         return ''
 
+
 def tiller_host():
-    tiller_host= ' --host '+str(ctx.node.properties['tiller-server-ip'])+':'+str(ctx.node.properties['tiller-server-port'])+' '
+    tiller_host = ' --host ' + str(
+        ctx.node.properties['tiller-server-ip']) + ':' + str(
+        ctx.node.properties['tiller-server-port']) + ' '
     ctx.logger.debug(tiller_host)
     return tiller_host
 
 
 def str_to_bool(s):
-    s=str(s)
+    s = str(s)
     if s == 'True' or s == 'true':
         return True
-    elif s == 'False' or s== 'false':
+    elif s == 'False' or s == 'false':
         return False
     else:
         raise False
@@ -144,54 +170,57 @@ def str_to_bool(s):
 @operation
 def config(**kwargs):
     # create helm value file on K8s master
-    #configPath = ctx.node.properties['config-path']
+    # configPath = ctx.node.properties['config-path']
     configJson = str(ctx.node.properties['config'])
     configUrl = str(ctx.node.properties['config-url'])
     configUrlInputFormat = str(ctx.node.properties['config-format'])
-    runtime_config = str(ctx.node.properties['runtime-config'])  #json
+    runtime_config = str(ctx.node.properties['runtime-config'])  # json
     componentName = ctx.node.properties['component-name']
-    config_dir_root= str(ctx.node.properties['config-dir'])
+    config_dir_root = str(ctx.node.properties['config-dir'])
     stable_repo_url = str(ctx.node.properties['stable-repo-url'])
-    ctx.logger.debug("debug "+ configJson + runtime_config )
-    #load input config
-    config_dir=config_dir_root+str(ctx.deployment.id)
+    ctx.logger.debug("debug " + configJson + runtime_config)
+    # load input config
+    config_dir = config_dir_root + str(ctx.deployment.id)
     try:
         os.makedirs(config_dir)
     except OSError as e:
         if e.errno != errno.EEXIST:
             raise
-    ctx.logger.debug('tls-enable type '+str(type(str_to_bool(ctx.node.properties['tls-enable']))) )
-    #create TLS cert files
+    ctx.logger.debug('tls-enable type ' + str(
+        type(str_to_bool(ctx.node.properties['tls-enable']))))
+
+    # create TLS cert files
     if str_to_bool(ctx.node.properties['tls-enable']):
-        ctx.logger.debug('tls enable' )
+        ctx.logger.debug('tls enable')
         ca_value = ctx.node.properties['ca']
         cert_value = ctx.node.properties['cert']
         key_value = ctx.node.properties['key']
-        ca= open(config_dir+'/ca.cert.pem',"w+")
+        ca = open(config_dir + '/ca.cert.pem', "w+")
         ca.write(ca_value)
         ca.close()
-        cert= open(config_dir+'/helm.cert.pem',"w+")
+        cert = open(config_dir + '/helm.cert.pem', "w+")
         cert.write(cert_value)
         cert.close()
-        key= open(config_dir+'/helm.key.pem',"w+")
+        key = open(config_dir + '/helm.key.pem', "w+")
         key.write(key_value)
         key.close()
     else:
-        ctx.logger.debug('tls disable' )
+        ctx.logger.debug('tls disable')
 
     # create helm value.yaml file
-    configPath=config_dir_root+str(ctx.deployment.id)+'/'+componentName+'.yaml'
+    configPath = config_dir_root + str(
+        ctx.deployment.id) + '/' + componentName + '.yaml'
     ctx.logger.debug(configPath)
 
-    configObj ={}
+    configObj = {}
     if configJson == '' and configUrl == '':
         ctx.logger.debug("Will use default HELM value")
     elif configJson == '' and configUrl != '':
         response = urllib2.urlopen(configUrl)
         if configUrlInputFormat == 'json':
-             configObj = json.load(response)
+            configObj = json.load(response)
         elif configUrlInputFormat == 'yaml':
-             configObj = yaml.load(response)
+            configObj = yaml.load(response)
         else:
             raise NonRecoverableError("Unable to get config input format.")
     elif configJson != '' and configUrl == '':
@@ -204,19 +233,18 @@ def config(**kwargs):
     if runtime_config == '':
         ctx.logger.debug("there is no runtime config value")
     else:
-        runtime_config_obj= json.loads(runtime_config)
-        mergedict(configObj,runtime_config_obj)
+        runtime_config_obj = json.loads(runtime_config)
+        mergedict(configObj, runtime_config_obj)
 
     with open(configPath, 'w') as outfile:
         yaml.safe_dump(configObj, outfile, default_flow_style=False)
 
-    output = execute_command('helm init --client-only --stable-repo-url '+stable_repo_url)
-    if output == False :
+    output = execute_command(
+        'helm init --client-only --stable-repo-url ' + stable_repo_url)
+    if output == False:
         raise NonRecoverableError("helm init failed")
 
 
-
-
 @operation
 def start(**kwargs):
     # install the ONAP Helm chart
@@ -224,88 +252,100 @@ def start(**kwargs):
     chartRepo = ctx.node.properties['chart-repo-url']
     componentName = ctx.node.properties['component-name']
     chartVersion = ctx.node.properties['chart-version']
-    config_dir_root= str(ctx.node.properties['config-dir'])
-    configPath=config_dir_root+str(ctx.deployment.id)+'/'+componentName+'.yaml'
+    config_dir_root = str(ctx.node.properties['config-dir'])
+    configPath = config_dir_root + str(
+        ctx.deployment.id) + '/' + componentName + '.yaml'
     namespace = ctx.node.properties['namespace']
     configJson = str(ctx.node.properties['config'])
     configUrl = str(ctx.node.properties['config-url'])
-    runtimeconfigJson =  str(ctx.node.properties['runtime-config'])
-
+    runtimeconfigJson = str(ctx.node.properties['runtime-config'])
 
     chart = chartRepo + "/" + componentName + "-" + chartVersion + ".tgz"
     chartName = namespace + "-" + componentName
 
     if configJson == '' and runtimeconfigJson == '' and configUrl == '':
-        installCommand = 'helm install '+ chart + ' --name ' + chartName + ' --namespace ' + namespace+tiller_host()+tls()
+        installCommand = 'helm install ' + chart + ' --name ' + chartName + \
+                         ' --namespace ' + namespace + tiller_host() + tls()
     else:
-        installCommand = 'helm install ' + chart + ' --name ' + chartName + ' --namespace ' + namespace + ' -f '+ configPath +tiller_host()+tls()
+        installCommand = 'helm install ' + chart + ' --name ' + chartName + \
+                         ' --namespace ' + namespace + ' -f ' + configPath + \
+                         tiller_host() + tls()
 
-    output =execute_command(installCommand)
-    if output == False :
-        return ctx.operation.retry(message='helm install failed, re-try after 5 second ',
-                                   retry_after=5)
+    output = execute_command(installCommand)
+    if output == False:
+        return ctx.operation.retry(
+            message='helm install failed, re-try after 5 second ',
+            retry_after=5)
 
     get_current_helm_value(chartName)
     get_helm_history(chartName)
 
+
 @operation
 def stop(**kwargs):
     # delete the ONAP helm chart
-    #configure_admin_conf()
+    # configure_admin_conf()
     # get properties from node
     namespace = ctx.node.properties['namespace']
     component = ctx.node.properties['component-name']
     chartName = namespace + "-" + component
-    config_dir_root= str(ctx.node.properties['config-dir'])
+    config_dir_root = str(ctx.node.properties['config-dir'])
     # Delete helm chart
-    command = 'helm delete --purge '+ chartName+tiller_host()+tls()
-    output =execute_command(command)
-    config_dir=config_dir_root+str(ctx.deployment.id)
+    command = 'helm delete --purge ' + chartName + tiller_host() + tls()
+    output = execute_command(command)
+    config_dir = config_dir_root + str(ctx.deployment.id)
     shutil.rmtree(config_dir)
-    if output == False :
+    if output == False:
         raise NonRecoverableError("helm delete failed")
 
+
 @operation
 def upgrade(**kwargs):
     # upgrade the helm chart
     componentName = ctx.node.properties['component-name']
-    config_dir_root= str(ctx.node.properties['config-dir'])
-    configPath=config_dir_root+str(ctx.deployment.id)+'/'+componentName+'.yaml'
+    config_dir_root = str(ctx.node.properties['config-dir'])
+    configPath = config_dir_root + str(
+        ctx.deployment.id) + '/' + componentName + '.yaml'
     componentName = ctx.node.properties['component-name']
     namespace = ctx.node.properties['namespace']
     configJson = kwargs['config']
-    chartRepo =  kwargs['chart_repo']
+    chartRepo = kwargs['chart_repo']
     chartVersion = kwargs['chart_version']
 
     ctx.logger.debug('debug ' + str(configJson))
     chartName = namespace + "-" + componentName
-    chart=chartRepo + "/" + componentName + "-" + chartVersion + ".tgz"
+    chart = chartRepo + "/" + componentName + "-" + chartVersion + ".tgz"
     if str(configJson) == '':
-        upgradeCommand = 'helm upgrade '+ chartName + ' '+ chart+tiller_host()+tls()
+        upgradeCommand = 'helm upgrade ' + chartName + ' ' + chart + \
+                         tiller_host() + tls()
     else:
         with open(configPath, 'w') as outfile:
             yaml.safe_dump(configJson, outfile, default_flow_style=False)
-        #configure_admin_conf()
-        upgradeCommand = 'helm upgrade '+ chartName + ' '+ chart + ' -f ' + configPath+tiller_host()+tls()
-    output=execute_command(upgradeCommand)
-    if output == False :
-        return ctx.operation.retry(message='helm upgrade failed, re-try after 5 second ',
-                                   retry_after=5)
+        # configure_admin_conf()
+        upgradeCommand = 'helm upgrade ' + chartName + ' ' + chart + ' -f ' +\
+                         configPath + tiller_host() + tls()
+    output = execute_command(upgradeCommand)
+    if output == False:
+        return ctx.operation.retry(
+            message='helm upgrade failed, re-try after 5 second ',
+            retry_after=5)
     get_current_helm_value(chartName)
     get_helm_history(chartName)
 
+
 @operation
 def rollback(**kwargs):
     # rollback to some revision
     componentName = ctx.node.properties['component-name']
     namespace = ctx.node.properties['namespace']
     revision = kwargs['revision']
-    #configure_admin_conf()
+    # configure_admin_conf()
     chartName = namespace + "-" + componentName
-    rollbackCommand = 'helm rollback '+ chartName + ' '+ revision+tiller_host()+tls()
-    output=execute_command(rollbackCommand)
-    if output == False :
-        return ctx.operation.retry(message='helm rollback failed, re-try after 5 second ',
-                                   retry_after=5)
+    rollbackCommand = 'helm rollback ' + chartName + ' ' + revision + tiller_host() + tls()
+    output = execute_command(rollbackCommand)
+    if output == False:
+        return ctx.operation.retry(
+            message='helm rollback failed, re-try after 5 second ',
+            retry_after=5)
     get_current_helm_value(chartName)
     get_helm_history(chartName)
index 7dc8272..a37293e 100644 (file)
@@ -13,7 +13,7 @@
 # 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.
-#============LICENSE_END============================================
+# ============LICENSE_END============================================
 
 from cloudify.decorators import workflow
 from cloudify.workflows import ctx
@@ -22,8 +22,10 @@ import urllib2
 import json
 import yaml
 
+
 @workflow
-def upgrade(node_instance_id,config_json,config_url,config_format,chartVersion,chartRepo,**kwargs):
+def upgrade(node_instance_id, config_json, config_url, config_format,
+            chartVersion, chartRepo, **kwargs):
     node_instance = ctx.get_node_instance(node_instance_id)
 
     if not node_instance_id:
@@ -33,29 +35,30 @@ def upgrade(node_instance_id,config_json,config_url,config_format,chartVersion,c
 
     kwargs = {}
     if config_json == '' and config_url == '':
-         kwargs['config'] = config_json
+        kwargs['config'] = config_json
     elif config_json == '' and config_url != '':
-         response = urllib2.urlopen(config_url)
-    if config_format == 'json':
-         kwargs['config'] = json.load(response)
-    elif config_format == 'yaml':
-         kwargs['config'] = yaml.load(response)
-    else:
-         raise NonRecoverableError("Unable to get config input format")
+        response = urllib2.urlopen(config_url)
+        if config_format == 'json':
+            kwargs['config'] = json.load(response)
+        elif config_format == 'yaml':
+            kwargs['config'] = yaml.load(response)
+        else:
+            raise NonRecoverableError("Unable to get config input format")
     elif config_json != '' and config_url == '':
-         kwargs['config'] = config_json
+        kwargs['config'] = config_json
+
     else:
         raise NonRecoverableError("Unable to get Json config input")
 
-    kwargs['chart_version'] = str(chartVersion)
-    kwargs['chart_repo'] = str(chartRepo)
-    operation_args = {'operation': 'upgrade',}
-    operation_args['kwargs'] = kwargs
-    node_instance.execute_operation(**operation_args)
+kwargs['chart_version'] = str(chartVersion)
+kwargs['chart_repo'] = str(chartRepo)
+operation_args = {'operation': 'upgrade', }
+operation_args['kwargs'] = kwargs
+node_instance.execute_operation(**operation_args)
 
 
 @workflow
-def rollback(node_instance_id,revision,**kwargs):
+def rollback(node_instance_id, revision, **kwargs):
     node_instance = ctx.get_node_instance(node_instance_id)
 
     if not node_instance_id:
@@ -65,6 +68,6 @@ def rollback(node_instance_id,revision,**kwargs):
 
     kwargs = {}
     kwargs['revision'] = str(revision)
-    operation_args = {'operation': 'rollback',}
+    operation_args = {'operation': 'rollback', }
     operation_args['kwargs'] = kwargs
     node_instance.execute_operation(**operation_args)
index 2a60585..c15d98e 100644 (file)
@@ -13,7 +13,7 @@
 # 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.
-#============LICENSE_END============================================
+# ============LICENSE_END============================================
 
 
 from setuptools import setup
@@ -27,7 +27,8 @@ setup(
     version='2.3.0',
     author='Nicolas Hu(AT&T)',
     author_email='jh245g@att.com',
-    description='This plugin will install/uninstall/upgrade/rollback helm charts of ONAP components. ',
+    description='This plugin will install/uninstall/upgrade/rollback helm '
+                'charts of ONAP components. ',
 
     # This must correspond to the actual packages in the plugin.
     packages=['plugin'],