44 "fmt"
55 "os"
66 "os/exec"
7+ "time"
78
89 "path/filepath"
910
@@ -25,6 +26,7 @@ type Config struct {
2526
2627type azureFunctionsDeployer struct {
2728 functions []* common.Function
29+ config * Config
2830}
2931
3032func newAzureFunctionsDeployer () * azureFunctionsDeployer {
@@ -33,14 +35,14 @@ func newAzureFunctionsDeployer() *azureFunctionsDeployer {
3335
3436func (afd * azureFunctionsDeployer ) Deploy (cfg * config.Configuration ) {
3537 afd .functions = cfg .Functions
36- DeployAzureFunctions (afd .functions )
38+ afd . config = DeployAzureFunctions (afd .functions )
3739}
3840
3941func (afd * azureFunctionsDeployer ) Clean () {
40- CleanAzureFunctions (afd .functions )
42+ CleanAzureFunctions (afd .config , afd . functions )
4143}
4244
43- func DeployAzureFunctions (functions []* common.Function ) {
45+ func DeployAzureFunctions (functions []* common.Function ) * Config {
4446 // 1. Copy exec_func.py to azurefunctions_setup
4547 // 2. Initialize resources required for Azure Functions deployment
4648 // 3. Create function folders
@@ -53,6 +55,12 @@ func DeployAzureFunctions(functions []*common.Function) {
5355 log .Fatalf ("Error loading azure functions config yaml: %s" , err )
5456 }
5557
58+ // Set unique names for Azure Resources
59+ timestamp := time .Now ().Format ("150405" ) // HHMMSS format
60+ config .AzureConfig .ResourceGroup = fmt .Sprintf ("%s-%s" , config .AzureConfig .ResourceGroup , timestamp ) // invitro-rg-XXXXXX
61+ config .AzureConfig .StorageAccountName = fmt .Sprintf ("%s%s" , config .AzureConfig .StorageAccountName , timestamp ) // invitrostorageXXXXXX
62+ config .AzureConfig .FunctionAppName = fmt .Sprintf ("%s-%s" , config .AzureConfig .FunctionAppName , timestamp ) // invitro-functionapp-XXXXXX
63+
5664 // Define the base directory containing functions to be zipped individually
5765 baseDir := "azure_functions_for_zip"
5866 sharedWorkloadDir := filepath .Join ("azurefunctions_setup" , "shared_azure_workload" )
@@ -80,15 +88,11 @@ func DeployAzureFunctions(functions []*common.Function) {
8088 if err := DeployFunctions (config , zipBaseDir , functions ); err != nil {
8189 log .Fatalf ("Error deploying function: %s" , err )
8290 }
83- }
8491
85- func CleanAzureFunctions (functions []* common.Function ) {
86- // Load azurefunctionsconfig yaml file
87- config , err := LoadConfig ("azurefunctions_setup/azurefunctionsconfig.yaml" )
88- if err != nil {
89- log .Fatalf ("Error loading azure functions config yaml: %s" , err )
90- }
92+ return config
93+ }
9194
95+ func CleanAzureFunctions (config * Config , functions []* common.Function ) {
9296 log .Infof ("Performing cleanup of experiment..." )
9397
9498 baseDir := "azure_functions_for_zip"
@@ -132,6 +136,12 @@ func InitAzureFunctions(config *Config, functions []*common.Function) {
132136
133137 // 2. Create Storage Account
134138 if err := CreateStorageAccount (config ); err != nil {
139+
140+ cleanupErr := DeleteResourceGroup (config )
141+ if cleanupErr != nil {
142+ log .Errorf ("Failed to delete resource group during cleanup: %v" , cleanupErr )
143+ }
144+
135145 log .Fatalf ("Error during Storage Account creation: %s" , err )
136146 }
137147
@@ -140,16 +150,34 @@ func InitAzureFunctions(config *Config, functions []*common.Function) {
140150 functionAppName := fmt .Sprintf ("%s-%d" , config .AzureConfig .FunctionAppName , i )
141151
142152 if err := CreateFunctionApp (config , functionAppName ); err != nil {
153+
154+ cleanupErr := DeleteResourceGroup (config )
155+ if cleanupErr != nil {
156+ log .Errorf ("Failed to delete resource group during cleanup: %v" , cleanupErr )
157+ }
158+
143159 log .Fatalf ("Error during Function App creation: %s" , err )
144160 }
145161
146162 // Set SCM_DO_BUILD_DURING_DEPLOYMENT
147163 if err := SetSCMSettings (config , functionAppName ); err != nil {
164+
165+ cleanupErr := DeleteResourceGroup (config )
166+ if cleanupErr != nil {
167+ log .Errorf ("Failed to delete resource group during cleanup: %v" , cleanupErr )
168+ }
169+
148170 log .Fatalf ("failed to set SCM settings: %s" , err )
149171 }
150172
151173 // Set ENABLE_ORYX_BUILD
152174 if err := SetORYXSettings (config , functionAppName ); err != nil {
175+
176+ cleanupErr := DeleteResourceGroup (config )
177+ if cleanupErr != nil {
178+ log .Errorf ("Failed to delete resource group during cleanup: %v" , cleanupErr )
179+ }
180+
153181 log .Fatalf ("failed to set Oryx settings: %s" , err )
154182 }
155183 }
@@ -250,31 +278,6 @@ func SetORYXSettings(config *Config, functionAppName string) error {
250278
251279/* Function to create folders and copy files to the folders */
252280
253- // func CreateFunctionFolders(baseDir string, functions []*common.Function) error {
254- // for i := 0; i < len(functions); i++ {
255- // folderName := fmt.Sprintf("function%d", i)
256- // folderPath := filepath.Join(baseDir, folderName)
257-
258- // // Create the function folder
259- // if err := os.MkdirAll(folderPath, os.ModePerm); err != nil {
260- // return fmt.Errorf("failed to create folder %s: %w", folderPath, err)
261- // }
262-
263- // // Copy azurefunctionsworkload.py, exec_func.py and function.json into each function folder
264- // if err := common.CopyFile("azurefunctions_setup/shared_azure_workload/azurefunctionsworkload.py", filepath.Join(folderPath, "azurefunctionsworkload.py")); err != nil {
265- // return fmt.Errorf("failed to copy azurefunctionsworkload.py to %s: %w", folderPath, err)
266- // }
267- // if err := common.CopyFile("azurefunctions_setup/shared_azure_workload/exec_func.py", filepath.Join(folderPath, "exec_func.py")); err != nil {
268- // return fmt.Errorf("failed to copy exec_func.py to %s: %w", folderPath, err)
269- // }
270- // if err := common.CopyFile("azurefunctions_setup/shared_azure_workload/function.json", filepath.Join(folderPath, "function.json")); err != nil {
271- // return fmt.Errorf("failed to copy function.json to %s: %w", folderPath, err)
272- // }
273- // }
274- // log.Debugf("Created %d function folders with copies of azurefunctionsworkload.py, exec_func.py and function.json under %s folder.\n", len(functions), baseDir)
275- // return nil
276- // }
277-
278281func CreateFunctionFolders (baseDir , sharedWorkloadDir string , functions []* common.Function ) error {
279282 for i := 0 ; i < len (functions ); i ++ {
280283 folderName := fmt .Sprintf ("function%d" , i )
@@ -345,13 +348,7 @@ func DeployFunctions(config *Config, baseDir string, functions []*common.Functio
345348 "--src" , zipFilePath ,
346349 "--build-remote" , "true" )
347350
348- //var stdout, stderr bytes.Buffer // debugging output
349- //cmd.Stdout = &stdout // debugging output
350- //cmd.Stderr = &stderr // debugging output
351-
352351 if err := cmd .Run (); err != nil {
353- //log.Errorf("Deployment stdout:\n%s", stdout.String()) // debugging output
354- //log.Errorf("Deployment stderr:\n%s", stderr.String()) // debugging output
355352 return fmt .Errorf ("failed to deploy %s to function app %s: %w" , zipFilePath , functionAppName , err )
356353 }
357354
0 commit comments