Skip to content

Fix hostname detection in EC2

Balasankar 'Balu' C requested to merge external-url-detection-ec2-fix into master

In EC2 instances where public ip assignment is disabled, our hostname detection doesn't work correctly. The curl command used to get public ip will yield an XML message, not a hostname. But, that XML message will satisfy our check for a "non empty string".

Closes: #2877 (closed)

ubuntu@ip-10-0-2-212:~$ fqdn=$(curl -s http://169.254.169.254/latest/meta-data/public-hostname)
ubuntu@ip-10-0-2-212:~$ [ -n "${fqdn}" ] && echo "Yes"
Yes
ubuntu@ip-10-0-2-212:~$ echo $fqdn
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>404 - Not Found</title> </head> <body> <h1>404 - Not Found</h1> </body> </html>

When we try to replace GENERATED_EXTERNAL_URL with this string, sed fails due to unterminated command

sudo dpkg -i gitlab-ee_10.1.0+rnightly.66910.7bcf6c57-0_amd64.deb 
Selecting previously unselected package gitlab-ee.
(Reading database ... 51035 files and directories currently installed.)
Preparing to unpack gitlab-ee_10.1.0+rnightly.66910.7bcf6c57-0_amd64.deb ...
Unpacking gitlab-ee (10.1.0+rnightly.66910.7bcf6c57-0) ...
Setting up gitlab-ee (10.1.0+rnightly.66910.7bcf6c57-0) ...
sed: -e expression #1, char 37: unterminated `s' command
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: ["gitlab-ee"]
Synchronizing Cookbooks:
  - gitlab-ee (0.0.1)
  - package (0.1.0)
  - gitlab (0.0.1)
  - repmgr (0.1.0)
  - consul (0.0.0)
  - runit (0.14.2)
  - mattermost (0.1.0)
  - registry (0.1.0)
  - gitaly (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...

================================================================================
Recipe Compile Error in /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab-ee/recipes/default.rb
================================================================================

RuntimeError
------------
GitLab external URL must include a schema and FQDN, e.g. http://gitlab.example.com/

Cookbook Trace:
---------------
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/gitlab_rails.rb:75:in `parse_external_url'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/gitlab_rails.rb:25:in `parse_variables'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/package/libraries/helpers/settings_helper.rb:174:in `block in generate_config'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/package/libraries/helpers/settings_helper.rb:172:in `each'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/package/libraries/helpers/settings_helper.rb:172:in `generate_config'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/config.rb:26:in `from_file'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/recipes/default.rb:26:in `from_file'
  /opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab-ee/recipes/default.rb:20:in `from_file'

Relevant File Content:
----------------------
/opt/gitlab/embedded/cookbooks/cache/cookbooks/gitlab/libraries/gitlab_rails.rb:

 68:  
 69:      def parse_external_url
 70:        return unless Gitlab['external_url']
 71:  
 72:        uri = URI(Gitlab['external_url'].to_s)
 73:  
 74:        unless uri.host
 75>>         raise "GitLab external URL must include a schema and FQDN, e.g. http://gitlab.example.com/"
 76:        end
 77:        Gitlab['user']['git_user_email'] ||= "gitlab@#{uri.host}"
 78:        Gitlab['gitlab_rails']['gitlab_host'] = uri.host
 79:        Gitlab['gitlab_rails']['gitlab_email_from'] ||= "gitlab@#{uri.host}"
 80:  
 81:        case uri.scheme
 82:        when "http"
 83:          Gitlab['gitlab_rails']['gitlab_https'] = false
 84:          Nginx.parse_proxy_headers('nginx', false)

Platform:
---------
x86_64-linux


Running handlers:
Running handlers complete
Chef Client failed. 0 resources updated in 04 seconds
dpkg: error processing package gitlab-ee (--install):
 subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
 gitlab-ee

Our existing logic has no way to fallback to http://gitlab.example.com in an EC2 instance. This MR adds that as well as a check for an XML message.

Edited by GitLab Release Tools Bot

Merge request reports