Python Cloud Service中的Azure环境变量 [英] Azure Environment Variables in Python Cloud Service

查看:102
本文介绍了Python Cloud Service中的Azure环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Microsoft Azure上运行的Python云服务,当它在开发阶段,生产阶段和生产阶段运行时,应该使用其他存储帐户(用于blob存储和队列).

I have a Python Cloud Service running on Microsoft Azure which should be using a different Storage Account (for blob storage and queues) when it's running in dev vs. staging vs. production.

我不想对存储帐户凭据进行硬编码,而是从环境中获取它们.另外,我想要一个环境变量或一些表明我是在演出还是生产中的东西.当我尝试print(os.environ)时,看不到任何Azure存储凭据,也没有表示暂存或生产状态的值.

I'd rather not hard-code the Storage Account credentials, but get them from the environment. Alternatively I would like an environment variable or something that indicates whether I'm in staging or production. When I try print(os.environ) I don't see any azure storage credentials, nor a value that indicates staging or production.

有什么办法可以做到这一点?

Is there any way to achieve this?

推荐答案

我正在回答自己的问题,因为我使用了多种解决方案组合来做出对我有用的东西.

I'm answering my own question, since I've used a combination of solutions to make something that works for me.

我能够在多个ServiceConfiguration文件中定义自己的设置,例如ServiceConfiguration.Local.cscfgServiceConfiguration.Dev.cscfgServiceConfiguration.Production.cscfg.在<ConfigurationSettings>下,添加<Setting name="settingname" value="settingvalue" />,或使用Visual Studio中的界面.发布时,您可以选择要使用的配置文件,而无需修改任何代码.额外的优势是,在发布服务之后,还可以通过Azure门户修改这些设置.请参阅这篇文章这篇文章

I was able to define my settings in multiple ServiceConfiguration files, such as ServiceConfiguration.Local.cscfg, ServiceConfiguration.Dev.cscfgand ServiceConfiguration.Production.cscfg. Under <ConfigurationSettings>, add <Setting name="settingname" value="settingvalue" />, or use the interface in Visual Studio. When publishing you can then pick which configuration file to use, without having to modify any code. Added advantage is that these settings can also be modified via the Azure portal after the service has been published. See this post and this post.

下一个挑战是将这些变量注入Python环境.与ServiceDefinition.csdef中定义的变量不同,配置设置不适用于Python环境.但是,它们存储在DLL中的某个位置,可以使用某些C#方法调用进行访问并注入到Python环境中(我对整个过程一无所知,我只是遵循之前的任何位置:

The next challenge is to inject these variables into the Python environment. Unlike variables that are defined in ServiceDefinition.csdef, the configuration settings are not available to the Python environment. They are however stored in a DLL somewhere, can be accessed and injected into the Python environment using some C# method calls (I'm pretty ignorant about this whole process, I just followed this post) . Just add these lines to LaunchWorker.ps1, anywhere before iex "py $worker_command":

# search for the Dll
$Splathashtable = @{
                    'Path' = "$env:windir\Microsoft.NET\assembly\";
                    'Filter' = 'Microsoft.WindowsAzure.ServiceRuntime.dll';
                    'Include' = '*.dll'
                    }

$dllfile = Get-ChildItem @Splathashtable -Recurse  | Select-Object -Last 1 
# selecting only one object, in case of multiple results

# add the DLL to the current PowerShell session
Add-Type -Path $dllfile.FullName    

# Call the Static method on the class to retrieve the setting value
$Setting = [Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment]::GetConfigurationSettingValue('settingname')

# add setting to environment
[Environment]::SetEnvironmentVariable('settingname', $Setting)

现在您将找到通过os.environ.get('SETTINGNAME')

这篇关于Python Cloud Service中的Azure环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆