【问题标题】:Set Bluemix VCAP_SERVICES environment variable locally so that I can develop locally?在本地设置 Bluemix VCAP_SERVICES 环境变量,以便我可以在本地开发?
【发布时间】:2016-12-07 00:55:52
【问题描述】:

我正在尝试在本地设置我的 Bluemix VCAP_SERVICES environment variable,但我在终端中收到此错误:

NoSQL:找不到命令

复制步骤

  1. 登录到 Bluemix.net
  2. 部署hello world flask application
  3. 将 Bluemix Cloudant 服务绑定到应用程序
  4. 从 Python 应用程序的运行时/环境变量中复制 VCAP_SERVICES 环境变量
  5. 在本地编辑器中删除 Mac 终端上的所有换行符
  6. vi ~/.bash_profile
  7. 使用i 进入插入模式
  8. 粘贴到VCAPSERVICES,我的是这样的:

    VCAP_SERVICES="{"VCAP_SERVICES":{"cloudantNoSQLDB": [{"credentials": {"host": "fakehostc-bluemix.cloudant.com","password":"fakepassword4da6de3a12a83362b26a","port": 443,"url": "https://fakeURLc-bluemix:fakeab96175c-bluemix.cloudant.com","username": "fakeusername-b749-399cfbd1175c-bluemix"},"label":"cloudantNoSQLDB","name":"Cloudant NoSQL DB-p2","plan":"Lite","provider":null,"syslog_drain_url":null,"tags":["data_management","ibm_created","ibm_dedicated_public"]}]}}"
    export VCAP_SERVICES
    
  9. 保存文件并使用:wq!退出vi

  10. 使用. ~/.bash_profile 获取修改后的文件以使用新的 VCAP 环境变量设置终端窗口

我在复制和设置本地 Bluemix VCAP_Services 环境变量时做错了什么?

如果我复制整个内容,我会收到行太长的错误。如何轻松地将整个 Bluemix Python Runtime VCAP_SERVICES 变量复制并粘贴到我的本地 Mac .bash_profile 环境设置中,而无需手动按摩 JSON 和所有这些换行符等?

我不想使用本地文件来存储这些文件,因为当我从开发、测试、登台和生产移动时,它不是很安全。

【问题讨论】:

  • 我改进了您问题的格式,使其更易于阅读。请查看 help centre 中的 Stack Overflow 格式文档,以便您下次可以自己执行此操作。请注意,列表中的代码块需要为每个列表级别缩进四个空格,再加上另外四个空格来表示代码块。我还在您的步骤之前移动了您的部分问题陈述以重现更好的上下文。祝你好运!
  • 感谢克里斯的编辑

标签: python macos environment-variables ibm-cloud cloudant


【解决方案1】:

我想出答案在 VCAP_SERVICES 的开头和结尾使用单引号

VCAP_SERVICES='{"cloudantNoSQLDB": [{"credentials": {"host": "fakehostc-bluemix.cloudant.com","password":"fakepassword4da6de3a12a83362b26a","port": 443,"url": "https://fakeURLc-bluemix:fakeab96175c-bluemix.cloudant.com","username": "fakeusername-b749-399cfbd1175c-bluemix"},"label":"cloudantNoSQLDB","name":"Cloudant NoSQL DB-p2","plan":"Lite"," provider":null,"syslog_drain_url":null,"tags":["data_management","ibm_created","ibm_dedicated_public"]}]}'

下面是获取 VCAP Services 环境变量并在 Cloudant 上进行基本操作的相应代码:

# 1. Parse VCAP_SERVICES Variable and connect to DB         
vcap = json.loads(os.getenv("VCAP_SERVICES"))['cloudantNoSQLDB']        
serviceUsername = vcap[0]['credentials']['username']
servicePassword = vcap[0]['credentials']['password']    
serviceURL = vcap[0]['credentials']['url']

# Create Cloudant DB connection
# This is the name of the database we are working with.
databaseName = "databasedemo"

# This is a simple collection of data,
# to store within the database.
sampleData = [
    [1, "one", "boiling", 100],
    [2, "two", "hot", 40],
    [3, "three", "warm", 20],
    [4, "four", "cold", 10],
    [5, "five", "freezing", 0]
]

# Use the Cloudant library to create a Cloudant client.
client = Cloudant(serviceUsername, servicePassword, url=serviceURL)

# Connect to the server
client.connect()

# 2.  Creating a database within the service instance.

# Create an instance of the database.
myDatabaseDemo = client.create_database(databaseName)

# Check that the database now exists.
if myDatabaseDemo.exists():
    print "'{0}' successfully created.\n".format(databaseName)

# 3.  Storing a small collection of data as documents within the database.

# Create documents using the sample data.
# Go through each row in the array
for document in sampleData:
    # Retrieve the fields in each row.
    number = document[0]
    name = document[1]
    description = document[2]
    temperature = document[3]

    # Create a JSON document that represents
    # all the data in the row.
    jsonDocument = {
        "numberField": number,
        "nameField": name,
        "descriptionField": description,
        "temperatureField": temperature
    }

    # Create a document using the Database API.
    newDocument = myDatabaseDemo.create_document(jsonDocument)

    # Check that the document exists in the database.
    if newDocument.exists():
        print "Document '{0}' successfully created.".format(number)

# 4.  Retrieving a complete list of the documents.

# Simple and minimal retrieval of the first
# document in the database.
result_collection = Result(myDatabaseDemo.all_docs)
print "Retrieved minimal document:\n{0}\n".format(result_collection[0])

# Simple and full retrieval of the first
# document in the database.
result_collection = Result(myDatabaseDemo.all_docs, include_docs=True)
print "Retrieved full document:\n{0}\n".format(result_collection[0])

# Use a Cloudant API endpoint to retrieve
# all the documents in the database,
# including their content.

# Define the end point and parameters
end_point = '{0}/{1}'.format(serviceURL, databaseName + "/_all_docs")
params = {'include_docs': 'true'}

# Issue the request
response = client.r_session.get(end_point, params=params)

# Display the response content
print "{0}\n".format(response.json())

# 5.  Deleting the database.

# Delete the test database.
try :
    client.delete_database(databaseName)
except CloudantException:
    print "There was a problem deleting '{0}'.\n".format(databaseName)
else:
    print "'{0}' successfully deleted.\n".format(databaseName)

# 6.  Closing the connection to the service instance.

# Disconnect from the server
client.disconnect()

【讨论】:

    【解决方案2】:

    在本地创建 VCAP_SERVICES 环境变量是一种反模式。我建议在本地运行时只使用连接信息。

    选项 1

    if 'VCAP_SERVICES' in os.environ:
        services = json.loads(os.getenv('VCAP_SERVICES'))
        cloudant_url = services['cloudantNoSQLDB'][0]['credentials']['url']
    else:
        cloudant_url = "https://fakeURLc-bluemix:fakeab96175c-bluemix.cloudant.com"
    

    选项 2

    如果您不想将凭据硬编码到代码中,则可以创建一个.env 文件:

    export LOCAL_CLOUDANT_URL=https://fakeURLc-bluemix:fakeab96175c-bluemix.cloudant.com
    

    在你的python代码中:

    if 'VCAP_SERVICES' in os.environ:
        services = json.loads(os.getenv('VCAP_SERVICES'))
        cloudant_url = services['cloudantNoSQLDB'][0]['credentials']['url']
    else:
        cloudant_url = os.environ['LOCAL_CLOUDANT_URL']
    

    然后source .env,然后再运行您的应用程序。

    请务必将.env 添加到.gitignore.cfignore

    【讨论】:

    • 嗨 Ram,我已经有了读取 VCAP_Services 环境变量的 python 代码。我的问题是在从仪表板中的 Bluemix Python 运行时环境复制图标复制它或执行 cf env myapp 之后设置它 vi ~/.bash_profile 。我发现我需要在它的开头和结尾添加单引号。
    【解决方案3】:

    有一个 cf CLI 插件可以从您的应用程序中获取 VCAP_SERVICES 并帮助您在本地进行设置。我在我的 Mac 上使用过它,根本不需要调整引号。

    结帐https://github.com/jthomas/copyenv

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 2014-10-28
      • 1970-01-01
      • 2015-05-30
      • 2013-11-15
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      相关资源
      最近更新 更多