1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! # Azure Functions for Rust
//!
//! The Azure Functions for Rust crate supports creating Azure Functions with Rust.
//!
//! The following Azure Functions trigger bindings are supported:
//!
//! * [Blob triggers](bindings/struct.BlobTrigger.html)
//! * [HTTP triggers](bindings/struct.HttpRequest.html)
//! * [Queue triggers](bindings/struct.QueueTrigger.html)
//! * [Timer triggers](bindings/struct.TimerInfo.html)
//!
//! The following Azure Functions input bindings are supported:
//!
//! * [Blob input](bindings/struct.Blob.html)
//! * [Table input](bindings/struct.Table.html)
//!
//! The following Azure Functions output bindings are supported:
//!
//! * [Blob output](bindings/struct.Blob.html)
//! * [HTTP output](bindings/struct.HttpResponse.html)
//! * [Queue message output](bindings/struct.QueueMessage.html)
//! * [Table output](bindings/struct.Table.html)
//!
//! Eventually more bindings will be implemented, including custom binding data.
//!
//! # Example
//!
//! Start by installing the Azure Functions for Rust SDK:
//!
//! ```bash
//! $ cargo install azure-functions-sdk
//! ```
//!
//! Create a new Azure Functions for Rust application:
//!
//! ```bash
//! $ cargo func new-app hello && cd hello
//!
//! Create a HTTP-triggered function:
//!
//! ```bash
//! $ cargo func new http -n hello
//! ```
//!
//! This generates `src/functions/hello.rs` with the following contents:
//!
//! ```rust,ignore
//! use azure_functions::{
//!     bindings::{HttpRequest, HttpResponse},
//!     func,
//! };
//!
//! #[func]
//! pub fn hello(req: &HttpRequest) -> HttpResponse {
//!     "Hello from Rust!".into()
//! }
//! ```
//!
//! Azure Functions are implemented by applying a `#[func]` attribute to a Rust function.
//!
//! Run the application with `cargo func run`:
//!
//! ```bash
//! $ cargo func run
//! ```
//!
//! The above Azure Function can be invoked with `http://localhost:8080/api/hello?name=Peter`.
//!
//! The expected response would be `Hello from Rust, Peter!`.
#![deny(unused_extern_crates)]
#![cfg_attr(test, recursion_limit = "128")]

#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
#[macro_use(matches)]
extern crate matches;
#[macro_use]
extern crate lazy_static;

#[doc(no_inline)]
pub use azure_functions_codegen::func;

#[doc(hidden)]
pub use azure_functions_shared::codegen;

mod cli;
mod logger;
mod registry;
mod util;

pub mod bindings;
pub mod blob;
pub mod http;
#[doc(hidden)]
pub mod rpc;
pub mod timer;
#[doc(no_inline)]
pub use azure_functions_codegen::export;
pub use azure_functions_shared::Context;

use crate::registry::Registry;
use futures::Future;
use serde::Serialize;
use serde_json::{to_string_pretty, Serializer};
use std::env::{current_dir, current_exe};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Arc, Mutex};
use tempfile::TempDir;
use xml::writer::XmlEvent;
use xml::EmitterConfig;

// This is a workaround to the issue that `file!` expands to be workspace-relative
// and cargo does not have an environment variable for the workspace directory.
// Thus, this walks up the manifest directory until it hits "src" in the file's path.
// This function is sensitive to cargo and rustc changes.
fn get_source_file_path(manifest_dir: &Path, file: &Path) -> PathBuf {
    let mut manifest_dir = Path::new(manifest_dir);
    for component in file.components() {
        if component.as_os_str() == "src" {
            break;
        }
        manifest_dir = manifest_dir
            .parent()
            .expect("expected another parent for the manifest directory");
    }

    manifest_dir.join(file)
}

fn has_rust_files(directory: &Path) -> bool {
    fs::read_dir(directory)
        .unwrap_or_else(|_| panic!("failed to read directory '{}'", directory.display()))
        .any(|p| match p {
            Ok(p) => {
                let p = p.path();
                p.is_file() && p.extension().map(|x| x == "rs").unwrap_or(false)
            }
            _ => false,
        })
}

fn create_script_root(script_root: &Path, verbose: bool) {
    if script_root.exists() {
        if verbose {
            println!(
                "Using existing Azure Functions application at '{}'.",
                script_root.display()
            );
        }
    } else {
        if verbose {
            println!(
                "Creating Azure Functions application at '{}'.",
                script_root.display()
            );
        }

        fs::create_dir_all(&script_root).unwrap_or_else(|_| {
            panic!(
                "Failed to create Azure Functions application directory '{}'",
                script_root.display()
            )
        });
    }
}

fn create_host_file(script_root: &Path, verbose: bool) {
    let host_json = script_root.join("host.json");
    if host_json.exists() {
        return;
    }

    if verbose {
        println!(
            "Creating host configuration file '{}'.",
            host_json.display()
        );
    }

    fs::write(
        &host_json,
        to_string_pretty(&json!(
        {
            "version": "2.0",
            "logging": {
                "logLevel": {
                    "default": "Warning"
                }
            }
        }))
        .unwrap(),
    )
    .unwrap_or_else(|_| panic!("Failed to create '{}'", host_json.display()));
}

fn create_local_settings_file(script_root: &Path, worker_dir: &Path, verbose: bool) {
    let settings = script_root.join("local.settings.json");
    if settings.exists() {
        return;
    }

    if verbose {
        println!("Creating local settings file '{}'.", settings.display());
    }

    fs::write(
        &settings,
        to_string_pretty(&json!(
        {
            "IsEncrypted": false,
            "Values": {
                "FUNCTIONS_WORKER_RUNTIME": "Rust",
                "languageWorkers:workersDirectory": worker_dir.parent().unwrap()
            },
            "ConnectionStrings": {
            }
        }))
        .unwrap(),
    )
    .unwrap_or_else(|_| panic!("Failed to create '{}'", settings.display()));
}

fn create_worker_dir(script_root: &Path, verbose: bool) -> PathBuf {
    let worker_dir = script_root.join("workers").join("rust");

    if verbose {
        println!("Creating worker directory '{}'.", worker_dir.display());
    }

    fs::create_dir_all(&worker_dir).unwrap_or_else(|_| {
        panic!(
            "Failed to create directory for worker executable '{}'",
            worker_dir.display()
        )
    });

    worker_dir
}

fn copy_worker_executable(worker_dir: &Path, verbose: bool) {
    if verbose {
        println!(
            "Copying current worker executable to '{}'.",
            worker_dir.display()
        );
    }

    fs::copy(
        current_exe().expect("Failed to determine the path to the current executable"),
        worker_dir.join(if cfg!(windows) {
            "rust_worker.exe"
        } else {
            "rust_worker"
        }),
    )
    .expect("Failed to copy worker executable");
}

fn create_worker_config_file(worker_dir: &Path, verbose: bool) {
    let config = worker_dir.join("worker.config.json");
    if config.exists() {
        return;
    }

    if verbose {
        println!("Creating worker config file '{}'.", config.display());
    }

    fs::write(
        &config,
        to_string_pretty(&json!(
        {
            "description":{
                "language": "Rust",
                "extensions": [".rs"],
                "defaultExecutablePath": "workers/rust/rust_worker",
                "arguments": ["run"]
            }
        }))
        .unwrap(),
    )
    .unwrap_or_else(|_| panic!("Failed to create '{}'", config.display()));
}

fn delete_existing_function_directories(script_root: &Path, verbose: bool) {
    for entry in fs::read_dir(&script_root).expect("failed to read script root directory") {
        let path = script_root.join(entry.expect("failed to read script root entry").path());
        if !path.is_dir() || !has_rust_files(&path) {
            continue;
        }

        if verbose {
            println!(
                "Deleting existing Rust function directory '{}'.",
                path.display()
            );
        }

        fs::remove_dir_all(&path)
            .unwrap_or_else(|_| panic!("Failed to delete function directory '{}", path.display()));
    }
}

fn create_function_directory(script_root: &Path, function_name: &str, verbose: bool) -> PathBuf {
    let function_dir = script_root.join(function_name);

    if verbose {
        println!("Creating function directory '{}'.", function_dir.display());
    }

    fs::create_dir(&function_dir).unwrap_or_else(|_| {
        panic!(
            "Failed to create function directory '{}'",
            function_dir.display()
        )
    });

    function_dir
}

fn copy_source_file(function_dir: &Path, source_file: &Path, function_name: &str, verbose: bool) {
    let destination_file = function_dir.join(
        source_file
            .file_name()
            .expect("expected the source file to have a file name"),
    );

    if source_file.is_file() {
        if verbose {
            println!(
                "Copying source file '{}' to '{}' for Azure Function '{}'.",
                source_file.display(),
                destination_file.display(),
                function_name
            );
        }

        fs::copy(&source_file, destination_file)
            .unwrap_or_else(|_| panic!("Failed to copy source file '{}'", source_file.display()));
    } else {
        if verbose {
            println!(
                "Creating empty source file '{}' for Azure Function '{}'.",
                destination_file.display(),
                function_name
            );
        }

        fs::write(
                &destination_file,
                "// This file is intentionally empty.\n\
                 // The original source file was not available when the Functions Application was initialized.\n"
            ).unwrap_or_else(|_| panic!("Failed to create '{}'", destination_file.display()));
    }
}

fn create_function_config_file(
    function_dir: &Path,
    info: &'static codegen::Function,
    verbose: bool,
) {
    let function_json = function_dir.join("function.json");

    if verbose {
        println!(
            "Creating function configuration file '{}' for Azure Function '{}'.",
            function_json.display(),
            info.name
        );
    }

    let mut output = fs::File::create(&function_json)
        .unwrap_or_else(|_| panic!("Failed to create '{}'", function_json.display()));

    info.serialize(&mut Serializer::pretty(&mut output))
        .unwrap_or_else(|_| panic!("Failed to serialize metadata for function '{}'", info.name));
}

fn initialize_script_root(
    script_root: &str,
    sync: bool,
    verbose: bool,
    registry: &Arc<Mutex<Registry<'static>>>,
) {
    let script_root = current_dir()
        .expect("failed to get current directory")
        .join(script_root);

    create_script_root(&script_root, verbose);

    create_host_file(&script_root, verbose);

    let worker_dir = create_worker_dir(&script_root, verbose);

    create_local_settings_file(&script_root, &worker_dir, verbose);

    copy_worker_executable(&worker_dir, verbose);

    create_worker_config_file(&worker_dir, verbose);

    delete_existing_function_directories(&script_root, verbose);

    for (name, info) in registry.lock().unwrap().iter() {
        let function_dir = create_function_directory(&script_root, name, verbose);

        let source_file = get_source_file_path(
            Path::new(
                info.manifest_dir
                    .as_ref()
                    .expect("Functions should have a manifest directory.")
                    .as_ref(),
            ),
            Path::new(
                info.file
                    .as_ref()
                    .expect("Functions should have a file.")
                    .as_ref(),
            ),
        );

        copy_source_file(&function_dir, &source_file, name, verbose);

        create_function_config_file(&function_dir, info, verbose);
    }

    if sync {
        sync_extensions(script_root.to_str().unwrap(), verbose, &registry);
    }
}

fn write_property(writer: &mut xml::EventWriter<&mut fs::File>, name: &str, value: &str) {
    writer.write(XmlEvent::start_element(name)).unwrap();
    writer.write(XmlEvent::characters(value)).unwrap();
    writer.write(XmlEvent::end_element()).unwrap();
}

fn write_extensions_project_file(path: &Path, registry: &Registry<'static>) {
    let mut project_file =
        fs::File::create(path).expect("Failed to create extensions project file.");

    let mut writer = EmitterConfig::new()
        .perform_indent(true)
        .create_writer(&mut project_file);

    writer
        .write(XmlEvent::start_element("Project").attr("Sdk", "Microsoft.NET.Sdk"))
        .unwrap();

    writer
        .write(XmlEvent::start_element("PropertyGroup"))
        .unwrap();

    write_property(&mut writer, "TargetFramework", "netstandard2.0");
    write_property(&mut writer, "CopyBuildOutputToPublishDirectory", "false");
    write_property(&mut writer, "CopyOutputSymbolsToPublishDirectory", "false");
    write_property(&mut writer, "GenerateDependencyFile", "false");

    writer.write(XmlEvent::end_element()).unwrap();

    writer.write(XmlEvent::start_element("ItemGroup")).unwrap();

    for extension in registry.iter_binding_extensions() {
        writer
            .write(
                XmlEvent::start_element("PackageReference")
                    .attr("Include", extension.0)
                    .attr("Version", extension.1),
            )
            .unwrap();
        writer.write(XmlEvent::end_element()).unwrap();
    }

    writer.write(XmlEvent::end_element()).unwrap();
    writer.write(XmlEvent::end_element()).unwrap();
}

fn write_generator_project_file(path: &Path) {
    let mut project_file =
        fs::File::create(path).expect("Failed to create generator project file.");

    let mut writer = EmitterConfig::new()
        .perform_indent(true)
        .create_writer(&mut project_file);

    writer
        .write(XmlEvent::start_element("Project").attr("Sdk", "Microsoft.NET.Sdk"))
        .unwrap();

    writer
        .write(XmlEvent::start_element("PropertyGroup"))
        .unwrap();

    write_property(&mut writer, "TargetFramework", "netstandard2.0");

    writer.write(XmlEvent::end_element()).unwrap();

    writer.write(XmlEvent::start_element("ItemGroup")).unwrap();

    writer
        .write(
            XmlEvent::start_element("PackageReference")
                .attr(
                    "Include",
                    "Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator",
                )
                .attr("Version", "1.0.1")
                .attr("PrivateAssets", "all"),
        )
        .unwrap();

    writer.write(XmlEvent::end_element()).unwrap();
    writer.write(XmlEvent::end_element()).unwrap();
    writer.write(XmlEvent::end_element()).unwrap();
}

fn sync_extensions(script_root: &str, verbose: bool, registry: &Arc<Mutex<Registry<'static>>>) {
    let reg = registry.lock().unwrap();

    if !reg.has_binding_extensions() {
        if verbose {
            println!("No binding extensions are needed.");
        }
        return;
    }

    let temp_dir = TempDir::new().expect("failed to create temporary directory");
    let extensions_project_path = temp_dir.path().join("extensions.csproj");
    let metadata_project_path = temp_dir.path().join("metadata.csproj");
    let output_directory = current_dir()
        .expect("failed to get current directory")
        .join(script_root);

    write_extensions_project_file(&extensions_project_path, &reg);
    write_generator_project_file(&metadata_project_path);

    if verbose {
        println!("Restoring extension assemblies...");
    }

    if !Command::new("dotnet")
        .args(&[
            "publish",
            "/v:q",
            "/nologo",
            "-c",
            "Release",
            "-o",
            output_directory.join("bin").to_str().unwrap(),
            extensions_project_path.to_str().unwrap(),
        ])
        .current_dir(temp_dir.path())
        .status()
        .map_err(|e| format!("failed to spawn dotnet: {}", e))
        .unwrap()
        .success()
    {
        panic!("failed to restore extension assemblies.");
    }

    if verbose {
        println!("Generating extension metadata...");
    }

    if !Command::new("dotnet")
        .args(&[
            "msbuild",
            "/t:_GenerateFunctionsExtensionsMetadataPostPublish",
            "/v:q",
            "/nologo",
            "/restore",
            "-p:Configuration=Release",
            &format!("-p:PublishDir={}/", output_directory.to_str().unwrap()),
            metadata_project_path.to_str().unwrap(),
        ])
        .current_dir(temp_dir.path())
        .status()
        .map_err(|e| format!("failed to spawn dotnet: {}", e))
        .unwrap()
        .success()
    {
        panic!("failed to generate extension metadata.");
    }
}

fn run_worker(
    worker_id: &str,
    host: &str,
    port: u32,
    max_message_length: Option<i32>,
    registry: &Arc<Mutex<Registry<'static>>>,
) {
    ctrlc::set_handler(|| {}).expect("failed setting SIGINT handler");

    let client = rpc::Client::new(worker_id.to_string(), max_message_length);

    println!("Connecting to Azure Functions host at {}:{}.", host, port);

    client
        .connect(host, port)
        .and_then(|client| {
            println!(
                "Connected to Azure Functions host version {}.",
                client.host_version().unwrap()
            );

            client.process_all_messages(&registry)
        })
        .wait()
        .unwrap();
}

/// The main entry point for the Azure Functions for Rust worker.
///
/// # Examples
///
/// ```rust,ignore
/// fn main() {
///     azure_functions::worker_main(::std::env::args(), export!{
///         my_module::my_function
///     });
/// }
/// ```
pub fn worker_main(args: impl Iterator<Item = String>, functions: &[&'static codegen::Function]) {
    let matches = cli::create_app().get_matches_from(args);
    let registry = Arc::new(Mutex::new(Registry::new(functions)));

    if let Some(matches) = matches.subcommand_matches("init") {
        initialize_script_root(
            matches
                .value_of("script_root")
                .expect("A script root is required."),
            matches.is_present("sync"),
            matches.is_present("verbose"),
            &registry,
        );
        return;
    }

    if let Some(matches) = matches.subcommand_matches("sync-extensions") {
        sync_extensions(
            matches
                .value_of("script_root")
                .expect("A script root is required."),
            matches.is_present("verbose"),
            &registry,
        );
        return;
    }

    if let Some(matches) = matches.subcommand_matches("run") {
        run_worker(
            matches
                .value_of("worker_id")
                .expect("A worker id is required."),
            matches.value_of("host").expect("A host is required."),
            matches
                .value_of("port")
                .map(|port| port.parse::<u32>().expect("Invalid port number"))
                .expect("A port number is required."),
            matches
                .value_of("max_message_length")
                .map(|len| len.parse::<i32>().expect("Invalid maximum message length")),
            &registry,
        );
        return;
    }

    panic!("expected a subcommand.");
}