Esempio n. 1
0
    def get_security_group_id(self):
        """新建安全组"""
        CREATE_SECURITY_GROUPS = "http://{}/awstack-resource/v1/security_groups".format(
            GetModel.awcloud_ip())

        security_group = {"name": "panacube3.0", "description": "3.0项目部署"}

        q.put({"type": "云平台", "speed_p": "stop", "percentage": "69"})
        resp = requests.post(CREATE_SECURITY_GROUPS,
                             data=json.dumps(security_group),
                             headers=self.append_headers(),
                             verify=False)
        # ['id]
        group_obj = resp.json()

        if 'code' in group_obj:
            if group_obj['code'] == '02310100':
                _url = "http://{}/awstack-resource/v1/security_groups".format(
                    GetModel.awcloud_ip())
                data = requests.get(_url,
                                    headers=self.append_headers(),
                                    verify=False)
                js_data = data.json()['data']['data']
                _id = None
                for i in js_data:
                    if i['name'] == 'default':
                        self.set_rule(i['id'], OPEN_PORT)
                        _id = i['id']
                return {'id': _id}

        if group_obj['data'] is not None:
            self.set_rule(group_obj['data']['data']['id'], OPEN_PORT)
        return group_obj
Esempio n. 2
0
def clear_code(token):
    """
    清除验证码
    :param token:
    :return:
    """
    url = 'http://{}:80/awstack-user/v1/params'.format(GetModel.awcloud_ip())

    headers = {
        "Content-Type": "application/json",
        "X-Auth-Token": token,
    }
    body = {
        "paramId": 917,
        "enterpriseUid": "0",
        "regionUid": "0",
        "regionKey": "0",
        "parentId": 1,
        "paramName": "LOGIN_CAPTCHA_ISOPEN",
        "path": "1/917/",
        "paramValue": "-1"
    }
    resp = requests.put(url, headers=headers, data=json.dumps(body))
    if resp.status_code == 200:
        return True
    return False
Esempio n. 3
0
    def set_rule(self, id, port):
        """
        设置安全组的开放端口
        :param id:
        :param port:
        :return:
        """
        ADD_GROUP_RULE = "http://{}/awstack-resource/v1/security_groups/rule".format(
            GetModel.awcloud_ip())

        if isinstance(port, list):
            for i in port:
                rule_body = {
                    "cidr": "0.0.0.0/0",
                    "direction": "ingress",
                    "enabled": "1",
                    "ethertype": "IPV4",
                    "fromPort": i,
                    "groupId": id,
                    "ipProtocol": "tcp",
                    "toPort": i
                }
                if i == "-1":
                    rule_body['ipProtocol'] = "icmp"
                requests.post(ADD_GROUP_RULE,
                              data=json.dumps(rule_body),
                              headers=self.append_headers(),
                              verify=False)
Esempio n. 4
0
 def update_image_info(self, image_id):
     current_app.logger.info("storage image id: {}".format(image_id))
     url = 'http://{}/awstack-resource/v1/image/{}'.format(
         GetModel.awcloud_ip(), image_id)
     request_data = {
         "name": "智能存储镜像",
         "disk_format": "qcow2",
         "architecture": "x86_64",
         "os_version": None,
         "os_type": "Linux",
         "os_distro": "CentOS X64",
         "is_public": True,
         "is_protected": False,
         "imageUid": "a5eb23fb-0b3b-4cb2-b4ce-5886b191994c",
         "vol_size": "10",
         "minimum_disk": "10",
         "hw_disk_bus": "scsi",
         "hw_video_model": "vga",
         "hw_vif_model": "virtio",
         "hw_qemu_guest_agent": "true",
         "hw_boot_menu": "false",
         "hw_vif_multiqueue_enabled": "false"
     }
     headers = self.headers
     headers['Content-Type'] = 'application/json;charset=UTF-8'
     resp = requests.put(url,
                         data=json.dumps(request_data),
                         headers=headers,
                         verify=False)
     current_app.logger.info("update storage image info: {}".format(
         resp.json()))
     return resp.json()
Esempio n. 5
0
    def login_params(self):
        authUrl = 'http://{}'.format(GetModel.awcloud_ip())

        return {
            "url": authUrl + "/awstack-user/v1/login",
            "json": self.request_data,
            "headers": {
                "Content-Type": "application/json"
            }
        }
Esempio n. 6
0
    def get_node_info(self):
        """
        获取node物理节点
        :return:
        """
        GET_NODE = "http://{}/awstack-user/v1/enterprises/66666666666666666666666666666666/regions/8b4f22a536f845769820b5777d549238/nodes/list".format(
            GetModel.awcloud_ip())

        resp = requests.get(GET_NODE, verify=False)
        return resp.json()
Esempio n. 7
0
 def get_cloud_status(self, cloud_id):
     url = 'http://{}/awstack-resource/v1/server/{}'.format(
         GetModel.awcloud_ip(), cloud_id)
     resp = requests.get(url, headers=self.append_headers(), verify=False)
     current_app.logger.info("get cloud ID {} status: {}".format(
         cloud_id,
         resp.json().get('data').get('data').get('status')))
     if resp.json()['data']['data'].get('status') == 'ACTIVE':
         return True
     return False
Esempio n. 8
0
 def get_sds_partitions(self):
     import copy
     url = 'http://{}/awstack-user/v1/region/FFFFF/ceph'.format(
         GetModel.awcloud_ip())
     headers = copy.deepcopy(self.headers)
     headers['X-Register-Code'] = 'FFFFF'
     resp = requests.get(url, headers=headers, verify=False)
     resp_data = resp.json()
     if resp_data['code'] == '02120701':
         raise ValueError('获取osd tree信息失败')
     return resp_data
Esempio n. 9
0
    def get_volume_type(self):
        """
        获取云硬盘ID
        :return:
        """
        GET_VOLUMES = "http://{}/awstack-user/v1/storage/list".format(
            GetModel.awcloud_ip())

        resp = requests.get(GET_VOLUMES,
                            headers=self.append_headers(),
                            verify=False)
        return resp.json()
Esempio n. 10
0
    def create_flavor(self):
        """创建规格"""
        CREATE_FLAVOR = "http://{}/awstack-resource/v1/flavor".format(
            GetModel.awcloud_ip())

        q.put({"type": "云平台", "speed_p": "stop", "percentage": "49"})
        resp = requests.post(CREATE_FLAVOR,
                             data=json.dumps(flavor_info),
                             headers=self.append_headers(),
                             verify=False)
        js_data = resp.json()
        current_app.logger.info(
            'create flavor return data: {}'.format(js_data))
        if js_data['code'] == '02013201':
            d = requests.get("http://{}/awstack-resource/v1/flavors".format(
                GetModel.awcloud_ip()),
                             headers=self.headers,
                             verify=False)
            for i in d.json()['data']['data']:
                if i['name'] == 'panacube3.0':
                    return {"id": i['id']}
        return js_data
Esempio n. 11
0
    def get_external_network(self):
        """
        获取外部网络
        :return:
        """
        # ip = model.get_params('awcloud_ip')
        ip = GetModel.awcloud_ip()
        current_app.logger.info('海云IP地址为--->{}'.format(ip))
        GET_EXTERNAL_NETWORK = "http://{}/awstack-resource/v1/getExternalNetworks".format(
            ip)

        resp = requests.get(GET_EXTERNAL_NETWORK,
                            headers=self.headers,
                            verify=False)
        return resp.json()
Esempio n. 12
0
 def get_network_card(self, ip, network_id):
     """
     查询该ip是否可用
     :param ip: ip是需要设置panacube3.0的IP  network_id是外部网络id
     :return:
     """
     _url = 'http://{}/awstack-resource/v1/networks/{}'.format(
         GetModel.awcloud_ip(), network_id)
     resp = requests.get(_url, headers=self.headers, verify=False)
     for i in resp.json()['data']['data']:
         if ip in i['ip']:
             current_app.logger.info('创建云主机Ip已存在---{}'.format(ip))
             return "null"
     current_app.logger.info('验证KVM IP: {}'.format(ip))
     return ip
Esempio n. 13
0
    def upload_image(self, path, _type="bigdata"):
        """
        上传镜像
        :param path: 镜像path
        :param name: 镜像名字
        :param _t:   镜像类型
        :param size: 镜像大小
        :return:
        """
        UPLOAD_IMAGE = "http://{}/awstack-resource/v1/uploadimagez".format(
            GetModel.awcloud_ip())

        self.create_project()
        image_data = {
            'file': ('demo.iso', open(path, 'rb')),
            "videoQueue": "false",
            "qemu": "false",
            "diskCtrl": "false",
            "disk_format": "qcow2",
            "architecture": "x86_64",
            "os_distro": "CentOS X64",
            "os_type": "Linux",
        }
        if _type == "bigdata":
            image_data.update(name="智能存储镜像",
                              vol_size="3",
                              is_public="true",
                              minimum_disk="3")
        else:
            image_data.update(name="panacube3.0",
                              vol_size="2",
                              is_public="false",
                              minimum_disk="2")
        m = MultipartEncoder(
            fields=image_data,
            boundary='---------------------------7de1ae242c06ca')
        _headers = self.append_headers()
        m = MultipartEncoderMonitor(m)
        _headers["Content-Type"] = m.content_type
        resp = requests.post(UPLOAD_IMAGE,
                             data=m,
                             headers=_headers,
                             verify=False)

        return resp.json()
Esempio n. 14
0
def push_message():
    """
    消息推送
    :return:
    """
    URL = 'http://{}/awstack-user/v1/params'.format(GetModel.awcloud_ip())
    current_app.logger.info('message push API: {}'.format(URL))
    manager = AWCloudManage('', '')
    is_login = True
    while is_login:
        try:
            manager._login()
            is_login = False
        except ConnectionError as e:
            print "docker 未启动连接失败"
            time.sleep(3)
    headers = manager.get_auth_header()
    body = {
        "enterpriseUid":
        "66666666666666666666666666666666",
        "paramValue":
        "http://{}/v1/awcloud-message/".format(GetModel.panacube_ip()),
        "paramName":
        "resource_pass",
        "parentId":
        939,
        "regionUid":
        "8b4f22a536f845769820b5777d549238",
        "paramLevel":
        2,
        "paramDesc":
        "消息推送",
        "regionKey":
        0
    }
    resp = requests.post(URL, data=json.dumps(body), headers=headers)
    current_app.logger.info("message return info: {}".format(resp.json()))
    if resp.json().get('code') == '01170101':
        print 'push message: 参数名称已存在'
    current_app.logger.info('push message: 添加消息推送成功')
Esempio n. 15
0
def update_storage_image():
    """
    更新智能存储镜像
    :return:
    """
    q.put({
        "type": "镜像上传",
        "size": "上传大小",
        "speed": "conducting",
        "percentage": "{}".format(generate_random(1, 6))
    })
    if GetModel.deploy_type() == 'kvm':
        awcloud = ClearAwCloud(GetModel.get_auth_header())
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "{}".format(generate_random(17, 36))
        })
        image_obj = awcloud.upload_image(PathDir.image_path())
        print '上传的镜像信息: %s' % image_obj
        image_id = image_obj['id']
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "{}".format(generate_random(38, 52))
        })
        model.update_or_create('storage_img_id', image_id)
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "{}".format(generate_random(57, 78))
        })
        f = l = 79
        while True:
            resp = awcloud.update_image_info(image_id)
            f += 4
            l += 10
            if l >= 90:
                q.put({
                    "type": "镜像上传",
                    "size": "上传大小",
                    "speed": "conducting",
                    "percentage": "{}".format(generate_random(f, l))
                })
            else:
                f -= 2
                l -= 3
                q.put({
                    "type": "镜像上传",
                    "size": "上传大小",
                    "speed": "conducting",
                    "percentage": "{}".format(generate_random(f, l))
                })
            if resp.get('status') != 415:
                if resp['code'] == '0':
                    current_app.logger.info(
                        'update storage image property successful')
                else:
                    current_app.logger.info(
                        'update storage image property fail')
                break
            else:
                time.sleep(2)
        os.system("sh {set_tag} {openstack} {id}".format(
            set_tag=PathDir.tag_shell_path(),
            openstack=PathDir.openstack(),
            id=image_id))
    else:
        awcloud = ClearAwCloud(GetModel.get_auth_header())
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "23"
        })
        model.update_or_create('awcloud_ip', GetModel.awcloud_ip())
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "48"
        })
        image_id = awcloud.upload_image(PathDir.image_path()).get('id')
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "{}".format(generate_random(56, 88))
        })
        model.update_or_create('storage_img_id', image_id)
        while True:
            resp = awcloud.update_image_info(image_id)
            if resp.get('status') != 415:
                if resp['code'] == '0':
                    current_app.logger.info(
                        'update storage image property successful')
                else:
                    current_app.logger.info(
                        'update storage image property fail')
                break
            else:
                time.sleep(2)
        os.system("sh {set_tag} {openstack} {id}".format(
            set_tag=PathDir.tag_shell_path(),
            openstack=PathDir.openstack(),
            id=image_id))
    from app_projects.deploy.get_config import move_storage_code
    storage_code_name = move_storage_code()
    node_list = os.popen("consul members | awk '{ print $1 }'").read().split(
        '\n')[1:-1]
    for item in node_list:
        os.system("scp -r /var/deploy/%s %s:/usr/local/udsafe/%s" %
                  (storage_code_name, item, storage_code_name))
        q.put({
            "type": "镜像上传",
            "size": "上传大小",
            "speed": "conducting",
            "percentage": "{}".format(generate_random(89, 96))
        })
Esempio n. 16
0
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()
Esempio n. 17
0
    def create_project(self):
        """创建项目"""
        project_info = {
            "project": {
                "name": "panacube_3.0",
                "nameNe": "panacube_3.0",
                "domainUid": self.headers.get('domain_id'),
                "enabled": True,
                "description": "panacube3.0项目"
            },
            "quotas": [
                {
                    "enterpriseUid": self.headers.get('enterprise_id'),
                    "type": "project_quota",
                    "name": "cores",
                    "isShow": True,
                    "hardLimit": CPU_QUOTA,
                    "domainUid": self.headers.get('domain_id')
                },
                {
                    "enterpriseUid": self.headers.get('enterprise_id'),
                    "type": "project_quota",
                    "name": "                                    ",
                    "isShow": True,
                    "hardLimit": MEMORY_QUOTA,  # 内存大小
                    "domainUid": self.headers.get('domain_id')
                },
                {
                    "enterpriseUid": self.headers.get('enterprise_id'),
                    "type": "project_quota",
                    "name": "gigabytes",
                    "isShow": True,
                    "hardLimit": STORAGE_QUOTA,  # 存储配额
                    "domainUid": self.headers.get('domain_id')
                }
            ]
        }

        CREATE_PROJECT_URL = "http://{}/awstack-user/v1/enterprises/66666666666666666666666666666666/projects/check?enterpriseUid=66666666666666666666666666666666".format(
            GetModel.awcloud_ip())
        current_app.logger.info('开始创建项目')
        try:
            requests.keep_alive = False
            resp = requests.post(CREATE_PROJECT_URL,
                                 data=json.dumps(project_info),
                                 headers=self.headers,
                                 verify=False)
        except Exception as e:
            raise e

        if resp.json()['code'] == "##":
            re = requests.get(
                "http://{}/awstack-user/v1/enterprises/66666666666666666666666666666666/projects"
                .format(GetModel.awcloud_ip()),
                headers=self.headers,
                verify=False)
            for i in re.json()['data']['data']:
                if i['name'] == 'panacube_3.0':
                    self.project_id = i['projectUid']
                    self.project_name = i['name']
        elif resp.json()['code'] == '00010105':
            # docker重启会导致token失效
            manager = AWCloudManage()
            login_data = manager._login()
            if login_data['code'] == 0:
                self.headers = manager.get_auth_header()
                model.update_or_create('get_auth_header',
                                       manager.get_auth_header())
                self.create_project()
            time.sleep(2)
        else:
            self.project_id = resp.json(
            )['data']['data']['project']['projectUid']
            self.project_name = resp.json()['data']['data']['project']['name']
Esempio n. 18
0
    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()