Class: SmartCloud::Apps::App

Inherits:
Base
  • Object
show all
Defined in:
lib/smart_cloud/apps/app.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

configure_logger_for, included, #logger, logger_for

Constructor Details

#initializeApp

Returns a new instance of App.



5
6
# File 'lib/smart_cloud/apps/app.rb', line 5

def initialize
end

Class Method Details

.clean_up(container_path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/smart_cloud/apps/app.rb', line 145

def self.clean_up(container_path)
	env_vars = SmartCloud::Apps::App.get_env_vars(container_path)
	return unless env_vars

	logger.info "Cleaning up ..."

	# Clean up very old versions
	Dir.chdir("#{container_path}/releases") do
		app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
		destroy_count = app_versions.count - env_vars['KEEP_RELEASES'].to_i
		if destroy_count > 0
			logger.debug "Deleting older application releases ..."
			destroy_count.times do
				FileUtils.rm_r(File.join(Dir.pwd, app_versions.shift))
			end
		end
	end
end

.create(*args) ⇒ Object

Creating App!

Example:

>> SmartCloud::Apps::Rails.create
=> Creation Complete

Arguments:

appname => (String)

username => (String)



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
# File 'lib/smart_cloud/apps/app.rb', line 17

def self.create(*args)
	args.flatten!
	appname = args.empty? ? '' : args.shift
	username = args.empty? ? '' : args.shift

	raise "Please provide appname and username" if appname.empty? || username.empty?

	if SmartCloud::Docker.running?
		repository_path = "#{SmartCloud.config.user_home_path}/.smartcloud/apps/repositories/#{appname}.git"
		container_path = "#{SmartCloud.config.user_home_path}/.smartcloud/apps/containers/#{appname}"
		print "-----> Creating Application ... "

		# Checking if app with given name already exists
		if Dir.exist?(repository_path)
			puts "failed. App with name '#{appname}' already exists."
			exit
		end

		# Creating Directories
		FileUtils.mkdir_p(repository_path)
		FileUtils.mkdir_p(container_path)

		# Initializing bare repo and pre-receive
		Dir.chdir(repository_path) do
			%x[git init --bare]
			%x[chmod +x #{SmartCloud.config.user_home_path}/.smartcloud/grids/prereceiver/pre-receive]
			%x[ln -s #{SmartCloud.config.user_home_path}/.smartcloud/grids/prereceiver/pre-receive #{repository_path}/hooks/pre-receive]
			puts "done"
		end

		# Creating Environment File
		if File.exist?("#{SmartCloud.config.user_home_path}/.smartcloud/config/environment.rb")
			require "#{SmartCloud.config.user_home_path}/.smartcloud/config/environment"
		end
		unless File.exist? "#{container_path}/env"
			print "-----> Creating App Environment ... "
			page = <<~HEREDOC
				## System
				USERNAME=#{username}
				KEEP_RELEASES=3

				## Docker
				VIRTUAL_HOST=#{appname}.#{SmartCloud.config.apps_domain}
				LETSENCRYPT_HOST=#{appname}.#{SmartCloud.config.apps_domain}
				LETSENCRYPT_EMAIL=#{SmartCloud.config.sysadmin_email}
				LETSENCRYPT_TEST=false
			HEREDOC
			puts "done" if system("echo '#{page}' > #{container_path}/env")
		end
	end
end

.destroy(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/smart_cloud/apps/app.rb', line 69

def self.destroy(*args)
	args.flatten!
	appname = args.empty? ? '' : args.shift

	raise "Please provide appname" if appname.empty?

	if SmartCloud::Docker.running?
		# Stopping & Removing old container
		self.stop(appname)

		# Destroying Directories
		print "-----> Deleting App #{appname} ... "
		repository_path = "#{SmartCloud.config.user_home_path}/.smartcloud/apps/repositories/#{appname}.git"
		container_path = "#{SmartCloud.config.user_home_path}/.smartcloud/apps/containers/#{appname}"
		FileUtils.rm_r(repository_path)
		FileUtils.rm_r(container_path)
		puts "done"
	end
end

.get_env_vars(container_path) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/smart_cloud/apps/app.rb', line 164

def self.get_env_vars(container_path)
	unless File.exist? "#{container_path}/env"
		logger.fatal "Environment could not be loaded ... Failed."
		return false
	end

	env_vars = {}
	File.open("#{container_path}/env").each_line do |line|
		line.chomp!
		next if line.empty? || line.start_with?('#')
	    key, value = line.split "="
	    env_vars[key] = value
	end

	env_vars
end

.start(*args) ⇒ Object



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
# File 'lib/smart_cloud/apps/app.rb', line 89

def self.start(*args)
	args.flatten!
	appname = args.empty? ? '' : args.shift
	app_version = args.empty? ? 0 : args.shift.to_i

	raise "Please provide appname" if appname.empty?

	logger.formatter = proc do |severity, datetime, progname, message|
		severity_text = { "DEBUG" => "\u{1f527} #{severity}:", "INFO" => " \u{276f}", "WARN" => "\u{2757} #{severity}:",
			"ERROR" => "\u{274c} #{severity}:", "FATAL" => "\u{2b55} #{severity}:", "UNKNOWN" => "\u{2753} #{severity}:"
		}
		"\t\t\t\t#{severity_text[severity]} #{message}\n"
	end

	if SmartCloud::Docker.running?
		container_path = "#{SmartCloud.config.user_home_path}/.smartcloud/apps/containers/#{appname}"

		Dir.chdir("#{container_path}/releases") do
			# Getting App Version
			if app_version == 0
				app_versions = Dir.glob('*').select { |f| File.directory? f }.sort
				app_version = app_versions.last
			end
			container_path_with_version = "#{container_path}/releases/#{app_version}"

			logger.info "Launching Application ..."

			app = SmartCloud::Apps::Rails.new
			app.start(appname, container_path, container_path_with_version)
		end
	end

	logger.formatter = nil
end

.stop(*args) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/smart_cloud/apps/app.rb', line 124

def self.stop(*args)
	args.flatten!
	appname = args.empty? ? '' : args.shift

	raise "Please provide appname" if appname.empty?

	container_name = appname

	if SmartCloud::Docker.running?
		container_id = `docker ps -a -q --filter='name=^#{container_name}$'`.chomp
		unless container_id.empty?
			logger.debug "Stopping & Removing container #{container_name} ..."
			if system("docker stop #{container_name} && docker rm #{container_name}", out: File::NULL)
				logger.debug "Stopped & Removed container #{container_name} ..."
			end
		else
			logger.debug "Container '#{container_name}' does not exist to stop."
		end
	end
end