I am trying to deploy my JiraAPI flask app on gunicorn in centos VM. i have used JIRA module for it. Here is my class
class Jira:
#initilizing the class
def __init__(self):
# private variables
self.logger = logging.getLogger('jiraUtil')
self.__email = environ.get('email')
self.__secret = environ.get('secret')
self.logger.debug("Connecting to Jira API")
jira_options = {
'server': environ.get('baseUrl'),
}
self.jira_con = JIRA(jira_options, basic_auth=(self.__email, self.__secret),max_retries=0)
if not self.jira_con:
self.logger.error("Error connecting to Jira")
raise ValueError("Error connecting to Jia")
def createIssue(self,issue_dict) -> dict:
self.logger.info("inside jira ticket creation")
new_issue_body = {}
try:
new_issue = self.jira_con.create_issue(fields=issue_dict)
print('new_issue')
new_issue_body['id'] = new_issue.id
new_issue_body['key'] = new_issue.key
new_issue_body['self'] = new_issue.self
return new_issue_body
except Exception as e:
self.logger.error("Error creating SSD ticket: ",e)
return new_issue_body
In my app.py
@app.route('/api/createTicket/', methods=['POST'])
def createTicket():
app.logger.info('Creating Issue Ticket')
request_data = request.get_json()
fields_dict = request_data.get('fields',None)
if fields_dict == None :
return jsonify({"result":"failure","status":"400", "message":"Bad Request"}),400
jiraObj = JiraUtil.Jira()
newIssueKey = jiraObj.createIssue(fields_dict)
if newIssueKey == None:
return jsonify({"result":"failure","status":"503", "message":"service Unavailable"}),500
return newIssueKey,200
if __name__ == '__main__':
app.run()
When i am running the app using flask run -h 0.0.0.0 -p 5000
it is working fine and I am able to connect with my local postman
the logs got generated :
INFO:werkzeug:_[31m_[1mWARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead._[0m
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://192.168.113.24:5000
INFO:werkzeug:_[33mPress CTRL C to quit_[0m
INFO:app:Creating Issue Ticket
DEBUG:jiraUtil:Connecting to Jira API
DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): codecharlee.atlassian.net:443
DEBUG:urllib3.connectionpool:https://codecharlee.atlassian.net:443 "GET /rest/api/2/serverInfo HTTP/1.1" 200 None
INFO:jiraUtil:inside jira ticket creation
but when i am running
gunicorn --bind 0.0.0.0:5000 app:app
it starts, but gets an error when hitting the api
INFO:app:Creating Issue Ticket
DEBUG:jiraUtil:Connecting to Jira API
ERROR:app:Exception on /api/createTicket/ [POST]
Traceback (most recent call last):
File "/opt/ctier/JenkinsPythonAPI/venv/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
File "/opt/ctier/JenkinsPythonAPI/venv/lib/python3.8/site-packages/flask/app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/ctier/JenkinsPythonAPI/venv/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/ctier/JenkinsPythonAPI/venv/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/opt/ctier/JenkinsPythonAPI/app.py", line 25, in createTicket
jiraObj = JiraUtil.Jira()
File "/opt/ctier/JenkinsPythonAPI/JiraUtil.py", line 21, in __init__
self.jira_con = JIRA(jira_options, basic_auth=(self.__email, self.__secret),max_retries=0)
File "/opt/ctier/JenkinsPythonAPI/venv/lib/python3.8/site-packages/jira/client.py", line 516, in __init__
assert isinstance(self._options["server"], str) # to help mypy
AssertionError
Please help
CodePudding user response:
The problem is that gunicorn is not able to read the environment variables that you have declared in the .env
file.
So, just configure a service file app.service in /etc/systemd/system
and add EnvironmentFile=/path_to_env_file
under [service]
and start the service.
For more info on configuring the env file in gunicorn service, you can see here.