-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_deploy.rb
More file actions
65 lines (55 loc) · 2.28 KB
/
_deploy.rb
File metadata and controls
65 lines (55 loc) · 2.28 KB
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
# Deployment Script to delete current contents of MSTIAM directory and replace with _Site
# NOTE: Make sure you have net-sftp and highline gems installed.
# If not, run "gem install net-sftp" or "gem install highline"
require 'highline/import'
require 'net/sftp'
puts "============ IAM Deployment Script ============"
# Connection information
host = "minersftp.mst.edu"
local_directory = "_site/"
remote_directory = "/USERWEB/mstiam"
# User input is retrieved using the highline gem
username = ask("Please enter your username: ")
password = ask("Enter password: ") { |q| q.echo = false }
# Simple method to print with an indention level specified
def level_print(message, count)
if count > 0
indention = ""
indention += " " until indention.length() == 2*count
print indention
end
puts message
end
# Recursively delete files in a directory and then remove directory
# NOTE: Directories have to be empty in order to delete them apparently
# NOTE: Due to some permission issues that were experienced with using lstat method to identify file type,
# this relies on StatusException to identify the file type. I.e., because sftp.remove expects a
# file, directories throw the error. Making this assumption works for now, just be warned.
def delete_directory_contents(directory, sftp, level)
level_print("Deleting: " + directory, level)
sftp.dir.entries(directory).each do |file|
begin
sftp.remove!(directory + "/" + file.name)
level_print("Deleted File: " + file.name, level + 1)
rescue Net::SFTP::StatusException => e
delete_directory_contents(directory + "/" + file.name, sftp, level + 1)
sftp.rmdir(directory + "/" + file.name)
level_print("Deleted Directory: " + file.name, level + 1)
# puts "Error: " + e.description
end
end
end
print "Initializing connection to: " + host + " ... "
begin
Net::SFTP.start(host, username, :password => password) do |sftp|
puts "Connected!"
# Delete existing files
delete_directory_contents(remote_directory, sftp, 0)
# Copy _Site contents to remote server
print "Uploading " + local_directory + " to " + remote_directory + " ... "
sftp.upload!(local_directory, remote_directory)
puts "Complete!"
end
rescue Net::SSH::AuthenticationFailed
puts "Authentication Failed!"
end