# Copyright (c) 2021, JINGROW
# For license information, please see license.txt


import jingrow
from jingrow.utils.fixtures import sync_fixtures

from jcloud.agent import Agent


def execute():
	settings = jingrow.get_pg("Jcloud Settings", "Jcloud Settings")
	settings.agent_repository_owner = "jingrow"

	settings.agent_github_access_token = input("GitHub Access Token: ")
	settings.save()

	sync_fixtures("jcloud")

	servers = jingrow.get_all(
		"Server", {"is_upstream_setup": True, "status": "Active"}, ["name", "proxy_server"]
	)
	for server in servers:
		proxy_server = jingrow.get_pg("Proxy Server", server.proxy_server)
		proxy_server.update_agent_ansible()

		agent = Agent(server.proxy_server, "Proxy Server")
		agent.update_upstream_private_ip(server.name)
