#!/usr/bin/python3.5
import os
import sys
import traceback

from indy_common.config_util import getConfig
from indy_common.config_helper import NodeConfigHelper

from ledger.compact_merkle_tree import CompactMerkleTree

from plenum.common.stack_manager import TxnStackManager
from plenum.common.ledger import Ledger
from stp_core.common.log import getlogger
from storage.helper import initHashStore

logger = getlogger()

ENV_FILE_PATH = "/etc/indy/indy.env"


def get_node_name():
    node_name = None
    node_name_key = 'NODE_NAME'

    if os.path.exists(ENV_FILE_PATH):
        with open(ENV_FILE_PATH, "r") as fenv:
            for line in fenv.readlines():
                if line.find(node_name_key) != -1:
                    node_name = line.split('=')[1].strip()
                    break
    else:
        logger.error("Path to env file does not exist")

    return node_name


def append_ips_to_env(node_ip):
    node_ip_key = 'NODE_IP'
    client_ip_key = 'CLIENT_IP'
    with open(ENV_FILE_PATH, "a") as fenv:
        fenv.write("\n{}={}\n".format(node_ip_key, node_ip))
        fenv.write("{}={}\n".format(client_ip_key, "0.0.0.0"))


def migrate_all():
    node_name = get_node_name()
    if node_name is None:
        logger.error("Could not get node name")
        return False

    config = getConfig()
    config_helper = NodeConfigHelper(node_name, config)

    hash_store = initHashStore(config_helper.ledger_dir, "pool", config, read_only=True)
    ledger = Ledger(CompactMerkleTree(hashStore=hash_store), dataDir=config_helper.ledger_dir,
                    fileName=config.poolTransactionsFile, read_only=True)
    nodeReg, _, _ = TxnStackManager.parseLedgerForHaAndKeys(ledger)

    if nodeReg is None:
        logger.error("Empty node registry returned by stack manager")
        return False

    if node_name not in nodeReg:
        logger.error("Node registry does not contain node {}".format(node_name))
        return False

    ha = nodeReg[node_name]
    if ha is None:
        logger.error("Empty HA for node {}".format(node_name))
        return False

    logger.info("HA for {}: {}".format(node_name, ha))

    try:
        append_ips_to_env(ha.host)
    except Exception:
        logger.error(traceback.print_exc())
        logger.error("Could not append node and client IPs to indy env file")
        return False

    return True


if migrate_all():
    logger.info("Migration complete: node and client IPs have been added to indy env file")
else:
    logger.error("Migration failed: node and client IPs have not been added to indy env file")
    sys.exit(1)

@@ -211,16 +211,107 @@
 rkleTree
+%0Afrom ledger.genesis_txn.genesis_txn_initiator_from_file import GenesisTxnInitiatorFromFile
 %0A%0Afrom p
@@ -1054,16 +1054,21 @@
 _key = '
+NODE_
 CLIENT_I
@@ -1247,150 +1247,35 @@
 def 
-migrate_all():%0A    node_name = get_node_name()%0A    if node_name is None:%0A        logger.error(%22Could not get 
+get_pool_ledger(
 node
- 
+_
 name
-%22)%0A        return False%0A
+):
 %0A   
@@ -1353,16 +1353,184 @@
 onfig)%0A%0A
+    genesis_txn_initiator = GenesisTxnInitiatorFromFile(config_helper.genesis_dir,%0A                                                        config.poolTransactionsFile)%0A
     hash
@@ -1596,38 +1596,20 @@
 nfig
-, read_only=True)%0A    ledger =
+)%0A    return
  Led
@@ -1652,16 +1652,34 @@
 _store),
+%0A                 
  dataDir
@@ -1701,26 +1701,24 @@
 ledger_dir,%0A
-  
             
@@ -1764,22 +1764,324 @@
 ile,
- read_only=Tru
+%0A                  ensureDurability=config.EnsureLedgerDurability,%0A                  genesis_txn_initiator=genesis_txn_initiator)%0A%0A%0Adef migrate_all():%0A    node_name = get_node_name()%0A    if node_name is None:%0A        logger.error(%22Could not get node name%22)%0A        return False%0A%0A    ledger = get_pool_ledger(node_nam
 e)%0A 

Remove 'read-only' flag from migration script.