Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ansible/ansible-examples.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSviatoslav Sydorenko <wk@sydorenko.org.ua>2018-06-24 16:20:52 +0300
committerSviatoslav Sydorenko <wk@sydorenko.org.ua>2018-06-30 18:53:07 +0300
commit4585ea1c4d53c230fed0d9642162444b21d59d06 (patch)
tree8d92bf57f4b1e8fc0aefcd9f9e139dc200c85c83
parentbedca7401f01411ec9d1b9231f5dc4a19ba12960 (diff)
Add a binary rust example module
-rw-r--r--rust-module-hello-world/Makefile13
-rw-r--r--rust-module-hello-world/library/.gitignore3
-rw-r--r--rust-module-hello-world/module-src/Cargo.lock85
-rw-r--r--rust-module-hello-world/module-src/Cargo.toml9
-rw-r--r--rust-module-hello-world/module-src/src/main.rs107
-rw-r--r--rust-module-hello-world/module-src/target/.gitignore3
-rw-r--r--rust-module-hello-world/rust.yml55
7 files changed, 275 insertions, 0 deletions
diff --git a/rust-module-hello-world/Makefile b/rust-module-hello-world/Makefile
new file mode 100644
index 0000000..15689bc
--- /dev/null
+++ b/rust-module-hello-world/Makefile
@@ -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
diff --git a/rust-module-hello-world/library/.gitignore b/rust-module-hello-world/library/.gitignore
new file mode 100644
index 0000000..0c7da30
--- /dev/null
+++ b/rust-module-hello-world/library/.gitignore
@@ -0,0 +1,3 @@
+!/.gitignore
+/.*
+/*
diff --git a/rust-module-hello-world/module-src/Cargo.lock b/rust-module-hello-world/module-src/Cargo.lock
new file mode 100644
index 0000000..77a88d2
--- /dev/null
+++ b/rust-module-hello-world/module-src/Cargo.lock
@@ -0,0 +1,85 @@
+[[package]]
+name = "dtoa"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "helloworld"
+version = "0.1.0"
+dependencies = [
+ "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "itoa"
+version = "0.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "proc-macro2"
+version = "0.4.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "serde_derive"
+version = "1.0.66"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_json"
+version = "1.0.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "syn"
+version = "0.14.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[metadata]
+"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab"
+"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
+"checksum proc-macro2 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "effdb53b25cdad54f8f48843d67398f7ef2e14f12c1b4cb4effc549a6462a4d6"
+"checksum quote 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e44651a0dc4cdd99f71c83b561e221f714912d11af1a4dff0631f923d53af035"
+"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95"
+"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79"
+"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875"
+"checksum syn 0.14.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c67da57e61ebc7b7b6fff56bb34440ca3a83db037320b0507af4c10368deda7d"
+"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
diff --git a/rust-module-hello-world/module-src/Cargo.toml b/rust-module-hello-world/module-src/Cargo.toml
new file mode 100644
index 0000000..082f059
--- /dev/null
+++ b/rust-module-hello-world/module-src/Cargo.toml
@@ -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"
diff --git a/rust-module-hello-world/module-src/src/main.rs b/rust-module-hello-world/module-src/src/main.rs
new file mode 100644
index 0000000..bbd7288
--- /dev/null
+++ b/rust-module-hello-world/module-src/src/main.rs
@@ -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,
+ });
+}
diff --git a/rust-module-hello-world/module-src/target/.gitignore b/rust-module-hello-world/module-src/target/.gitignore
new file mode 100644
index 0000000..0c7da30
--- /dev/null
+++ b/rust-module-hello-world/module-src/target/.gitignore
@@ -0,0 +1,3 @@
+!/.gitignore
+/.*
+/*
diff --git a/rust-module-hello-world/rust.yml b/rust-module-hello-world/rust.yml
new file mode 100644
index 0000000..25c2586
--- /dev/null
+++ b/rust-module-hello-world/rust.yml
@@ -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!"