feat: enable upload of templates
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-03-30 01:21:41 +01:00
parent bf9fb6d72c
commit 768ec95670
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
6 changed files with 538 additions and 1108 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
DRONE_HOST=https://ci.i.kjuulh.io
DRONE_USER=kjuulh

1402
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,3 +10,6 @@ tracing.workspace = true
tracing-subscriber.workspace = true
clap.workspace = true
dotenv.workspace = true
reqwest = { version = "0.12.2", features = ["json"]}
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"

View File

@ -1,4 +1,6 @@
use anyhow::Context;
use std::path::PathBuf;
use anyhow::anyhow;
use clap::{Parser, Subcommand};
#[derive(Parser)]
@ -10,7 +12,19 @@ struct Command {
#[derive(Subcommand)]
enum Commands {
Hello {},
Upload {
#[arg(long = "template")]
template: String,
#[arg(long = "drone-token", env = "DRONE_TOKEN")]
drone_token: String,
#[arg(long = "drone-host", env = "DRONE_HOST")]
drone_host: String,
#[arg(long = "drone-user", env = "DRONE_USER")]
drone_user: String,
},
}
#[tokio::main]
@ -19,10 +33,86 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let cli = Command::parse();
tracing::debug!("Starting cli");
tracing::debug!("starting cli");
if let Some(Commands::Hello { }) = cli.command {
println!("Hello!")
if let Some(Commands::Upload {
template,
drone_token,
drone_host,
drone_user,
}) = cli.command
{
tracing::info!(template, drone_host, "executing drone upload");
let template_path = PathBuf::from(&template);
if !template_path.exists() {
anyhow::bail!("template: {} doesn't exist", template)
}
let template_name = template_path
.file_name()
.ok_or(anyhow!(
"template: {} didn't include a proper filename",
&template
))?
.to_str()
.expect("filename to be utf8");
let template_data = tokio::fs::read_to_string(&template_path).await?;
let template_url = format!(
"{}/api/templates/{}/{}",
drone_host.trim_end_matches('/'),
drone_user,
template_name
);
let client = reqwest::Client::new();
let res = client
.put(&template_url)
.bearer_auth(&drone_token)
.json(&serde_json::json!({
"data": &template_data,
}))
.send()
.await?;
if let Err(e) = res.error_for_status() {
match e.status() {
Some(reqwest::StatusCode::NOT_FOUND) => {
tracing::debug!("template was not found attemping creation instead");
let template_url = format!(
"{}/api/templates/{}",
drone_host.trim_end_matches('/'),
drone_user,
);
let res = client
.post(&template_url)
.bearer_auth(&drone_token)
.json(&serde_json::json!({
"name": &template_name,
"data": &template_data,
}))
.send()
.await?;
if let Err(e) = res.error_for_status() {
anyhow::bail!(e)
}
tracing::debug!("template creation successful");
}
Some(status) => {
anyhow::bail!("error: {}, failed to upload template: {}", e, status)
}
None => {
anyhow::bail!(e)
}
}
}
tracing::info!(template, "successfully updated template");
}
Ok(())

View File

@ -1,15 +0,0 @@
version: "3"
services:
crdb:
restart: 'always'
image: 'cockroachdb/cockroach:v23.1.14'
command: 'start-single-node --advertise-addr 0.0.0.0 --insecure'
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health?ready=1"]
interval: '10s'
timeout: '30s'
retries: 5
start_period: '20s'
ports:
- 8080:8080
- '26257:26257'

View File

@ -0,0 +1,124 @@
kind: pipeline
name: default
type: docker
steps:
- name: load_secret
image: debian:buster-slim
volumes:
- name: ssh
path: /root/.ssh/
environment:
SSH_KEY:
from_secret: gitea_id_ed25519
commands:
- mkdir -p $HOME/.ssh/
- echo "$SSH_KEY" | base64 -d > $HOME/.ssh/id_ed25519
- chmod -R 600 ~/.ssh
- |
cat >$HOME/.ssh/config <<EOL
Host git.front.kjuulh.io
IdentityFile $HOME/.ssh/id_ed25519
IdentitiesOnly yes
UserKnownHostsFile=/dev/null
StrictHostKeyChecking no
EOL
- chmod 700 ~/.ssh/config
- name: build pr
image: kasperhermansen/cuddle-rust-cli-plan:main-1711754276
pull: always
volumes:
- name: ssh
path: /root/.ssh/
commands:
- eval `ssh-agent`
- ssh-add
- echo "$DOCKER_PASSWORD" | docker login --password-stdin --username="$DOCKER_USERNAME" docker.io
- export CLUSTER=clank-dev
- cuddle-rust-cli-plan pr
environment:
REGISTRY_CACHE_USERNAME:
from_secret: registry_cache_username
REGISTRY_CACHE_PASSWORD:
from_secret: registry_cache_password
REGISTRY_CACHE_TOKEN:
from_secret: registry_cache_token
REGISTRY_CACHE_url:
from_secret: registry_cache_url
DOCKER_BUILDKIT: 1
DOCKER_PASSWORD:
from_secret: docker_password
DOCKER_USERNAME:
from_secret: docker_username
CUDDLE_SECRETS_PROVIDER: 1password
CUDDLE_ONE_PASSWORD_DOT_ENV: ".env.ci"
CUDDLE_SSH_AGENT: "true"
GIT_PASSWORD:
from_secret: git_password
CI_PREFIX: "/mnt/ci/ci"
DOCKER_HOST: "tcp://192.168.1.233:2376"
CUDDLE_PLEASE_TOKEN:
from_secret: cuddle_please_token
OP_SERVICE_ACCOUNT_TOKEN:
from_secret: op_service_account_token
when:
event:
- pull_request
exclude:
- main
- master
depends_on:
- "load_secret"
- name: build main
image: kasperhermansen/cuddle-rust-cli-plan:main-1711754276
pull: always
volumes:
- name: ssh
path: /root/.ssh/
commands:
- eval `ssh-agent`
- ssh-add
- echo "$DOCKER_PASSWORD" | docker login --password-stdin --username="$DOCKER_USERNAME" docker.io
- export CLUSTER=clank-prod
- cuddle-rust-cli-plan main
environment:
REGISTRY_CACHE_USERNAME:
from_secret: registry_cache_username
REGISTRY_CACHE_PASSWORD:
from_secret: registry_cache_password
REGISTRY_CACHE_TOKEN:
from_secret: registry_cache_token
REGISTRY_CACHE_url:
from_secret: registry_cache_url
DOCKER_BUILDKIT: 1
DOCKER_PASSWORD:
from_secret: docker_password
DOCKER_USERNAME:
from_secret: docker_username
CUDDLE_SECRETS_PROVIDER: 1password
CUDDLE_ONE_PASSWORD_DOT_ENV: ".env.ci"
CUDDLE_SSH_AGENT: "true"
GIT_PASSWORD:
from_secret: git_password
CI_PREFIX: "/mnt/ci/ci"
DOCKER_HOST: "tcp://192.168.1.233:2376"
CUDDLE_PLEASE_TOKEN:
from_secret: cuddle_please_token
OP_SERVICE_ACCOUNT_TOKEN:
from_secret: op_service_account_token
when:
event:
- push
branch:
- main
- master
exclude:
- pull_request
depends_on:
- "load_secret"
volumes:
- name: ssh
temp: {}