def handle_uploaded_file(request): order_no = request.POST.get('order_no','') host = request.POST.get('deploy_host','') user = request.POST.get('deploy_user','') file_req = request.FILES['uploadfile'] file_name = file_req.name try: path = '/data/uploadpackage/' + order_no + '/' if not os.path.exists(path): os.makedirs(path) destination = open(path + file_name, 'wb+') for chunk in file_req.chunks(): destination.write(chunk) destination.close() else: destination = open(path + file_name, 'wb+') for chunk in file_req.chunks(): destination.write(chunk) destination.close() moduleinfo = application.objects.filter(host_id=host,username=user).values('module') module = moduleinfo[0]['module'] vhost_ip = vhost.objects.all().filter(host_id=host).values('host_ip') host_ip = vhost_ip[0]['host_ip'] userinfo = app_user.objects.all().filter(username = user).values('username','password') username = userinfo[0]['username'] password = userinfo[0]['password'] sftp2(host_ip,username,password,path + file_name,'/app/'+username+'/'+module+'/applications/'+file_name) #step1: release ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy_all/;sh release-'+module+'-*.sh > '+username+'_upload_deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy_all/;grep releaseok '+username+'_upload_deploy.log;') if (len(result)>0): break; else: sleep(10) ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy_all/;sh stop-'+module+'-srv.sh >> '+username+'_upload_deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy_all/;grep stopok '+username+'_upload_deploy.log;') if (len(result)>0): break; else: sleep(10) ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy_all/;sh start-'+module+'-srv.sh >> '+username+'_upload_deploy.log; ') while True: result = ssh2_deploy_log(host_ip,username,password, r"cd /data/"+username+"/logs/;awk 'NF{a=$0}END{print a}' "+module+"-?01-srv01-"+(datetime.now().date()).strftime('%Y%m%d')+".log|grep -i RUNNING ;") if (len(result)>0): break; else: sleep(30) except Exception, e: logging.error(e)
def project_user_instance(request): order_no_req = request.GET.get('order_no','') host_user_req = request.GET.get('host_user','') ops_req = request.GET.get('ops','') cursor = connection.cursor() if(order_no_req): order_no = vhost.objects.filter(order_no=order_no_req).values('order_no','order_name').distinct() host_instance_info = [] appusers = [] # please notice the parameter formart if(not host_user_req): cursor.execute("select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s",[order_no_req]) else: cursor.execute("select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s and app_user.username = %s",[order_no_req,host_user_req]) vhosts = dictfetchall(cursor) if(len(vhosts)>0): for host in vhosts: host_ip = host['host_ip'] username = host['username'] password = host['password'] appusers.append(username) #result = ssh2_deploy(host_ip, username, password, r"cdbin;for var in (`ls start-* | sed -e 's/start-//g' -e 's/.sh//g'`);do process=`ps -ef|grep java|grep $var|wc -l`;if [ $process -eq 1 ]; then echo $var start else echo $var stop fi done;") if(ops_req): ssh2_deploy(host_ip, username, password, r'cdbin;sh '+ops_req+'-admin.sh 2>&1 &;') ssh2_deploy(host_ip, username, password, r'cdbin;sh '+ops_req+'allsrv.sh 2>&1 &;') sleep(30) else: sftp2(host_ip, username, password, '/app/scripts/deploy/instance.sh', '/tmp/instance.sh') result = ssh2_deploy(host_ip, username, password, r'sh /tmp/instance.sh;') ssh2_deploy(host_ip, username, password, r'rm -fr /tmp/instance.sh;') for instance in result: insatnce = instance.strip('\n').split(' ') host_instance_info.append({'host_ip':host_ip,'username':username,'password':password,'instance_name':insatnce[0],'instance_status':insatnce[1]}) cursor.execute("select distinct(app_user.username) from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s",[order_no_req]) deploy_user = dictfetchall(cursor) cursor.close() return render(request,'weblogic_instances.html',{'order_no':order_no,'deploy_user':deploy_user,'host_instance_info': host_instance_info})
def project_user_instance(request): order_no_req = request.GET.get('order_no', '') host_user_req = request.GET.get('host_user', '') ops_req = request.GET.get('ops', '') cursor = connection.cursor() if (order_no_req): order_no = vhost.objects.filter(order_no=order_no_req).values( 'order_no', 'order_name').distinct() host_instance_info = [] appusers = [] # please notice the parameter formart if (not host_user_req): cursor.execute( "select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s", [order_no_req]) else: cursor.execute( "select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s and app_user.username = %s", [order_no_req, host_user_req]) vhosts = dictfetchall(cursor) if (len(vhosts) > 0): for host in vhosts: host_ip = host['host_ip'] username = host['username'] password = host['password'] appusers.append(username) #result = ssh2_deploy(host_ip, username, password, r"cdbin;for var in (`ls start-* | sed -e 's/start-//g' -e 's/.sh//g'`);do process=`ps -ef|grep java|grep $var|wc -l`;if [ $process -eq 1 ]; then echo $var start else echo $var stop fi done;") if (ops_req): ssh2_deploy(host_ip, username, password, r'cdbin;sh ' + ops_req + '-admin.sh 2>&1 &;') ssh2_deploy(host_ip, username, password, r'cdbin;sh ' + ops_req + 'allsrv.sh 2>&1 &;') sleep(30) else: sftp2(host_ip, username, password, '/app/scripts/deploy/instance.sh', '/tmp/instance.sh') result = ssh2_deploy(host_ip, username, password, r'sh /tmp/instance.sh;') ssh2_deploy(host_ip, username, password, r'rm -fr /tmp/instance.sh;') for instance in result: insatnce = instance.strip('\n').split(' ') host_instance_info.append({ 'host_ip': host_ip, 'username': username, 'password': password, 'instance_name': insatnce[0], 'instance_status': insatnce[1] }) cursor.execute( "select distinct(app_user.username) from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s", [order_no_req]) deploy_user = dictfetchall(cursor) cursor.close() return render( request, 'weblogic_instances.html', { 'order_no': order_no, 'deploy_user': deploy_user, 'host_instance_info': host_instance_info })
def handle_uploaded_file(request): order_no = request.POST.get('order_no', '') host = request.POST.get('deploy_host', '') user = request.POST.get('deploy_user', '') file_req = request.FILES['uploadfile'] file_name = file_req.name try: path = '/data/uploadpackage/' + order_no + '/' if not os.path.exists(path): os.makedirs(path) destination = open(path + file_name, 'wb+') for chunk in file_req.chunks(): destination.write(chunk) destination.close() else: destination = open(path + file_name, 'wb+') for chunk in file_req.chunks(): destination.write(chunk) destination.close() moduleinfo = application.objects.filter(host_id=host, username=user).values('module') module = moduleinfo[0]['module'] vhost_ip = vhost.objects.all().filter(host_id=host).values('host_ip') host_ip = vhost_ip[0]['host_ip'] userinfo = app_user.objects.all().filter(username=user).values( 'username', 'password') username = userinfo[0]['username'] password = userinfo[0]['password'] sftp2(host_ip, username, password, path + file_name, '/app/' + username + '/' + module + '/applications/' + file_name) #step1: release ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy_all/;sh release-' + module + '-*.sh > ' + username + '_upload_deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy_all/;grep releaseok ' + username + '_upload_deploy.log;') if (len(result) > 0): break else: sleep(10) ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy_all/;sh stop-' + module + '-srv.sh >> ' + username + '_upload_deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy_all/;grep stopok ' + username + '_upload_deploy.log;') if (len(result) > 0): break else: sleep(10) ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy_all/;sh start-' + module + '-srv.sh >> ' + username + '_upload_deploy.log; ') while True: result = ssh2_deploy_log( host_ip, username, password, r"cd /data/" + username + "/logs/;awk 'NF{a=$0}END{print a}' " + module + "-?01-srv01-" + (datetime.now().date()).strftime('%Y%m%d') + ".log|grep -i RUNNING ;") if (len(result) > 0): break else: sleep(30) except Exception, e: logging.error(e)
def tomcat_deploy(data_req): data_txt_web = [] data_txt_app = [] for data in data_req: hostname = data['host_name'] username = data['username'] if ('web' in hostname): web_index = username.index('web') module = username[0:web_index] + '-' + username[web_index:] password = app_user.objects.all().filter( host_id=data['host_id'], username=data['username']).values('password')[0]['password'] data_txt_web.append({ 'host_id': data['host_id'], 'hostname': hostname, 'area': data['area'], 'ip': data['ip'], 'username': username, 'password': password, 'module': module, 'instance': data['instance'], 'primary': '', 'version_num': data['version_num'] }) elif ('app' in hostname): app_index = username.index('app') module = username[0:app_index] + '-' + username[app_index:] password = app_user.objects.all().filter( host_id=data['host_id'], username=data['username']).values('password')[0]['password'] data_txt_app.append({ 'host_id': data['host_id'], 'hostname': hostname, 'area': data['area'], 'ip': data['ip'], 'username': username, 'password': password, 'module': module, 'instance': data['instance'], 'primary': '', 'version_num': data['version_num'] }) ''' cloudops step: 1.write text file 2.copy file to cloudops host 3.excute shell scripts ''' #web server web_server_count = len(data_txt_web) if (web_server_count > 0): for i in range(web_server_count): if ('01' in data_txt_web[i]['area']): cmd = r'' for info in data_txt_web: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' + info[ 'username'] + ' ' + info['password'] + ' ' + info[ 'module'] + ' ' + info['instance'] + ' ' + info[ 'version_num'] + ' \\n' cmd = cmd[:-2] cmd = r'cd /app/' + data_txt_web[i][ 'username'] + '/;echo "' + cmd + '" > data.txt' #set primary host for cloudops data_txt_web[i]['primary'] = 'primary' #var host_ip = data_txt_web[i]['ip'] username = data_txt_web[i]['username'] password = data_txt_web[i]['password'] module = data_txt_web[i]['module'] application.objects.create( host_id=data_txt_web[i]['host_id'], area=data_txt_web[i]['area'], middleware='tomcat', module=module, username=username, version_num=data_txt_web[i]['version_num'], instance_num=data_txt_web[i]['instance'], primary='primary', create_date=today, status='deployed') #write data text ssh2_deploy(host_ip, username, password, cmd) #put template file to target host sftp2(host_ip, username, password, '/app/tmt/tomcat.tar', '/app/' + username + '/tomcat.tar') #tar xvf file name and run 3 scripts cmd_deploy_step1 = r'cd /app/'+username+';tar xf tomcat.tar;sh 01_startdoc.sh >>cloudops.log;'\ 'sh 02_mvserver.sh >>cloudops.log;' ssh2_deploy(host_ip, username, password, cmd_deploy_step1) #app server app_server_count = len(data_txt_app) if (app_server_count > 0): for i in range(app_server_count): if ('01' in data_txt_app[i]['area']): cmd = '' for info in data_txt_app: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' + info[ 'username'] + ' ' + info['password'] + ' ' + info[ 'module'] + ' ' + info['instance'] + ' ' + info[ 'version_num'] + ' \\n' cmd = r'cd /app/' + data_txt_app[i][ 'username'] + ';echo "' + cmd + ' " > data.txt' #set primary host for cloudops data_txt_app[i]['primary'] = 'primary' #var host_ip = data_txt_app[i]['ip'] username = data_txt_app[i]['username'] password = data_txt_app[i]['password'] module = data_txt_app[i]['module'] application.objects.create( host_id=data_txt_app[i]['host_id'], area=data_txt_app[i]['area'], middleware='tomcat', module=module, username=username, version_num=data_txt_app[i]['version_num'], instance_num=data_txt_app[i]['instance'], primary='primary', create_date=today, status='deployed') #write data text ssh2_deploy(host_ip, username, password, cmd) #put template file to target host sftp2(host_ip, username, password, '/app/tmt/tomcat.tar', '/app/' + username + '/tomcat.tar') cmd_deploy_step1 = r'cd /app/'+username+';tar xf tomcat.tar;sh 01_startdoc.sh >>cloudops.log;'\ 'sh 02_mvserver.sh >>cloudops.log;' ssh2_deploy(host_ip, username, password, cmd_deploy_step1) logging.info(data_txt_web) logging.info(data_txt_app) data_txt = data_txt_web + data_txt_app logging.info(data_txt) #input database application app_data = [] for deploy_info in data_txt: if (not deploy_info['primary']): app_data.append( application(host_id=deploy_info['host_id'], area=deploy_info['area'], middleware='tomcat', module=deploy_info['module'], username=deploy_info['username'], version_num=deploy_info['version_num'], instance_num=deploy_info['instance'], primary=deploy_info['primary'], create_date=today, status='deployed')) application.objects.bulk_create(app_data)
def welogic_deploy(data_req): data_txt_web = [] data_txt_app = [] for data in data_req: hostname = data['host_name'] username = data['username'] if ('web' in hostname): web_index = username.index('web') module = username[0:web_index] + '-' + username[web_index:] password = app_user.objects.all().filter( host_id=data['host_id'], username=data['username']).values('password')[0]['password'] data_txt_web.append({ 'host_id': data['host_id'], 'hostname': hostname, 'area': data['area'], 'ip': data['ip'], 'username': username, 'password': password, 'module': module, 'instance': data['instance'], 'primary': '', 'console_port': data['console_port'] }) elif ('app' in hostname): app_index = username.index('app') module = username[0:app_index] + '-' + username[app_index:] password = app_user.objects.all().filter( host_id=data['host_id'], username=data['username']).values('password')[0]['password'] data_txt_app.append({ 'host_id': data['host_id'], 'hostname': hostname, 'area': data['area'], 'ip': data['ip'], 'username': username, 'password': password, 'module': module, 'instance': data['instance'], 'primary': '', 'console_port': data['console_port'] }) ''' deploy step: 1.write text file 2.copy file to deploy host 3.excute shell scripts ''' #web server web_server_count = len(data_txt_web) if (web_server_count > 0): for i in range(web_server_count): if ('01' in data_txt_web[i]['area']): cmd = r'' for info in data_txt_web: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' + info[ 'username'] + ' ' + info['password'] + ' ' + info[ 'module'] + ' /app/mw test.war ' + info[ 'instance'] + ' ' + info[ 'console_port'] + ' \\n' cmd = cmd[:-2] cmd = r'cd /app/' + data_txt_web[i][ 'username'] + '/;echo "' + cmd + '" > data.txt' #set primary host for deploy data_txt_web[i]['primary'] = 'primary' #var host_ip = data_txt_web[i]['ip'] username = data_txt_web[i]['username'] password = data_txt_web[i]['password'] module = data_txt_web[i]['module'] application.objects.create( host_id=data_txt_web[i]['host_id'], area=data_txt_web[i]['area'], middleware='weblogic', module=module, username=username, console_port=data_txt_web[i]['console_port'], instance_num=data_txt_web[i]['instance'], package='test.war', primary='primary', create_date=today, status='deployed') try: #write data text ssh2_deploy(host_ip, username, password, cmd) #put template file to target host sftp2(host_ip, username, password, '/app/testweb/bak/TEMPLATE.tar', '/app/' + username + '/TEMPLATE.tar') #tar xvf file name and run 3 scripts cmd_deploy_step1 = r'cd /app/'+username+';tar xf TEMPLATE.tar;source .cshrc;sh APP.sh;'\ 'cp test.war /app/'+username+'/deploy/applications/'+module+'/;'\ 'cd deploy/scripts;sh 1_envinitall.sh >deploy.log;' ssh2_deploy(host_ip, username, password, cmd_deploy_step1) while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup1 deploy.log') logging.info(result) logging.info(len(result)) if (len(result) == web_server_count): break else: sleep(30) # web-machinfo.txt version-web-machinfo.txt cmd_web_machinfo = '' cmd_version_web_machinfo = '' for info in data_txt_web: cmd_web_machinfo = cmd_web_machinfo + info[ 'ip'] + ' ' + info['password'] + ' ' + info[ 'username'] + ' /app/' + info[ 'username'] + '/' + info['module'] + ' \\n' for i in range(int(info['instance'])): cmd_version_web_machinfo = cmd_version_web_machinfo + info[ 'ip'] + ':' + str( int(info['console_port']) + 1 + i) + ' \\n' cmd_web_machinfo = cmd_web_machinfo[:-2] cmd_version_web_machinfo = cmd_version_web_machinfo[:-2] # run DEP.sh for deploy programe cmd_run_deploy = r'cd /app/' + username + '/; sh DEP.sh;cd deploy_all/;echo "' + cmd_web_machinfo + '" > web-machinfo.txt;echo "' + cmd_version_web_machinfo + '" > version-web-machinfo.txt' ssh2_deploy(host_ip, username, password, cmd_run_deploy) #run step 2 and step 3 ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;sh 2_createdomain.sh >>deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup2 deploy.log') if (len(result) == web_server_count): break else: sleep(30) ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;sh 3_deployappall.sh >>deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup3 deploy.log') if (len(result) == web_server_count): break else: sleep(60) continue except: application.objects.all().filter( host_id=data_txt_web[i]['host_id'], username=username).delete() #app server app_server_count = len(data_txt_app) if (app_server_count > 0): for i in range(app_server_count): if ('01' in data_txt_app[i]['area']): cmd = '' for info in data_txt_app: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' + info[ 'username'] + ' ' + info['password'] + ' ' + info[ 'module'] + ' /app/mw test.war ' + info[ 'instance'] + ' ' + info[ 'console_port'] + ' \\n' cmd = r'cd /app/' + data_txt_app[i][ 'username'] + ';echo "' + cmd + ' " > data.txt' #set primary host for deploy data_txt_app[i]['primary'] = 'primary' #var host_ip = data_txt_app[i]['ip'] username = data_txt_app[i]['username'] password = data_txt_app[i]['password'] module = data_txt_app[i]['module'] application.objects.create( host_id=data_txt_app[i]['host_id'], area=data_txt_app[i]['area'], middleware='weblogic', module=module, username=username, console_port=data_txt_app[i]['console_port'], instance_num=data_txt_app[i]['instance'], package='test.war', primary='primary', create_date=today, status='deployed') try: #write data text ssh2_deploy(host_ip, username, password, cmd) #put template file to target host sftp2(host_ip, username, password, '/app/testweb/bak/TEMPLATE.tar', '/app/' + username + '/TEMPLATE.tar') cmd_deploy = 'cd /app/'+username+';tar xf TEMPLATE.tar;source .cshrc;sh APP.sh;'\ 'cp test.war /app/'+username+'/deploy/applications/'+module+'/;'\ 'cd deploy/scripts;sh 1_envinitall.sh > deploy.log;' ssh2_deploy(host_ip, username, password, cmd_deploy) while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup1 deploy.log') logging.info(result) logging.info(len(result)) if (len(result) == app_server_count): break else: sleep(30) # web-machinfo.txt version-web-machinfo.txt cmd_app_machinfo = '' cmd_version_app_machinfo = '' for info in data_txt_web: cmd_app_machinfo = cmd_app_machinfo + info[ 'ip'] + ' ' + info['password'] + ' ' + info[ 'username'] + ' /app/' + info[ 'username'] + '/' + info['module'] + ' \\n' for i in range(int(info['instance'])): cmd_version_app_machinfo = cmd_version_app_machinfo + info[ 'ip'] + ':' + str( int(info['console_port']) + 1 + i) + ' \\n' cmd_app_machinfo = cmd_app_machinfo[:-2] cmd_version_app_machinfo = cmd_version_app_machinfo[:-2] # run DEP.sh for deploy programe cmd_run_deploy = r'cd /app/' + username + '/; sh DEP.sh;cd deploy_all/;echo "' + cmd_app_machinfo + '" > app-machinfo.txt;echo "' + cmd_version_app_machinfo + '" > version-app-machinfo.txt' ssh2_deploy(host_ip, username, password, cmd_run_deploy) #run step 2 and step 3 ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;sh 2_createdomain.sh >>deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup2 deploy.log') if (len(result) == app_server_count): break else: sleep(30) ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;sh 3_deployappall.sh >>deploy.log;') while True: result = ssh2_deploy( host_ip, username, password, r'cd /app/' + username + '/deploy/scripts;grep setup3 deploy.log') if (len(result) == app_server_count): break else: sleep(60) continue except: application.objects.all().filter( host_id=data_txt_app[i]['host_id'], username=username).delete() logging.info(data_txt_web) logging.info(data_txt_app) data_txt = data_txt_web + data_txt_app logging.info(data_txt) #input database application app_data = [] for deploy_info in data_txt: if (not deploy_info['primary']): app_data.append( application(host_id=deploy_info['host_id'], area=deploy_info['area'], middleware='weblogic', module=deploy_info['module'], username=deploy_info['username'], console_port=deploy_info['console_port'], instance_num=deploy_info['instance'], package='test.war', primary=deploy_info['primary'], create_date=today, status='deployed')) application.objects.bulk_create(app_data)
def welogic_deploy(data_req): data_txt_web = [] data_txt_app = [] for data in data_req: hostname = data['host_name'] username = data['username'] if('web' in hostname): web_index = username.index('web') module = username[0:web_index] + '-' + username[web_index:] password = app_user.objects.all().filter(host_id=data['host_id'],username=data['username']).values('password')[0]['password'] data_txt_web.append({'host_id':data['host_id'],'hostname':hostname,'area':data['area'],'ip':data['ip'],'username':username,'password':password,'module':module,'instance':data['instance'],'primary':'','console_port':data['console_port']}) elif('app' in hostname): app_index = username.index('app') module = username[0:app_index] + '-' + username[app_index:] password = app_user.objects.all().filter(host_id=data['host_id'],username=data['username']).values('password')[0]['password'] data_txt_app.append({'host_id':data['host_id'],'hostname':hostname,'area':data['area'],'ip':data['ip'],'username':username,'password':password,'module':module,'instance':data['instance'],'primary':'','console_port':data['console_port']}) ''' deploy step: 1.write text file 2.copy file to deploy host 3.excute shell scripts ''' #web server web_server_count = len(data_txt_web) if (web_server_count > 0): for i in range(web_server_count): if('01' in data_txt_web[i]['area']): cmd = r'' for info in data_txt_web: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' +info['username']+ ' ' + info['password'] + ' ' + info['module'] + ' /app/mw test.war ' + info['instance'] + ' '+ info['console_port'] +' \\n' cmd = cmd[:-2] cmd = r'cd /app/'+data_txt_web[i]['username']+'/;echo "'+ cmd + '" > data.txt' #set primary host for deploy data_txt_web[i]['primary'] = 'primary' #var host_ip = data_txt_web[i]['ip'] username = data_txt_web[i]['username'] password = data_txt_web[i]['password'] module = data_txt_web[i]['module'] application.objects.create(host_id=data_txt_web[i]['host_id'],area=data_txt_web[i]['area'],middleware='weblogic',module=module,username=username,console_port=data_txt_web[i]['console_port'],instance_num=data_txt_web[i]['instance'],package='test.war',primary='primary',create_date=today,status='deployed') try : #write data text ssh2_deploy(host_ip,username,password,cmd) #put template file to target host sftp2(host_ip,username,password,'/app/testweb/bak/TEMPLATE.tar','/app/'+username+'/TEMPLATE.tar') #tar xvf file name and run 3 scripts cmd_deploy_step1 = r'cd /app/'+username+';tar xf TEMPLATE.tar;source .cshrc;sh APP.sh;'\ 'cp test.war /app/'+username+'/deploy/applications/'+module+'/;'\ 'cd deploy/scripts;sh 1_envinitall.sh >deploy.log;' ssh2_deploy(host_ip,username,password,cmd_deploy_step1) while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup1 deploy.log') logging.info(result) logging.info(len(result)) if (len(result)==web_server_count): break; else: sleep(30) # web-machinfo.txt version-web-machinfo.txt cmd_web_machinfo = '' cmd_version_web_machinfo = '' for info in data_txt_web: cmd_web_machinfo = cmd_web_machinfo + info['ip'] + ' ' + info['password'] + ' ' +info['username']+ ' /app/' + info['username'] + '/' + info['module'] +' \\n' for i in range(int(info['instance'])): cmd_version_web_machinfo = cmd_version_web_machinfo + info['ip'] + ':' + str(int(info['console_port']) + 1 + i)+' \\n' cmd_web_machinfo = cmd_web_machinfo[:-2] cmd_version_web_machinfo = cmd_version_web_machinfo[:-2] # run DEP.sh for deploy programe cmd_run_deploy = r'cd /app/'+username+'/; sh DEP.sh;cd deploy_all/;echo "' + cmd_web_machinfo +'" > web-machinfo.txt;echo "' + cmd_version_web_machinfo +'" > version-web-machinfo.txt' ssh2_deploy(host_ip,username,password,cmd_run_deploy) #run step 2 and step 3 ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy/scripts;sh 2_createdomain.sh >>deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup2 deploy.log') if (len(result)==web_server_count): break; else: sleep(30) ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy/scripts;sh 3_deployappall.sh >>deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup3 deploy.log') if (len(result)==web_server_count): break; else: sleep(60) continue; except: application.objects.all().filter(host_id=data_txt_web[i]['host_id'],username=username).delete() #app server app_server_count = len(data_txt_app) if ( app_server_count > 0): for i in range(app_server_count): if('01' in data_txt_app[i]['area']): cmd = '' for info in data_txt_app: cmd = cmd + info['area'] + ' ' + info['ip'] + ' ' +info['username']+ ' ' + info['password'] + ' ' + info['module'] + ' /app/mw test.war ' + info['instance'] +' '+info['console_port'] +' \\n' cmd = r'cd /app/'+data_txt_app[i]['username']+';echo "'+ cmd + ' " > data.txt' #set primary host for deploy data_txt_app[i]['primary'] = 'primary' #var host_ip = data_txt_app[i]['ip'] username = data_txt_app[i]['username'] password = data_txt_app[i]['password'] module = data_txt_app[i]['module'] application.objects.create(host_id=data_txt_app[i]['host_id'],area=data_txt_app[i]['area'],middleware='weblogic',module=module,username=username,console_port=data_txt_app[i]['console_port'],instance_num=data_txt_app[i]['instance'],package='test.war',primary='primary',create_date=today,status='deployed') try: #write data text ssh2_deploy(host_ip,username,password,cmd) #put template file to target host sftp2(host_ip,username,password,'/app/testweb/bak/TEMPLATE.tar','/app/'+username+'/TEMPLATE.tar') cmd_deploy = 'cd /app/'+username+';tar xf TEMPLATE.tar;source .cshrc;sh APP.sh;'\ 'cp test.war /app/'+username+'/deploy/applications/'+module+'/;'\ 'cd deploy/scripts;sh 1_envinitall.sh > deploy.log;' ssh2_deploy(host_ip,username,password,cmd_deploy) while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup1 deploy.log') logging.info(result) logging.info(len(result)) if (len(result)==app_server_count): break; else: sleep(30) # web-machinfo.txt version-web-machinfo.txt cmd_app_machinfo = '' cmd_version_app_machinfo = '' for info in data_txt_web: cmd_app_machinfo = cmd_app_machinfo + info['ip'] + ' ' + info['password'] + ' ' +info['username']+ ' /app/' + info['username'] + '/' + info['module'] +' \\n' for i in range(int(info['instance'])): cmd_version_app_machinfo = cmd_version_app_machinfo + info['ip'] + ':' + str(int(info['console_port']) + 1 + i)+' \\n' cmd_app_machinfo = cmd_app_machinfo[:-2] cmd_version_app_machinfo = cmd_version_app_machinfo[:-2] # run DEP.sh for deploy programe cmd_run_deploy = r'cd /app/'+username+'/; sh DEP.sh;cd deploy_all/;echo "' + cmd_app_machinfo +'" > app-machinfo.txt;echo "' + cmd_version_app_machinfo +'" > version-app-machinfo.txt' ssh2_deploy(host_ip,username,password,cmd_run_deploy) #run step 2 and step 3 ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy/scripts;sh 2_createdomain.sh >>deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup2 deploy.log') if (len(result)==app_server_count): break; else: sleep(30) ssh2_deploy(host_ip,username,password,r'cd /app/'+username+'/deploy/scripts;sh 3_deployappall.sh >>deploy.log;') while True: result = ssh2_deploy(host_ip,username,password, r'cd /app/'+username+'/deploy/scripts;grep setup3 deploy.log') if (len(result)==app_server_count): break; else: sleep(60) continue; except: application.objects.all().filter(host_id=data_txt_app[i]['host_id'],username=username).delete() logging.info(data_txt_web) logging.info(data_txt_app) data_txt = data_txt_web + data_txt_app logging.info(data_txt) #input database application app_data = [] for deploy_info in data_txt: if(not deploy_info['primary']): app_data.append(application(host_id=deploy_info['host_id'],area=deploy_info['area'],middleware='weblogic',module=deploy_info['module'],username=deploy_info['username'],console_port=deploy_info['console_port'],instance_num=deploy_info['instance'],package='test.war',primary=deploy_info['primary'],create_date=today,status='deployed')) application.objects.bulk_create(app_data) #run startup scripts and check the deploy status
def project_user_instance(request): order_no_req = request.GET.get("order_no", "") host_user_req = request.GET.get("host_user", "") ops_req = request.GET.get("ops", "") cursor = connection.cursor() if order_no_req: order_no = vhost.objects.filter(order_no=order_no_req).values("order_no", "order_name").distinct() host_instance_info = [] appusers = [] # please notice the parameter formart if not host_user_req: cursor.execute( "select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s", [order_no_req], ) else: cursor.execute( "select host.host_ip,app_user.username,app_user.password from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s and app_user.username = %s", [order_no_req, host_user_req], ) vhosts = dictfetchall(cursor) if len(vhosts) > 0: for host in vhosts: host_ip = host["host_ip"] username = host["username"] password = host["password"] appusers.append(username) # result = ssh2_deploy(host_ip, username, password, r"cdbin;for var in (`ls start-* | sed -e 's/start-//g' -e 's/.sh//g'`);do process=`ps -ef|grep java|grep $var|wc -l`;if [ $process -eq 1 ]; then echo $var start else echo $var stop fi done;") if ops_req: ssh2_deploy(host_ip, username, password, r"cdbin;sh " + ops_req + "-admin.sh 2>&1 &;") ssh2_deploy(host_ip, username, password, r"cdbin;sh " + ops_req + "allsrv.sh 2>&1 &;") sleep(30) else: sftp2(host_ip, username, password, "/app/scripts/cloudops/instance.sh", "/tmp/instance.sh") result = ssh2_deploy(host_ip, username, password, r"sh /tmp/instance.sh;") ssh2_deploy(host_ip, username, password, r"rm -fr /tmp/instance.sh;") for instance in result: insatnce = instance.strip("\n").split(" ") host_instance_info.append( { "host_ip": host_ip, "username": username, "password": password, "instance_name": insatnce[0], "instance_status": insatnce[1], } ) cursor.execute( "select distinct(app_user.username) from application_vhost host,application_app_user app_user where host.host_id = app_user.host_id and host.order_no= %s", [order_no_req], ) deploy_user = dictfetchall(cursor) cursor.close() return render( request, "weblogic_instances.html", {"order_no": order_no, "deploy_user": deploy_user, "host_instance_info": host_instance_info}, )
def handle_uploaded_file(request): order_no = request.POST.get("order_no", "") host = request.POST.get("deploy_host", "") user = request.POST.get("deploy_user", "") file_req = request.FILES["uploadfile"] file_name = file_req.name try: path = "/data/uploadpackage/" + order_no + "/" if not os.path.exists(path): os.makedirs(path) destination = open(path + file_name, "wb+") for chunk in file_req.chunks(): destination.write(chunk) destination.close() else: destination = open(path + file_name, "wb+") for chunk in file_req.chunks(): destination.write(chunk) destination.close() moduleinfo = application.objects.filter(host_id=host, username=user).values("module") module = moduleinfo[0]["module"] vhost_ip = vhost.objects.all().filter(host_id=host).values("host_ip") host_ip = vhost_ip[0]["host_ip"] userinfo = app_user.objects.all().filter(username=user).values("username", "password") username = userinfo[0]["username"] password = userinfo[0]["password"] sftp2( host_ip, username, password, path + file_name, "/app/" + username + "/" + module + "/applications/" + file_name, ) # step1: release ssh2_deploy( host_ip, username, password, r"cd /app/" + username + "/deploy_all/;sh release-" + module + "-*.sh > " + username + "_upload_deploy.log;", ) while True: result = ssh2_deploy( host_ip, username, password, r"cd /app/" + username + "/deploy_all/;grep releaseok " + username + "_upload_deploy.log;", ) if len(result) > 0: break else: sleep(10) ssh2_deploy( host_ip, username, password, r"cd /app/" + username + "/deploy_all/;sh stop-" + module + "-srv.sh >> " + username + "_upload_deploy.log;", ) while True: result = ssh2_deploy( host_ip, username, password, r"cd /app/" + username + "/deploy_all/;grep stopok " + username + "_upload_deploy.log;", ) if len(result) > 0: break else: sleep(10) ssh2_deploy( host_ip, username, password, r"cd /app/" + username + "/deploy_all/;sh start-" + module + "-srv.sh >> " + username + "_upload_deploy.log; ", ) while True: result = ssh2_deploy_log( host_ip, username, password, r"cd /data/" + username + "/logs/;awk 'NF{a=$0}END{print a}' " + module + "-?01-srv01-" + (datetime.now().date()).strftime("%Y%m%d") + ".log|grep -i RUNNING ;", ) if len(result) > 0: break else: sleep(30) except Exception, e: logging.error(e)
def tomcat_deploy(data_req): data_txt_web = [] data_txt_app = [] for data in data_req: hostname = data["host_name"] username = data["username"] if "web" in hostname: web_index = username.index("web") module = username[0:web_index] + "-" + username[web_index:] password = ( app_user.objects.all() .filter(host_id=data["host_id"], username=data["username"]) .values("password")[0]["password"] ) data_txt_web.append( { "host_id": data["host_id"], "hostname": hostname, "area": data["area"], "ip": data["ip"], "username": username, "password": password, "module": module, "instance": data["instance"], "primary": "", "version_num": data["version_num"], } ) elif "app" in hostname: app_index = username.index("app") module = username[0:app_index] + "-" + username[app_index:] password = ( app_user.objects.all() .filter(host_id=data["host_id"], username=data["username"]) .values("password")[0]["password"] ) data_txt_app.append( { "host_id": data["host_id"], "hostname": hostname, "area": data["area"], "ip": data["ip"], "username": username, "password": password, "module": module, "instance": data["instance"], "primary": "", "version_num": data["version_num"], } ) """ cloudops step: 1.write text file 2.copy file to cloudops host 3.excute shell scripts """ # web server web_server_count = len(data_txt_web) if web_server_count > 0: for i in range(web_server_count): if "01" in data_txt_web[i]["area"]: cmd = r"" for info in data_txt_web: cmd = ( cmd + info["area"] + " " + info["ip"] + " " + info["username"] + " " + info["password"] + " " + info["module"] + " " + info["instance"] + " " + info["version_num"] + " \\n" ) cmd = cmd[:-2] cmd = r"cd /app/" + data_txt_web[i]["username"] + '/;echo "' + cmd + '" > data.txt' # set primary host for cloudops data_txt_web[i]["primary"] = "primary" # var host_ip = data_txt_web[i]["ip"] username = data_txt_web[i]["username"] password = data_txt_web[i]["password"] module = data_txt_web[i]["module"] application.objects.create( host_id=data_txt_web[i]["host_id"], area=data_txt_web[i]["area"], middleware="tomcat", module=module, username=username, version_num=data_txt_web[i]["version_num"], instance_num=data_txt_web[i]["instance"], primary="primary", create_date=today, status="deployed", ) # write data text ssh2_deploy(host_ip, username, password, cmd) # put template file to target host sftp2(host_ip, username, password, "/app/tmt/tomcat.tar", "/app/" + username + "/tomcat.tar") # tar xvf file name and run 3 scripts cmd_deploy_step1 = ( r"cd /app/" + username + ";tar xf tomcat.tar;sh 01_startdoc.sh >>cloudops.log;" "sh 02_mvserver.sh >>cloudops.log;" ) ssh2_deploy(host_ip, username, password, cmd_deploy_step1) # app server app_server_count = len(data_txt_app) if app_server_count > 0: for i in range(app_server_count): if "01" in data_txt_app[i]["area"]: cmd = "" for info in data_txt_app: cmd = ( cmd + info["area"] + " " + info["ip"] + " " + info["username"] + " " + info["password"] + " " + info["module"] + " " + info["instance"] + " " + info["version_num"] + " \\n" ) cmd = r"cd /app/" + data_txt_app[i]["username"] + ';echo "' + cmd + ' " > data.txt' # set primary host for cloudops data_txt_app[i]["primary"] = "primary" # var host_ip = data_txt_app[i]["ip"] username = data_txt_app[i]["username"] password = data_txt_app[i]["password"] module = data_txt_app[i]["module"] application.objects.create( host_id=data_txt_app[i]["host_id"], area=data_txt_app[i]["area"], middleware="tomcat", module=module, username=username, version_num=data_txt_app[i]["version_num"], instance_num=data_txt_app[i]["instance"], primary="primary", create_date=today, status="deployed", ) # write data text ssh2_deploy(host_ip, username, password, cmd) # put template file to target host sftp2(host_ip, username, password, "/app/tmt/tomcat.tar", "/app/" + username + "/tomcat.tar") cmd_deploy_step1 = ( r"cd /app/" + username + ";tar xf tomcat.tar;sh 01_startdoc.sh >>cloudops.log;" "sh 02_mvserver.sh >>cloudops.log;" ) ssh2_deploy(host_ip, username, password, cmd_deploy_step1) logging.info(data_txt_web) logging.info(data_txt_app) data_txt = data_txt_web + data_txt_app logging.info(data_txt) # input database application app_data = [] for deploy_info in data_txt: if not deploy_info["primary"]: app_data.append( application( host_id=deploy_info["host_id"], area=deploy_info["area"], middleware="tomcat", module=deploy_info["module"], username=deploy_info["username"], version_num=deploy_info["version_num"], instance_num=deploy_info["instance"], primary=deploy_info["primary"], create_date=today, status="deployed", ) ) application.objects.bulk_create(app_data)