Class: Cloudler

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudler.rb

Constant Summary collapse

VERSION =
'0.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.commandObject

Returns the value of attribute command.



9
10
11
# File 'lib/cloudler.rb', line 9

def command
  @command
end

.filesObject

Returns the value of attribute files.



9
10
11
# File 'lib/cloudler.rb', line 9

def files
  @files
end

.gemsObject

Returns the value of attribute gems.



9
10
11
# File 'lib/cloudler.rb', line 9

def gems
  @gems
end

.hostsObject

Returns the value of attribute hosts.



9
10
11
# File 'lib/cloudler.rb', line 9

def hosts
  @hosts
end

.passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/cloudler.rb', line 9

def password
  @password
end

.pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/cloudler.rb', line 9

def path
  @path
end

.precommandsObject

Returns the value of attribute precommands.



9
10
11
# File 'lib/cloudler.rb', line 9

def precommands
  @precommands
end

.usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/cloudler.rb', line 9

def username
  @username
end

Class Method Details

.execute_command(ssh) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/cloudler.rb', line 97

def self.execute_command ssh
	# We cd to @path so that the user has access to their uploaded files when
	# they try to run them
	ssh.exec! "cd #{@path} && #{@command}" do |ch, stream, data|
		puts data
	end
end

.execute_precommands(ssh) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/cloudler.rb', line 81

def self.execute_precommands ssh
	@precommands.each do |command|
		# We cd to @path so that the user can modify or run their uploaded files if
		# they need to
		ssh.exec "cd #{@path} && #{command}" do |ch,stream,data|
			puts data
		end
	end
end

.initObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cloudler.rb', line 105

def self.init
	File.open "Cloudfile", 'w' do |f|
		f.write <<-EOS
host 'HOSTNAME' # or for multiple servers, use ['HOST1', 'HOST2', ...]
username 'USERNAME'
password 'PASSWORD'
path 'PATH' # Optional path to upload the files to. By default it is /root/.cloudler, or /home/[username]/.cloudler
precommands [] # Optional list of commands to run before executing the main command
command 'COMMAND'
files [] # Optional list of files to upload
gems [] # Optional list of gems to install
		EOS
	end
end

.install_gems(ssh) ⇒ Object



91
92
93
94
95
# File 'lib/cloudler.rb', line 91

def self.install_gems ssh
	ssh.exec! "gem install #{@gems.join ' '}" do |ch, stream, data|
		puts data
	end
end

.run(to_run = [:upload, :precommands, :gems, :command]) ⇒ Object



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
# File 'lib/cloudler.rb', line 24

def self.run to_run = [:upload, :precommands, :gems, :command]
	# Set some defaults
	@files ||= []
	@gems ||= []
	@precommands ||= []
	@path ||= 
		if @username == 'root'
			'/root/.cloudler'
		else
			"/home/#{@username}/.cloudler"
		end

	@hosts.each do |host|
		Net::SSH.start(host, @username, :password => @password) do |ssh|
			if to_run.member? :upload
				puts "Uploading files..."
				upload_files ssh	
				puts "Files uploaded."
			end

			if to_run.member? :precommands
				if @precommands.length > 0
					puts "Executing pre-commands"
					execute_precommands ssh
					puts "Pre-commands executed."
				end
			end

			if to_run.member? :gems
				if @gems.length > 0 
					puts "Installing gems..."
					install_gems ssh
					puts "Gems installed"
				end
			end

			if to_run.member? :command
				puts "Executing command..."
				execute_command ssh
				puts "Command finished."
			end
		end				
	end
end

.test_connectionObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cloudler.rb', line 12

def self.test_connection
	@hosts.each do |host|
		begin
			Net::SSH.start(host, @username, :password => @password) do |ssh|
				puts "Connected to #{host} successfully."
			end
		rescue
			puts "Error connecting to #{host}."
		end
	end
end

.upload_files(ssh) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cloudler.rb', line 69

def self.upload_files ssh
	ssh.exec! "rm -rf #{@path}"
	ssh.exec! "mkdir #{@path}"

	# By default, Cloudler uploads the entire current directory
	if @files.length > 0
  	ssh.scp.upload!(@files.join(' '), @path, :recursive => true)
	else
		ssh.scp.upload!('.', @path, :recursive => true)
	end
end