|
37 | 37 | import io.functionmesh.compute.models.MeshWorkerServiceCustomConfig; |
38 | 38 | import io.kubernetes.client.custom.Quantity; |
39 | 39 | import lombok.extern.slf4j.Slf4j; |
| 40 | +import org.apache.commons.io.FileUtils; |
40 | 41 | import org.apache.commons.lang3.RandomStringUtils; |
41 | 42 | import org.apache.commons.lang3.StringUtils; |
42 | 43 | import org.apache.logging.log4j.util.Strings; |
| 44 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
43 | 45 | import org.apache.pulsar.client.admin.PulsarAdminException; |
44 | 46 | import org.apache.pulsar.common.functions.ConsumerConfig; |
45 | 47 | import org.apache.pulsar.common.functions.FunctionConfig; |
|
54 | 56 | import org.apache.pulsar.functions.proto.InstanceCommunication; |
55 | 57 | import org.apache.pulsar.functions.utils.FunctionCommon; |
56 | 58 | import org.apache.pulsar.functions.utils.FunctionConfigUtils; |
| 59 | +import org.apache.pulsar.packages.management.core.common.PackageMetadata; |
| 60 | +import org.glassfish.jersey.media.multipart.FormDataContentDisposition; |
57 | 61 |
|
58 | 62 | import javax.ws.rs.core.Response; |
59 | 63 | import java.io.File; |
60 | 64 | import java.io.IOException; |
| 65 | +import java.io.InputStream; |
61 | 66 | import java.nio.file.Files; |
62 | 67 | import java.nio.file.Path; |
63 | 68 | import java.nio.file.Paths; |
@@ -650,4 +655,44 @@ private static Class<?>[] extractTypeArgs(final FunctionConfig functionConfig, |
650 | 655 | return typeArgs; |
651 | 656 | } |
652 | 657 |
|
| 658 | + private static String generatePackageURL(final String tenant, |
| 659 | + final String namespace, |
| 660 | + final String functionName) { |
| 661 | + return String.format("function://%s/%s/%s", tenant, namespace, functionName); |
| 662 | + } |
| 663 | + |
| 664 | + public static String uploadPackageToPackageService(PulsarAdmin admin, |
| 665 | + final String tenant, |
| 666 | + final String namespace, |
| 667 | + final String functionName, |
| 668 | + final InputStream uploadedInputStream, |
| 669 | + final FormDataContentDisposition fileDetail, |
| 670 | + String tempDirectory) throws Exception { |
| 671 | + Path tempDirectoryPath = Paths.get(tempDirectory); |
| 672 | + if (Files.notExists(tempDirectoryPath)) { |
| 673 | + Files.createDirectories(tempDirectoryPath); |
| 674 | + } |
| 675 | + Path filePath = Files.createTempFile(tempDirectoryPath, |
| 676 | + RandomStringUtils.random(5, true, true).toLowerCase(), fileDetail.getFileName()); |
| 677 | + FileUtils.copyInputStreamToFile(uploadedInputStream, filePath.toFile()); |
| 678 | + uploadedInputStream.close(); |
| 679 | + |
| 680 | + PackageMetadata packageMetadata = new PackageMetadata(); |
| 681 | + String packageName = generatePackageURL(tenant, namespace, functionName); |
| 682 | + packageMetadata.setContact("mesh-worker-service"); |
| 683 | + packageMetadata.setDescription("mesh-worker-service created for " + packageName); |
| 684 | + Map<String, String> properties = new HashMap<>(); |
| 685 | + properties.put("tenant", tenant); |
| 686 | + properties.put("namespace", namespace); |
| 687 | + properties.put("functionName", functionName); |
| 688 | + properties.put("fileName", fileDetail.getFileName()); |
| 689 | + properties.put("size", Long.toString(filePath.toFile().length())); |
| 690 | + long checksum = FileUtils.checksumCRC32(filePath.toFile()); |
| 691 | + properties.put("checksum", Long.toString(checksum)); |
| 692 | + packageMetadata.setProperties(properties); |
| 693 | + admin.packages().upload(packageMetadata, packageName, filePath.toString()); |
| 694 | + log.info("upload file {} to package service {} successfully", filePath, packageName); |
| 695 | + Files.deleteIfExists(filePath); |
| 696 | + return packageName; |
| 697 | + } |
653 | 698 | } |
0 commit comments