Skip to content

Instantly share code, notes, and snippets.

@lichti
Last active July 1, 2018 02:02
Show Gist options
  • Save lichti/eeff03191edbb6087a776a06e181bbb5 to your computer and use it in GitHub Desktop.
Save lichti/eeff03191edbb6087a776a06e181bbb5 to your computer and use it in GitHub Desktop.
import subprocess
from flask import (Flask, abort, request, jsonify, Response)
from pybitbucket.bitbucket import Bitbucket, Client
from pybitbucket.auth import (
Authenticator,
Anonymous, BasicAuthenticator,
OAuth1Authenticator,
OAuth2Grant, OAuth2Authenticator)
from pybitbucket.repository import (
RepositoryRole, RepositoryType, RepositoryForkPolicy,
Repository, RepositoryV1,
RepositoryPayload)
from pybitbucket.ref import (Ref, Tag, Branch)
app = Flask(__name__)
@app.route('/')
def raiz():
return 'Hello, World!'
@app.route('/deploy', methods = ['POST'])
def deploy():
allowed_users = ["lichti", "arturfelipe.sousa", "camilla", "diego"]
allowed_channels = ["newserver"]
expected_token = 'xxxx'
user = request.form['user_name']
command = request.form['command']
channel = request.form['channel_name']
command_text = request.form['text']
token = request.form['token']
bitbucket = Client(
BasicAuthenticator(
'lichti',
'xxx',
'[email protected]'))
if token != expected_token:
result = {}
result['response_type'] = "in_channel"
result['text'] = "``` Wrong token %s ```" % token
return jsonify(**result)
if user not in allowed_users:
result = {}
result['response_type'] = "in_channel"
result['text'] = "``` %s: You are not allowed to run this command ```" % user
return jsonify(**result)
if channel not in allowed_channels:
result = {}
result['response_type'] = "in_channel"
result['text'] = "``` This command can not be run on this channel: %s ```" % channel
return jsonify(**result)
try:
Branch.find_branch_by_ref_name_in_repository(command_text,'aventurashop','aventura-shop',bitbucket)
except:
result = {}
result['response_type'] = "in_channel"
result['text'] = "``` Branch not exists: %s ```" % command_text
return jsonify(**result)
else:
result = {}
result['response_type'] = "in_channel"
result['text'] = "``` Deploy started: %s ```" % command_text
subprocess.Popen(['nohup', '/home/slackbot/deploy.sh', command_text],
stdout=open('stdout', 'w'),
stderr=open('stderr', 'a'),
preexec_fn=os.setpgrp
)
return jsonify(**result)
if __name__ == "__main__":
app.run(host='0.0.0.0',port='8005')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment