Skip to content

Commit

Permalink
WIP: get docker logs for azure service app via azure SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
arstanaly committed Sep 25, 2023
1 parent d19d215 commit fa45045
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
13 changes: 3 additions & 10 deletions cmd/kosli/snapshotAzureFunctions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"io"

"github.com/kosli-dev/cli/internal/azure"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions internal/azure/azure_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}

0 comments on commit fa45045

Please sign in to comment.