bitnami gitlab 使用 gitlab-rails 命令

公司的前任配置管理员安装的 gitlab 使用的是 bitnami 出品的全家桶,和官方的 gitlab 安装方式彻底不一样,包括配置文件、启动的方式、各类命令行工具都不相同。node

他离职以后,gitlab 就交给新配置管理员了,可是对方对 gitlab 了解有限,基本上有什么疑难杂症还得找我。mysql

咱们的 gitlab 使用的是版本为 8.5.1,安装在 /opt 目录。linux

由于咱们 gitlab 接入了 ldap,而且将 ldap 中的一个用户设置成了管理员,而此次又不知道是什么缘由致使它被锁定了。锁定以后没法解锁,报 This user cannot be unlocked manually from GitLab 这样的错,同时也没法删除该用户,由于使用它建立了不少的组。git

谷歌了一下,解决办法是经过 gitlab-rails 命令手动修改用户的状态,可是我根本没有找到这个命令。后来不死心,仔细找了找,终于在 /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/bin/ 找到了 rails 命令。github

不过执行直接报错:sql

# /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/bin/rails
/usr/bin/env: ruby: No such file or directory
复制代码

须要 ruby 环境。虽然能够直接 yum install ruby 来安装,可是自带的版本才 1.8.7,不用装我也知道太老,因而找安装 ruby 的方法。在官网上找到了一种 RVM 安装的方式,这种方式相似于 Python 的 pyenv,能够管理多个 ruby 版本。那没什么好想的,就它了。数据库

安装 ruby

# curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
# \curl -sSL https://get.rvm.io | bash -s stable
复制代码

安装在 /usr/local/rvm。安装完成以后提示要将使用 rvm 的用户加入到 rvm 组,不过我这里使用 root,就无论了。vim

接着就是安装 ruby 了,安装什么版本呢?我也不知道,可是 rvm 知道,执行下面命令便可。ruby

cd /opt/gitlab-8.5.1-0/apps/gitlab/htdocs
/usr/local/rvm/bin/rvm list known
复制代码

紧接着你就可以看到提示:bash

Required ruby-2.1.8 is not installed.
To install do: 'rvm install "ruby-2.1.8"'
复制代码

固然下面还有不少内容,咱们不用管,rvm 自动提示咱们须要安装 2.1.8 版本的 ruby。之因此会出现这样的状况,我猜想是由于当前目录下存在 Gemfile、Gemfile.lock 等文件。

安装 ruby 2.1.8,请确保 yum 源可用:

/usr/local/rvm/bin/rvm install "ruby-2.1.8"
复制代码

安装的位置为 /usr/local/rvm/rubies/ruby-2.1.8/bin/ruby

安装完成以后咱们修改 /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/bin/rails 文件,将第一行改成 /usr/local/rvm/rubies/ruby-2.1.8/bin/ruby,而后执行 rails 一样会失败,报对应的组件不存在。

安装依赖

不用慌,由于它依赖的包咱们尚未装,这时返回到它的上级目录,就是 htdocs,它下面存在 Gemfile。

而后执行下面命令:

/usr/local/rvm/wrappers/ruby-2.1.8/bundle install
复制代码

它开始安装 Gemfile 里面的依赖包,安装过程当中难为会报错,由于总有些开发包你没有安装。报错了也不用慌,基本上它都会讲解决办法告诉你。

好比我这里安装 charlock_holmes 失败:

Fetching charlock_holmes 0.7.3
Installing charlock_holmes 0.7.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/gems/charlock_holmes-0.7.3/ext/charlock_holmes
/usr/local/rvm/rubies/ruby-2.1.8/bin/ruby -r ./siteconf20190319-934-13vhsli.rb extconf.rb
checking for main() in -licui18n... no
which: no brew in
(/usr/local/rvm/gems/ruby-2.1.8/bin:/usr/local/rvm/gems/ruby-2.1.8@global/bin:/usr/local/rvm/rubies/ruby-2.1.8/bin:/usr/lib64/qt-3.3/bin:/usr/local/jdk1.7.0_79/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
checking for main() in -licui18n... no

# 这里已经告诉你解决办法
***************************************************************************************
*********** icu required (brew install icu4c or apt-get install libicu-dev) ***********
***************************************************************************************
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/usr/local/rvm/rubies/ruby-2.1.8/bin/ruby
	--with-icu-dir
	--without-icu-dir
	--with-icu-include
	--without-icu-include=${icu-dir}/include
	--with-icu-lib
	--without-icu-lib=${icu-dir}/lib
	--with-icui18nlib
	--without-icui18nlib
	--with-icui18nlib
	--without-icui18nlib

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0/charlock_holmes-0.7.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/gems/charlock_holmes-0.7.3 for inspection.
Results logged to /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0/charlock_holmes-0.7.3/gem_make.out

An error occurred while installing charlock_holmes (0.7.3), and Bundler cannot continue.
Make sure that `gem install charlock_holmes -v '0.7.3' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  gitlab_git was resolved to 8.2.0, which depends on
    github-linguist was resolved to 4.7.5, which depends on
      charlock_holmes
复制代码

上面已经说了要咱们 brew install icu4c or apt-get install libicu-dev,这里是 macOS 和 Ubuntu 的解决办法,CentOS 的解决方法则是 yum install libicu-devel

有些报错可能不会直接将解决办法告诉你,可是它也将缘由告诉你了,好比这个报错:

Fetching rugged 0.24.0b13
Installing rugged 0.24.0b13 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/gems/rugged-0.24.0b13/ext/rugged
/usr/local/rvm/rubies/ruby-2.1.8/bin/ruby -r ./siteconf20190319-2037-829l33.rb extconf.rb
checking for gmake... yes
checking for cmake... no
ERROR: CMake is required to build Rugged.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.
复制代码

说缺乏 cmake,那咱们直接 yum install cmake

接下来还遇到两次报错,一次是 mysql2,提示 yum install mysql-devel;还有一次是 pg,说是找不到 libpq-fe.h,使用 yum provides "*/libpq-fe.h",能够找到是 postgresql-devel,yum insall 完事。

须要注意的是,你最好一次性都安装成功,若是只安装一半,会影响 gitlab 的正常使用。最大的体现就是没法合并分支,并且拉分支的话会报 Branch creation was rejected by Git hook 这样的错。

安装 nodejs

再次执行 rails 会报错,提示没有 nodejs 运行环境:

# ./rails console
...
/opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/gems/execjs-2.6.0/lib/execjs/runtimes.rb:48:in `autodetect': Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable) from /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/vendor/bundle/ruby/2.1.0/gems/execjs-2.6.0/lib/execjs.rb:5:in `<module:ExecJS>'
复制代码

既然没有,那就下一个呗:

wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz
tar xf node-v10.15.3-linux-x64.tar.xz -C /usr/local
cd /usr/local
ln -s node-v10.15.3-linux-x64/ node
ln -s `pwd`/node/bin/node /usr/bin/node
复制代码

修改 postgresql 配置

再次运行 ./rails console 成功进入。可是一旦你执行命令,它会报错,连不上 postgresql:

2.1.8 :001 > User.find_by_any_email("EMAIL")
PG::ConnectionBad: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
复制代码

默认经过 Unix 域套接字来链接数据库,可是这个套接字文件不在 /tmp 下。这个问题解决起来也简单,只须要找到如今的套接字文件,而后链接到 /tmp 目录下便可。固然,你也能够直接修改 pgsql 的配置文件,一次性解决问题。不管如何,咱们都得先找到它的配置文件。

其实只要在 gitlab 的安装目录稍微找找就可以找到,我这里就直接打开了:

vim /opt/gitlab-8.5.1-0/postgresql/data/postgresql.conf

# 这行就指定了套接字的目录
unix_socket_directories = '/opt/gitlab-8.5.1-0/postgresql'
复制代码

看到以后,你可能当即就 ls 这个目录了,咦,怎么啥都没有?由于这个文件是以 . 开头,你要经过 -a 参数才能看到,域套接字的格式就是 .s.PGSQL. + 端口。

找到以后,咱们直接作个软链接:

ln -s /opt/gitlab-8.5.1-0/postgresql/.s.PGSQL.5432 /tmp/
复制代码

继续执行 rails 命令:

# ./rails console

2.1.8 :001 > User.find_by_any_email("EMAIL")
PG::ConnectionBad: fe_sendauth: no password supplied
复制代码

又 ™ 报错了,说没有指定密码。pgsql 虽然连上了,可是却没有密码,因而我赶忙看了下 rails 的使用帮助,可是并无找到有输入密码的地方,并且我也不知道它的密码是啥。。

又仔细的看了下 rails 命令的输出,因而发现了这行:

Loading development environment (Rails 4.2.5.1)
复制代码

说是加载了开发环境的配置,它居然还有环境之分,这我也是第一次知道,毕竟对 gitlab 了解很少。因而又翻了翻 gitlab 的配置文件,找到了数据库方面的配置,我这里就直接打开了。

# vim /opt/gitlab-8.5.1-0/apps/gitlab/htdocs/config/database.yml

#
# PRODUCTION
#
production:
 adapter: postgresql
 encoding: unicode
 database: 手动打码
 pool: 10
 username: 手动打码
 password: 手动打码
 host: 127.0.0.1
 port: 5432
  # port: 5432

#
# Development specific
#
development:
 adapter: postgresql
 encoding: unicode
 database: 手动打码
 pool: 5
 username: 手动打码
 password: 手动打码
复制代码

环境挺多,这里只列出了开发和生成环境,由于我这里的开发环境的密码没有配置,因此才会出现上面没有认证的状况。解决办法就是直接使用生产环境的配置,因而看了看 rails 的用法,找到了指定生产环境的方法。

# ./rails console -e production

2.1.8 :002 > user = User.find_by_any_email("MAIL")
复制代码

顺利的找到一堆结果,而后修改下用户的状态就可以将用户解锁:

2.1.8 :003 > user.state = "active"
2.1.8 :004 > user.save
2.1.8 :005 > exit
复制代码

ok,文章到此结束。