def block(): if str(GetModel.deploy_type()) == 'kvm': if os.path.exists('/usr/local/udsafe/automatedkvm.txt'): with open('/usr/local/udsafe/automatedkvm.txt', 'r') as f: kvm_ip = f.read() return json.dumps({'ip': kvm_ip}) else: w_kvm = { 'ip': GetModel.kvm_ip() } os.system('mkdir -p /usr/local/udsafe/') with open('/usr/local/udsafe/automatedkvm.txt', 'wb') as f: f.write(GetModel.kvm_ip()) return json.dumps(w_kvm) else: return json.dumps({'ip': GetModel.panacube_ip()})
def insert_sqls(data): print 'KVM>>>IP>>> {}'.format(GetModel.kvm_ip()) db = re_conn() curr = db.cursor() if isinstance(data, list): for item in data: if isinstance(item, dict): insert_sql(item) else: logger.info('写入数据库参数类型有误---dict类型') return False db.close() else: logger.info('写入数据库参数类型有误----list类型') return False
def create_cloud(): """ 创建云平台 :return: """ from app_projects.tools.image import kvm_image_upload q.put({"type": "云主机部署", "size": "上传大小", "speed": "start", "percentage": "{}".format(0)}) kvm_image_upload() from app_projects.tools.env_deploy import deploy_kvm deploy_kvm() global default_dict if 'bigData' in default_dict: push_bigdata_db() default_dict['cloudPlatform'] = 'yes' deployment_complete() w_kvm = { 'ip': GetModel.kvm_ip() } q.put({"type": "云主机部署", "size": "上传大小", "speed": "stop", "percentage": "{}".format(100)}) return json.dumps({"code": 0, 'message': 'Successful'})
def re_conn(): db_ip = str(GetModel.kvm_ip()) _conn_status = True _max_retries_count = 10 # 设置最大重试次数 _conn_retries_count = 0 # 初始重试次数 _conn_timeout = 10 # 连接超时时间为3秒 while _conn_status and _conn_retries_count <= _max_retries_count: try: db = MySQLdb.connect(db_ip, 'udsafe', 'udsafe', 'panacube', charset='utf8', connect_timeout=_conn_timeout) _conn_status = False return db except Exception as e: print e _conn_retries_count += 1 print _conn_retries_count print 'connect db is error!!' continue
def deploy_kvm(update=False): """ 部署虚拟机 :return: """ print update, '正在执行操作' if update is False: panacube_ip = GetModel.kvm_ip() else: with open('/usr/local/udsafe/automatedkvm.txt', 'r') as f: panacube_ip = f.read() if update is False: awcloud_obj = ClearAwCloud(GetModel.get_auth_header()) q.put({ "type": "云主机部署", "speed": "conducting", "percentage": "{}".format(generate_random(33, 37)) }) res_data = awcloud_obj.run() # 创建云主机 if res_data['code'] != u'0': return json.dumps({"code": 1, 'message': res_data['message']}) # 替换配置文件ip if update is True: parent_speed = 98 _type = '云主机升级' else: parent_speed = 86 _type = '云主机部署' q.put({ "type": _type, "speed": "conducting", "percentage": "{}".format(generate_random(81, 86)) }) is_docker = get_cmd("cat {} | grep 'IS_DOCKER = ' | grep -v os".format( PathDir.local_settings())).split('=')[-1] os.system("""sed -i "s/{}/ {}/" {file}""".format( is_docker, "False", file=PathDir.local_settings())) saas_ip = get_cmd("cat {} | grep 'SAAS_IP = ' | grep -v os".format( PathDir.local_settings())).split('= ')[-1] os.system("""sed -i "s/{}/{}/" {file} | grep -v os""".format( saas_ip, "\\'{}\\'".format(GetModel.awcloud_ip()), file=PathDir.local_settings())) mysql_ip = get_cmd("cat {} | grep 'MYSQL_IP = ' | grep -v open".format( PathDir.local_settings())) os.system( """sed -i "s/{}/ MYSQL_IP = {}/" {file} | grep -v os""".format( mysql_ip, "\\'{}\\'".format(panacube_ip), file=PathDir.local_settings())) mysql_port = get_cmd("cat {} | grep 'MYSQL_PORT = ' | grep -v os".format( PathDir.local_settings())).split('= ')[-1] os.system("""sed -i "s/{}/{}/" {file}""".format( mysql_port, 3306, file=PathDir.local_settings())) if update is False: cloud_id = res_data.get('data').get('data')[0] f = l = 86 while True: if l >= 97: f -= 7 l -= 15 else: f += 7 l += 15 if l == f: f -= 3 d = generate_random(f, l) if d > parent_speed: parent_speed = d if parent_speed > 100: parent_speed = 98 q.put({ "type": "云主机部署", "speed": "conducting", "percentage": "{}".format(parent_speed) }) current_app.logger.info('正在等待虚拟机启动') if awcloud_obj.get_cloud_status(cloud_id) is True: current_app.logger.info('虚拟机启动成功') break time.sleep(3) chmod_id_rsa = 'chmod 600 {id_rsa}'.format(id_rsa=PathDir.panacube_idrsa()) os.system(chmod_id_rsa) # 添加mysql配置文件 # append_mariadb = mariadb_con.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip) # os.system(append_mariadb) copy_panacube = "scp -i {id_rsa} -r {project_path} root@{panacube_ip}:/home/udsafe/".format( id_rsa=PathDir.panacube_idrsa(), project_path=PathDir.project_path(), panacube_ip=panacube_ip) time.sleep(60) os.system(copy_panacube) # 重启mariadb os.system( restart_mariadb.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip)) # 更新数据库 os.system( update_db.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip, sql_name=config.get('pancube_sql')['sql_name'])) if 'TrueOS' in get_all_path()['bigdataPath']: gateway_system = 'FreeBSD' else: gateway_system = 'CentOS' # os.system(del_param_name.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip)) os.system( set_bigdata_type.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip, gateway_system=gateway_system)) print '更新3.0 packages安装包' os.system( update_pip_packages.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip)) print 'pip 安装结束' # 重启服务 os.system( restart_supervisord_all.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip)) # if update is False: # 更新数据库 # os.system(update_db.format(id_rsa=PathDir.panacube_idrsa(), panacube_ip=panacube_ip)) if update is False: # 添加消息推送 push_message() # 添加大数据配置 push_bigdata_db()
def run(self): q.put({ "type": "云主机部署", "percentage": "37", "speed": "conducting", "size": "上传大小" }) url = "http://{}/awstack-resource/v1/server".format( GetModel.awcloud_ip()) q.put({ "type": "云主机部署", "size": "上传大小", "speed": "conducting", "percentage": "{}".format(generate_random(37, 58)) }) self.create_project() flavor_id = self.create_flavor()['id'] q.put({ "type": "云主机部署", "size": "上传大小", "speed": "conducting", "percentage": "{}".format(generate_random(59, 71)) }) current_app.logger.info("flavor ID: {}".format(flavor_id)) server_info = { "admin_pass": "", "name": "panacube3", "hostname": "panacube3", "fixed_ip": GetModel.kvm_ip(), "count": 1, "network_id": GetModel.network_id(), "keypair_id": "", "use_local": False, "flavor": flavor_id, "security_id": self.get_security_group_id()['id'], "dataVolumes": [], "block_device_mapping": { "disk_bus": "" }, # 硬盘控制器映射 "os_type": "linux", "image_id": GetModel.image_id(None), # 镜像ID "volumeSize": 100, # 硬盘大小 "volume_type": self.get_volume_type()[0]['volumeTypeId'] # 硬盘ID } current_app.logger.info("create cloud info: {}".format(server_info)) resp = requests.post(url, data=json.dumps(server_info), headers=self.append_headers(), verify=False) current_app.logger.info('create cloud return data: {}'.format( resp.json())) if resp.json()['code'] == '02010303': raise ValueError("创建云主机所选规格不足") current_app.logger.info('创建云主机{}'.format(resp.json())) q.put({ "type": "云主机部署", "size": "上传大小", "speed": "conducting", "percentage": "{}".format(generate_random(72, 80)) }) with open('/usr/local/udsafe/automatedkvm.txt', 'w') as f: f.write(GetModel.kvm_ip()) return resp.json()