[go: up one dir, main page]

Skip to content

Commit

Permalink
Add a binary rust example module
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Jun 30, 2018
1 parent bedca74 commit 4585ea1
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rust-module-hello-world/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: all clean rust

all: rust

clean:
rm -f library/rust_helloworld
cd module-src && \
cargo clean

rust:
cd module-src && \
cargo build && \
cp -v target/debug/helloworld ../library/rust_helloworld
3 changes: 3 additions & 0 deletions rust-module-hello-world/library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!/.gitignore
/.*
/*
85 changes: 85 additions & 0 deletions rust-module-hello-world/module-src/Cargo.lock

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

9 changes: 9 additions & 0 deletions rust-module-hello-world/module-src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "helloworld"
version = "0.1.0"
authors = ["Sviatoslav Sydorenko <wk+ansible-github@sydorenko.org.ua>"]

[dependencies]
serde = "1.0.66"
serde_derive = "1.0.66"
serde_json = "1.0.20"
107 changes: 107 additions & 0 deletions rust-module-hello-world/module-src/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
extern crate serde;
extern crate serde_json;

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::process;

#[macro_use]
extern crate serde_derive;

use serde_json::Error;


fn default_name_arg() -> String {
String::from("World")
}


#[derive(Serialize, Deserialize)]
struct ModuleArgs {
#[serde(default = "default_name_arg")]
name: String,
}


#[derive(Clone, Serialize, Deserialize)]
struct Response {
msg: String,
changed: bool,
failed: bool,
}


fn exit_json(response_body: Response) {
return_response(response_body)
}


fn fail_json(response_body: Response) {
let failed_response = &mut response_body.clone();
failed_response.failed = true;
return_response(failed_response.clone())
}


fn return_response(resp: Response) {
println!("{}", serde_json::to_string(&resp).unwrap());
process::exit(resp.failed as i32);
}


fn read_file_contents(file_name: &str) -> Result<String, Box<std::io::Error>> {
let mut json_string = String::new();
File::open(file_name)?.read_to_string(&mut json_string)?;
Ok(json_string)
}


fn parse_module_args(json_input: String) -> Result<ModuleArgs, Error> {
Ok(
ModuleArgs::from(
serde_json::from_str(
json_input.as_str()
)?
)
)
}


fn main() {
let args: Vec<String> = env::args().collect();
let program = &args[0];
let input_file_name = match args.len() {
2 => &args[1],
_ => {
eprintln!("module '{}' expects exactly one argument!", program);
fail_json(Response {
msg: "No module arguments file provided".to_owned(),
changed: false,
failed: true,
});
""
}
};
let json_input = read_file_contents(input_file_name).map_err(|err| {
eprintln!("Could not read file '{}': {}", input_file_name, err);
fail_json(Response {
msg: format!("Could not read input JSON file '{}': {}", input_file_name, err),
changed: false,
failed: true,
})
}).unwrap();
let module_args = parse_module_args(json_input).map_err(|err| {
eprintln!("Error during parsing JSON module arguments: {}", err);
fail_json(Response {
msg: format!("Malformed input JSON module arguments: {}", err),
changed: false,
failed: true,
})
}).unwrap();
exit_json(Response {
msg: format!("Hello, {}!", module_args.name.as_str()),
changed: true,
failed: false,
});
}
3 changes: 3 additions & 0 deletions rust-module-hello-world/module-src/target/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!/.gitignore
/.*
/*
55 changes: 55 additions & 0 deletions rust-module-hello-world/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
- hosts: localhost
tasks:
- debug:
msg: Testing a binary module written in Rust

- debug:
var: ansible_system

- name: ping
ping:

- name: Hello, World!
action: rust_helloworld
register: hello_world

- assert:
that:
- >
hello_world.msg == "Hello, World!"
- name: Hello, Ansible!
action: rust_helloworld
args:
name: Ansible
register: hello_ansible

- assert:
that:
- >
hello_ansible.msg == "Hello, Ansible!"
- name: Async Hello, World!
action: rust_helloworld
async: 10
poll: 1
register: async_hello_world

- assert:
that:
- >
async_hello_world.msg == "Hello, World!"
- name: Async Hello, Ansible!
action: rust_helloworld
args:
name: Ansible
async: 10
poll: 1
register: async_hello_ansible

- assert:
that:
- >
async_hello_ansible.msg == "Hello, Ansible!"

0 comments on commit 4585ea1

Please sign in to comment.