def assets_info(request, ID): salt_api = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt_api.login() the_asset = Assets.objects.get(id=ID) ip = the_asset.lan_ip res = eval(salt_api.fun(ip, 'grains.items'))['return'][0][ip] the_asset.hostname = res['host'] the_asset.mac = res['hwaddr_interfaces']['eth0'] the_asset.cpu_model = res['cpu_model'] the_asset.cpu_num = res['num_cpus'] the_asset.memory = sizeformat(res['mem_total'], unit='MB') the_asset.system_type = res['os'] the_asset.system_version = '.'.join(map(str, res['osrelease_info'])) if type(res['osrelease_info']) == list else str( res['osrelease_info']) the_asset.system_arch = res['osarch'] the_asset.kernel = res['kernel'] the_asset.kernel_version = res['kernelrelease'] res = eval(salt_api.fun(ip, 'disk.usage'))['return'][0][ip] disk_status = 'Disk Usage:</br>' for key1 in res.keys(): disk_status += key1 + ':</br>' for key2 in res[key1]: if key2 in ['available', 'used', ]: disk_status += '%s -- %s</br>' % (key2, sizeformat(res[key1][key2], unit='KB')) elif key2 in ['filesystem']: disk_status += '%s -- %s</br>' % (key2, res[key1][key2]) the_asset.desc = disk_status the_asset.save() salt_api.logout() hosts = Assets.objects.all() return render_to_response('assets/assets.html', locals(), RequestContext(request))
def exec_rollback(request, ID): the_rollback = RollBack.objects.get(id=ID) if the_rollback.in_use: return http_error(request, u'正式环境为此版本') else: old_rollback = RollBack.objects.get(in_use=True) old_rollback.in_use = False old_rollback.save() the_project = Project.objects.get(name=the_rollback.project) ln_path = os.path.abspath(the_project.server_path) pro_server_path = os.path.abspath( the_project.server_path) + "_%s" % the_rollback.hash pro_host_list = list(eval(the_project.pro_host_list)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() salt.cmd(host_list_str, 'rm -f %s' % ln_path) salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path)) salt.logout() the_rollback.in_use = True the_rollback.save() pro = ReleaseRecord(project=the_project.name, branch=the_rollback.branch, hash=the_rollback.hash, release_user=request.user) pro.save() return http_success(request, u'版本回退成功')
def exec_rollback(request, ID): the_rollback = RollBack.objects.get(id=ID) if the_rollback.in_use: return http_error(request, u'正式环境为此版本') else: old_rollback = RollBack.objects.get(in_use=True) old_rollback.in_use = False old_rollback.save() the_project = Project.objects.get(name=the_rollback.project) ln_path = os.path.abspath(the_project.server_path) pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_rollback.hash pro_host_list = list(eval(the_project.pro_host_list)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() salt.cmd(host_list_str, 'rm -f %s' % ln_path) salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path)) salt.logout() the_rollback.in_use = True the_rollback.save() pro = ReleaseRecord(project=the_project.name, branch=the_rollback.branch, hash=the_rollback.hash, release_user=request.user) pro.save() return http_success(request, u'版本回退成功')
def pro_release(request, ID): try: the_pre_record = PreRecord.objects.get(id=ID) the_release = Test.objects.all().get(id=the_pre_record.test_id) the_project = Project.objects.get(name=the_release.project) pro_host_list = list(eval(the_project.pro_host_list)) ln_path = os.path.abspath(the_project.server_path) test_server_path = os.path.abspath(the_release.server_path) pro_server_path = os.path.abspath( the_project.server_path) + "_%s" % the_release.last_hash local_path = os.path.join('/ops/%s' % the_project.name, test_server_path.lstrip('/'), str(the_pre_record.test_id)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() for i in pro_host_list: stdout = rrsync(local_path, i, pro_server_path, ['.git*']) result = stdout.replace('\n', '</br>') #import os cmd_rm = 'rm -rf %s' % ln_path cmd_ln = 'ln -s %s %s' % (pro_server_path, ln_path) cmd_ansible_rm = "ansible php1 -i /etc/ansible/hosts -mshell -a '%s'" % cmd_rm cmd_ansible_ln = "ansible php1 -i /etc/ansible/hosts -mshell -a '%s' " % cmd_ln #print cmd_ansible_ln #cmd="echo %s > /tmp/1.log"%cmd_ansible_rm #os.system(cmd) os.system(cmd_ansible_rm) os.system(cmd_ansible_ln) salt.cmd(host_list_str, 'rm -rf %s' % ln_path) salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path)) print 'ln -s %s %s' % (pro_server_path, ln_path) salt.logout() try: the_rollback = RollBack.objects.get(in_use=True) the_rollback.in_use = False the_rollback.save() except: pass i_rollback = RollBack(project=the_project.name, branch=the_release.last_branch, hash=the_release.last_hash, in_use=True) i_rollback.save() pro = ReleaseRecord(project=the_project.name, branch=the_release.last_branch, hash=the_release.last_hash, release_user=request.user) pro.save() return render_to_response('release/release_result.html', locals(), RequestContext(request)) except: return http_error(request, u'发布出现错误,请检查预发布记录')
def del_rollback(request, ID): the_rollback = RollBack.objects.get(id=ID) if the_rollback.in_use: return http_error(request, u'不能删除正在使用的版本') else: the_project = Project.objects.get(name=the_rollback.project) pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_rollback.hash pro_host_list = list(eval(the_project.pro_host_list)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() salt.cmd(host_list_str, 'rm -rf %s' % pro_server_path) salt.logout() the_rollback.delete() return http_success(request, u'版本删除成功')
def del_rollback(request, ID): the_rollback = RollBack.objects.get(id=ID) if the_rollback.in_use: return http_error(request, u'不能删除正在使用的版本') else: the_project = Project.objects.get(name=the_rollback.project) pro_server_path = os.path.abspath( the_project.server_path) + "_%s" % the_rollback.hash pro_host_list = list(eval(the_project.pro_host_list)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() salt.cmd(host_list_str, 'rm -rf %s' % pro_server_path) salt.logout() the_rollback.delete() return http_success(request, u'版本删除成功')
def pro_release(request, ID): try: the_pre_record = PreRecord.objects.get(id=ID) the_release = Test.objects.all().get(id=the_pre_record.test_id) the_project = Project.objects.get(name=the_release.project) pro_host_list = list(eval(the_project.pro_host_list)) ln_path = os.path.abspath(the_project.server_path) test_server_path = os.path.abspath(the_release.server_path) pro_server_path = os.path.abspath(the_project.server_path) + "_%s" % the_release.last_hash local_path = os.path.join('/ops/%s' % the_project.name, test_server_path.lstrip('/'), str(the_pre_record.test_id)) host_list_str = ','.join(pro_host_list) salt = SaltApi(SALTAPI_URL, SALTAPI_USER, SALTAPI_PASSWORD) salt.login() for i in pro_host_list: stdout = rrsync(local_path, i, pro_server_path, ['.git*']) result = stdout.replace('\n', '</br>') salt.cmd(host_list_str, 'rm -rf %s' % ln_path) salt.cmd(host_list_str, 'ln -s %s %s' % (pro_server_path, ln_path)) salt.logout() try: the_rollback = RollBack.objects.get(in_use=True) the_rollback.in_use = False the_rollback.save() except: pass i_rollback = RollBack(project=the_project.name, branch=the_release.last_branch, hash=the_release.last_hash, in_use=True) i_rollback.save() pro = ReleaseRecord(project=the_project.name, branch=the_release.last_branch, hash=the_release.last_hash, release_user=request.user) pro.save() return render_to_response('release/release_result.html', locals(), RequestContext(request)) except: return http_error(request, u'发布出现错误,请检查预发布记录')