Exemple #1
0
        public static async Task Run([TimerTrigger("%WorkflowInitiatorTimer%")] TimerInfo myTimer, TraceWriter log)
        {
            log.Info($"Initializing AEMO workflow: {DateTime.Now}");

            bool fakeIt = true;

            var fakeItIsASetting = Environment.GetEnvironmentVariable("FakeIt") == null ? false : true;

            if (fakeItIsASetting)
            {
                fakeIt = bool.Parse(Environment.GetEnvironmentVariable("FakeIt"));
            }

            // Authenticate for Azure REST API
            var accessToken = await AuthenticateAzure.Authenticate(log);

            if (string.IsNullOrEmpty(accessToken))
            {
                throw new NullReferenceException("The access_token was returned as null");
            }

            var resourceGroupAndFunctionAppList = Environment.GetEnvironmentVariable("ResourceGroupAndFunctionAppList");
            var environment   = Environment.GetEnvironmentVariable("Environment");
            var emailHtmlBody = Environment.GetEnvironmentVariable("EmailHtmlBody");
            var slackBody     = Environment.GetEnvironmentVariable("SlackBody");
            var smsBody       = Environment.GetEnvironmentVariable("SmsBody");

            // A list of all apps that require stop, update config and start
            List <string> rgsWithApps = resourceGroupAndFunctionAppList.Split(';').ToList();

            // Did we stop all the apps?
            bool allStopped = await StartStopApps(log, accessToken, rgsWithApps, false);

            if (allStopped)
            {
                log.Info($"Function apps successfully stopped: {DateTime.Now}");

                // TODO : Move this inside if stopped maybe?
                var aemoFtpPassword = await Persistence.GetCurrentAemoPassword(log);

                string newPassword = PasswordGenerator.GeneratePassword(true, true, true, true, false, 8);

                // Call AEMO password changer
                bool passwordChanged = await ChangeAemoPassword(log, newPassword, aemoFtpPassword, fakeIt);

                if (passwordChanged)
                {
                    string rotatedPasswordUrlFromKeyVault = await Persistence.RotateAndAddNewAemoPassword(log, aemoFtpPassword, newPassword, fakeIt);

                    // Start function app
                    bool allStarted = await StartStopApps(log, accessToken, rgsWithApps, true);

                    if (allStarted)
                    {
                        log.Info($"Function app successfully started: {DateTime.Now}");
                    }

                    emailHtmlBody = string.Format(emailHtmlBody, environment, rotatedPasswordUrlFromKeyVault);
                    slackBody     = string.Format(slackBody, environment, rotatedPasswordUrlFromKeyVault);
                    smsBody       = string.Format(smsBody, environment, rotatedPasswordUrlFromKeyVault);

                    // send notifiaction to developers
                    await Messaging.SendNotification(log, fakeIt, emailHtmlBodyOverride : emailHtmlBody, slackBodyOverride : slackBody, smsBodyOverride : smsBody);
                }
                else
                {
                    var exceptionMessage = $"<h1>AEMO Password Changer Workflow Message</h1></br><span style = \"color:red;\">Could not change the AMO password: {DateTime.Now}</span>";
                    log.Error(exceptionMessage);
                    await Messaging.SendNotification(log, false, emailSubjectOverride : $"The [{environment}] AEMO password changer failed to change the AEMO password!", emailHtmlBodyOverride : exceptionMessage, slackBodyOverride : exceptionMessage, smsBodyOverride : exceptionMessage, slackIconOverride : ":bandit:");
                }
            }
            else
            {
                // Start function app
                bool allStarted = await StartStopApps(log, accessToken, rgsWithApps, true);

                if (allStarted)
                {
                    log.Info($"Function app successfully started: {DateTime.Now}");
                }
            }

            log.Info($"Finalizing AEMO workflow: {DateTime.Now}");
        }