Skip to content

Commit

Permalink
Changed the types so that each webjob is responsible for it's own dir…
Browse files Browse the repository at this point in the history
…ectory/package location.

This means we can get rid oft he set params from package/deploy.

Removed the uri type extension as it's not used any more.
  • Loading branch information
TWith2Sugars committed Dec 22, 2014
1 parent 250a272 commit 921b7ce
Showing 1 changed file with 24 additions and 45 deletions.
69 changes: 24 additions & 45 deletions src/app/FakeLib/AzureWebJobs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ open Fake
open System.IO
open System
open System.Net

type Uri with
member this.SubDomain = this.Host.Split([|'.'|],2).[0]
open System.Collections.Generic

/// The running modes of webjobs
[<RequireQualifiedAccess>]
Expand All @@ -23,7 +21,11 @@ type WebJob =
/// Specifies what type of webjob this is. Note that this also determines it's deployment location on Azure
JobType : WebJobType
/// The project to be zipped and deployed as a webjob
Project : string }
Project : string
/// The directory path of the webjob to zip
DirectoryToPackage : string
// The package path to once zipped
PackageLocation: string }

/// The website that webjobs are deployed to
type WebSite =
Expand All @@ -37,65 +39,42 @@ type WebSite =
/// The webjobs to deploy to this web site
WebJobs : WebJob list }

/// TypeScript task parameter type
type WebJobParams =
{
/// Specifies the directory of the files to
InputPath : string
/// Specifies the zip output path.
OutputPath : string }

/// Default parameters for the WebJobs task
let WebJobDefaultParams webJobProject =
{ OutputPath = "bin"
InputPath = "src/" + webJobProject + "/bin/Release"}

let private jobTypePath webJob =
match webJob.JobType with
let private jobTypePath webJobType =
match webJobType with
| WebJobType.Continuous -> "continuous"
| WebJobType.Triggered -> "triggered"

let private webJobPath outputPath webSite webJob =
sprintf "%s/%s/webjobs/%s" outputPath webSite.Url.SubDomain (jobTypePath webJob)

let private webJobZipPath outputPath webSite webJob =
sprintf "%s/%s.zip" (webJobPath outputPath webSite webJob) webJob.Name

let private zipWebJob setParams webSite webJob =
let parameters = setParams (WebJobDefaultParams webJob.Project)
let zipDirectory = directoryInfo (webJobPath parameters.OutputPath webSite webJob)
ensureDirExists zipDirectory
let zipName = Path.Combine(zipDirectory.FullName, webJob.Name + ".zip")
let filesToZip = Directory.GetFiles(parameters.InputPath, "*.*", SearchOption.AllDirectories)
tracefn "Zipping %s webjob to %O" webJob.Project zipDirectory
CreateZip parameters.InputPath zipName "" 0 false filesToZip
let private zipWebJob webSite webJob =
let packageFile = fileInfo webJob.PackageLocation
ensureDirExists packageFile.Directory
let zipName = webJob.PackageLocation
let filesToZip = Directory.GetFiles(webJob.DirectoryToPackage, "*.*", SearchOption.AllDirectories)
tracefn "Zipping %s webjob to %s" webJob.Project webJob.PackageLocation
CreateZip webJob.DirectoryToPackage zipName "" 0 false filesToZip

/// This task to can be used create a zip for each webjob to deploy to a website
/// The output structure is: `outputpath/{websitename}/webjobs/{continuous/triggered}/{webjobname}.zip`
/// ## Parameters
///
/// - `setParams` - Function used to overwrite webjobs outputpath.
/// - `webSites` - The websites and webjobs to build zips from.
let PackageWebJobs setParams webSites =
webSites |> List.iter (fun webSite -> webSite.WebJobs |> List.iter (zipWebJob setParams webSite))
let PackageWebJobs webSites =
webSites |> List.iter (fun webSite -> webSite.WebJobs |> List.iter (zipWebJob webSite))

let private deployWebJobToWebSite setParams webSite webJob =
let parameters = setParams WebJobDefaultParams webJob.Project
let uploadApi = Uri(webSite.Url, sprintf"api/zip/site/wwwroot/App_Data/jobs/%s/%s" (jobTypePath webJob) webJob.Name)
let filePath = (webJobZipPath parameters.OutputPath webSite webJob)
let private deployWebJobToWebSite webSite webJob =
let uploadApi = Uri(webSite.Url, sprintf"api/zip/site/wwwroot/App_Data/jobs/%s/%s" (jobTypePath webJob.JobType) webJob.Name)
let filePath = webJob.PackageLocation
tracefn "Deploying %s webjob to %O" filePath uploadApi
use client = new WebClient()

client.Credentials <-NetworkCredential(webSite.UserName, webSite.Password)
client.UploadData(uploadApi,"PUT",File.ReadAllBytes(filePath)) |> ignore

let private deployWebJobsToWebSite setParams webSite =
webSite.WebJobs |> List.iter (deployWebJobToWebSite setParams webSite)
let private deployWebJobsToWebSite webSite =
webSite.WebJobs |> List.iter (deployWebJobToWebSite webSite)

/// This task to can be used deploy a prebuilt webjob zip to a website
/// ## Parameters
///
/// - `setParams` - Function used to overwrite webjobs outputpath.
/// - `webSites` - The websites and webjobs to deploy.
let DeployWebJobs setParams webSites =
webSites |> List.iter(deployWebJobsToWebSite setParams)
let DeployWebJobs webSites =
webSites |> List.iter deployWebJobsToWebSite

0 comments on commit 921b7ce

Please sign in to comment.