Skip to content

Instantly share code, notes, and snippets.

@wki
Created June 6, 2018 06:12
Show Gist options
  • Save wki/1effc5a45fcbfb82e5715ec0abc318fd to your computer and use it in GitHub Desktop.
Save wki/1effc5a45fcbfb82e5715ec0abc318fd to your computer and use it in GitHub Desktop.
#r "paket:
nuget Fake.Core.Target
nuget SSH.NET //"
#load "./.fake/build.fsx/intellisense.fsx"
module Ssh =
open Renci.SshNet
open Fake.Core
let connection host username =
let homeDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile)
let defaultSshKeyFile = System.IO.Path.Combine(homeDir, ".ssh/id_rsa")
let sshKeyFile = Environment.environVarOrDefault "FAKE_SSH_KEY" defaultSshKeyFile
let privateKeyFile = new PrivateKeyFile(sshKeyFile)
new PrivateKeyConnectionInfo(host, username, privateKeyFile)
let connect host username =
let client = new SshClient(connection host username)
client.Connect()
client
let run command (ssh:SshClient) =
let result = ssh.RunCommand command
if result.ExitStatus <> 0 then failwith (sprintf "error while running '%s'" command)
printfn "run result: %s" result.Result
ssh
let close (ssh:SshClient) =
ssh.Disconnect()
module Sftp =
open Renci.SshNet
let connect host username =
let client = new SftpClient(Ssh.connection host username)
client.Connect()
client
let cp destination file (sftp: SftpClient) =
use stream = System.IO.File.Open(file, System.IO.FileMode.Open)
let destinationFile = sprintf "%s/%s" destination (System.IO.Path.GetFileName(file))
sftp.UploadFile(stream, destinationFile, canOverride=true)
sftp
let cpN destination (sftp: SftpClient) (files: string list) =
files
|> List.iter (fun f -> cp destination f sftp |> ignore)
sftp
let chmod mode file (sftp: SftpClient) =
sftp.ChangePermissions(file, mode)
sftp
let chmodN mode (sftp: SftpClient) (files: string list) =
files
|> List.iter (fun f -> chmod f mode sftp |> ignore)
let mkdir dir (sftp: SftpClient) =
if not(sftp.Exists(dir)) then
sftp.CreateDirectory(dir)
sftp
let rm path (sftp: SftpClient) =
if (sftp.Exists(path)) then
sftp.Delete(path)
sftp
let close (sftp: SftpClient) =
sftp.Disconnect()
open Fake.Core
open Fake.DotNet
// Default target
Target.create "Default" (fun _ ->
Trace.trace "Hello World from FAKE"
)
Target.create "Deploy" (fun _ ->
Trace.trace "Deploy"
let host = "192.168.2.27"
let username = "wolfgang"
Sftp.connect host username
|> Sftp.mkdir "/tmp/target"
|> Sftp.cpN "/tmp" <| [ @"C:\Users\wkink\tmp\sample.md"; @"C:\Users\wkink\tmp\NetStdClassLib\NetStdClassLib.sln" ]
|> Sftp.cp "/tmp" @"C:\Users\wkink\tmp\NetStdClassLib\NetStdClassLib\Class1.cs"
|> Sftp.chmod 0o755s "/tmp/sample.md"
|> Sftp.close
Ssh.connect host username
|> Ssh.run "/bin/ls /tmp"
//|> Ssh.run "/bin/bash -c 'rm -f /tmp/x.txt'"
|> Ssh.close
)
open Fake.Core.TargetOperators
"Default"
==> "Deploy"
// start build
Target.runOrDefault "Default"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment