Exemple #1
0
        static void Main(string[] args)
        {
            GenericLogsInterface.SetToGenericLogs();
            ParseAndValidateOptions(args);

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            switch (_runtimeMode)
            {
            case LocalAmbrosiaRuntimeModes.DebugInstance:
                var myRuntime = new AmbrosiaRuntime();
                myRuntime.InitializeRepro(_instanceName, _serviceLogPath, _checkpointToLoad, _currentVersion,
                                          _isTestingUpgrade, _serviceReceiveFromPort, _serviceSendToPort);
                return;

            case LocalAmbrosiaRuntimeModes.AddReplica:
            case LocalAmbrosiaRuntimeModes.RegisterInstance:
                if (_runtimeMode == LocalAmbrosiaRuntimeModes.AddReplica)
                {
                    _isActiveActive = true;
                }

                var dataProvider = new CRA.DataProvider.Azure.AzureDataProvider(Environment.GetEnvironmentVariable("AZURE_STORAGE_CONN_STRING"));
                var client       = new CRAClientLibrary(dataProvider);
                client.DisableArtifactUploading();

                var replicaName             = $"{_instanceName}{_replicaNumber}";
                AmbrosiaRuntimeParams param = new AmbrosiaRuntimeParams();
                param.createService = _recoveryMode == AmbrosiaRecoveryModes.A
                        ? (bool?)null
                        : (_recoveryMode != AmbrosiaRecoveryModes.N);
                param.pauseAtStart             = _isPauseAtStart;
                param.persistLogs              = _isPersistLogs;
                param.logTriggerSizeMB         = _logTriggerSizeMB;
                param.activeActive             = _isActiveActive;
                param.upgradeToVersion         = _upgradeVersion;
                param.currentVersion           = _currentVersion;
                param.serviceReceiveFromPort   = _serviceReceiveFromPort;
                param.serviceSendToPort        = _serviceSendToPort;
                param.serviceName              = _instanceName;
                param.serviceLogPath           = _serviceLogPath;
                param.AmbrosiaBinariesLocation = _binariesLocation;
                param.storageConnectionString  = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONN_STRING");

                try
                {
                    if (client.DefineVertexAsync(param.AmbrosiaBinariesLocation, () => new AmbrosiaRuntime()).GetAwaiter().GetResult() != CRAErrorCode.Success)
                    {
                        throw new Exception();
                    }

                    // Workaround because of limitation in parameter serialization in CRA
                    XmlSerializer xmlSerializer = new XmlSerializer(param.GetType());
                    string        serializedParams;
                    using (StringWriter textWriter = new StringWriter())
                    {
                        xmlSerializer.Serialize(textWriter, param);
                        serializedParams = textWriter.ToString();
                    }

                    if (client.InstantiateVertexAsync(replicaName, param.serviceName, param.AmbrosiaBinariesLocation, serializedParams).GetAwaiter().GetResult() != CRAErrorCode.Success)
                    {
                        throw new Exception();
                    }
                    client.AddEndpointAsync(param.serviceName, AmbrosiaRuntime.AmbrosiaDataInputsName, true, true).Wait();
                    client.AddEndpointAsync(param.serviceName, AmbrosiaRuntime.AmbrosiaDataOutputsName, false, true).Wait();
                    client.AddEndpointAsync(param.serviceName, AmbrosiaRuntime.AmbrosiaControlInputsName, true, true).Wait();
                    client.AddEndpointAsync(param.serviceName, AmbrosiaRuntime.AmbrosiaControlOutputsName, false, true).Wait();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error trying to upload service. Exception: " + e.Message);
                }

                return;

            default:
                throw new NotSupportedException($"Runtime mode: {_runtimeMode} not supported.");
            }
        }