Island: Send analytics on island startup instead of object creation

This change makes the logic more evident, because sending the analytics is not done during the initialization of an object
This commit is contained in:
vakarisz 2022-08-12 09:32:50 +03:00
parent bec7bef1b3
commit c61737abc0
3 changed files with 26 additions and 30 deletions

View File

@ -5,8 +5,11 @@ import sys
from pathlib import Path from pathlib import Path
import gevent.hub import gevent.hub
import requests
from gevent.pywsgi import WSGIServer from gevent.pywsgi import WSGIServer
from monkey_island.cc import Version
from monkey_island.cc.deployment import Deployment
from monkey_island.cc.server_utils.consts import ISLAND_PORT from monkey_island.cc.server_utils.consts import ISLAND_PORT
from monkey_island.cc.setup.config_setup import get_server_config from monkey_island.cc.setup.config_setup import get_server_config
@ -150,6 +153,7 @@ def _start_island_server(
error_log=logger, error_log=logger,
) )
_log_init_info() _log_init_info()
_send_analytics(container)
http_server.serve_forever() http_server.serve_forever()
@ -173,3 +177,25 @@ def _log_web_interface_access_urls():
"To access the web interface, navigate to one of the the following URLs using your " "To access the web interface, navigate to one of the the following URLs using your "
f"browser: {web_interface_urls}" f"browser: {web_interface_urls}"
) )
ANALYTICS_URL = (
"https://m15mjynko3.execute-api.us-east-1.amazonaws.com/default?version={"
"version}&deployment={deployment}"
)
def _send_analytics(di_container):
version = di_container.resolve(Version)
deployment = di_container.resolve(Deployment)
url = ANALYTICS_URL.format(deployment=deployment.value, version=version.version_number)
try:
response = requests.get(url).json()
logger.info(
f"Version number and deployment type was sent to analytics server."
f" The response is: {response}"
)
except requests.exceptions.ConnectionError as err:
logger.info(
f"Failed to send deployment type and version " f"number to the analytics server: {err}"
)

View File

@ -1,28 +0,0 @@
import logging
import requests
from monkey_island.cc import Version
from monkey_island.cc.deployment import Deployment
logger = logging.getLogger(__name__)
ANALYTICS_URL = (
"https://m15mjynko3.execute-api.us-east-1.amazonaws.com/default?version={"
"version}&deployment={deployment}"
)
class Analytics:
def __init__(self, version: Version, deployment: Deployment):
self.version = version.version_number
self.deployment = deployment.value
self._send_analytics()
def _send_analytics(self):
url = ANALYTICS_URL.format(deployment=self.deployment, version=self.version)
response = requests.get(url).json()
logger.info(
f"Version number and deployment type was sent to analytics server."
f" The response is: {response}"
)

View File

@ -53,7 +53,6 @@ from monkey_island.cc.services.utils.network_utils import get_ip_addresses
from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL
from . import AuthenticationService from . import AuthenticationService
from .analytics import Analytics
from .reporting.report import ReportService from .reporting.report import ReportService
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -74,7 +73,6 @@ def initialize_services(data_dir: Path) -> DIContainer:
ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME) ILockableEncryptor, RepositoryEncryptor(data_dir / REPOSITORY_KEY_FILE_NAME)
) )
container.register_instance(Version, container.resolve(Version)) container.register_instance(Version, container.resolve(Version))
container.register_instance(Analytics, container.resolve(Analytics))
container.register(Publisher, Publisher) container.register(Publisher, Publisher)
container.register_instance(IEventQueue, container.resolve(PyPubSubEventQueue)) container.register_instance(IEventQueue, container.resolve(PyPubSubEventQueue))