fallimenti = []
iteration_count = []

print("STARTING + BEST PHASE")
###########################################################################################
#                                   FTpi+best                                             #
###########################################################################################
for simulation_count in range(0, 3):
    if os.environ.get('SKIP_BEST', None) != None:
        break
    start = time.time()
    fallimento = 0
    for i in range(0, DEVICES_NUMBER):
        deviceId = i + 1
        _, device1 = fd.add_device("10.10.20." + str(deviceId), "cisco",
                                   "cisco")

    dev_list = fog_torch()
    dev_list = dev_list_sort(dev_list)
    # Uploading Application
    code, localapp = fd.add_app("./NettestApp2V1_lxc.tar.gz",
                                publish_on_upload=True)
    print("STARTING TO DEPLOY", simulation_counter())
    for myapp_index in range(0, DEPLOYMENT_NUMBER):
        if DEPLOYMENT_NUMBER % 200 == 0:
            dev_list = fog_torch()
            dev_list = dev_list_sort(dev_list)
        else:
            dev_list = dev_list_sort(dev_list)

        dep = "dep" + str(myapp_index)
FOG_DIRECTOR_HOST = "127.0.0.1:5000"

def reset_simulation():
    url = "http://%s/simulationreset" % "127.0.0.1:5000"
    r = requests.get(url)
    return r.json()
reset_simulation()

fd = FogDirector(FOG_DIRECTOR_HOST)
code = fd.authenticate("admin", "admin_123")
if code == 401:
    print("Failed Authentication")

# Adding devices
_, device1 = fd.add_device("10.10.20.51", "cisco", "cisco")
_, device2 = fd.add_device("10.10.20.52", "cisco", "cisco")
_, device3 = fd.add_device("10.10.20.53", "cisco", "cisco")

code, localapp = fd.add_app("./NettestApp2V1_lxc.tar.gz", publish_on_upload=True)

_, myapp1 = fd.create_myapp(localapp["localAppId"], "dep1")
_, myapp2 = fd.create_myapp(localapp["localAppId"], "dep2", minjobs=1)

# Deploying on devices with default resources, 
# that are c1.small profile, defined by custom library
code, res = fd.install_app("dep1", ["10.10.20.51"])
while code == 400:
    code, res = fd.install_app("dep1", ["10.10.20.51"])

code, res = fd.install_app("dep2", ["10.10.20.52"])