From fa45045216520bf8d63df812673455312d146d10 Mon Sep 17 00:00:00 2001 From: Arstanaly Rysbekov Date: Mon, 25 Sep 2023 11:52:44 +0200 Subject: [PATCH] WIP: get docker logs for azure service app via azure SDK --- cmd/kosli/snapshotAzureFunctions.go | 13 +++--------- internal/azure/azure_functions.go | 32 +++++++++++++++++++---------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/cmd/kosli/snapshotAzureFunctions.go b/cmd/kosli/snapshotAzureFunctions.go index 1faf7bfb2..f279ce0dd 100644 --- a/cmd/kosli/snapshotAzureFunctions.go +++ b/cmd/kosli/snapshotAzureFunctions.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "github.com/kosli-dev/cli/internal/azure" @@ -60,18 +59,12 @@ func (o *snapshotAzureFunctionsOptions) run(args []string) error { return err } for _, webapp := range webAppInfo { - fmt.Printf("Webapp name: %s\n", *webapp.Name) - fmt.Printf("Webapp image: %s\n", *webapp.Properties.SiteConfig.LinuxFxVersion) - for _, host := range webapp.Properties.EnabledHostNames { - fmt.Printf("Webapp host: %s\n", *host) + err := o.azureCredentials.GetDockerLogs(*webapp.Name) + if err != nil { + return err } } - err = o.azureCredentials.GetDockerLogs() - if err != nil { - return err - } - // envName := args[0] // TODO: Change later for azure function environments diff --git a/internal/azure/azure_functions.go b/internal/azure/azure_functions.go index 1441f22f4..cb7881a9e 100644 --- a/internal/azure/azure_functions.go +++ b/internal/azure/azure_functions.go @@ -2,8 +2,8 @@ package azure import ( "context" + "fmt" "io" - "net/http" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appservice/armappservice/v2" @@ -23,6 +23,7 @@ func (staticCreds *AzureStaticCredentials) GetWebAppsInfo() ([]*armappservice.Si return nil, err } + // Docs: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/resourcemanager/appservice/armappservice/README.md appserviceClientFactory, err := armappservice.NewClientFactory(staticCreds.SubscriptionId, credentials, nil) if err != nil { return nil, err @@ -43,26 +44,35 @@ func (staticCreds *AzureStaticCredentials) GetWebAppsInfo() ([]*armappservice.Si return webAppsInfo, nil } -func (staticCreds *AzureStaticCredentials) GetDockerLogs() error { - client := &http.Client{} +func (staticCreds *AzureStaticCredentials) GetDockerLogs(appServiceName string) error { + credentials, err := azidentity.NewClientSecretCredential(staticCreds.TenantId, staticCreds.ClientId, staticCreds.ClientSecret, nil) + if err != nil { + return err + } - req, err := http.NewRequest("GET", "https://tsha256.scm.azurewebsites.net/api/vfs/LogFiles/2023_09_14_10-30-0-8_docker.log", nil) + // Docs: https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/resourcemanager/appservice/armappservice/README.md + appserviceClientFactory, err := armappservice.NewClientFactory(staticCreds.SubscriptionId, credentials, nil) if err != nil { return err } - req.SetBasicAuth(staticCreds.ClientId, staticCreds.ClientSecret) - resp, err := client.Do(req) + webAppsClient := appserviceClientFactory.NewWebAppsClient() + + ctx := context.Background() + fmt.Println("Getting logs for app service: ", appServiceName) + response, err := webAppsClient.GetContainerLogsZip(ctx, staticCreds.ResourceGroupName, appServiceName, nil) if err != nil { return err } - if resp.Body != nil { - defer resp.Body.Close() + fmt.Println("Got logs for app service: ", appServiceName) + if response.Body != nil { + defer response.Body.Close() } - body, err := io.ReadAll(resp.Body) + fmt.Println("Reading logs for app service: ", appServiceName) + body, err := io.ReadAll(response.Body) if err != nil { return err } - println(string(body)) - + fmt.Println(len(body)) + // fmt.Println(string(body)) return nil }