Exemple #1
0
def wait_result(oj):
    """ 等待安装, 并获取结果.

    """
    # 如果机器在资产系统中, 删掉机器(post 阶段脚本会向资产系统申请
    # hostname 和 ip, 并且进行初始化机器, 初始化之后  status 是
    # creating).
    # 放在 reboot 之后的原因是防止删除之后 agent 继续上报.
    if asset_utils.is_exist_for_sn(oj.sn):
        asset_utils.delete_from_sn(oj.sn)

    # 安装信息进 redis.
    client.hset(oj.sn, "idc", json.dumps(oj.idc))
    client.hset(oj.sn, "usage", json.dumps(oj.usage))

    client.hset(oj.sn, "hostname", json.dumps(""))
    client.hset(oj.sn, "ip", json.dumps(""))

    # 循环等待安装完成.
    timeout = 1500
    interval = 15
    timetotal = 0

    installed = False
    in_asset = False

    while timetotal < timeout:
        if not installed:
            hostname = json.loads(client.hget(oj.sn, "hostname"))
            ip = json.loads(client.hget(oj.sn, "ip"))

            if "" in [hostname, ip]:
                time.sleep(interval)
                timetotal += interval
            else:
                installed = True
                # 设置 hostname, ip
                oj.hostname = hostname
                oj.ip = ip

                # 清除 puppet 证书.
                puppet.check_puppet_cert(oj.hostname)
                # 设置 user_data, 装机之后机器会获取并执行 user_data 中的内容.
                user_data.set_user_data(oj.hostname, oj.user_data)
                # 设置 node id, 配置机器会使用.
                node.set_node_id(oj.hostname, oj.node_id)

                # 增加 DNS.
                if dns_utils.record_exist(oj.hostname):
                    dns_utils.record_delete(oj.hostname)
                dns_utils.record_add(oj.hostname, oj.ip)
        elif installed and not in_asset:
            try:
                status = asset_utils.get_value_from_sn(oj.sn, "status")
                if status == "online":
                    in_asset = True
                    break
            except Exception, e:
                pass
            finally:
Exemple #2
0
def one(data):
    """ 单台机器安装.

    """
    idc = data["idc"]
    sn = data["sn"]
    _type = data["type"]
    version = data["version"]
    usage = data["usage"]
    user_data = data["user_data"]

    # 如果机器在资产系统中, 删掉机器(post 阶段脚本会向资产系统申请
    # hostname 和 ip, 并且进行初始化机器, 初始化之后  status 是
    # creating).
    # 有一个问题:
    # 此种安装方式是手动安装, 为了防止删除之后 agent 继续上报,
    # 应该在安装前手动把机器关机.
    if asset_utils.is_exist_for_sn(sn):
        asset_utils.delete_from_sn(sn)

    # 安装系统进入 redis.
    client.hset(sn, "idc", json.dumps(idc))
    client.hset(sn, "usage", json.dumps(usage))

    client.hset(sn, "hostname", json.dumps(""))
    client.hset(sn, "ip", json.dumps(""))

    # 设置 user_data, 装机之后机器会获取并执行 user_data 中的内容.
    if user_data is None:
        client_user_data.exists(sn) and client_user_data.delete(sn)
    else:
        client_user_data.set(sn, json.dumps(user_data))

    # 循环等待安装完成.
    installed, in_asset, hostname, ip = utils.wait(sn)

    # 检查安装完成情况.
    if not installed:
        raise Exception("install timeout")
    elif installed and not in_asset:
        raise Exception("install success,but not uploaded to asset sys")
    else:
        # 检查 hostname 是否已经成功添加 DNS.
        recordred = dns_utils.record_exist(hostname)
        if not recordred:
            raise Exception("dns add fail")

    data["hostname"] = hostname
    data["ip"] = ip
    return data
Exemple #3
0
    def get_unique_data(self):
        """ 获取每个装机任务需要的信息.

        """
        self.unique_data = list()
        for vmh in self.vmhs:
            # 拷贝一份 common data.
            data = copy.deepcopy(self.common_data)

            # 保存宿主机.
            data["vmh"] = vmh

            # 保存 rpc_client.
            data["rpc_client"] = utils.get_rpc_client(data["vmh"])

            # 更新宿主机资源.
            for item in ("vcpu", "mem", "space"):
                x = utils.get_info(data["vmh"], item)
                if item == "space":
                    x["free"] = x["free"] - self.os_size - self.data_size
                else:
                    x["free"] = x["free"] - getattr(self, item)
                utils.update_info(data["vmh"], item, x)

            # 获取 vmname 并更新 vmname.
            data["vmname"] = utils.get_vmname(data["vmh"])
            utils.update_vmname(data["vmh"], data["vmname"])

            # 生成虚拟机的 uuid, 并且当做虚拟机的 sn.
            data["sn"] = data["uuid"] = utils_common.random_uuid()

            # 获取 hostname, ip, network 和 gateway 信息.
            # 获取 hostname, ip 成功后, 会自动在资产系统中创建记录, status 为
            # creating, 等待 agent 上报资产后, 变成 online.
            network = utils.get_network(data["vmh"])
            hostname, ip = asset_utils.apply_hostname_ip(
                data["sn"], "vm", data["usage"], data["idc"], network)
            gateway = utils.get_gateway_from_ip(ip)
            data["hostname"] = hostname
            data["ip"] = ip
            data["network"] = network
            data["gateway"] = gateway

            # 清除 puppet 证书.
            for h in [hostname, hostname + ".nosa.me"]:
                if utils.get_puppet_cert(h) == 200:
                    if utils.delete_puppet_cert(h) == 200:
                        self.logger.info(
                            "{hostname} cert deleted".format(hostname=h))
                    else:
                        self.logger.info(
                            "{hostname} cert delete failed".format(hostname=h))

            # 如果 dns 里面已经有 hostname 解析, 删除.
            if dns_utils.record_exist(data["hostname"]):
                dns_utils.record_delete(data["hostname"])

            # 不同的 type 要有不同的处理.
            if self.type == "wmi":
                # 当 type 等于 wmi 时, agent 会修改镜像的 ip 和 hostname.
                pass
            else:
                # 如果不是 wmi, 装机 post 脚本会来请求拿到 hostname.
                self.client.set(data["ip"], data["hostname"])

            # 记录 hostname 和 ip.
            self.logger.info("hostname:{hostname}, ip:{ip}".format(
                hostname=data["hostname"], ip=data["ip"]))

            # 设置 user data, 机器装好之后会取数据.
            self.set_user_data(data["hostname"])

            # 保存 data.
            self.unique_data.append(data)
Exemple #4
0
            return data

        if not utils.wait_status(data["sn"]):
            error_message = "not uploaded to asset sys"
            self.logger.warning(error_message)

            # 装失败了, 在资产系统中删除.
            if asset_utils.is_exist_for_sn(data["sn"]):
                asset_utils.delete_from_sn(data["sn"])

            data["code"] = 1
            data["error_message"] = error_message
            utils.update_info_from_rpc(data["vmh"])
            return data

        if not dns_utils.record_exist(data["hostname"]):
            error_message = "dns record not added"
            self.logger.warning(error_message)

            # 装失败了, 在资产系统中删除.
            if asset_utils.is_exist_for_sn(data["sn"]):
                asset_utils.delete_from_sn(data["sn"])

            data["code"] = 1
            data["error_message"] = error_message
            utils.update_info_from_rpc(data["vmh"])
            return data

        data["code"] = 0
        data["error_message"] = None
        utils.update_info_from_rpc(data["vmh"])
Exemple #5
0
    client.hset(sn, "hostname", json.dumps(""))
    client.hset(sn, "ip", json.dumps(""))

    # 设置 user_data, 装机之后机器会获取并执行 user_data 中的内容.
    if user_data is None:
        client_user_data.exists(sn) and client_user_data.delete(sn)
    else:
        client_user_data.set(sn, json.dumps(user_data))

    # 循环等待安装完成.
    installed, in_asset, hostname, ip = utils.wait(sn)

    # 删除 pxelinux 配置文件.
    ilo_oj.del_tftp(mac)

    # 检查安装完成情况.
    if not installed:
        raise Exception("install timeout")
    elif installed and not in_asset:
        raise Exception("install success,but not uploaded to asset sys")
    else:
        # 检查 hostname 是否已经成功添加 DNS.
        recordred = dns_utils.record_exist(hostname)
        if not recordred:
            raise Exception("dns add fail")

    data["hostname"] = hostname
    data["ip"] = ip
    return data