Ejemplo n.º 1
0
        public async Task ListFiles()
        {
            _siteSettings = _publishSettingsService.GetSettingsByPublishMethod(PublishMethods.MSDeploy);


            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(10000);

                var requestUri = $"https://{_siteSettings.ApiUrl}/api/vfs/";

                var request = new HttpRequestMessage(HttpMethod.Get, requestUri);

                request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                              HttpHelpers.GetAuthenticationString(_siteSettings));

                var response = await httpClient.SendAsync(
                    request, HttpCompletionOption.ResponseHeadersRead);

                if (!response.IsSuccessStatusCode)
                {
                    _localLogService.Log($"Error: Count not get files from {requestUri}");
                    return;
                }

                var result = await response.Content.ReadAsStringAsync();

                var r = result;
            }
        }
Ejemplo n.º 2
0
        public static Dictionary <string, string> GetAuthHeadersForGoo(KuduSiteSettings settings)
        {
            var authString = GetAuthenticationString(settings);

            return(new Dictionary <string, string>
            {
                {
                    "Authorization", "Basic " + authString
                }
            });
        }
Ejemplo n.º 3
0
        public async Task SendFile(string fullPath)
        {
            if (!File.Exists(fullPath))
            {
                return;
            }

            byte[] data = null;

            try
            {
                data = File.ReadAllBytes(fullPath);
            }
            catch (Exception ex)
            {
                _localLogService.Log($"Error. Could not read {fullPath}. Probably locked or something like that. {ex.ToString()}");
                return;
            }


            var baseDir = Directory.GetCurrentDirectory();

            var offsetPath = fullPath.Replace(baseDir, "");

            _siteSettings = _publishSettingsService.GetSettingsByPublishMethod(PublishMethods.MSDeploy);

            var offsetForUrl = offsetPath.Replace('\\', '/');

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(10000);

                var requestUri = $"https://{_siteSettings.ApiUrl}/api/vfs/site/wwwroot{offsetForUrl}";

                var request = new HttpRequestMessage(HttpMethod.Put, requestUri);

                request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                              HttpHelpers.GetAuthenticationString(_siteSettings));

                var content = new ByteArrayContent(data);
                request.Content = content;
                request.Headers.Add("If-Match", "*");
                var result = await httpClient.SendAsync(request);

                if (result.IsSuccessStatusCode)
                {
                    _localLogService.Log($"Sent {requestUri}");
                }
                else
                {
                    _localLogService.Log($"Failed {requestUri}, {result.ToString()}");
                }
            }
        }
Ejemplo n.º 4
0
        async Task <string> _getKey(KuduSiteSettings siteSettings, FunctionSettings funcSettings)
        {
            var requestUri = $"{siteSettings.LiveUrl.Replace("http", "https")}/admin/functions/{funcSettings.name}/keys";

            var funcKey = _paramService.Get("key");

            var headers = new Dictionary <string, string>();

            headers.Add("x-functions-key", funcKey);

            var result = await requestUri.GetAndParse <FunctionKey>(headers);

            if (result == null)
            {
                return(null);
            }

            var k = result.keys.FirstOrDefault(_ => _.name == KsConstants.Functions.Default);

            return(k?.value);
        }
Ejemplo n.º 5
0
        async Task <bool> _doRequest(string url, string stringMethod,
                                     List <NameValuePair> headers,
                                     string body, KuduSiteSettings settings)
        {
            _localLogService.LogInfo($" > Calling: {url}");

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(10000);

                var requestUri = url;

                HttpMethod method = default(HttpMethod);

                switch (stringMethod)
                {
                case "get":
                    method = HttpMethod.Get;
                    break;

                case "put":
                    method = HttpMethod.Put;
                    break;

                case "delete":
                    method = HttpMethod.Delete;
                    break;

                case "post":
                    method = HttpMethod.Post;
                    break;

                case "head":
                    method = HttpMethod.Head;
                    break;

                case "options":
                    method = HttpMethod.Options;
                    break;

                case null:
                    method = HttpMethod.Get;
                    break;

                default:
                    method = HttpMethod.Get;
                    break;
                    //note that patch is not supported by Httpmethod!
                }

                var request = new HttpRequestMessage(method, requestUri);

                request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                              HttpHelpers.GetAuthenticationString(settings));

                if (headers != null)
                {
                    foreach (var h in headers)
                    {
                        request.Headers.Add(h.name, h.value);
                    }
                }

                if (body != null)
                {
                    var content = new StringContent(body);
                    request.Content = content;
                }

                var result = await httpClient.SendAsync(request);

                if (!result.IsSuccessStatusCode)
                {
                    _localLogService.LogError($">>> {await result.Content.ReadAsStringAsync()}");
                    _localLogService.LogError($">>> Code: {result.StatusCode}");
                    _localLogService.LogError($">>> Reason: {result.ReasonPhrase}");
                    return(false);
                }
                else
                {
                    _localLogService.Log($">>> {await result.Content.ReadAsStringAsync()}");
                    _localLogService.Log($">>> Code: {result.StatusCode}");
                    return(true);
                }
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> StartLog()
        {
            _localLogService.LogInfo("Starting logstream");
            _siteSettings = _publishSettingsService.GetSettingsByPublishMethod(PublishMethods.MSDeploy);
            StopLog();
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(Timeout.Infinite);

                var requestUri = $"https://{_siteSettings.ApiUrl}/logstream/application";

                var request = new HttpRequestMessage(HttpMethod.Get, requestUri);

                request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                              HttpHelpers.GetAuthenticationString(_siteSettings));

                var response = await httpClient.SendAsync(
                    request, HttpCompletionOption.ResponseHeadersRead);

                if (!response.IsSuccessStatusCode)
                {
                    return(false);
                }

                _currentStream = await response.Content.ReadAsStreamAsync();

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Task.Run(async() =>
                {
                    using (var reader = new StreamReader(_currentStream))
                    {
                        var previous = new List <string>();
                        while (!reader.EndOfStream && _currentStream != null)
                        {
                            //We are ready to read the stream
                            try
                            {
                                var currentLine = await reader.ReadLineAsync();

                                //it doubles up for some reason :/
                                if (previous.Contains(currentLine))
                                {
                                    continue;
                                }

                                previous.Add(currentLine);

                                _localLogService.Log($" > {currentLine}");
                            }
                            catch (Exception ex)
                            {
                                return;
                            }
                        }
                    }
                    StopLog();
                    StartLog();
                });



#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                return(true);
            }
        }
Ejemplo n.º 7
0
 public static string GetAuthenticationString(KuduSiteSettings settings)
 {
     return(Convert.ToBase64String(
                Encoding.UTF8.GetBytes($"{settings.UserName}:{settings.Password}")));
 }
Ejemplo n.º 8
0
        public async Task <bool> GetFiles(string subPath = null)
        {
            _siteSettings = _publishSettingsService.GetSettingsByPublishMethod(PublishMethods.MSDeploy);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMilliseconds(10000);

                var requestUri = $"https://{_siteSettings.ApiUrl}/api/zip/site/wwwroot/";

                if (!string.IsNullOrEmpty(subPath))
                {
                    subPath     = subPath.Trim('/').Trim('\\');
                    requestUri += $"{subPath}/";
                }

                _localLogService.Log($"Grabbing from {requestUri}");

                var request = new HttpRequestMessage(HttpMethod.Get, requestUri);

                request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                              HttpHelpers.GetAuthenticationString(_siteSettings));

                var response = await httpClient.SendAsync(
                    request, HttpCompletionOption.ResponseHeadersRead);

                if (!response.IsSuccessStatusCode)
                {
                    _localLogService.Log($"Error: Count not get files from {requestUri}");
                    return(false);
                }

                var result = await response.Content.ReadAsByteArrayAsync();

                var saveFile = Path.GetTempFileName();

                File.WriteAllBytes(saveFile, result);

                try
                {
                    var dir = Directory.GetCurrentDirectory();
                    if (!string.IsNullOrEmpty(subPath))
                    {
                        dir += $"\\{subPath}";
                    }

                    ZipFile.ExtractToDirectory(saveFile, dir);

                    foreach (var f in Directory.GetFiles(dir, "*", SearchOption.AllDirectories))
                    {
                        if (_fileWatcherService.Validate(f))
                        {
                            _localLogService.Log($" - {f.Replace(dir, "")}");
                        }
                    }
                }
                catch (System.IO.IOException ex)
                {
                    _localLogService.LogWarning(
                        "k-scratch will not overwrite existing files. Please 'get' in to an empty directory.");
                    return(false);
                }
                finally
                {
                    File.Delete(saveFile);
                }

                return(true);
            }
        }
Ejemplo n.º 9
0
        public async Task <bool> UploadFiles(string subPath = null)
        {
            _siteSettings = _publishSettingsService.GetSettingsByPublishMethod(PublishMethods.MSDeploy);

            var dir = Directory.GetCurrentDirectory();

            if (!string.IsNullOrEmpty(subPath))
            {
                dir += $"\\{subPath}";
            }

            var tmpFile = Path.GetTempFileName();

            if (File.Exists(tmpFile))
            {
                File.Delete(tmpFile);
            }
            ZipFile.CreateFromDirectory(dir, tmpFile);

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = TimeSpan.FromMinutes(10);

                var requestUri = $"https://{_siteSettings.ApiUrl}/api/zip/site/wwwroot/";

                if (!string.IsNullOrEmpty(subPath))
                {
                    subPath     = subPath.Trim('/').Trim('\\');
                    requestUri += $"{subPath}/";
                }

                var request = new HttpRequestMessage(HttpMethod.Put, requestUri);

                try
                {
                    using (var stream = File.OpenRead(tmpFile))
                    {
                        request.Content = new StreamContent(File.OpenRead(tmpFile));

                        request.Headers.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                      HttpHelpers.GetAuthenticationString(_siteSettings));

                        var response = await httpClient.SendAsync(request);

                        if (!response.IsSuccessStatusCode)
                        {
                            _localLogService.Log($"Error: Could not upload files files to {requestUri} - {response.ReasonPhrase}");
                            return(false);
                        }

                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    _localLogService.Log($"Problem uploadin files: {ex.Message}");
                }
                finally
                {
                    File.Delete(tmpFile);
                }

                return(false);
            }
        }