Skip to content

Commit

Permalink
Merge pull request #47 from Achiefs/46-package-template
Browse files Browse the repository at this point in the history
Include `index_template.json` inside package and apply minor fixes
  • Loading branch information
okynos authored May 19, 2022
2 parents 00d9b12 + 0a5a4c3 commit 4768f7f
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/code-testing-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
steps:
- name: Check out repository code, branch='${{ github.ref }}'
uses: actions/checkout@v2
- name: Create config folder
run: sudo mkdir -p /etc/fim/ && sudo chown -R runner:runner /etc/fim
- name: Create events file and folder
run: sudo mkdir -p /var/lib/fim/ && sudo touch /var/lib/fim/events.json && sudo chown -R runner:runner /var/lib/fim
- name: Create log file and folder
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/coverall.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
with:
toolchain: stable
override: true
- name: Create config folder
run: sudo mkdir -p /etc/fim/ && sudo chown -R runner:runner /etc/fim
- name: Create events file and folder
run: sudo mkdir -p /var/lib/fim/ && sudo touch /var/lib/fim/events.json && sudo chown -R runner:runner /var/lib/fim
- name: Create log file and folder
Expand Down
2 changes: 1 addition & 1 deletion pkg/deb/builder.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Copyright (C) 2021, Achiefs.
# Dependencies: curl, devscripts, equivs, tar, gcc, gzip
# Dependencies: curl, devscripts, equivs, tar, gcc, gzip, pkg-config, libssl-dev
set -ex

brand="fim"
Expand Down
2 changes: 1 addition & 1 deletion pkg/deb/debian/changelog
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fim (0.3.0-1) bionic; urgency=medium

* More info: https://github.com/Achiefs/fim/releases/tag/v0.3.0

-- Jose Fernandez <[email protected]> Tue, 17 May 2022 12:00:00 +0000
-- Jose Fernandez <[email protected]> Wed, 18 May 2022 22:00:00 +0000

fim (0.2.1-1) bionic; urgency=medium

Expand Down
1 change: 1 addition & 0 deletions pkg/deb/debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ override_dh_install:

install -m 0750 ${BUILD_DIR}/target/release/fim ${BIN_DIR}/
install -m 0640 ${BUILD_DIR}/config/linux/config.yml ${CONFIG_DIR}/
install -m 0640 ${BUILD_DIR}/config/index_template.json ${CONFIG_DIR}/
install -m 0640 ${BUILD_DIR}/pkg/deb/debian/copyright ${DOC_DIR}/
install -m 0644 ${BUILD_DIR}/pkg/fim.service ${SYS_DIR}/
gzip -9 ${BUILD_DIR}/pkg/deb/debian/changelog -c -n > ${DOC_DIR}/changelog.Debian.gz
Expand Down
3 changes: 3 additions & 0 deletions pkg/deb/repository/test/fim_0.3.0-1_amd64.deb
Git LFS file not shown
2 changes: 1 addition & 1 deletion pkg/rpm/builder.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

# Copyright (C) 2021, Achiefs.
# Dependencies: rpm-build, tar, gcc
# Dependencies: rpm-build, tar, gcc, openssl-devel
set -ex

brand="fim"
Expand Down
4 changes: 3 additions & 1 deletion pkg/rpm/fim.spec
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mkdir -p -m 640 ${RPM_BUILD_ROOT}/usr/share/man/man1

install -m 0750 target/release/fim ${RPM_BUILD_ROOT}%{_bindir}/
install -m 0640 config/linux/config.yml ${RPM_BUILD_ROOT}%{_configdir}/
install -m 0640 config/index_template.json ${RPM_BUILD_ROOT}%{_configdir}/
install -m 0644 pkg/fim.service ${RPM_BUILD_ROOT}/lib/systemd/system/
install -m 0644 pkg/fim.1 ${RPM_BUILD_ROOT}/usr/share/man/man1/

Expand Down Expand Up @@ -91,13 +92,14 @@ rm -fr %{buildroot}
%attr(750, root, root) %{_bindir}/fim
%dir %attr(750, root, root) %{_configdir}
%attr(640, root, root) %config(noreplace) %{_configdir}/config.yml
%attr(640, root, root) %{_configdir}/index_template.json
%attr(644, root, root) /lib/systemd/system/fim.service
%attr(644, root, root) /usr/share/man/man1/fim.1.gz

# -----------------------------------------------------------------------------

%changelog
* Tue May 17 2022 support <[email protected]> - 0.3.0
* Wed May 18 2022 support <[email protected]> - 0.3.0
- More info: https://github.com/Achiefs/fim/releases/tag/v0.3.0

* Tue Feb 25 2022 support <[email protected]> - 0.2.1
Expand Down
3 changes: 3 additions & 0 deletions pkg/rpm/repository/test/fim-0.3.0-1.x86_64.rpm
Git LFS file not shown
60 changes: 52 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ impl Config {
let insecure = match yaml[0]["events"]["endpoint"]["insecure"].as_bool() {
Some(value) => value,
None => {
println!("[WARN] events->endpoint->insecure not found in config.yml, using 'false'.");
false
if events_destination != *"file" {
println!("[WARN] events->endpoint->insecure not found in config.yml, using 'false'.");
false
}else{ false }
}
};

Expand Down Expand Up @@ -257,6 +259,8 @@ pub fn get_config_path(system: &str) -> String {
#[cfg(test)]
mod tests {
use super::*;
// To use files IO operations.
use std::{fs, env};

// ------------------------------------------------------------------------

Expand Down Expand Up @@ -303,10 +307,8 @@ mod tests {

#[test]
fn test_new_config_windows() {
let default_path = format!("./config/{}/config.yml", "windows");
let config = Config::new("windows");
assert_eq!(config.version, String::from(VERSION));
assert_eq!(config.path, default_path);
assert_eq!(config.events_destination, String::from("file"));
assert_eq!(config.endpoint_address, String::from("Not_used"));
assert_eq!(config.endpoint_user, String::from("Not_used"));
Expand All @@ -324,10 +326,8 @@ mod tests {

#[test]
fn test_new_config_linux() {
let default_path = format!("./config/{}/config.yml", "linux");
let config = Config::new("linux");
assert_eq!(config.version, String::from(VERSION));
assert_eq!(config.path, default_path);
assert_eq!(config.events_destination, String::from("file"));
assert_eq!(config.endpoint_address, String::from("Not_used"));
assert_eq!(config.endpoint_user, String::from("Not_used"));
Expand All @@ -345,10 +345,8 @@ mod tests {

#[test]
fn test_new_config_macos() {
let default_path = format!("./config/{}/config.yml", "macos");
let config = Config::new("macos");
assert_eq!(config.version, String::from(VERSION));
assert_eq!(config.path, default_path);
assert_eq!(config.events_destination, String::from("file"));
assert_eq!(config.endpoint_address, String::from("Not_used"));
assert_eq!(config.endpoint_user, String::from("Not_used"));
Expand Down Expand Up @@ -517,5 +515,51 @@ mod tests {
assert_eq!(get_config_path("windows"), default_path_windows);
assert_eq!(get_config_path("linux"), default_path_linux);
assert_eq!(get_config_path("macos"), default_path_macos);

let path = "./config.yml";
fs::rename(default_path_windows, path).unwrap();
assert_eq!(get_config_path("windows"), path);
fs::rename(path, default_path_windows).unwrap();

fs::rename(default_path_linux, path).unwrap();
assert_eq!(get_config_path("linux"), path);
fs::rename(path, default_path_linux).unwrap();

fs::rename(default_path_macos, path).unwrap();
assert_eq!(get_config_path("macos"), path);
fs::rename(path, default_path_macos).unwrap();

let relative_path_windows = "./../../config/windows";
let relative_config_windows = "./../../config/windows/config.yml";
let relative_path_linux = "./../../config/linux";
let relative_config_linux = "./../../config/linux/config.yml";
let relative_path_macos = "./../../config/macos";
let relative_config_macos = "./../../config/macos/config.yml";

fs::create_dir_all(relative_path_windows).unwrap();
fs::rename(default_path_windows, relative_config_windows).unwrap();
assert_eq!(get_config_path("windows"), relative_config_windows);
fs::rename(relative_config_windows, default_path_windows).unwrap();

fs::create_dir_all(relative_path_linux).unwrap();
fs::rename(default_path_linux, relative_config_linux).unwrap();
assert_eq!(get_config_path("linux"), relative_config_linux);
fs::rename(relative_config_linux, default_path_linux).unwrap();

fs::create_dir_all(relative_path_macos).unwrap();
fs::rename(default_path_macos, relative_config_macos).unwrap();
assert_eq!(get_config_path("macos"), relative_config_macos);
fs::rename(relative_config_macos, default_path_macos).unwrap();

fs::remove_dir_all("./../../config").unwrap();

if env::consts::OS == "linux" {
let linux_path = "/etc/fim";
let config_linux = "/etc/fim/config.yml";
fs::create_dir_all(linux_path).unwrap();
fs::rename(default_path_linux, config_linux).unwrap();
assert_eq!(get_config_path("linux"), config_linux);
fs::rename(config_linux, default_path_linux).unwrap();
}
}
}
4 changes: 0 additions & 4 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ pub fn get_checksum(file: String) -> String {
debug!("File Not found error ignoring...");
String::from("UNKNOWN")
},
ErrorKind::InvalidData => {
debug!("File data not valid ignoring...");
String::from("UNKNOWN")
},
_ => {
debug!("Error not handled: {:?}", e.kind());
String::from("UNKNOWN")
Expand Down
28 changes: 26 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ use tokio_util::codec::{BytesCodec, FramedRead};
use reqwest::{Client, Body};
use reqwest::header;
// To log the program process
use log::debug;
use log::{info, debug};
// To manage paths
use std::path::Path;

fn get_template_path() -> String {
let relative_path = "./../../config/index_template.json";
let config_path = "/etc/fim/index_template.json";
let default_path = "config/index_template.json";
if Path::new(default_path).exists() {
String::from(default_path)
}else if Path::new("./index_template.json").exists() {
String::from("./index_template.json")
}else if Path::new(relative_path).exists() {
String::from(relative_path)
}else{
String::from(config_path)
}
}

pub async fn create_index(name: String, address: String, user: String, pass: String, insecure: bool){
let file = File::open("config/index_template.json").await.unwrap();
let template_path = get_template_path();
info!("Loaded index template from: {}", template_path);
let file = File::open(template_path).await.unwrap();
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
let url = format!("{}/{}", address, name);
Expand Down Expand Up @@ -42,4 +61,9 @@ mod tests {
String::from("admin"), String::from("admin"), true) );
}

#[test]
fn test_get_template_path() {
assert_eq!(get_template_path(), "config/index_template.json");
}

}

0 comments on commit 4768f7f

Please sign in to comment.