諸事情があり、OSのrootユーザは持っているけどadminユーザのパスワードが分からない(変更も今はできない)+Web画面で新規ユーザの作成を制限しているGitlabに対し、ユーザを追加するという必要があった。 で、そういった場合には「gitlab-rails」コマンドを使って処理をすることで、コンソール上からadminユーザのパスワード変更やユーザの追加と言った処理を行えるようだ。

まず、以下のコマンドを実行してgitlab-railsでコンソールに接続する。 コンソールの起動まで少し時間がかかる。

gitlab-rails console production

コンソールが起動したら、後は処理を実行していくだけだ。

rootユーザのパスワード変更

rootユーザのパスワード変更をする場合は、gitlab-railsのコンソールで以下のようにコマンドを実行すればいい。

user = User.where(id: 1).first
user.password = 'パスワード'
user.password_confirmation = 'パスワード'
user.save!
[root@BS-PUB-GITLAB ~]# gitlab-rails console production
Loading production environment (Rails 4.2.6)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id: 1, email: "admin@example.com", encrypted_password: "$2a$10$RLIb1nhSE98E.kmtklRwV.29ty3kf8MdB4.Egxf9x9k...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2016-05-29 23:35:29", updated_at: "2017-10-28 20:40:13", name: "Administrator", admin: true, projects_limit: 10, skype: "", linkedin: "", twitter: "", authentication_token: "mGH1k4eNx1a3jVsUFwsh", theme_id: 2, bio: nil, failed_attempts: 2, locked_at: nil, username: "root", can_create_group: true, can_create_team: false, state: "active", color_scheme_id: 1, notification_level: 1, password_expires_at: nil, created_by_id: nil, last_credential_check_at: nil, avatar: nil, confirmation_token: nil, confirmed_at: "2016-05-29 23:35:29", confirmation_sent_at: nil, unconfirmed_email: nil, hide_no_ssh_key: false, website_url: "", notification_email: "admin@example.com", hide_no_password: false, password_automatically_set: false, location: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, otp_required_for_login: false, otp_backup_codes: nil, public_email: "", dashboard: 0, project_view: 0, consumed_timestep: nil, layout: 0, hide_project_limit: false, unlock_token: nil, otp_grace_period_started_at: nil, ldap_email: false, external: false>
irb(main):002:0> user.password = 'P@ssw0rd'
=> "P@ssw0rd"
irb(main):003:0> user.password_confirmation = 'P@ssw0rd'
=> "P@ssw0rd"
irb(main):004:0> user.save!
=> true

新規ユーザの登録

gitlab-railsのコンソールから新規ユーザを作成する場合、以下のようにコマンドを実行する。

User.create(
  :username => 'UserID',
  :name => 'Test_User',
  :password => 'Password',
  :password_confirmation => 'Password',
  :email => 'MailAddress',
  :admin => true)
[root@BS-PUB-GITLAB ~]# gitlab-rails console production
Loading production environment (Rails 4.2.6)
irb(main):001:0>
irb(main):002:0* User.create(
irb(main):003:1* :username => 'test',
irb(main):004:1* :name => 'Test_User',
irb(main):005:1* :password => 'P@ssw0rd',
irb(main):006:1* :password_confirmation => 'P@ssw0rd',
irb(main):007:1* :email => 'test@blacknon.local',
irb(main):008:1* :admin => true)
=> #<User id: 3, email: "test@blacknon.local", encrypted_password: "$2a$10$APm/YZbCRgoS6iFp3B9bF.mXmmbw8enwTUIQ5HrjL4z...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2017-10-29 09:02:51", updated_at: "2017-10-29 09:02:51", name: "Test_User", admin: true, projects_limit: 10, skype: "", linkedin: "", twitter: "", authentication_token: "MdZgvrg-B1Q9GSJDAGPp", theme_id: 2, bio: nil, failed_attempts: 0, locked_at: nil, username: "test", can_create_group: true, can_create_team: false, state: "active", color_scheme_id: 1, notification_level: 1, password_expires_at: nil, created_by_id: nil, last_credential_check_at: nil, avatar: nil, confirmation_token: "cpn3dfiNjCmQjK-qKEPG", confirmed_at: "2017-10-29 09:02:52", confirmation_sent_at: "2017-10-29 09:02:52", unconfirmed_email: nil, hide_no_ssh_key: false, website_url: "", notification_email: "test@blacknon.local", hide_no_password: false, password_automatically_set: false, location: nil, encrypted_otp_secret: nil, encrypted_otp_secret_iv: nil, encrypted_otp_secret_salt: nil, otp_required_for_login: false, otp_backup_codes: nil, public_email: "", dashboard: 0, project_view: 0, consumed_timestep: nil, layout: 0, hide_project_limit: false, unlock_token: nil, otp_grace_period_started_at: nil, ldap_email: false, external: false>
irb(main):009:0> user.save!
NameError: undefined local variable or method `user' for main:Object
 from (irb):9
 from /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
 from /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
 from /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
 from /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
 from /opt/gitlab/embedded/service/gem/ruby/2.1.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
 from bin/rails:9:in `require'
 from bin/rails:9:in `<main>'