[ 
https://issues.apache.org/jira/browse/CLOUDSTACK-10290?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16480577#comment-16480577
 ] 

ASF GitHub Bot commented on CLOUDSTACK-10290:
---------------------------------------------

nvazquez 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_r189249271
 
 

 ##########
 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) {
+                String dataType = item[CONFIGDATA_DIR];
+                String fileName = item[CONFIGDATA_FILE];
+                String content = item[CONFIGDATA_CONTENT];
+                LOG.debug(String.format("[createConfigDriveIsoForVM] 
dataType=%s, filename=%s, content=%s",
+                        dataType, fileName, 
(fileName.equals(PASSWORD_FILE)?"********":content)));
+
+                // create file with content in folder
+                if (dataType != null && !dataType.isEmpty()) {
+                    //create folder
+                    File typeFolder = new File(tempDirName + 
ConfigDrive.cloudStackConfigDriveName + dataType);
+                    if (typeFolder.exists() || typeFolder.mkdirs()) {
+                        if (StringUtils.isNotEmpty(content)) {
+                            File file = new File(typeFolder, fileName + 
".txt");
+                            try  {
+                                if (fileName.equals(USERDATA_FILE)) {
+                                    // User Data is passed as a base64 encoded 
string
+                                    FileUtils.writeByteArrayToFile(file, 
Base64.decodeBase64(content));
+                                } else {
+                                    FileUtils.write(file, content, 
com.cloud.utils.StringUtils.getPreferredCharset());
+                                }
+                            } catch (IOException ex) {
+                                throw new CloudRuntimeException("Failed to 
create file ", ex);
+                            }
+                        }
+                    } else {
+                        throw new CloudRuntimeException("Failed to create 
folder: " + typeFolder);
+                    }
+
+                    //now write the file to the OpenStack directory
+                    metaData = buildOpenStackMetaData(metaData, dataType, 
fileName, content);
+                }
+            }
+            writeFile(openStackFolder, "meta_data.json", metaData.toString());
+
+            String linkResult = linkUserData(tempDirName);
+            if (linkResult != null) {
+                String errMsg = "Unable to create user_data link due to " + 
linkResult;
+                throw new CloudRuntimeException(errMsg);
+            }
+
+            File tmpIsoStore = new File(tempDirName, new 
File(isoFileName).getName());
+            Script command = new Script("/usr/bin/genisoimage", 
Duration.standardSeconds(300), LOG);
 
 Review comment:
   Can this also be extracted into a method?

----------------------------------------------------------------
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:
us...@infra.apache.org


> Config drive - only supported for secondary storage
> ---------------------------------------------------
>
>                 Key: CLOUDSTACK-10290
>                 URL: https://issues.apache.org/jira/browse/CLOUDSTACK-10290
>             Project: CloudStack
>          Issue Type: Bug
>      Security Level: Public(Anyone can view this level - this is the 
> default.) 
>    Affects Versions: 4.11.0.0
>            Reporter: Rohit Yadav
>            Assignee: Daan Hoogland
>            Priority: Major
>
> Userdata disk looks like this:
>  <disk type='file' device='cdrom'>
>        <driver name='qemu' type='raw' cache='none'/>
>        <source 
> file='/mnt/eba12ff3-c3a6-394a-bf0f-23291f1f6266/configdrive.iso'/>
>        <backingStore/>
>        <target dev='hdd' bus='ide'/>
>        <readonly/>
>        <alias name='ide0-1-1'/>
>        <address type='drive' controller='0' bus='1' target='0' unit='1'/>
>      </disk>
> Mount is:
> root# df /mnt/eba12ff3-c3a6-394a-bf0f-23291f1f6266
>  Filesystem                                                       1K-blocks   
>  Used Available Use% Mounted on
>  some-nfs-server.com:/nfs/secondary/ConfigDrive/i-2-24-VM  66391040 2973696  
> 63417344   5% /mnt/eba12ff3-c3a6-394a-bf0f-23291f1f6266
>  
> issue: where to find a primary storage for a VM that is not yet deployed. The 
> configdrive is created before a storage is assigned. This order of execution 
> must be reversed for this to work.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to