forked from ansible/ansible-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
!/.gitignore | ||
/.* | ||
/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
!/.gitignore | ||
/.* | ||
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |