Пример #1
0
    def user_data_fixup(self, user_data):
        """
        The original user-data used for creating the master can become obsolete after upgrades. There are 5
        fields from the original user-data that need to be "fixed". The SERVER_BINARY_TAR_URL, SALT_TAR_URL,
        SERVER_BINARY_TAR_HASH, SALT_TAR_HASH and the wget command that downloads the bootstrap-script.
        """
        from ax.platform.kube_env_config import kube_env_update
        # TODO: It's not ideal to use env variables for passing arguments.
        # Env variables could be different between running as server and from upgrade.
        kube_version = os.getenv('KUBE_VERSION', os.getenv('NEW_KUBE_VERSION')).strip()
        cluster_install_version = os.getenv('AX_CLUSTER_INSTALL_VERSION', os.getenv('NEW_CLUSTER_INSTALL_VERSION')).strip()
        server_binary_tar_hash = os.getenv('SERVER_BINARY_TAR_HASH', os.getenv('NEW_KUBE_SERVER_SHA1')).strip()
        salt_tar_hash = os.getenv('SALT_TAR_HASH', os.getenv('NEW_KUBE_SALT_SHA1')).strip()
        updates = {
            "new_kube_version": kube_version,
            "new_cluster_install_version": cluster_install_version,
            "new_kube_server_hash": server_binary_tar_hash,
            "new_kube_salt_hash": salt_tar_hash,
            "new_api_servers": self.attributes['private_ip_address'],
        }
        dec = zlib.decompressobj(32 + zlib.MAX_WBITS)  # offset 32 to skip the header
        unzipped_user_data = dec.decompress(base64.b64decode(user_data))

        # Zip output buffer. For details: http://bit.ly/2gv3WKt
        comp = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
        zipped_data = comp.compress(kube_env_update(unzipped_user_data, updates)) + comp.flush()

        # Convert output to base64 encoded
        logger.info("User data fixup completed")
        return base64.b64encode(zipped_data)
Пример #2
0
    def _update_launch_config(self,
                              old_name,
                              new_name,
                              retain_spot_price=False):
        """
        Upgrade old_name launch config to new_name.
        Return OK if new launch config is already created.
        Raise error if both old and new don't exist.
        """
        logger.info("Converting launch config %s to %s ...", old_name,
                    new_name)
        ami_name = os.getenv("AX_AWS_IMAGE_NAME")
        assert ami_name, "Fail to detect AMI name from environ"
        ami_id = AMI(
            aws_region=self._region,
            aws_profile=self._profile).get_ami_id_from_name(ami_name=ami_name)
        logger.info("Using ami %s for new minion launch configuration", ami_id)

        cluster_config = AXClusterConfig(cluster_name_id=self._cluster_name_id,
                                         aws_profile=self._profile)
        if LaunchConfig(new_name,
                        aws_profile=self._profile,
                        aws_region=self._region).get() is not None:
            # Launch config already updated, nop.
            logger.debug("New launch config %s already there. No creation.",
                         new_name)
            return

        lc = LaunchConfig(old_name,
                          aws_profile=self._profile,
                          aws_region=self._region)
        config = lc.get()
        assert config is not None, "Empty old launch config and new launch config"
        user_data = config.pop("UserData")
        logger.debug("Existing launch config %s: %s", old_name, config)

        updates = {
            "new_kube_version": self._new_kube_version,
            "new_cluster_install_version": self._new_cluster_install_version,
            "new_kube_server_hash": self._new_kube_server_hash,
            "new_kube_salt_hash": self._new_kube_salt_hash,
        }

        # Replace ImageId and everything listed in default_kube_up_env.
        config["ImageId"] = ami_id
        config["IamInstanceProfile"] = AXClusterInstanceProfile(
            self._cluster_name_id,
            aws_profile=self._profile).get_minion_instance_profile_name()
        user_data = zlib.decompressobj(32 +
                                       zlib.MAX_WBITS).decompress(user_data)
        user_data = kube_env_update(user_data, updates)
        comp = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
        config["UserData"] = comp.compress(user_data) + comp.flush()

        # Add AX Volume device mappings.
        orig_block_devices = config.pop("BlockDeviceMappings")

        block_devices = []
        for device in orig_block_devices:
            if device["DeviceName"] != AX_VOL_DISK:
                block_devices.append(device)
        vol_device = {}
        vol_device["DeviceName"] = AX_VOL_DISK
        ebs_dict = {}
        ebs_dict["DeleteOnTermination"] = True
        ebs_dict["VolumeSize"] = cluster_config.get_ax_vol_size()
        ebs_dict["VolumeType"] = self._ax_vol_disk_type

        vol_device["Ebs"] = ebs_dict
        block_devices.append(vol_device)
        config["BlockDeviceMappings"] = block_devices
        logger.debug("New block device mappings: %s",
                     config["BlockDeviceMappings"])

        lc.copy(new_name, config, retain_spot_price=retain_spot_price)
        logger.info("Converting launch config %s to %s ... DONE.", old_name,
                    new_name)