Skip to content

Commit

Permalink
Clean up with actual ec2 instance detection from imds.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHenson committed Oct 6, 2023
1 parent 8799e34 commit c03323d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 deletions.
13 changes: 10 additions & 3 deletions awscrt/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ class SystemEnvironment:
def __init__(self):
self._env = _awscrt.load_system_environment()

if self.is_ec2_nitro_instance():
self._detected_instance_type = self.get_ec2_instance_type()
else:
self._detected_instance_type = None

def is_ec2_nitro_instance(self) -> bool:
return _awscrt.is_env_ec2(self._env)

def get_ec2_instance_type(self) -> str:
return _awscrt.get_ec2_instance_type(self._env)

def get_ec2_instance_type(self) -> str:
return self._detected_instance_type

def is_crt_s3_optimized_for_system_env(self) -> bool:
return _awscrt.is_crt_s3_optimized_for_system(self._env, self._detected_instance_type)
4 changes: 0 additions & 4 deletions awscrt/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@
from awscrt.http import HttpRequest
from awscrt.io import ClientBootstrap, TlsConnectionOptions
from awscrt.auth import AwsCredentialsProvider, AwsSignatureType, AwsSignedBodyHeaderType, AwsSignedBodyValue, AwsSigningAlgorithm, AwsSigningConfig
from awscrt.common import SystemEnvironment
import awscrt.exceptions
import threading
from enum import IntEnum

def is_crt_s3_optimized_for_system_env(env: SystemEnvironment, optInstanceTypeOverride: str = None) -> bool:
return _awscrt.s3_is_crt_s3_optimized_for_system_env(env._env, optInstanceTypeOverride)

class S3RequestType(IntEnum):
"""The type of the AWS S3 request"""

Expand Down
2 changes: 1 addition & 1 deletion crt/aws-c-s3
Submodule aws-c-s3 updated 2 files
+7 −0 include/aws/s3/s3.h
+126 −0 source/s3.c
49 changes: 43 additions & 6 deletions source/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args) {
struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_byte_cursor system_virt_name = aws_system_environment_get_virtualization_vendor(env);
}

if (aws_byte_cursor_eq_c_str_ignore_case(&system_virt_name, "amazon ec2")) {
if (aws_s3_is_running_on_ec2(env)) {
Py_RETURN_TRUE;
}

Expand All @@ -89,11 +87,50 @@ PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args) {
struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_allocator *allocator = aws_py_get_allocator();

struct aws_string *instance_type = aws_s3_get_ec2_instance_type(allocator, env);

if (instance_type) {
PyObject *ret_value = PyUnicode_FromAwsString(&instance_type);
aws_string_destroy(instance_type);
return ret_value;
}

struct aws_byte_cursor product_name = aws_system_environment_get_virtualization_product_name(env);
return NULL;
}

PyObject *aws_py_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args) {
PyObject *env_capsule = NULL;
const char *instance_type_str = NULL;
Py_ssize_t instance_type_str_len = 0;

if (!PyArg_ParseTuple(args, "Oz#", &env_capsule, &instance_type_str, &instance_type_str_len)) {
return PyErr_AwsLastError();
}

struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_byte_cursor *instance_type_to_pass = NULL;
struct aws_byte_cursor instance_type_cur;

if (instance_type_str_len > 0) {
instance_type_cur = aws_byte_cursor_from_array(instance_type_str, (size_t)instance_type_str_len);
instance_type_to_pass = &instance_type_cur;
}

return PyUnicode_FromAwsByteCursor(&product_name);
bool is_optimized = aws_s3_is_optimized_for_system_env(env, instance_type_to_pass);

if (is_optimized) {
Py_RETURN_TRUE;
}

Py_RETURN_FALSE;
}

PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args) {
Expand Down
1 change: 1 addition & 0 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ PyObject *aws_py_thread_join_all_managed(PyObject *self, PyObject *args);
PyObject *aws_py_load_system_environment(PyObject *self, PyObject *args);
PyObject *aws_py_is_env_ec2(PyObject *self, PyObject *args);
PyObject *aws_py_get_ec2_instance_type(PyObject *self, PyObject *args);
PyObject *aws_py_is_crt_s3_optimized_for_system(PyObject *self, PyObject *args);

#endif /* AWS_CRT_PYTHON_COMMON_H */
3 changes: 2 additions & 1 deletion source/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(load_system_environment, METH_NOARGS),
AWS_PY_METHOD_DEF(is_env_ec2, METH_VARARGS),
AWS_PY_METHOD_DEF(get_ec2_instance_type, METH_VARARGS),
AWS_PY_METHOD_DEF(is_crt_s3_optimized_for_system, METH_VARARGS),


/* IO */
AWS_PY_METHOD_DEF(is_alpn_available, METH_NOARGS),
Expand Down Expand Up @@ -794,7 +796,6 @@ static PyMethodDef s_module_methods[] = {
AWS_PY_METHOD_DEF(s3_client_new, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_client_make_meta_request, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_meta_request_cancel, METH_VARARGS),
AWS_PY_METHOD_DEF(s3_is_crt_s3_optimized_for_system_env, METH_VARARGS),


/* WebSocket */
Expand Down
2 changes: 0 additions & 2 deletions source/s3.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "module.h"

PyObject *aws_py_s3_is_crt_s3_optimized_for_system_env(PyObject *self, PyObject *args);

PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args);
PyObject *aws_py_s3_client_make_meta_request(PyObject *self, PyObject *args);

Expand Down
31 changes: 0 additions & 31 deletions source/s3_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,34 +182,3 @@ PyObject *aws_py_s3_client_new(PyObject *self, PyObject *args) {
Py_DECREF(capsule);
return NULL;
}

PyObject *aws_py_s3_is_crt_s3_optimized_for_system_env(PyObject *self, PyObject *args) {
PyObject *env_capsule = NULL;
const char *override_str = NULL;
Py_ssize_t override_str_len = 0;

if (!PyArg_ParseTuple(args, "Oz#", &env_capsule, &override_str, &override_str_len)) {
return PyErr_AwsLastError();
}

struct aws_system_environment *env = PyCapsule_GetPointer(env_capsule, s_capsule_name_sys_env);
if (!env) {
return PyErr_AwsLastError();
}

struct aws_byte_cursor *override_param = NULL;
struct aws_byte_cursor override_cur;

if (override_str_len > 0) {
override_cur = aws_byte_cursor_from_array(override_str, (size_t)override_str_len);
override_param = &override_cur;
}

bool is_optimized = aws_s3_is_optimized_for_system_env(env, override_param);

if (is_optimized) {
Py_RETURN_TRUE;
}

Py_RETURN_FALSE;
}

0 comments on commit c03323d

Please sign in to comment.