Created
January 20, 2021 13:46
-
-
Save ihordyrman/88a4eee0f33101374542348ed93f2dff to your computer and use it in GitHub Desktop.
Azure Functions Orchestration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using CoreFtp; | |
using Microsoft.Azure.Management.AppService.Fluent; | |
using Microsoft.Azure.Management.Fluent; | |
using Microsoft.Azure.Management.ResourceManager.Fluent; | |
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication; | |
using Microsoft.Azure.Management.ResourceManager.Fluent.Core; | |
using Newtonsoft.Json; | |
namespace FunctionPublisher | |
{ | |
internal static class Program | |
{ | |
private static async Task Main(string[] args) | |
{ | |
AzureCredentials credentials = SdkContext.AzureCredentialsFactory | |
.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); | |
IAzure azure = Azure | |
.Configure() | |
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) | |
.Authenticate(credentials) | |
.WithSubscription("subscription-id"); | |
// .WithDefaultSubscriptionAsync(); | |
IFunctionApp app = azure.AppServices.FunctionApps.Define("app-name") | |
.WithRegion(Region.EuropeWest) | |
.WithNewResourceGroup("resource-group") | |
.DefineDiagnosticLogsConfiguration() | |
.WithApplicationLogging() | |
.WithLogLevel(Microsoft.Azure.Management.AppService.Fluent.Models.LogLevel.Information) | |
.WithApplicationLogsStoredOnStorageBlob("bloblurl") | |
.Attach() | |
.Create(); | |
IPublishingProfile profile = await app.GetPublishingProfileAsync(); | |
// Create appSettings.json | |
var settings = new Settings | |
{ | |
ConnectionString = "this is connection string", | |
ContainerName = "this is container name" | |
}; | |
CreateJsonFile(settings); | |
// Upload files to the function | |
UploadFilesToFunction(profile, "path to Function.cs"); | |
UploadFilesToFunction(profile, "path to appSettings.json"); | |
// Remove appsettings.json | |
File.Delete("appSettings.json"); | |
await app.SyncTriggersAsync(); | |
} | |
private static void CreateJsonFile(Settings settings) | |
{ | |
using StreamWriter file = File.CreateText(@"appSettings.json"); | |
var serializer = new JsonSerializer(); | |
serializer.Serialize(file, settings); | |
} | |
private static void UploadFilesToFunction(IPublishingProfile profile, string filePath) | |
{ | |
string host = profile.FtpUrl.Split(new[] { '/' }, 2)[0]; | |
using var ftpClient = new FtpClient(new FtpClientConfiguration | |
{ | |
Host = host, | |
Username = profile.FtpUsername, | |
Password = profile.FtpPassword | |
}); | |
var fileInfo = new FileInfo(filePath); | |
ftpClient.LoginAsync().GetAwaiter().GetResult(); | |
if (ftpClient.ListDirectoriesAsync().GetAwaiter().GetResult().Any(fni => fni.Name != "site")) | |
ftpClient.CreateDirectoryAsync("site").GetAwaiter().GetResult(); | |
ftpClient.ChangeWorkingDirectoryAsync("./site").GetAwaiter().GetResult(); | |
if (ftpClient.ListDirectoriesAsync().GetAwaiter().GetResult().All(fni => fni.Name != "wwwroot")) | |
ftpClient.CreateDirectoryAsync("wwwroot").GetAwaiter().GetResult(); | |
ftpClient.ChangeWorkingDirectoryAsync("./wwwroot").GetAwaiter().GetResult();; | |
string fileName = Path.GetFileName(filePath); | |
while (fileName.Contains("/")) | |
{ | |
int slash = fileName.IndexOf("/"); | |
string subDir = fileName.Substring(0, slash); | |
ftpClient.CreateDirectoryAsync(subDir).GetAwaiter().GetResult(); | |
ftpClient.ChangeWorkingDirectoryAsync("./" + subDir); | |
fileName = fileName.Substring(slash + 1); | |
} | |
using var writeStream = ftpClient.OpenFileWriteStreamAsync(fileName).GetAwaiter().GetResult(); | |
var fileReadStream = fileInfo.OpenRead(); | |
fileReadStream.CopyToAsync(writeStream).GetAwaiter().GetResult(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment