WASX7017E: Jython exception File "<string>" - websphere

When i try to execute my Jython script i get this error:
WASX7017E: Exception received while running file "/opt/test_wsadmin_configura_datasource.jython"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
File "<string>", line 29, in ?
NameError: dbuser
This is my test_wsadmin_configura_datasource.jython
##
# src: https://www.ibm.com/developerworks/community/blogs/timdp/entry/automating_application_installation_and_configuration_into_websphere_application_server46?lang=en
#
# eseguire con wsadmin.sh -lang jython
# get an environment variable
import os;
#installRoot = os.environ["INSTALL_ROOT"]
# useful variables
cell = AdminControl.getCell()
node = AdminControl.getNode()
server = AdminControl.getConfigId(AdminControl.queryNames("node="+node+",type=Server,*"))
varmap = AdminConfig.list('VariableMap', server)
appman = AdminControl.queryNames("type=ApplicationManager,*")
def createJ2CAuthAlias(alias,description,user,password):
sec = AdminConfig.getid('/Cell:'+ cell +'/Security:/')
alias_attr = ["alias", alias]
desc_attr = ["description", description]
userid_attr = ["userId", user]
password_attr = ["password", password]
attrs = [alias_attr, desc_attr, userid_attr, password_attr]
authdata = AdminConfig.create('JAASAuthData', sec, attrs)
print "J2C Auth Alias created ---> " + alias
AdminConfig.save()
return
createJ2CAuthAlias(dbuser,description,DBUSER,PASS)
WebSphere is running inside original docker image ibmcom/websphere-traditional:8.5.5.11-install
How can i solve?
EDIT1: Here found that the issue can be related to a non-UTF8 character.
These errors can occur because there are UTF-8 characters in the file that are not valid.
...
An easy way to determine if a character that is not valid is causing the error is to enter export LANG=C and run the script again.
export LANG=C does not change the result.

Just found that double quoting arguments does the job:
createJ2CAuthAlias("dbuser","description","DBUSER","PASS")

Related

unable to describe path using smbclient.SambaClient.exists

Python 3.6.9 and smbclient
TypeError: exists() missing 1 required positional argument: 'path'
import datetime
import smbclient
today = datetime.date.today()
first = today.replace(day=1)
last_month_end = first - datetime.timedelta(days=1)
folder_date = last_month_end.strftime("%m_%d_%Y")
smb = smbclient.SambaClient('server', 'share', username='username', password='password', domain='domain')
print(folder_date)
print(smb.listdir("Dept/Technology/Shared/GES_YCHARTS/" + folder_date))
if smbclient.SambaClient.exists('Dept/Technology/Shared/GES_YCHARTS/12_31_2022/12_31_2022 GES PI.xlsx'):
print('YES')
output:
12_31_2022
['12_31_2022 GES PH.xlsx', '12_31_2022 GES PI.xlsx']
Traceback (most recent call last):
File "./smbclient_test.py", line 18, in
if smbclient.SambaClient.exists('Dept/Technology/Shared/GES_YCHARTS/12_31_2022/12_31_2022 GES PI.xlsx'):
TypeError: exists() missing 1 required positional argument: 'path'
have tried many different forms of slashes & quotes

How do I setup Airflow's email configuration to send an email on errors?

I'm trying to make an Airflow task intentionally fail and error out by passing in a Bash line (thisshouldnotrun) that doesn't work. Airflow is outputting the following:
[2017-06-15 17:44:17,869] {bash_operator.py:94} INFO - /tmp/airflowtmpLFTMX7/run_bashm2MEsS: line 7: thisshouldnotrun: command not found
[2017-06-15 17:44:17,869] {bash_operator.py:97} INFO - Command exited with return code 127
[2017-06-15 17:44:17,869] {models.py:1417} ERROR - Bash command failed
Traceback (most recent call last):
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
result = task_copy.execute(context=context)
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in execute
raise AirflowException("Bash command failed")
AirflowException: Bash command failed
[2017-06-15 17:44:17,871] {models.py:1433} INFO - Marking task as UP_FOR_RETRY
[2017-06-15 17:44:17,878] {models.py:1462} ERROR - Bash command failed
Traceback (most recent call last):
File "/home/ubuntu/.local/bin/airflow", line 28, in <module>
args.func(args)
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/bin/cli.py", line 585, in test
ti.run(ignore_task_deps=True, ignore_ti_state=True, test_mode=True)
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/utils/db.py", line 53, in wrapper
result = func(*args, **kwargs)
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/models.py", line 1374, in run
result = task_copy.execute(context=context)
File "/home/ubuntu/.local/lib/python2.7/site-packages/airflow/operators/bash_operator.py", line 100, in execute
raise AirflowException("Bash command failed")
airflow.exceptions.AirflowException: Bash command failed
Will Airflow send an email for these kind of errors? If not, what would be the best way to send an email for these errors?
I'm not even sure if airflow.cfg is setup properly... Since the ultimate goal is to test the email alerting notification, I want to make sure airflow.cfg is setup properly. Here's the setup:
[email]
email_backend = airflow.utils.email.send_email_smtp
[smtp]
# If you want airflow to send emails on retries, failure, and you want to use
# the airflow.utils.email.send_email_smtp function, you have to configure an
# smtp server here
smtp_host = emailsmtpserver.region.amazonaws.com
smtp_starttls = True
smtp_ssl = False
# Uncomment and set the user/pass settings if you want to use SMTP AUTH
# smtp_user = airflow_data_user
# smtp_password = password
smtp_port = 587
smtp_mail_from = airflow_data_user#domain.com
What is smtp_starttls? I can't find any info for it in the documentation or online. If we have 2-factor authentication needed to view emails, will that be an issue here for Airflow?
Here's my Bash command:
task1_bash_command = """
export PATH=/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
export rundate=`TZ='America/Los_Angeles' date +%F -d "yesterday"`
export AWS_CONFIG_FILE="/home/ubuntu/.aws/config"
/home/ubuntu/bin/snowsql -f //home/ubuntu/sql/script.sql 1> /home/ubuntu/logs/"$rundate"_dev.log 2> /home/ubuntu/logs/"$rundate"_error_dev.log
if [ -e /home/ubuntu/logs/"$rundate"_error_dev.log ]
then
exit 64
fi
And my task:
task1 = BashOperator(
task_id = 'run_bash',
bash_command = task1_bash_command,
dag = dag,
retries = 2,
email_on_failure = True,
email = 'username#domain.com')
smtp_starttls basically means Use TLS
Set this to False and set smtp_ssl to True if you want to use SSL instead. You probably need smtp_user and smtp_password for either.
Airflow will not handle 2 step authentication. However, is you are using AWS you likely don't need it as your SMTP (SES) credentials are different from your AWS credentials.
See here.
EDIT:
For airflow to send an email on failure, there are a couple things that need to be set on your task, email_on_failure and email.
See here for example:
def throw_error(**context):
raise ValueError('Intentionally throwing an error to send an email.')
t1 = PythonOperator(task_id='throw_error_and_email',
python_callable=throw_error,
provide_context=True,
email_on_failure=True,
email='your.email#whatever.com',
dag=dag)
Use below link for creating airflow dag.
How to trigger daily DAG run at midnight local time instead of midnight UTC time
Approach 1 :
You can setup SMTP locally and make it send email on jobs failure.
[email]
email_backend = airflow.utils.email.send_email_smtp
[smtp]
smtp_host = localhost
smtp_starttls = False
smtp_ssl = False
smtp_port = 25
smtp_mail_from = noreply#company.com
Approach 2 : You can use Gmail to send email.
I have written an article to do this.
https://helptechcommunity.wordpress.com/2020/04/04/airflow-email-configuration/
If we have 2-factor authentication needed to view emails, will that be an issue here for Airflow?
You can use google app password to get your way around 2 factor authentication
https://support.google.com/mail/answer/185833?hl=en-GB
Source - https://docs.aws.amazon.com/mwaa/latest/userguide/configuring-env-variables.html

Twilio return method as POST value

I am trying to build on a test Twilio script in order that the POST request can just use values returned by method is a script.
The values are set as environmental variables and I have checked that they are correct in the Python shell using the same command as the script.
Python 2.7 being used.
The environmental variables are stored as strings and return as such.
from twilio.rest import TwilioRestClient
import os
ACCOUNT_SID = os.environ['ACCOUNT_SID']
AUTH_TOKEN = os.environ['AUTH_TOKEN']
client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
def target_number():
return os.environ['TARGET_NUMBER']
def my_number():
return os.environ['MY_NUMBER']
message = client.messages.create(
body="foo bar", # Message body, if any
to=target_number(),
from_=my_number(),
)
print message.body
I am getting the following error:
python my_twilio_testing_script.py
Traceback (most recent call last):
File "my_twilio_testing_script.py", line 19, in <module>
to=target_number(),
File "my_twilio_testing_script.py", line 9, in target_number
return os.environ['TARGET_NUMBER']
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: 'TARGET_NUMBER'
Twilio developer evangelist here.
It appears that some of your environment variables are working, otherwise the script would blow up when you set the account SID and auth token.
So it seems that maybe TARGET_NUMBER is perhaps not set. In the terminal window in which you run the script, are you able to perform the following bash command:
$ echo $TARGET_NUMBER
Does that show the number you expect? If not, you may need to export the environment variable again.

"pymysql.err.InternalError: (1046, u'No database selected')" error message when running Python script from command line

I'm trying to run a Python script that connects to a MySQL database through PyMySQL. The script is effectively:
import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
# Do stuff.
When I run the script in the interpreter, I don't get any errors, but when I try to run it from the command line, I get the following error:
Traceback (most recent call last):
File "s02_prepare_data_RNN.py", line 264, in <module>
(omniture, urls, years, global_regions) = get_omniture_data("omniture_results")
File "s02_prepare_data_RNN.py", line 76, in get_omniture_data
sso_to_accountid = get_sso_accountids()
File "s02_prepare_data_RNN.py", line 31, in get_sso_accountids
cursor.execute(query)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/cursors.py", line 134, in execute
result = self._query(query)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/cursors.py", line 282, in _query
conn.query(q)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 768, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 929, in _read_query_result
result.read()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 1125, in read
first_packet = self.connection._read_packet()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 893, in _read_packet
packet.check_error()
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/connections.py", line 369, in check_error
err.raise_mysql_exception(self._data)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/err.py", line 120, in raise_mysql_exception
_check_mysql_exception(errinfo)
File "/home/rdu/malcorn/.local/lib/python2.6/site-packages/pymysql/err.py", line 115, in _check_mysql_exception
raise InternalError(errno, errorvalue)
pymysql.err.InternalError: (1046, u'No database selected')
Modifying my code from:
import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
# Do stuff.
to:
import pymysql
cnx = pymysql.connect(read_default_file = "/directory/my.cnf", cursorclass = pymysql.cursors.DictCursor)
cursor = cnx.cursor()
if __name__ == "__main__":
# Do stuff.
fixed the error. I had the thought that Python might be trying to execute the queries before the connection was established, so I tried putting the main part of my program under if __name__ == "__main__": and that fixed it. I'm still not 100% what is going on, though. I had assumed the code would wait for the connection to be established before proceeding to the following lines, but this fix suggests that's not the case.
It's also worth noting that I was only getting the error when running the original script from the command line on a server that has Python 2.6. When I ran the original script from the command line on my local machine that has Python 2.7, I did not get the error.
Anyway, if __name__ == "__main__": is good Python style, so I'll make sure to use it in the future.
Sometimes this may simply happen when the database URI is not built correctly. For me, the error occurred when my pymysql connection string (URL) looked like this:
mysql+pymysql://:#localhost/
To fix it, it needed to look like:
mysql+pymysql://<my-database-user-name>:#localhost/<my-database-name> # or replace localhost with your particular host name
If you don't select the database in connection, you must add this line.
connection = pymysql.connect(host=DB_host, user=DB_user, password=DB_password) # NO DB_NAME SPECIFED
cursor = connection.cursor()
cursor.execute(f'CREATE DATABASE IF NOT EXISTS {DB_NAME}') # ADD THIS LINE!
connection.select_db(DB_NAME)

IBM Websphere - exception in wsadmin script for AutoDeployment

I'm trying to write a simple Jython script to AutoDeploy a web application on the IBM Websphere Application Server. However, I'm a novice in Python, so I can't uderstand, why do I become the following Error:
WASX7209I: Connected to process "dmgr" on node was7CellManager01 using SOAP connector; The type of process is: DeploymentManager
WASX7017E: Exception received while running file "deploy_test.py"; exception information: com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
File "<string>", line 14, in ?
TypeError: sequence subscript must be integer or slice
My script:
appname='name'
source='app.ear'
nodeName='was7Node01'
cell='was7Cell01'
server='server1'
contextRoot='/deploymenttest'
# 1. node
# 2. cell
# 3. server
# 4. Application Name
# 5. ContextRoot
# 5. JNDI target name
attrs = [
'-node ', nodeName,
' -cell ', cell,
' -server ', server,
' -appname ', appname,
' -CtxRootForWebMod ', contextRoot,
' -MapResRefToEJB ', [
[
appname,"",
source+',WEB-INF/web.xml',
'jdbc/appdb','javax.sql.DataSource',
'jbdc/app22','DefaultPrincipalMapping',
'was7CellManager01/db2inst1',""
]
[
appname,"",
source+',WEB-INF/web.xml',
'jdbc/app1db','javax.sql.DataSource',
'jbdc/app22','DefaultPrincipalMapping',
'was7CellManager01/db2inst1',""
]
]
]
AdminApp.install(source, attrs)
Any ideas?
Thank you very much in advance.
There is a missing coma between both of your MapResRefToEJB values # line 27.

Resources