Esempio n. 1
0
        public async Task RequestToDownloadContainer(RequestDownloadContainerInfo info)
        {
            ServerResponse response = new ServerResponse()
            {
                payload = new Dictionary <string, string>()
            };

            response.payload.Add("Status", "request received.");
            await SendResponse(response);

            _packageService = _serviceAccessor("ContainerService");

            string resourceName = await _packageService.DownloadPackageAsync(Context.ConnectionId, info);

            string downloadUrl = $"./download/file?name={resourceName}";

            response.payload.Clear();
            response.payload.Add("Status", "download completed.");
            response.payload.Add("DownloadUrl", downloadUrl);
            await SendResponse(response);
        }
Esempio n. 2
0
        public async Task <string> DownloadPackageAsync(string connectionID, RequestDownloadInfo requestInfo)
        {
            // cast
            RequestDownloadContainerInfo info = (RequestDownloadContainerInfo)requestInfo;

            // server response
            ServerResponse response = new ServerResponse()
            {
                payload = new Dictionary <string, string>()
            };

            string _outputDirectory = $"{_environment.ContentRootPath}/wwwroot/{_configuration.GetValue<string>("DownloadPath")}";

            // check if output directory exists
            if (!Directory.Exists(_outputDirectory))
            {
                Directory.CreateDirectory(_outputDirectory);
            }

            string fileName = info.image.Replace("/", "-").Replace(":", "-");

            string connectionSubName = $"container-{fileName}-{connectionID}-{DateTime.Now:yyyymmddHHmmss}";

            // send message
            response.payload.Clear();
            response.payload.Add("Resource", $"{connectionSubName} created.");
            await _downloadHubContext.Clients.Client(connectionID).Response(response);

            // docker pull
            Process compiler = new Process();

            compiler.StartInfo.FileName               = "docker";
            compiler.StartInfo.Arguments              = $"pull {info.image}";
            compiler.StartInfo.UseShellExecute        = false;
            compiler.StartInfo.RedirectStandardOutput = true;
            compiler.OutputDataReceived              += async(sender, args) => await sendMessageAsync(connectionID, args.Data);

            compiler.Start();
            compiler.BeginOutputReadLine();
            compiler.WaitForExit();

            // send message
            response.payload.Clear();
            response.payload.Add("DownloadCounter", $"download finished.");
            await _downloadHubContext.Clients.Client(connectionID).Response(response);

            // docker save
            Process compiler2 = new Process();

            compiler2.StartInfo.FileName               = "docker";
            compiler2.StartInfo.Arguments              = $"save -o {_outputDirectory}/{connectionSubName}.zip {info.image}";
            compiler2.StartInfo.UseShellExecute        = false;
            compiler2.StartInfo.RedirectStandardOutput = true;
            compiler2.OutputDataReceived              += (sender, args) => Console.WriteLine("received output: {0}", args.Data);
            compiler2.Start();
            compiler2.BeginOutputReadLine();
            compiler2.WaitForExit();

            string readableSize = FileUtil.getFileHumanReadableSize($"{_outputDirectory}/{connectionSubName}.zip");

            // send message
            response.payload.Clear();
            response.payload.Add("CompressStatus", $"compressed ok, file sieze: {readableSize}.");
            await _downloadHubContext.Clients.Client(connectionID).Response(response);

            // delete image
            Process compiler3 = new Process();

            compiler3.StartInfo.FileName        = "docker";
            compiler3.StartInfo.Arguments       = $"rmi {info.image}";
            compiler3.StartInfo.UseShellExecute = false;
            compiler3.Start();
            compiler3.WaitForExit();

            return(connectionSubName);
        }