Skip to content

Commit

Permalink
Merge branch 'nightly' into release/nightly/bin/nanocld/0.15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s authored Jun 5, 2024
2 parents 56fae2f + f570dc7 commit dcfe5a9
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 94 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: E2E Tests

on:
push:
branches: ["nightly"]
pull_request:
branches: ["nightly"]
workflow_dispatch: {}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/nightly' }}

env:
CARGO_TERM_COLOR: always

jobs:
ubuntu_e2e_test:
strategy:
fail-fast: true
runs-on: ubuntu-latest

steps:
# Clone project
- uses: actions/checkout@v3
# Install buildx
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v2
# Set buildx cache
- name: Cache register
uses: actions/cache@v3
with:
path: ~/buildx-cache
key: buildx-cache
# Install cargo make
- uses: davidB/rust-cargo-make@v1
- name: Install bats
run: |
sudo apt-get update
sudo apt-get install -y bats
# Build nightly image and install nanocl
- name: Prepare CI
run: |
sh ./scripts/build_images.sh
NANOCL_CHANNEL=nightly cargo build --release --bin nanocl
sudo cp target/release/nanocl /usr/bin/nanocl
sudo chmod +x /usr/bin/nanocl
sudo groupadd nanocl
sudo usermod -aG nanocl $USER
newgrp nanocl
nanocl install -t installer.yml
sleep 20
docker ps -a
docker logs ndaemon.system.c
sudo chmod -R 777 /var/lib/nanocl
sudo chmod -R 777 /run/nanocl
nanocl version
# Run E2E tests
- name: E2E
run: |
bats ./tests/e2e.bats
27 changes: 27 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ jobs:
key: buildx-cache
# Install cargo make
- uses: davidB/rust-cargo-make@v1
# Debug ci with ngrok and ssh
# - name: Download ngrok
# run: curl -sO https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-linux-amd64.tgz
# shell: bash
# - name: Unzip ngrok
# run: tar -xf ngrok-v3-stable-linux-amd64.tgz
# shell: bash
# - name: Add ~/.ssh directory
# run: mkdir -p ~/.ssh
# shell: bash
# - name: Add SSH public key to authorized_keys
# run: echo "${{ secrets.WORKFLOW_SSH_PUB_KEY }}" >> ~/.ssh/authorized_keys
# shell: bash
# - name: Fix home directory permissions
# run: chmod 755 ~
# shell: bash
# - run: chmod 600 ~/.ssh/authorized_keys
# shell: bash
# - name: Set ngrok auth token
# run: ./ngrok authtoken ${{ secrets.NGROK_TOKEN }}
# shell: bash
# - name: Debug message
# run: echo "Starting ngrok tunnel..."
# shell: bash
# - name: Setup ngrok tunnel
# run: timeout 1h ./ngrok tcp 22 &
# shell: bash
# Cache Rust
- uses: Swatinem/rust-cache@v2
with:
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"ndns",
"newgrp",
"nexthat",
"nhnr",
"nmetrics",
"nographic",
"nproxy",
Expand Down
16 changes: 8 additions & 8 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions bin/nanocl/src/commands/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,13 @@ async fn exec_cargo_revert(
opts: &CargoRevertOpts,
) -> IoResult<()> {
let client = &cli_conf.client;
let waiter =
wait_cargo_state(&opts.name, args, NativeEventAction::Start, client)
.await?;
let cargo = client
.revert_cargo(&opts.name, &opts.history_id, args.namespace.as_deref())
.await?;
waiter.await??;
utils::print::print_yml(cargo)?;
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions bin/nanocl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,14 @@ mod tests {
.list_history_cargo(CARGO_NAME, None)
.await
.unwrap()
.first()
.last()
.unwrap()
.clone();
assert_cli_ok!("cargo", "revert", CARGO_NAME, &history.key.to_string());
// Try to stop a cargo
assert_cli_ok!("cargo", "stop", CARGO_NAME);
// Try to remove cargo
assert_cli_ok!("cargo", "rm", "-y", CARGO_NAME);
assert_cli_ok!("cargo", "rm", "-fy", CARGO_NAME);
}

/// Test state file when then include other state files
Expand Down
2 changes: 1 addition & 1 deletion bin/nanocld/specs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The notable exception is running `Cargo`, which consists of several `API` calls.
## OpenAPI Specification
This API is documented in **OpenAPI format** using [Utoipa](https://github.com/juhaku/utoipa)<br />
The specification is generated automatically when running in development only.<br />
When releasing a version, the generated file is transfered to our [Documentation](https://github.com/next-hat/documentation).
When releasing a version, the generated file is transferred to our [Documentation](https://github.com/next-hat/documentation).


## Cross-Origin Resource Sharing
Expand Down
6 changes: 5 additions & 1 deletion bin/nanocld/specs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ info:
## OpenAPI Specification
This API is documented in **OpenAPI format** using [Utoipa](https://github.com/juhaku/utoipa)<br />
The specification is generated automatically when running in development only.<br />
When releasing a version, the generated file is transfered to our [Documentation](https://github.com/next-hat/documentation).
When releasing a version, the generated file is transferred to our [Documentation](https://github.com/next-hat/documentation).
## Cross-Origin Resource Sharing
Expand Down Expand Up @@ -7200,6 +7200,10 @@ components:
type: boolean
description: Disable logging for this target
nullable: true
Ssl:
allOf:
- $ref: '#/components/schemas/ProxySsl'
nullable: true
additionalProperties: false
UriTarget:
type: object
Expand Down
14 changes: 10 additions & 4 deletions bin/nanocld/src/objects/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,16 @@ impl ObjPatchByPk for CargoDb {
cmd,
image,
env: Some(env_vars),
host_config: Some(HostConfig {
binds: Some(volumes),
..cargo.spec.container.host_config.unwrap_or_default()
}),
host_config: if !volumes.is_empty()
|| cargo.spec.container.host_config.is_some()
{
Some(HostConfig {
binds: Some(volumes),
..cargo.spec.container.host_config.unwrap_or_default()
})
} else {
None
},
..cargo.spec.container
}
} else {
Expand Down
94 changes: 48 additions & 46 deletions bin/nanocld/src/system/docker_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ use nanocl_stubs::system::{
};

use crate::{
vars,
models::{
CargoDb, ObjPsStatusDb, ProcessDb, ProcessUpdateDb, SystemState, VmDb,
},
repositories::generic::*,
models::{ObjPsStatusDb, ProcessDb, ProcessUpdateDb, SystemState},
vars,
};

/// Take actions when a docker event is received
Expand Down Expand Up @@ -104,50 +106,50 @@ async fn exec_docker(
_ => {}
}
}
// "die" => {
// let actual_status =
// ObjPsStatusDb::read_by_pk(&kind_key, &state.inner.pool).await?;
// match (&kind, &actual_status.wanted) {
// (EventActorKind::Cargo, status)
// if status != &ObjPsStatusKind::Stop.to_string()
// || status != &ObjPsStatusKind::Start.to_string() =>
// {
// ObjPsStatusDb::update_actual_status(
// &kind_key,
// &ObjPsStatusKind::Fail,
// &state.inner.pool,
// )
// .await?;
// let cargo =
// CargoDb::transform_read_by_pk(&kind_key, &state.inner.pool).await?;
// state.emit_warning_native_action(
// &cargo,
// NativeEventAction::Fail,
// Some(format!("Process {name}")),
// );
// }
// (EventActorKind::Vm, status)
// if status != &ObjPsStatusKind::Stop.to_string()
// || status != &ObjPsStatusKind::Start.to_string() =>
// {
// ObjPsStatusDb::update_actual_status(
// &kind_key,
// &ObjPsStatusKind::Fail,
// &state.inner.pool,
// )
// .await?;
// let vm =
// VmDb::transform_read_by_pk(&kind_key, &state.inner.pool).await?;
// state.emit_warning_native_action(
// &vm,
// NativeEventAction::Fail,
// Some(format!("Process {name}")),
// );
// }
// _ => {}
// }
// action.clone_into(&mut event.action);
// }
"die" => {
let actual_status =
ObjPsStatusDb::read_by_pk(&kind_key, &state.inner.pool).await?;
match (&kind, &actual_status.wanted) {
(EventActorKind::Cargo, status)
if status != &ObjPsStatusKind::Stop.to_string()
|| status != &ObjPsStatusKind::Start.to_string() =>
{
ObjPsStatusDb::update_actual_status(
&kind_key,
&ObjPsStatusKind::Fail,
&state.inner.pool,
)
.await?;
let cargo =
CargoDb::transform_read_by_pk(&kind_key, &state.inner.pool).await?;
state.emit_warning_native_action(
&cargo,
NativeEventAction::Fail,
Some(format!("Process {name}")),
);
}
(EventActorKind::Vm, status)
if status != &ObjPsStatusKind::Stop.to_string()
|| status != &ObjPsStatusKind::Start.to_string() =>
{
ObjPsStatusDb::update_actual_status(
&kind_key,
&ObjPsStatusKind::Fail,
&state.inner.pool,
)
.await?;
let vm =
VmDb::transform_read_by_pk(&kind_key, &state.inner.pool).await?;
state.emit_warning_native_action(
&vm,
NativeEventAction::Fail,
Some(format!("Process {name}")),
);
}
_ => {}
}
action.clone_into(&mut event.action);
}
"destroy" => {
state.spawn_emit_event(event);
let _ = ProcessDb::del_by_pk(&id, &state.inner.pool).await;
Expand Down
19 changes: 5 additions & 14 deletions bin/nanocld/src/system/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn stopping(
/// and push the action into the task manager
/// The task manager will execute the action in background
/// eg: starting, deleting, updating a living object
async fn _exec_event(e: &Event, state: &SystemState) -> IoResult<()> {
pub async fn exec_event(e: &Event, state: &SystemState) -> IoResult<()> {
match e.kind {
EventKind::Error | EventKind::Warning => return Ok(()),
_ => {}
Expand All @@ -232,11 +232,12 @@ async fn _exec_event(e: &Event, state: &SystemState) -> IoResult<()> {
// This is to avoid data races conditions when manipulating an object
let task_key = format!("{}@{key}", &actor.kind);
let action = NativeEventAction::from_str(e.action.as_str())?;
match actor.kind {
EventActorKind::Cargo | EventActorKind::Vm => {
match (&actor.kind, &action) {
(EventActorKind::Cargo | EventActorKind::Vm, _) => {
state.inner.task_manager.wait_task(&task_key).await;
}
EventActorKind::Job if action == NativeEventAction::Destroying => {
(EventActorKind::Job, NativeEventAction::Destroying) => {
log::debug!("Removing task for job {key}");
state.inner.task_manager.remove_task(&task_key).await;
}
_ => {}
Expand Down Expand Up @@ -273,13 +274,3 @@ async fn _exec_event(e: &Event, state: &SystemState) -> IoResult<()> {
.await;
Ok(())
}

pub fn exec_event(e: &Event, state: &SystemState) {
let e = e.clone();
let state = state.clone();
rt::spawn(async move {
if let Err(err) = _exec_event(&e, &state).await {
log::error!("exec_event: {err}");
}
});
}
10 changes: 6 additions & 4 deletions bin/nanocld/src/system/system_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ impl SystemState {
self.inner.arbiter.clone().exec_fn(move || {
rt::spawn(async move {
while let Some(e) = rx.next().await {
super::exec_event(&e, &self);
let self_ptr = self.clone();
if let Err(err) = super::exec_event(&e, &self).await {
log::error!("system::run: exec_event {err}");
}
let event_emitter_raw = self.inner.event_emitter_raw.clone();
rt::spawn(async move {
if let Err(err) = self_ptr.inner.event_emitter_raw.emit(&e) {
log::error!("system::run: raw emit {err}");
if let Err(err) = event_emitter_raw.emit(&e) {
log::error!("system::run: emit {err}");
}
});
}
Expand Down
Loading

0 comments on commit dcfe5a9

Please sign in to comment.