Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ derive_builder = "0.12.0"
indexmap = { version = "2.0.0", features = ["serde"], optional = true }
serde_yaml = "0.9"
lapin = { version = "2.3.1", features = ["serde_json"] }
futures-lite = "1.13.0"
futures-lite = "2.2.0"
clap = { version = "4.4.8", features = ["derive"] }
brotli = "3.4.0"
serde_path_to_error = "0.1.14"
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ COPY --from=builder /app/target/release/server .
COPY --from=builder /app/.env .
COPY --from=builder /app/configuration.yaml .
COPY --from=builder /usr/local/cargo/bin/sqlx sqlx
COPY ./access_control.conf.dist /app

EXPOSE 8000

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


Stacker - is an application that helps users to create custom IT solutions based on dockerized open
source apps and user's custom applications docker containers. Users can build their own stack of applications, and
source apps and user's custom applications docker containers. Users can build their own project of applications, and
deploy the final result to their favorite clouds using TryDirect API.

Application development will include:
Expand Down Expand Up @@ -36,9 +36,9 @@ Stacker (API) - Serves API clients
Authentication made through TryDirect OAuth, here we have only client
Database (Read only)
Logging/Tracing (Files) / Quickwit for future
/stack (WebUI, as a result we have a JSON)
/stack/deploy -> sends deploy command to TryDirect Install service
/stack/deploy/status - get installation progress (rabbitmq client),
/project (WebUI, as a result we have a JSON)
/project/deploy -> sends deploy command to TryDirect Install service
/project/deploy/status - get installation progress (rabbitmq client),

#### TODO
Find out how to get user's token for queue
Expand Down Expand Up @@ -76,7 +76,7 @@ sqlx migrate revert

#### Deploy
```
curl -X POST -H "Content-Type: application/json" -d @custom-stack-payload-2.json http://127.0.0.1:8000/stack
curl -X POST -H "Content-Type: application/json" -d @custom-stack-payload-2.json http://127.0.0.1:8000/project
```


Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ volumes:
services:

stacker:
image: trydirect/stacker:0.0.6
image: trydirect/stacker:0.0.7
build: .
container_name: stacker
restart: always
volumes:
- ./files:/app/files
- ./docker/local/configuration.yaml:/app/configuration.yaml
- ./access_control.conf:/app/access_control.conf
- ./migrations:/app/migrations
- ./docker/local/.env:/app/.env
ports:
Expand Down
3 changes: 2 additions & 1 deletion docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ networks:
services:

stacker:
image: trydirect/stacker:0.0.5
image: trydirect/stacker:0.0.7
build: .
container_name: stacker
restart: always
volumes:
- ./stacker/files:/app/files
- ./configuration.yaml:/app/configuration.yaml
- ./access_control.conf:/app/access_control.conf
- ./migrations:/app/migrations
- ./.env:/app/.env
ports:
Expand Down
2 changes: 2 additions & 0 deletions docker/local/configuration.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
app_host: 0.0.0.0
app_port: 8000
auth_url: https://dev.try.direct/server/user/oauth_server/api/me
max_clients_number: 2

database:
host: 172.17.0.2
port: 5432
Expand Down
2 changes: 1 addition & 1 deletion migrations/20230903063840_creating_rating_tables.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
CREATE TYPE rate_category AS ENUM (
'application',
'cloud',
'stack',
'project',
'deploymentSpeed',
'documentation',
'design',
Expand Down
2 changes: 1 addition & 1 deletion migrations/20230905145525_creating_stack_tables.down.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
-- Add down migration script here
DROP TABLE user_stack;
DROP TABLE project;
11 changes: 6 additions & 5 deletions migrations/20230905145525_creating_stack_tables.up.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
CREATE TABLE user_stack (
CREATE TABLE project (
id serial4 NOT NULL,
stack_id uuid NOT NULL,
user_id VARCHAR(50) NOT NULL,
name TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
body JSON NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT user_stack_pkey PRIMARY KEY (id)
CONSTRAINT project_pkey PRIMARY KEY (id)
);

CREATE INDEX idx_stack_id ON user_stack(stack_id);
CREATE INDEX idx_stack_user_id ON user_stack(user_id);
CREATE INDEX idx_project_stack_id ON project(stack_id);
CREATE INDEX idx_project_user_id ON project(user_id);
CREATE INDEX idx_project_name ON project(name);
2 changes: 2 additions & 0 deletions migrations/20240228125751_creating_deployments.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add up migration script here
DROP table deployment;
14 changes: 14 additions & 0 deletions migrations/20240228125751_creating_deployments.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- Add up migration script here
CREATE TABLE deployment (
id serial4 NOT NULL,
project_id integer NOT NULL,
body JSON NOT NULL,
deleted BOOLEAN DEFAULT FALSE,
status VARCHAR(32) NOT NULL,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT fk_project FOREIGN KEY(project_id) REFERENCES project(id),
CONSTRAINT deployment_pkey PRIMARY KEY (id)
);

CREATE INDEX idx_deployment_project_id ON deployment(project_id);
2 changes: 2 additions & 0 deletions migrations/20240229072555_creating_cloud.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add down migration script here
DROP table cloud;
14 changes: 14 additions & 0 deletions migrations/20240229072555_creating_cloud.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE cloud (
id serial4 NOT NULL,
user_id VARCHAR(50) NOT NULL,
provider VARCHAR(50) NOT NULL,
cloud_token VARCHAR(255) ,
cloud_key VARCHAR(255),
cloud_secret VARCHAR(255),
save_token BOOLEAN DEFAULT FALSE,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT user_cloud_pkey PRIMARY KEY (id)
);

CREATE INDEX idx_deployment_user_cloud_user_id ON cloud(user_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add down migration script here
ALTER table project DROP COLUMN cloud_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add up migration script here
ALTER table project ADD COLUMN cloud_id INT CONSTRAINT project_cloud_id REFERENCES cloud(id) ON UPDATE CASCADE ON DELETE CASCADE;

3 changes: 3 additions & 0 deletions migrations/20240229080559_creating_cloud_server.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP INDEX idx_server_user_id;
DROP INDEX idx_server_cloud_id;
DROP table server;
22 changes: 22 additions & 0 deletions migrations/20240229080559_creating_cloud_server.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Add up migration script here

CREATE TABLE server (
id serial4 NOT NULL,
user_id VARCHAR(50) NOT NULL,
cloud_id integer NOT NULL,
project_id integer NOT NULL,
region VARCHAR(50) NOT NULL,
zone VARCHAR(50),
server VARCHAR(255) NOT NULL,
os VARCHAR(100) NOT NULL,
disk_type VARCHAR(100),
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL,
CONSTRAINT user_server_pkey PRIMARY KEY (id),
CONSTRAINT fk_server FOREIGN KEY(cloud_id) REFERENCES cloud(id),
CONSTRAINT fk_server_project FOREIGN KEY(project_id) REFERENCES project(id) ON UPDATE CASCADE ON DELETE CASCADE
);

CREATE INDEX idx_server_user_id ON server(user_id);
CREATE INDEX idx_server_cloud_id ON server(cloud_id);
CREATE INDEX idx_server_project_id ON server(project_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add down migration script here
ALTER table project DROP COLUMN request_json;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER table project ADD COLUMN request_json JSON NOT NULL DEFAULT '{}';
3 changes: 3 additions & 0 deletions migrations/20240307113718_alter_cloud_alter_project.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
ALTER table project ADD COLUMN cloud_id INT CONSTRAINT project_cloud_id REFERENCES cloud(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER table cloud DROP COLUMN project_id;
3 changes: 3 additions & 0 deletions migrations/20240307113718_alter_cloud_alter_project.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add up migration script here
ALTER table project DROP COLUMN cloud_id;
ALTER table cloud ADD COLUMN project_id INT CONSTRAINT cloud_project_id REFERENCES project(id) ON UPDATE CASCADE ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add down migration script here
DROP INDEX idx_server_cloud_id;
alter table server ADD column cloud_id integer NOT NULL;
2 changes: 2 additions & 0 deletions migrations/20240315143712_remove_cloud_id_from_server.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Add up migration script here
alter table server drop column cloud_id;
2 changes: 2 additions & 0 deletions src/console/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod appclient;
mod callable;
pub mod mq;

pub use callable::*;
pub use mq::*;
89 changes: 89 additions & 0 deletions src/console/commands/mq/listener.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::configuration::get_configuration;
use actix_web::rt;
use actix_web::web;
use lapin::{Channel, Queue};
use lapin::options::{BasicAckOptions, BasicConsumeOptions};
use lapin::types::FieldTable;
use sqlx::PgPool;
use db::deployment;
use crate::{db, helpers};
use crate::helpers::mq_manager;
use crate::helpers::mq_manager::MqManager;
use futures_lite::stream::StreamExt;

pub struct ListenCommand {
}

impl ListenCommand {
pub fn new() -> Self {
Self {}
}
}

impl crate::console::commands::CallableTrait for ListenCommand {

fn call(&self) -> Result<(), Box<dyn std::error::Error>> {
rt::System::new().block_on(async {
let settings = get_configuration().expect("Failed to read configuration.");
let db_pool = PgPool::connect(&settings.database.connection_string())
.await
.expect("Failed to connect to database.");

let db_pool = web::Data::new(db_pool);

let mq_manager = MqManager::try_new(settings.amqp.connection_string())?;
let consumer_channel= mq_manager
.consume(
"install_progress",
"install_progress_*******"
)
.await?;


let mut consumer = consumer_channel
.basic_consume(
"install_progress",
"console_listener",
BasicConsumeOptions::default(),
FieldTable::default(),
)
.await
.expect("Basic consume");

// .map_err(|err| format!("Error {:?}", err));

tracing::info!("will consume");
// if let Ok(consumer) = consumer {
while let Some(delivery) = consumer.next().await {
let delivery = delivery.expect("error in consumer");
delivery.ack(BasicAckOptions::default()).await.expect("ack");
}
// }

// while let Some(delivery) = consumer.next().await {
// tracing::debug!(message=?delivery, "received message");
// if let Ok(delivery) = delivery {
// delivery
// .ack(BasicAckOptions::default())
// .await
// .expect("basic_ack");
// }
// }


// on_complete()
// let deployment = crate::models::deployment::Deployment {
// id: 0,
// project_id: 0,
// deleted: false,
// status: "".to_string(),
// body: Default::default(),
// created_at: Default::default(),
// updated_at: Default::default(),
// };
// deployment::update(db_pool.get_ref(), deployment).await?;

Ok(())
})
}
}
2 changes: 2 additions & 0 deletions src/console/commands/mq/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod listener;
pub use listener::*;
15 changes: 15 additions & 0 deletions src/console/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ enum Commands {
#[command(subcommand)]
command: AppClientCommands,
},
MQ {
#[command(subcommand)]
command: AppMqCommands,
}
}

#[derive(Debug, Subcommand)]
Expand All @@ -22,6 +26,12 @@ enum AppClientCommands {
},
}

#[derive(Debug, Subcommand)]
enum AppMqCommands {
Listen {
},
}

//todo add documentation about how to add a new command
//todo the helper from console should have a nicer display

Expand All @@ -38,6 +48,11 @@ fn get_command(cli: Cli) -> Result<Box<dyn stacker::console::commands::CallableT
stacker::console::commands::appclient::NewCommand::new(user_id),
)),
},
Commands::MQ { command} => match command {
AppMqCommands::Listen {} => Ok(Box::new(
stacker::console::commands::mq::ListenCommand::new(),
)),
},
_ => Err("command does not match".to_string()),
}
}
2 changes: 1 addition & 1 deletion src/db/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn update(pool: &PgPool, client: models::Client) -> Result<models::Cli
.instrument(query_span)
.await
.map(|_|{
tracing::info!("Client {} have been saved to database", client.id);
tracing::info!("Client {} has been saved to the database", client.id);
client
})
.map_err(|err| {
Expand Down
Loading