|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Intel Corporation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of the Intel Corporation nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * |
| 28 | + */ |
| 29 | + |
| 30 | +package org.sofproject.gst.topo.ops; |
| 31 | + |
| 32 | +import java.io.BufferedReader; |
| 33 | +import java.io.FileReader; |
| 34 | +import java.io.IOException; |
| 35 | +import java.lang.reflect.InvocationTargetException; |
| 36 | +import java.nio.file.FileSystem; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.nio.file.Paths; |
| 39 | + |
| 40 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 41 | +import org.sofproject.core.AudioDevNodeProject; |
| 42 | +import org.sofproject.core.connection.AudioDevNodeConnection; |
| 43 | +import org.sofproject.core.ops.SimpleRemoteOp; |
| 44 | +import org.sofproject.topo.ui.json.JsonProperty; |
| 45 | +import org.sofproject.topo.ui.json.JsonUtils; |
| 46 | + |
| 47 | +import com.jcraft.jsch.ChannelExec; |
| 48 | +import com.jcraft.jsch.JSchException; |
| 49 | +import com.jcraft.jsch.Session; |
| 50 | + |
| 51 | +public class GstDockerOperation extends SimpleRemoteOp { |
| 52 | + |
| 53 | + private JsonUtils jsonUtils; |
| 54 | + |
| 55 | + public GstDockerOperation(AudioDevNodeConnection conn, JsonUtils jsonUtils) { |
| 56 | + super(conn); |
| 57 | + this.jsonUtils = jsonUtils; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean isCancelable() { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
| 67 | + monitor.beginTask("Sending json to docker", 100); |
| 68 | + |
| 69 | + try { |
| 70 | + if (!conn.isConnected()) { |
| 71 | + throw new InvocationTargetException(new IllegalStateException("Node not connected")); |
| 72 | + } |
| 73 | + |
| 74 | + JsonProperty jsonProperty = jsonUtils.getJsonProperty(); |
| 75 | + |
| 76 | + AudioDevNodeProject proj = conn.getProject(); |
| 77 | + Session session = conn.getSession(); |
| 78 | + ChannelExec channel = (ChannelExec) session.openChannel("exec"); |
| 79 | + channel.setInputStream(null); |
| 80 | + |
| 81 | + String jsonFileName = "pipeline.json"; |
| 82 | + String projectPath = proj.getProject().getLocation().toString(); |
| 83 | + Path partPathToJson = Paths.get(jsonProperty.getType().toLowerCase(), jsonProperty.getName(), jsonProperty.getVersion()); |
| 84 | + Path fullPathToJson = Paths.get(projectPath, partPathToJson.toString(), jsonFileName); |
| 85 | + String linuxPartPathToJson = partPathToJson.toString(); |
| 86 | + linuxPartPathToJson = linuxPartPathToJson.replace("\\", "/"); |
| 87 | + |
| 88 | + BufferedReader reader = new BufferedReader(new FileReader(fullPathToJson.toString())); |
| 89 | + String currentLine = reader.readLine(); |
| 90 | + reader.close(); |
| 91 | + |
| 92 | + channel.setPty(true); // for ctrl+c sending |
| 93 | + String command = String.format( |
| 94 | + "docker exec -t video_analytics_serving_gstreamer /bin/bash -c \"cd pipelines; mkdir -p %s; cd %s; touch %s; echo \'%s\' > %s\"", |
| 95 | + linuxPartPathToJson, linuxPartPathToJson, jsonFileName, currentLine.replace("\"", "\\\""), jsonFileName); |
| 96 | + channel.setCommand(command); |
| 97 | + |
| 98 | + channel.connect(); |
| 99 | + |
| 100 | + while (!channel.isEOF()) |
| 101 | + ; |
| 102 | + channel.disconnect(); |
| 103 | + monitor.beginTask("Sending json to docker", 1000); |
| 104 | + |
| 105 | + } catch (JSchException | IOException e) { |
| 106 | + throw new InvocationTargetException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments