Skip to content

Commit

Permalink
Merge pull request #82 from aiarena/feature/authless-s3-download
Browse files Browse the repository at this point in the history
Feature/authless s3 download
  • Loading branch information
danielvschoor authored Nov 24, 2023
2 parents e0ebd2c + bef7a0d commit 2531ca2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 20 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ members = [
"sc2_controller"]
resolver = "2"

resolver = "2"

[workspace.package]
version = "0.5.0"
authors = ["[email protected]"]


[profile.ci-dev]
inherits="dev"
incremental=false
Expand Down
31 changes: 19 additions & 12 deletions common/src/api/api_reference/aiarena/aiarena_api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ impl AiArenaApiClient {
}
}

pub async fn download_map(&self, map_url: &str) -> Result<Bytes, ApiError<AiArenaApiError>> {
pub async fn download_map(&self, map_url: &str, add_auth_header: bool) -> Result<Bytes, ApiError<AiArenaApiError>> {
// static string, so the constructor should catch any parse errors
let map_url = Url::parse(map_url).map_err(ApiError::from)?;
let request = self
.client
.request(reqwest::Method::GET, map_url)
.header(reqwest::header::AUTHORIZATION, self.token_header())
.build()?;

let mut request_builder = self.client
.request(reqwest::Method::GET, map_url);

if add_auth_header {
request_builder = request_builder.header(reqwest::header::AUTHORIZATION, self.token_header())
}
let request = request_builder.build()?;

let response = self.client.execute(request).await?;

Expand Down Expand Up @@ -111,15 +114,19 @@ impl AiArenaApiClient {
fn token_header(&self) -> String {
format!("Token {}", &self.token)
}
pub async fn download_zip(&self, url: &str) -> Result<Bytes, ApiError<AiArenaApiError>> {
pub async fn download_zip(&self, url: &str, add_auth_header: bool) -> Result<Bytes, ApiError<AiArenaApiError>> {
// static string, so the constructor should catch any parse errors
let url = Url::parse(url).map_err(ApiError::from)?;
let request = self
.client
.request(reqwest::Method::GET, url)
.header(reqwest::header::AUTHORIZATION, self.token_header())
.build()?;

let mut request_builder = self.client
.request(reqwest::Method::GET, url);

if add_auth_header {
request_builder = request_builder.header(reqwest::header::AUTHORIZATION, self.token_header())
}

let request = request_builder.build()?;

let response = self.client.execute(request).await?;

let status = response.status();
Expand Down
1 change: 1 addition & 0 deletions common/src/api/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub async fn stats_all(State(state): State<AppState>) -> Result<Json<Vec<Process
pub async fn shutdown(state: State<AppState>) -> Result<Json<TerminateResponse>, AppError> {
tracing::info!("Shutdown request received. Terminating all running processes");
let s = state.clone();

let _ = terminate_all(s, Json("graceful".to_string())).await?;

if let Err(e) = state.shutdown_sender.send(()).await {
Expand Down
1 change: 1 addition & 0 deletions common/src/configuration/ac_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct ACConfig {
pub temp_root: String,
pub validate_race: bool,
pub visualize: bool,
pub aws: bool
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize, Eq, PartialEq)]
Expand Down
2 changes: 2 additions & 0 deletions configs/arenaclient.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ BOT_CONT_2_HOST="127.0.0.1"
BOT_CONT_2_PORT=8082
SC2_CONT_HOST="127.0.0.1"
SC2_CONT_PORT=8083

AWS = false
4 changes: 3 additions & 1 deletion configs/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ BOT_CONT_1_PORT=8081
BOT_CONT_2_HOST="127.0.0.1"
BOT_CONT_2_PORT=8082
SC2_CONT_HOST="127.0.0.1"
SC2_CONT_PORT=8083
SC2_CONT_PORT=8083

AWS = false
2 changes: 2 additions & 0 deletions configs/local.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ VALIDATE_RACE = false
# Local
MATCHES_FILE=""
RESULTS_FILE=""

AWS = false
2 changes: 1 addition & 1 deletion kubernetes/overlays/staging/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data:
config.toml: |-
OLD_MATCH_DELETE_AFTER_MINUTES = 10
JOB_PREFIX= "staging"
WEBSITE_URL= "https://staging.aiarena.net"
WEBSITE_URL= "https://aiarena-test.net"
NAMESPACE= "arenaclients"
ARENACLIENTS_JSON_PATH = "arenaclients.json"
INTERVAL_SECONDS = 30
Expand Down
4 changes: 2 additions & 2 deletions proxy_controller/src/matches/sources/aiarena_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ impl HttpApiSource {
})?;
Ok(Self { api })
}
async fn download_map(&self, ai_match: &AiArenaMatch) -> Result<(), ApiError<AiArenaApiError>> {
async fn download_map(&self, ai_match: &AiArenaMatch, add_auth_header: bool) -> Result<(), ApiError<AiArenaApiError>> {
let map_url = &ai_match.map.file;
let map_name = &ai_match.map.name;
info!("Downloading map {}", map_name);
let map_bytes = self.api.download_map(map_url).await?;
let map_bytes = self.api.download_map(map_url, add_auth_header).await?;
let map_path = base_dir().join("maps").join(format!("{map_name}.SC2Map"));
let mut file = tokio::fs::File::create(map_path).await?;
Ok(file.write_all(&map_bytes).await?)
Expand Down
6 changes: 3 additions & 3 deletions proxy_controller/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn download_bot(
PlayerNum::One => current_match.bot1.bot_zip.clone(),
PlayerNum::Two => current_match.bot2.bot_zip.clone(),
};
api.download_zip(&download_url)
api.download_zip(&download_url, !settings.aws)
.await
.map_err(|e| AppError::Download(DownloadError::Other(e.to_string())))
}
Expand Down Expand Up @@ -133,7 +133,7 @@ pub async fn download_bot_data(
PlayerNum::One => current_match.bot1.bot_data.clone(),
PlayerNum::Two => current_match.bot2.bot_data.clone(),
} {
api.download_zip(&download_url)
api.download_zip(&download_url, !settings.aws)
.await
.map_err(|e| AppError::Download(DownloadError::Other(e.to_string())))
} else {
Expand Down Expand Up @@ -163,7 +163,7 @@ pub async fn download_map(State(state): State<Arc<RwLock<ProxyState>>>) -> Resul
)
.unwrap(); //Would've failed before this point already
let map_url = &current_match.map.file;
api.download_map(map_url)
api.download_map(map_url, !settings.aws)
.await
.map_err(|e| AppError::Download(DownloadError::Other(e.to_string())))
}

0 comments on commit 2531ca2

Please sign in to comment.