2016-12-07 17 views
1

私はローカルに自分のBluemix VCAP_SERVICES environment variableを設定しようとしていますが、私は、端末でこのエラーを取得しています:ローカルに開発できるようにBluemix VCAP_SERVICES環境変数をローカルに設定しますか?

のNoSQL:再現する

ステップ

を見つけていないコマンドを

  1. Bluemix.netにログイン
  2. hello world flask application
  3. 地元のエディタでPythonの
  4. のためのアプリケーションのランタイム/環境変数からVCAP_SERVICES環境変数は、すべての行を削除するアプリケーション
  5. コピーへ
  6. バインドBluemix Cloudantサービスは、Mac端末に壊し
  7. vi ~/.bash_profile
  8. VCAPSERVICESi
  9. ペーストで挿入モードに入り、鉱山は次のようになります。

    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 
    
  10. ファイルを保存して:wq!

  11. 出典新しいVCAP環境私が間違ってコピーを何をやっているし、私の地元のBluemixを設定する変数

でターミナル・ウィンドウを設定する. ~/.bash_profileと変更されたファイルでviを終了VCAP_Services環境変数?

私が全体をコピーすると、行が長すぎるというエラーが表示されます。 Bluemix PythonランタイムVCAP_SERVICES変数をローカルのMac .bash_profile環境設定に簡単にコピー&ペーストして、JSONとこれらの改行などを手作業でマッサージする必要はありませんか?

デベロッパー、テスト、ステージング、プロダクションから移動するときにローカルファイルを保存するのは安全ではないので、ローカルファイルを使用したくないです。

+0

私はあなたの質問の書式を読みやすくするために改善しました。 [ヘルプセンター](https://stackoverflow.com/help/formatting)のStack Overflowの書式設定のドキュメントを参照して、次回にこれを行うことができます。リスト内のコードブロックは、リストレベルごとに4つのスペースと、コードブロックを示すための4つのスペースをインデントする必要があることに注意してください。私はまた、より良い文脈のために再現するために、あなたのステップの前に問題文の一部を動かしました。がんばろう! – Chris

+0

編集のためのChrisありがとうございました –

答えて

2

私は答えは= '{ "cloudantNoSQLDB" VCAP_SERVICES

VCAP_SERVICESの最初と最後に単一引用符を使用して考え出した:[{ "資格情報":" {host ":" fakehostc-bluemix.cloudant.com "、" password ":" fakepassword4da6de3a12a83362b26a "、" port ":443、" url ":" https://fakeURLc-bluemix:fakeab96175c-bluemix.cloudant.com "、" username ":" fakeusername-b749-399cfbd1175c- 「Cloud」、「NoSQL DB-p2」、「plan」:「Lite」、「provider」:null、「syslog_drain_url」:null、「tags」: data_management "、" ibm_作成された」、 "ibm_dedicated_public"]}]}」

ここVCAPサービスの環境変数を取得し、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のenv変数をローカルに作成する反パターンです。私はローカルで走っているときに接続情報を使うことを勧めます。

オプション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を入力してください。

.gitignore.envを追加してくださいと.cfignore

+0

こんにちは、私はすでにVCAP_Services環境変数を読むためのPythonコードを持っています。私の問題は、ダッシュボードのBluemix Python Runtime環境のコピーアイコンかcf env myappをコピーした後にvi〜/ .bash_profileに設定することです。私はそれの最初と最後に一重引用符を追加する必要があることを理解しました。 –

2

その意志CF CLIプラグインがありますあなたのアプリからVCAP_SERVICESを取得し、ローカルで設定するのを手伝ってください。私はそれを私のMac上で使用しており、引用符をまったく調整する必要はありませんでした。

チェックアウトhttps://github.com/jthomas/copyenv

関連する問題