You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2018/05/18 12:16:08 UTC

[GitHub] rhtyd commented on a change in pull request #2651: CLOUDSTACK-10290: Introduce option to have config drives on primary storage

rhtyd commented on a change in pull request #2651: CLOUDSTACK-10290: Introduce option to have config drives on primary storage
URL: https://github.com/apache/cloudstack/pull/2651#discussion_r189249413
 
 

 ##########
 File path: engine/storage/configdrive/src/org/apache/cloudstack/storage/configdrive/ConfigDriveBuilder.java
 ##########
 @@ -0,0 +1,222 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.cloudstack.storage.configdrive;
+
+import static com.cloud.network.NetworkModel.CONFIGDATA_CONTENT;
+import static com.cloud.network.NetworkModel.CONFIGDATA_DIR;
+import static com.cloud.network.NetworkModel.CONFIGDATA_FILE;
+import static com.cloud.network.NetworkModel.PASSWORD_FILE;
+import static com.cloud.network.NetworkModel.USERDATA_FILE;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+import org.joda.time.Duration;
+
+import com.cloud.network.NetworkModel;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.script.Script;
+import com.google.common.base.Strings;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+public class ConfigDriveBuilder {
+
+    public static final Logger LOG = Logger.getLogger(ConfigDriveBuilder.class);
+
+    private static void writeFile(final File folder, final String file, final String content) {
+        if (folder == null || Strings.isNullOrEmpty(file)) {
+            return;
+        }
+        final File vendorDataFile = new File(folder, file);
+        try (final FileWriter fw = new FileWriter(vendorDataFile); final BufferedWriter bw = new BufferedWriter(fw)) {
+            bw.write(content);
+        } catch (IOException ex) {
+            throw new CloudRuntimeException("Failed to create config drive file " + file, ex);
+        }
+    }
+
+    public static String fileToBase64String(final File isoFile) throws IOException {
+        byte[] encoded = Base64.encodeBase64(FileUtils.readFileToByteArray(isoFile));
+        return new String(encoded, StandardCharsets.US_ASCII);
+    }
+
+    public static File base64StringToFile(final String encodedIsoData, final String folder, final String fileName) throws IOException {
+        byte[] decoded = Base64.decodeBase64(encodedIsoData.getBytes(StandardCharsets.US_ASCII));
+        Path destPath = Paths.get(folder, fileName);
+        return Files.write(destPath, decoded).toFile();
+    }
+
+    public static String buildConfigDrive(final List<String[]> vmData, final String isoFileName, final String driveLabel) {
+        if (vmData == null) {
+            throw new CloudRuntimeException("No VM metadata provided");
+        }
+
+        Path tempDir = null;
+        String tempDirName = null;
+        try {
+            tempDir = Files.createTempDirectory(ConfigDrive.CONFIGDRIVEDIR);
+            tempDirName = tempDir.toString();
+
+            File openStackFolder = new File(tempDirName + ConfigDrive.openStackConfigDriveName);
+            if (openStackFolder.exists() || openStackFolder.mkdirs()) {
+                writeFile(openStackFolder, "vendor_data.json", "{}");
+                writeFile(openStackFolder, "network_data.json", "{}");
+            } else {
+                throw new CloudRuntimeException("Failed to create folder " + openStackFolder);
+            }
+
+            JsonObject metaData = new JsonObject();
+            for (String[] item : vmData) {
 
 Review comment:
   To limit my effort scope, code in this class were previously in Nfs resource class. Most of the changes in this class are from a move-refactor. I'll further refactor if I've time, so yes I can try.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services