Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eng 486 update deployer with runtime changes #696

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions codegen/src/shuttle_main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ impl ToTokens for Loader {
};

let loader = quote! {
async fn loader<S: shuttle_runtime::StorageManager>(
mut #factory_ident: shuttle_runtime::ProvisionerFactory<S>,
async fn loader(
mut #factory_ident: shuttle_runtime::ProvisionerFactory,
logger: shuttle_runtime::Logger,
) -> #return_type {
use shuttle_service::Context;
Expand Down Expand Up @@ -299,8 +299,8 @@ mod tests {

let actual = quote!(#input);
let expected = quote! {
async fn loader<S: shuttle_runtime::StorageManager>(
mut _factory: shuttle_runtime::ProvisionerFactory<S>,
async fn loader(
mut _factory: shuttle_runtime::ProvisionerFactory,
logger: shuttle_runtime::Logger,
) -> ShuttleSimple {
use shuttle_service::Context;
Expand Down Expand Up @@ -379,8 +379,8 @@ mod tests {

let actual = quote!(#input);
let expected = quote! {
async fn loader<S: shuttle_runtime::StorageManager>(
mut factory: shuttle_runtime::ProvisionerFactory<S>,
async fn loader(
mut factory: shuttle_runtime::ProvisionerFactory,
logger: shuttle_runtime::Logger,
) -> ShuttleComplex {
use shuttle_service::Context;
Expand Down Expand Up @@ -494,8 +494,8 @@ mod tests {

let actual = quote!(#input);
let expected = quote! {
async fn loader<S: shuttle_runtime::StorageManager>(
mut factory: shuttle_runtime::ProvisionerFactory<S>,
async fn loader(
mut factory: shuttle_runtime::ProvisionerFactory,
logger: shuttle_runtime::Logger,
) -> ShuttleComplex {
use shuttle_service::Context;
Expand Down
24 changes: 12 additions & 12 deletions common/src/storage_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::{fs, io, path::PathBuf};

use uuid::Uuid;

pub trait StorageManager: Clone + Sync + Send {
pub trait StorageManager: Sync + Send {
/// Path for a specific service build files
fn service_build_path<S: AsRef<str>>(&self, service_name: S) -> Result<PathBuf, io::Error>;
fn service_build_path(&self, service_name: String) -> Result<PathBuf, io::Error>;

/// Path to folder for storing deployment files
fn deployment_storage_path<S: AsRef<str>>(
fn deployment_storage_path(
&self,
service_name: S,
oddgrd marked this conversation as resolved.
Show resolved Hide resolved
service_name: String,
deployment_id: &Uuid,
) -> Result<PathBuf, io::Error>;
}
Expand Down Expand Up @@ -58,21 +58,21 @@ impl ArtifactsStorageManager {
}

impl StorageManager for ArtifactsStorageManager {
fn service_build_path<S: AsRef<str>>(&self, service_name: S) -> Result<PathBuf, io::Error> {
let builds_path = self.builds_path()?.join(service_name.as_ref());
fn service_build_path(&self, service_name: String) -> Result<PathBuf, io::Error> {
let builds_path = self.builds_path()?.join(service_name.as_str());
fs::create_dir_all(&builds_path)?;

Ok(builds_path)
}

fn deployment_storage_path<S: AsRef<str>>(
fn deployment_storage_path(
&self,
service_name: S,
service_name: String,
deployment_id: &Uuid,
) -> Result<PathBuf, io::Error> {
let storage_path = self
.storage_path()?
.join(service_name.as_ref())
.join(service_name.as_str())
.join(deployment_id.to_string());
fs::create_dir_all(&storage_path)?;

Expand All @@ -93,13 +93,13 @@ impl WorkingDirStorageManager {
}

impl StorageManager for WorkingDirStorageManager {
fn service_build_path<S: AsRef<str>>(&self, _service_name: S) -> Result<PathBuf, io::Error> {
fn service_build_path(&self, _service_name: String) -> Result<PathBuf, io::Error> {
Ok(self.working_dir.clone())
}

fn deployment_storage_path<S: AsRef<str>>(
fn deployment_storage_path(
&self,
_service_name: S,
_service_name: String,
_deployment_id: &Uuid,
) -> Result<PathBuf, io::Error> {
Ok(self.working_dir.clone())
Expand Down
2 changes: 1 addition & 1 deletion deployer/src/deployment/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Queued {
) -> Result<Built> {
info!("Extracting received data");

let project_path = storage_manager.service_build_path(&self.service_name)?;
let project_path = storage_manager.service_build_path(self.service_name.clone())?;

extract_tar_gz_data(self.data.as_slice(), &project_path).await?;

Expand Down
1 change: 0 additions & 1 deletion deployer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub async fn start(
args.project,
)
.await;
let make_service = router.into_make_service();

info!(address=%args.api_address, "Binding to and listening at address");

Expand Down
32 changes: 19 additions & 13 deletions runtime/src/legacy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
net::{Ipv4Addr, SocketAddr},
ops::DerefMut,
str::FromStr,
sync::Mutex,
sync::{Arc, Mutex},
time::Duration,
};

Expand All @@ -14,7 +14,7 @@ use clap::Parser;
use core::future::Future;
use shuttle_common::{
backends::{auth::ClaimLayer, tracing::InjectPropagationLayer},
storage_manager::{StorageManager, WorkingDirStorageManager},
storage_manager::{ArtifactsStorageManager, StorageManager, WorkingDirStorageManager},
LogItem,
};
use shuttle_proto::{
Expand Down Expand Up @@ -47,21 +47,28 @@ use self::args::Args;

mod args;

pub async fn start(
loader: impl Loader<ProvisionerFactory<WorkingDirStorageManager>> + Send + 'static,
) {
pub async fn start(loader: impl Loader<ProvisionerFactory> + Send + 'static) {
let args = Args::parse();
let addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), args.port);

let provisioner_address = args.provisioner_address;
let mut server_builder =
Server::builder().http2_keepalive_interval(Some(Duration::from_secs(60)));

let storage_manager: Arc<dyn StorageManager> = match args.storage_manager_type {
oddgrd marked this conversation as resolved.
Show resolved Hide resolved
args::StorageManagerType::Artifacts => {
Arc::new(ArtifactsStorageManager::new(args.storage_manager_path))
}
args::StorageManagerType::WorkingDir => {
Arc::new(WorkingDirStorageManager::new(args.storage_manager_path))
}
};

let router = {
let legacy = Legacy::new(
provisioner_address,
loader,
WorkingDirStorageManager::new(args.storage_manager_path),
storage_manager,
Environment::Local,
);

Expand All @@ -72,24 +79,24 @@ pub async fn start(
router.serve(addr).await.unwrap();
}

pub struct Legacy<L, M, S> {
pub struct Legacy<L, S> {
// Mutexes are for interior mutability
logs_rx: Mutex<Option<UnboundedReceiver<LogItem>>>,
logs_tx: UnboundedSender<LogItem>,
stopped_tx: Sender<(StopReason, String)>,
provisioner_address: Endpoint,
kill_tx: Mutex<Option<oneshot::Sender<String>>>,
storage_manager: M,
storage_manager: Arc<dyn StorageManager>,
loader: Mutex<Option<L>>,
service: Mutex<Option<S>>,
env: Environment,
}

impl<L, M, S> Legacy<L, M, S> {
impl<L, S> Legacy<L, S> {
pub fn new(
provisioner_address: Endpoint,
loader: L,
storage_manager: M,
storage_manager: Arc<dyn StorageManager>,
env: Environment,
) -> Self {
let (tx, rx) = mpsc::unbounded_channel();
Expand Down Expand Up @@ -143,10 +150,9 @@ where
}

#[async_trait]
impl<L, M, S> Runtime for Legacy<L, M, S>
impl<L, S> Runtime for Legacy<L, S>
where
M: StorageManager + Send + Sync + 'static,
L: Loader<ProvisionerFactory<M>, Service = S> + Send + 'static,
L: Loader<ProvisionerFactory, Service = S> + Send + 'static,
S: Service + Send + 'static,
{
async fn load(&self, request: Request<LoadRequest>) -> Result<Response<LoadResponse>, Status> {
Expand Down
25 changes: 8 additions & 17 deletions runtime/src/provisioner_factory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::BTreeMap, path::PathBuf};
use std::{collections::BTreeMap, path::PathBuf, sync::Arc};

use async_trait::async_trait;
use shuttle_common::{
Expand All @@ -16,29 +16,23 @@ use tracing::{debug, info, trace};
use uuid::Uuid;

/// A factory (service locator) which goes through the provisioner crate
pub struct ProvisionerFactory<S>
where
S: StorageManager,
{
pub struct ProvisionerFactory {
service_name: ServiceName,
deployment_id: Uuid,
storage_manager: S,
storage_manager: Arc<dyn StorageManager>,
provisioner_client: ProvisionerClient<ClaimService<InjectPropagation<Channel>>>,
info: Option<DatabaseReadyInfo>,
secrets: BTreeMap<String, String>,
env: Environment,
}

impl<S> ProvisionerFactory<S>
where
S: StorageManager,
{
impl ProvisionerFactory {
pub(crate) fn new(
provisioner_client: ProvisionerClient<ClaimService<InjectPropagation<Channel>>>,
service_name: ServiceName,
deployment_id: Uuid,
secrets: BTreeMap<String, String>,
storage_manager: S,
storage_manager: Arc<dyn StorageManager>,
env: Environment,
) -> Self {
Self {
Expand All @@ -54,10 +48,7 @@ where
}

#[async_trait]
impl<S> Factory for ProvisionerFactory<S>
where
S: StorageManager + Sync + Send,
{
impl Factory for ProvisionerFactory {
async fn get_db_connection_string(
&mut self,
db_type: database::Type,
Expand Down Expand Up @@ -103,13 +94,13 @@ where

fn get_build_path(&self) -> Result<PathBuf, shuttle_service::Error> {
self.storage_manager
.service_build_path(self.service_name.as_str())
.service_build_path(self.service_name.to_string())
.map_err(Into::into)
}

fn get_storage_path(&self) -> Result<PathBuf, shuttle_service::Error> {
self.storage_manager
.deployment_storage_path(self.service_name.as_str(), &self.deployment_id)
.deployment_storage_path(self.service_name.to_string(), &self.deployment_id)
.map_err(Into::into)
}

Expand Down