Skip to content

Commit

Permalink
Create a database user if necessary and replace the runtime placehold…
Browse files Browse the repository at this point in the history
…ers if given
  • Loading branch information
coder-hugo committed Mar 17, 2022
1 parent 69fbe61 commit 2319a69
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
42 changes: 40 additions & 2 deletions exe/deploy-to-ionos
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require 'size_checker'
require 'remote_host'
require 'php_alias'
require 'passgen'
require 'date'
require 'template_renderer'

deploy_now = DeployNowApi.new(endpoint: ENV['SERVICE_HOST'],
api_key: ENV['API_KEY'],
Expand All @@ -18,6 +20,7 @@ config = ConfigurationParser.parse(dist_folder: ENV['DIST_FOLDER'], bootstrap: !
common_excludes = %w[logs .deploy-now .git .github]

ssh_user = { password: Passgen.generate(length: 30, symbols: true) }
database = {}

mutex = Mutex.new
cv = ConditionVariable.new
Expand All @@ -33,17 +36,52 @@ events_client = deploy_now.get_user_events do |event|
end
end
end
if event.type == :database_user
data = JSON.parse(event.data)
if data['id'] == database[:account_id]
mutex.synchronize do
puts "Created database user #{data['username']}"
database[:username] = data['username']
cv.signal
end
end
end
end

mutex.synchronize do
ssh_user[:account_id] = deploy_now.create_temporary_user(ssh_user[:password])

cv.wait(mutex, 120)
abort 'Failed to create temporary SSH user' unless ssh_user.include?(:username)
if branch_info.include? :database
database[:host] = branch_info[:database][:host]
database[:name] = branch_info[:database][:name]
database[:password] = Passgen.generate(length: 30, symbols: false)
database[:account_id] = deploy_now.create_database_user(database[:password])
end

start = DateTime.now

loop do
cv.wait(mutex, 120)

break if ssh_user.include?(:username) && (database.empty? || database.include?(:username))

if (DateTime.now - start) * 24 * 3600 > 120
if database.empty?
abort 'Failed to create temporary SSH user'
elsif ssh_user.include?(:username)
abort 'Failed to create database user'
else
abort 'Failed to create temporary SSH user and database user'
end
end
end
end

events_client.close

template_renderer = TemplateRenderer.new(files: ENV['RUNTIME_PLACEHOLDER_FILES'] || '')
template_renderer.render(branch_info[:app_url], database)

remote_host = RemoteHost.new(user: ssh_user, host: branch_info[:ssh_host])

SizeChecker.check(dist_folder: config.dist_folder,
Expand Down
1 change: 1 addition & 0 deletions lib/deploy_now_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def get_branch_info
{
app_url: is_production_branch ? project['domain'] : branch['webSpace']['siteUrl'],
last_deployment_date: branch['webSpace']['lastDeploymentDate'],
database: branch.include?('database') ? { host: branch['database']['host'], name: branch['database']['name'] } : nil,
storage_quota: branch['webSpaceQuota']['storageQuota'].to_i,
ssh_host: branch['webSpace']['sshHost'],
php_version: branch['webSpace']['phpVersion']
Expand Down
20 changes: 20 additions & 0 deletions lib/template_renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class TemplateRenderer
def initialize(options)
@files = options[:files].split(',')
end

def render(app_url, database)
@files.each do |file|
content = File.read(file).gsub('@APP_URL@', app_url)
unless database.empty?
content = content.gsub('@DB_USERNAME@', database[:username])
.gsub('@DB_PASSWORD@', database[:password])
.gsub('@DB_HOST@', database[:host])
.gsub('@DB_NAME@', database[:name])
end
File.write(file, content)
end
end
end

0 comments on commit 2319a69

Please sign in to comment.