-
Notifications
You must be signed in to change notification settings - Fork 27
/
SSHDeployer.swift
183 lines (164 loc) · 6.46 KB
/
SSHDeployer.swift
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
import Deployer
import Foundation
import EmceeLogging
import PathLib
import ProcessController
import Tmp
import UniqueIdentifierGenerator
public final class SSHDeployer: Deployer {
private let sshClientType: SSHClient.Type
private let logger: ContextualLogger
public init(
sshClientType: SSHClient.Type,
deploymentId: String,
deployables: [DeployableItem],
deployableCommands: [DeployableCommand],
destination: DeploymentDestination,
logger: ContextualLogger,
processControllerProvider: ProcessControllerProvider,
temporaryFolder: TemporaryFolder,
uniqueIdentifierGenerator: UniqueIdentifierGenerator
) throws {
self.sshClientType = sshClientType
self.logger = logger
try super.init(
deploymentId: deploymentId,
deployables: deployables,
deployableCommands: deployableCommands,
destination: destination,
logger: logger,
processControllerProvider: processControllerProvider,
temporaryFolder: temporaryFolder,
uniqueIdentifierGenerator: uniqueIdentifierGenerator
)
}
override public func deployToDestination(
pathToDeployable: [AbsolutePath: DeployableItem]
) throws {
do {
try deploy(pathToDeployable: pathToDeployable)
} catch {
log(destination, "Failed to deploy to this destination with error: \(error)")
}
}
/**
* Returns a remote path at remote destination which will contain all deployed contents for the given deployable.
*/
public static func remoteContainerPath(
forDeployable deployable: DeployableItem,
destination: DeploymentDestination,
deploymentId: String
) -> AbsolutePath {
return destination.remoteDeploymentPath.appending(
components: [deploymentId, deployable.name]
)
}
public static func remotePath(
deployable: DeployableItem,
file: DeployableFile,
destination: DeploymentDestination,
deploymentId: String
) -> AbsolutePath {
let container = remoteContainerPath(
forDeployable: deployable,
destination: destination,
deploymentId: deploymentId
)
return container.appending(relativePath: file.destination)
}
// MARK: - Private - Deploy
private func deploy(
pathToDeployable: [AbsolutePath: DeployableItem]
) throws {
log(destination, "Connecting")
let sshClient = try self.sshClientType.init(
host: destination.host,
port: destination.port,
username: destination.username,
authentication: destination.authentication
)
try sshClient.connectAndAuthenticate()
log(destination, "Connected and authenticated")
try pathToDeployable.forEach { (absolutePath: AbsolutePath, deployable: DeployableItem) in
let remoteDeploymentPath = SSHDeployer.remoteContainerPath(
forDeployable: deployable,
destination: destination,
deploymentId: deploymentId
)
try sshClient.execute(["rm", "-rf", remoteDeploymentPath.pathString])
try sshClient.execute(["mkdir", "-p", remoteDeploymentPath.pathString])
let remotePackagePath = remoteDeploymentPath.appending(component: "_package.zip")
try uploadFile(
sshClient: sshClient,
destination: destination,
localAbsolutePath: absolutePath,
remoteAbsolutePath: remotePackagePath
)
try deployPackageRemotely(
sshClient: sshClient,
destination: destination,
deployable: deployable,
remotePackagePath: remotePackagePath,
remoteDeploymentPath: remoteDeploymentPath
)
}
try invokeCommands(
sshClient: sshClient,
destination: destination
)
log(destination, "Finished deploying")
}
private func uploadFile(
sshClient: SSHClient,
destination: DeploymentDestination,
localAbsolutePath: AbsolutePath,
remoteAbsolutePath: AbsolutePath
) throws {
log(destination, "Uploading \(localAbsolutePath) -> \(remoteAbsolutePath)")
try sshClient.upload(localUrl: localAbsolutePath.fileUrl, remotePath: remoteAbsolutePath.pathString)
log(destination, "Uploaded \(localAbsolutePath) -> \(remoteAbsolutePath)")
}
private func deployPackageRemotely(
sshClient: SSHClient,
destination: DeploymentDestination,
deployable: DeployableItem,
remotePackagePath: AbsolutePath,
remoteDeploymentPath: AbsolutePath
) throws {
log(destination, "Deploying '\(deployable.name)'")
try sshClient.execute(["unzip", remotePackagePath.pathString, "-d", remoteDeploymentPath.pathString])
log(destination, "Deployed '\(deployable.name)'")
}
// MARK: - Private - Command Invocatoin
private func invokeCommands(sshClient: SSHClient, destination: DeploymentDestination) throws {
for command in deployableCommands {
let commandArgs: [String] = command.commandArgs.map { (arg: DeployableCommandArg) in
switch arg {
case let .string(value):
return value
case .item(let deployableItem, let relativePath):
var remotePath = SSHDeployer.remoteContainerPath(
forDeployable: deployableItem,
destination: destination,
deploymentId: deploymentId)
if let additionalPath = relativePath {
remotePath = remotePath.appending(component: additionalPath)
}
return remotePath.pathString
}
}
log(destination, "Executing command: \(command)")
try sshClient.execute(commandArgs)
log(destination, "Executed command")
}
}
// MARK: - Private - Logging
private func log(
_ destination: DeploymentDestination,
_ text: String,
file: String = #file,
line: UInt = #line
) {
logger.debug("\(destination.host): \(text)", file: file, line: line)
}
}